Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 1 | /* |
Sean Middleditch | 5565596 | 2009-10-24 15:38:46 -0700 | [diff] [blame] | 2 | * libtelnet - TELNET protocol handling library |
Sean Middleditch | ae39cee | 2009-03-22 22:47:30 -0400 | [diff] [blame] | 3 | * |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 4 | * Sean Middleditch |
| 5 | * sean@sourcemud.org |
| 6 | * |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 7 | * The author or authors of this code dedicate any and all copyright interest |
| 8 | * in this code to the public domain. We make this dedication for the benefit |
| 9 | * of the public at large and to the detriment of our heirs and successors. We |
| 10 | * intend this dedication to be an overt act of relinquishment in perpetuity of |
| 11 | * all present and future rights to this code under copyright law. |
| 12 | */ |
| 13 | |
Sean Middleditch | 6aef073 | 2009-03-12 23:27:35 -0400 | [diff] [blame] | 14 | #if !defined(LIBTELNET_INCLUDE) |
Sean Middleditch | aac2c12 | 2009-03-14 18:31:26 -0400 | [diff] [blame] | 15 | #define LIBTELNET_INCLUDE 1 |
Sean Middleditch | 6aef073 | 2009-03-12 23:27:35 -0400 | [diff] [blame] | 16 | |
Sean Middleditch | 991f536 | 2010-01-22 16:12:13 -0800 | [diff] [blame^] | 17 | /* C++ support */ |
| 18 | #if defined(__cplusplus) |
| 19 | extern "C" { |
| 20 | #endif |
| 21 | |
Sean Middleditch | e22b477 | 2009-03-22 16:44:40 -0400 | [diff] [blame] | 22 | /* printf type checking feature in GCC and some other compilers */ |
| 23 | #if __GNUC__ |
| 24 | # define TELNET_GNU_PRINTF(f,a) __attribute__((format(printf, f, a))) |
| 25 | # define TELNET_GNU_SENTINEL __attribute__((sentinel)) |
| 26 | #else |
| 27 | # define TELNET_GNU_PRINTF(f,a) |
| 28 | # define TELNET_GNU_SENTINEL |
| 29 | #endif |
| 30 | |
Sean Middleditch | 3032302 | 2009-03-14 21:45:28 -0400 | [diff] [blame] | 31 | /* forward declarations */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 32 | typedef struct telnet_t telnet_t; |
| 33 | typedef struct telnet_event_t telnet_event_t; |
Sean Middleditch | 34bb099 | 2009-03-21 00:20:44 -0400 | [diff] [blame] | 34 | typedef struct telnet_telopt_t telnet_telopt_t; |
Sean Middleditch | 3032302 | 2009-03-14 21:45:28 -0400 | [diff] [blame] | 35 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 36 | /* telnet special values */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 37 | #define TELNET_IAC 255 |
| 38 | #define TELNET_DONT 254 |
| 39 | #define TELNET_DO 253 |
| 40 | #define TELNET_WONT 252 |
| 41 | #define TELNET_WILL 251 |
| 42 | #define TELNET_SB 250 |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 43 | #define TELNET_GA 249 |
| 44 | #define TELNET_EL 248 |
| 45 | #define TELNET_EC 247 |
| 46 | #define TELNET_AYT 246 |
| 47 | #define TELNET_AO 245 |
| 48 | #define TELNET_IP 244 |
| 49 | #define TELNET_BREAK 243 |
| 50 | #define TELNET_DM 242 |
| 51 | #define TELNET_NOP 241 |
| 52 | #define TELNET_SE 240 |
| 53 | #define TELNET_EOR 239 |
| 54 | #define TELNET_ABORT 238 |
| 55 | #define TELNET_SUSP 237 |
| 56 | #define TELNET_EOF 236 |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 57 | |
| 58 | /* telnet options */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 59 | #define TELNET_TELOPT_BINARY 0 |
| 60 | #define TELNET_TELOPT_ECHO 1 |
| 61 | #define TELNET_TELOPT_RCP 2 |
| 62 | #define TELNET_TELOPT_SGA 3 |
| 63 | #define TELNET_TELOPT_NAMS 4 |
| 64 | #define TELNET_TELOPT_STATUS 5 |
| 65 | #define TELNET_TELOPT_TM 6 |
| 66 | #define TELNET_TELOPT_RCTE 7 |
| 67 | #define TELNET_TELOPT_NAOL 8 |
| 68 | #define TELNET_TELOPT_NAOP 9 |
| 69 | #define TELNET_TELOPT_NAOCRD 10 |
| 70 | #define TELNET_TELOPT_NAOHTS 11 |
| 71 | #define TELNET_TELOPT_NAOHTD 12 |
| 72 | #define TELNET_TELOPT_NAOFFD 13 |
| 73 | #define TELNET_TELOPT_NAOVTS 14 |
| 74 | #define TELNET_TELOPT_NAOVTD 15 |
| 75 | #define TELNET_TELOPT_NAOLFD 16 |
| 76 | #define TELNET_TELOPT_XASCII 17 |
| 77 | #define TELNET_TELOPT_LOGOUT 18 |
| 78 | #define TELNET_TELOPT_BM 19 |
| 79 | #define TELNET_TELOPT_DET 20 |
| 80 | #define TELNET_TELOPT_SUPDUP 21 |
| 81 | #define TELNET_TELOPT_SUPDUPOUTPUT 22 |
| 82 | #define TELNET_TELOPT_SNDLOC 23 |
| 83 | #define TELNET_TELOPT_TTYPE 24 |
| 84 | #define TELNET_TELOPT_EOR 25 |
| 85 | #define TELNET_TELOPT_TUID 26 |
| 86 | #define TELNET_TELOPT_OUTMRK 27 |
| 87 | #define TELNET_TELOPT_TTYLOC 28 |
| 88 | #define TELNET_TELOPT_3270REGIME 29 |
| 89 | #define TELNET_TELOPT_X3PAD 30 |
| 90 | #define TELNET_TELOPT_NAWS 31 |
| 91 | #define TELNET_TELOPT_TSPEED 32 |
| 92 | #define TELNET_TELOPT_LFLOW 33 |
| 93 | #define TELNET_TELOPT_LINEMODE 34 |
| 94 | #define TELNET_TELOPT_XDISPLOC 35 |
| 95 | #define TELNET_TELOPT_ENVIRON 36 |
| 96 | #define TELNET_TELOPT_AUTHENTICATION 37 |
| 97 | #define TELNET_TELOPT_ENCRYPT 38 |
| 98 | #define TELNET_TELOPT_NEW_ENVIRON 39 |
Sean Middleditch | e2122b2 | 2009-03-20 23:55:09 -0400 | [diff] [blame] | 99 | #define TELNET_TELOPT_MSSP 70 |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 100 | #define TELNET_TELOPT_COMPRESS2 86 |
| 101 | #define TELNET_TELOPT_ZMP 93 |
| 102 | #define TELNET_TELOPT_EXOPL 255 |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 103 | |
Sean Middleditch | e2122b2 | 2009-03-20 23:55:09 -0400 | [diff] [blame] | 104 | #define TELNET_TELOPT_MCCP2 86 |
| 105 | |
Sean Middleditch | 2d5c499 | 2009-03-22 22:21:42 -0400 | [diff] [blame] | 106 | /* special codes for the subnegotiation commands for certain telopts */ |
| 107 | #define TELNET_TTYPE_IS 0 |
| 108 | #define TELNET_TTYPE_SEND 1 |
| 109 | |
| 110 | #define TELNET_ENVIRON_IS 0 |
| 111 | #define TELNET_ENVIRON_SEND 1 |
| 112 | #define TELNET_ENVIRON_INFO 2 |
| 113 | #define TELNET_ENVIRON_VAR 0 |
| 114 | #define TELNET_ENVIRON_VALUE 1 |
| 115 | #define TELNET_ENVIRON_ESC 2 |
| 116 | #define TELNET_ENVIRON_USERVAR 3 |
| 117 | |
| 118 | #define TELNET_MSSP_VAR 1 |
| 119 | #define TELNET_MSSP_VAL 2 |
| 120 | |
Sean Middleditch | 08bb05f | 2009-03-15 23:29:46 -0400 | [diff] [blame] | 121 | /* libtelnet feature flags */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 122 | #define TELNET_FLAG_PROXY (1<<0) |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 123 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 124 | #define TELNET_PFLAG_DEFLATE (1<<7) |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 125 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 126 | /* error codes */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 127 | enum telnet_error_t { |
| 128 | TELNET_EOK = 0, |
| 129 | TELNET_EBADVAL, /* invalid parameter, or API misuse */ |
| 130 | TELNET_ENOMEM, /* memory allocation failure */ |
| 131 | TELNET_EOVERFLOW, /* data exceeds buffer size */ |
| 132 | TELNET_EPROTOCOL, /* invalid sequence of special bytes */ |
| 133 | TELNET_ECOMPRESS /* error handling compressed streams */ |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 134 | }; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 135 | typedef enum telnet_error_t telnet_error_t; |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 136 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 137 | /* event codes */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 138 | enum telnet_event_type_t { |
| 139 | TELNET_EV_DATA = 0, |
| 140 | TELNET_EV_SEND, |
| 141 | TELNET_EV_IAC, |
| 142 | TELNET_EV_WILL, |
| 143 | TELNET_EV_WONT, |
| 144 | TELNET_EV_DO, |
| 145 | TELNET_EV_DONT, |
| 146 | TELNET_EV_SUBNEGOTIATION, |
| 147 | TELNET_EV_COMPRESS, |
| 148 | TELNET_EV_WARNING, |
| 149 | TELNET_EV_ERROR |
Sean Middleditch | 3032302 | 2009-03-14 21:45:28 -0400 | [diff] [blame] | 150 | }; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 151 | typedef enum telnet_event_type_t telnet_event_type_t; |
Sean Middleditch | 3032302 | 2009-03-14 21:45:28 -0400 | [diff] [blame] | 152 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 153 | /* event information */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 154 | struct telnet_event_t { |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 155 | /* ZMP argument list */ |
| 156 | const char **argv; |
| 157 | size_t argc; |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 158 | /* data buffer: for DATA, SEND, SUBNEGOTIATION, and ERROR events */ |
Sean Middleditch | 8daf774 | 2009-03-19 02:05:24 -0400 | [diff] [blame] | 159 | const char *buffer; |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 160 | size_t size; |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 161 | /* type of event */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 162 | enum telnet_event_type_t type; |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 163 | /* IAC command */ |
| 164 | unsigned char command; |
| 165 | /* telopt info: for negotiation events SUBNEGOTIATION */ |
| 166 | unsigned char telopt; |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 167 | }; |
| 168 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 169 | /* event handler declaration */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 170 | typedef void (*telnet_event_handler_t)(telnet_t *telnet, |
| 171 | telnet_event_t *event, void *user_data); |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 172 | |
Sean Middleditch | 34bb099 | 2009-03-21 00:20:44 -0400 | [diff] [blame] | 173 | /* telopt support table element; use telopt of -1 for end marker */ |
| 174 | struct telnet_telopt_t { |
Sean Middleditch | bfc641e | 2009-03-22 16:26:06 -0400 | [diff] [blame] | 175 | short telopt; /* one of the TELOPT codes or -1 */ |
| 176 | unsigned char us; /* TELNET_WILL or TELNET_WONT */ |
| 177 | unsigned char him; /* TELNET_DO or TELNET_DONT */ |
Sean Middleditch | 34bb099 | 2009-03-21 00:20:44 -0400 | [diff] [blame] | 178 | }; |
| 179 | |
Sean Middleditch | d2466a0 | 2009-09-19 14:35:48 -0700 | [diff] [blame] | 180 | /* state tracker -- private data structure */ |
| 181 | struct telnet_t; |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 182 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 183 | /* initialize a telnet state tracker */ |
Sean Middleditch | d2466a0 | 2009-09-19 14:35:48 -0700 | [diff] [blame] | 184 | extern telnet_t* telnet_init(const telnet_telopt_t *telopts, |
Sean Middleditch | 34bb099 | 2009-03-21 00:20:44 -0400 | [diff] [blame] | 185 | telnet_event_handler_t eh, unsigned char flags, void *user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 186 | |
| 187 | /* free up any memory allocated by a state tracker */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 188 | extern void telnet_free(telnet_t *telnet); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 189 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 190 | /* push a byte buffer into the state tracker */ |
Sean Middleditch | 4f0c37f | 2009-03-20 23:08:55 -0400 | [diff] [blame] | 191 | extern void telnet_recv(telnet_t *telnet, const char *buffer, |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 192 | size_t size); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 193 | |
| 194 | /* send an iac command */ |
Sean Middleditch | 4f0c37f | 2009-03-20 23:08:55 -0400 | [diff] [blame] | 195 | extern void telnet_iac(telnet_t *telnet, unsigned char cmd); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 196 | |
Sean Middleditch | 2b4bfc4 | 2009-03-16 01:25:52 -0400 | [diff] [blame] | 197 | /* send negotiation, with RFC1143 checking. |
| 198 | * will not actually send unless necessary, but will update internal |
| 199 | * negotiation queue. |
| 200 | */ |
Sean Middleditch | 4f0c37f | 2009-03-20 23:08:55 -0400 | [diff] [blame] | 201 | extern void telnet_negotiate(telnet_t *telnet, unsigned char cmd, |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 202 | unsigned char opt); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 203 | |
| 204 | /* send non-command data (escapes IAC bytes) */ |
Sean Middleditch | 4f0c37f | 2009-03-20 23:08:55 -0400 | [diff] [blame] | 205 | extern void telnet_send(telnet_t *telnet, |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 206 | const char *buffer, size_t size); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 207 | |
Sean Middleditch | 90e79da | 2009-03-19 15:17:13 -0400 | [diff] [blame] | 208 | /* send IAC SB followed by the telopt code */ |
Sean Middleditch | 4f0c37f | 2009-03-20 23:08:55 -0400 | [diff] [blame] | 209 | extern void telnet_begin_sb(telnet_t *telnet, |
Sean Middleditch | 90e79da | 2009-03-19 15:17:13 -0400 | [diff] [blame] | 210 | unsigned char telopt); |
| 211 | |
| 212 | /* send IAC SE */ |
Sean Middleditch | 4f0c37f | 2009-03-20 23:08:55 -0400 | [diff] [blame] | 213 | #define telnet_finish_sb(telnet) telnet_iac((telnet), TELNET_SE) |
Sean Middleditch | 90e79da | 2009-03-19 15:17:13 -0400 | [diff] [blame] | 214 | |
| 215 | /* shortcut for sending a complete subnegotiation buffer. |
| 216 | * equivalent to: |
Sean Middleditch | 4f0c37f | 2009-03-20 23:08:55 -0400 | [diff] [blame] | 217 | * telnet_begin_sb(telnet, telopt); |
| 218 | * telnet_send(telnet, buffer, size); |
| 219 | * telnet_finish_sb(telnet); |
Sean Middleditch | 2b4bfc4 | 2009-03-16 01:25:52 -0400 | [diff] [blame] | 220 | */ |
Sean Middleditch | 4f0c37f | 2009-03-20 23:08:55 -0400 | [diff] [blame] | 221 | extern void telnet_subnegotiation(telnet_t *telnet, unsigned char telopt, |
Sean Middleditch | 90e79da | 2009-03-19 15:17:13 -0400 | [diff] [blame] | 222 | const char *buffer, size_t size); |
Sean Middleditch | 6aef073 | 2009-03-12 23:27:35 -0400 | [diff] [blame] | 223 | |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 224 | /* begin sending compressed data (server only) */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 225 | extern void telnet_begin_compress2(telnet_t *telnet); |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 226 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 227 | /* send formatted data with \r and \n translated, and IAC escaped */ |
Sean Middleditch | e22b477 | 2009-03-22 16:44:40 -0400 | [diff] [blame] | 228 | extern int telnet_printf(telnet_t *telnet, const char *fmt, ...) |
| 229 | TELNET_GNU_PRINTF(2, 3); |
Sean Middleditch | 4a15604 | 2009-03-16 17:10:58 -0400 | [diff] [blame] | 230 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 231 | /* send formatted data with just IAC escaped */ |
Sean Middleditch | 9dcfd4b | 2009-10-14 16:17:46 -0700 | [diff] [blame] | 232 | extern int telnet_raw_printf(telnet_t *telnet, const char *fmt, ...) |
Sean Middleditch | e22b477 | 2009-03-22 16:44:40 -0400 | [diff] [blame] | 233 | TELNET_GNU_PRINTF(2, 3); |
| 234 | |
Sean Middleditch | eb950a8 | 2009-03-22 23:04:28 -0400 | [diff] [blame] | 235 | /* send TTYPE/ENVIRON/NEW-ENVIRON/MSSP data */ |
| 236 | extern void telnet_format_sb(telnet_t *telnet, unsigned char telopt, |
| 237 | size_t count, ...); |
| 238 | |
Sean Middleditch | e22b477 | 2009-03-22 16:44:40 -0400 | [diff] [blame] | 239 | /* send ZMP commands */ |
| 240 | extern void telnet_send_zmp(telnet_t *telnet, size_t argc, const char **argv); |
| 241 | extern void telnet_send_zmpv(telnet_t *telnet, ...) TELNET_GNU_SENTINEL; |
Sean Middleditch | d58f49f | 2009-03-16 12:49:35 -0400 | [diff] [blame] | 242 | |
Sean Middleditch | 991f536 | 2010-01-22 16:12:13 -0800 | [diff] [blame^] | 243 | /* C++ support */ |
| 244 | #if defined(__cplusplus) |
| 245 | } /* extern "C" */ |
| 246 | #endif |
| 247 | |
Sean Middleditch | 6aef073 | 2009-03-12 23:27:35 -0400 | [diff] [blame] | 248 | #endif /* !defined(LIBTELNET_INCLUDE) */ |