blob: 52e2e185bdc8e0333c079f65177894058da7ba6c [file] [log] [blame]
Sean Middleditch29144852009-03-12 23:14:47 -04001/*
Sean Middleditch9de15982009-03-14 03:35:49 -04002 * Sean Middleditch
3 * sean@sourcemud.org
4 *
Sean Middleditch29144852009-03-12 23:14:47 -04005 * 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 Middleditch6aef0732009-03-12 23:27:35 -040012#if !defined(LIBTELNET_INCLUDE)
Sean Middleditchaac2c122009-03-14 18:31:26 -040013#define LIBTELNET_INCLUDE 1
Sean Middleditch6aef0732009-03-12 23:27:35 -040014
Sean Middleditche22b4772009-03-22 16:44:40 -040015/* printf type checking feature in GCC and some other compilers */
16#if __GNUC__
17# define TELNET_GNU_PRINTF(f,a) __attribute__((format(printf, f, a)))
18# define TELNET_GNU_SENTINEL __attribute__((sentinel))
19#else
20# define TELNET_GNU_PRINTF(f,a)
21# define TELNET_GNU_SENTINEL
22#endif
23
Sean Middleditch30323022009-03-14 21:45:28 -040024/* forward declarations */
Sean Middleditchf65f27d2009-03-19 02:32:04 -040025typedef struct telnet_t telnet_t;
26typedef struct telnet_event_t telnet_event_t;
Sean Middleditch34bb0992009-03-21 00:20:44 -040027typedef struct telnet_telopt_t telnet_telopt_t;
Sean Middleditch30323022009-03-14 21:45:28 -040028
Sean Middleditch29144852009-03-12 23:14:47 -040029/* telnet special values */
Sean Middleditchf65f27d2009-03-19 02:32:04 -040030#define TELNET_IAC 255
31#define TELNET_DONT 254
32#define TELNET_DO 253
33#define TELNET_WONT 252
34#define TELNET_WILL 251
35#define TELNET_SB 250
Sean Middleditchf65f27d2009-03-19 02:32:04 -040036#define TELNET_GA 249
37#define TELNET_EL 248
38#define TELNET_EC 247
39#define TELNET_AYT 246
40#define TELNET_AO 245
41#define TELNET_IP 244
42#define TELNET_BREAK 243
43#define TELNET_DM 242
44#define TELNET_NOP 241
45#define TELNET_SE 240
46#define TELNET_EOR 239
47#define TELNET_ABORT 238
48#define TELNET_SUSP 237
49#define TELNET_EOF 236
Sean Middleditch29144852009-03-12 23:14:47 -040050
51/* telnet options */
Sean Middleditchf65f27d2009-03-19 02:32:04 -040052#define TELNET_TELOPT_BINARY 0
53#define TELNET_TELOPT_ECHO 1
54#define TELNET_TELOPT_RCP 2
55#define TELNET_TELOPT_SGA 3
56#define TELNET_TELOPT_NAMS 4
57#define TELNET_TELOPT_STATUS 5
58#define TELNET_TELOPT_TM 6
59#define TELNET_TELOPT_RCTE 7
60#define TELNET_TELOPT_NAOL 8
61#define TELNET_TELOPT_NAOP 9
62#define TELNET_TELOPT_NAOCRD 10
63#define TELNET_TELOPT_NAOHTS 11
64#define TELNET_TELOPT_NAOHTD 12
65#define TELNET_TELOPT_NAOFFD 13
66#define TELNET_TELOPT_NAOVTS 14
67#define TELNET_TELOPT_NAOVTD 15
68#define TELNET_TELOPT_NAOLFD 16
69#define TELNET_TELOPT_XASCII 17
70#define TELNET_TELOPT_LOGOUT 18
71#define TELNET_TELOPT_BM 19
72#define TELNET_TELOPT_DET 20
73#define TELNET_TELOPT_SUPDUP 21
74#define TELNET_TELOPT_SUPDUPOUTPUT 22
75#define TELNET_TELOPT_SNDLOC 23
76#define TELNET_TELOPT_TTYPE 24
77#define TELNET_TELOPT_EOR 25
78#define TELNET_TELOPT_TUID 26
79#define TELNET_TELOPT_OUTMRK 27
80#define TELNET_TELOPT_TTYLOC 28
81#define TELNET_TELOPT_3270REGIME 29
82#define TELNET_TELOPT_X3PAD 30
83#define TELNET_TELOPT_NAWS 31
84#define TELNET_TELOPT_TSPEED 32
85#define TELNET_TELOPT_LFLOW 33
86#define TELNET_TELOPT_LINEMODE 34
87#define TELNET_TELOPT_XDISPLOC 35
88#define TELNET_TELOPT_ENVIRON 36
89#define TELNET_TELOPT_AUTHENTICATION 37
90#define TELNET_TELOPT_ENCRYPT 38
91#define TELNET_TELOPT_NEW_ENVIRON 39
Sean Middleditche2122b22009-03-20 23:55:09 -040092#define TELNET_TELOPT_MSSP 70
Sean Middleditchf65f27d2009-03-19 02:32:04 -040093#define TELNET_TELOPT_COMPRESS2 86
94#define TELNET_TELOPT_ZMP 93
95#define TELNET_TELOPT_EXOPL 255
Sean Middleditch29144852009-03-12 23:14:47 -040096
Sean Middleditche2122b22009-03-20 23:55:09 -040097#define TELNET_TELOPT_MCCP2 86
98
Sean Middleditch2d5c4992009-03-22 22:21:42 -040099/* special codes for the subnegotiation commands for certain telopts */
100#define TELNET_TTYPE_IS 0
101#define TELNET_TTYPE_SEND 1
102
103#define TELNET_ENVIRON_IS 0
104#define TELNET_ENVIRON_SEND 1
105#define TELNET_ENVIRON_INFO 2
106#define TELNET_ENVIRON_VAR 0
107#define TELNET_ENVIRON_VALUE 1
108#define TELNET_ENVIRON_ESC 2
109#define TELNET_ENVIRON_USERVAR 3
110
111#define TELNET_MSSP_VAR 1
112#define TELNET_MSSP_VAL 2
113
Sean Middleditch08bb05f2009-03-15 23:29:46 -0400114/* libtelnet feature flags */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400115#define TELNET_FLAG_PROXY (1<<0)
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400116
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400117#define TELNET_PFLAG_DEFLATE (1<<7)
Sean Middleditch29144852009-03-12 23:14:47 -0400118
119/* telnet states */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400120enum telnet_state_t {
121 TELNET_STATE_DATA = 0,
122 TELNET_STATE_IAC,
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400123 TELNET_STATE_WILL,
124 TELNET_STATE_WONT,
Sean Middleditch447d3ad2009-03-20 23:22:37 -0400125 TELNET_STATE_DO,
126 TELNET_STATE_DONT,
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400127 TELNET_STATE_SB,
128 TELNET_STATE_SB_DATA,
129 TELNET_STATE_SB_DATA_IAC
Sean Middleditch29144852009-03-12 23:14:47 -0400130};
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400131typedef enum telnet_state_t telnet_state_t;
Sean Middleditch29144852009-03-12 23:14:47 -0400132
133/* error codes */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400134enum telnet_error_t {
135 TELNET_EOK = 0,
136 TELNET_EBADVAL, /* invalid parameter, or API misuse */
137 TELNET_ENOMEM, /* memory allocation failure */
138 TELNET_EOVERFLOW, /* data exceeds buffer size */
139 TELNET_EPROTOCOL, /* invalid sequence of special bytes */
140 TELNET_ECOMPRESS /* error handling compressed streams */
Sean Middleditch29144852009-03-12 23:14:47 -0400141};
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400142typedef enum telnet_error_t telnet_error_t;
Sean Middleditch29144852009-03-12 23:14:47 -0400143
Sean Middleditch637df7f2009-03-15 12:57:32 -0400144/* event codes */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400145enum telnet_event_type_t {
146 TELNET_EV_DATA = 0,
147 TELNET_EV_SEND,
148 TELNET_EV_IAC,
149 TELNET_EV_WILL,
150 TELNET_EV_WONT,
151 TELNET_EV_DO,
152 TELNET_EV_DONT,
153 TELNET_EV_SUBNEGOTIATION,
154 TELNET_EV_COMPRESS,
155 TELNET_EV_WARNING,
156 TELNET_EV_ERROR
Sean Middleditch30323022009-03-14 21:45:28 -0400157};
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400158typedef enum telnet_event_type_t telnet_event_type_t;
Sean Middleditch30323022009-03-14 21:45:28 -0400159
Sean Middleditch637df7f2009-03-15 12:57:32 -0400160/* event information */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400161struct telnet_event_t {
Sean Middleditche5327da2009-03-21 00:54:50 -0400162 /* ZMP argument list */
163 const char **argv;
164 size_t argc;
Sean Middleditch637df7f2009-03-15 12:57:32 -0400165 /* data buffer: for DATA, SEND, SUBNEGOTIATION, and ERROR events */
Sean Middleditch8daf7742009-03-19 02:05:24 -0400166 const char *buffer;
Sean Middleditch340a51b2009-03-19 02:08:46 -0400167 size_t size;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400168 /* type of event */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400169 enum telnet_event_type_t type;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400170 /* IAC command */
171 unsigned char command;
172 /* telopt info: for negotiation events SUBNEGOTIATION */
173 unsigned char telopt;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400174};
175
Sean Middleditch637df7f2009-03-15 12:57:32 -0400176/* event handler declaration */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400177typedef void (*telnet_event_handler_t)(telnet_t *telnet,
178 telnet_event_t *event, void *user_data);
Sean Middleditch637df7f2009-03-15 12:57:32 -0400179
Sean Middleditch34bb0992009-03-21 00:20:44 -0400180/* telopt support table element; use telopt of -1 for end marker */
181struct telnet_telopt_t {
Sean Middleditchbfc641e2009-03-22 16:26:06 -0400182 short telopt; /* one of the TELOPT codes or -1 */
183 unsigned char us; /* TELNET_WILL or TELNET_WONT */
184 unsigned char him; /* TELNET_DO or TELNET_DONT */
Sean Middleditch34bb0992009-03-21 00:20:44 -0400185};
186
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400187/* state tracker */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400188struct telnet_t {
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400189 /* user data */
190 void *ud;
Sean Middleditch34bb0992009-03-21 00:20:44 -0400191 /* telopt support table */
192 const telnet_telopt_t *telopts;
Sean Middleditch637df7f2009-03-15 12:57:32 -0400193 /* event handler */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400194 telnet_event_handler_t eh;
Sean Middleditch9de15982009-03-14 03:35:49 -0400195#ifdef HAVE_ZLIB
Sean Middleditch30323022009-03-14 21:45:28 -0400196 /* zlib (mccp2) compression */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400197 z_stream *z;
Sean Middleditch9de15982009-03-14 03:35:49 -0400198#endif
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400199 /* RFC1143 option negotiation states */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400200 struct telnet_rfc1143_t *q;
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400201 /* sub-request buffer */
Sean Middleditch8daf7742009-03-19 02:05:24 -0400202 char *buffer;
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400203 /* current size of the buffer */
Sean Middleditch340a51b2009-03-19 02:08:46 -0400204 size_t buffer_size;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400205 /* current buffer write position (also length of buffer data) */
Sean Middleditch340a51b2009-03-19 02:08:46 -0400206 size_t buffer_pos;
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400207 /* current state */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400208 enum telnet_state_t state;
Sean Middleditch08bb05f2009-03-15 23:29:46 -0400209 /* option flags */
210 unsigned char flags;
Sean Middleditchda0e6952009-03-15 13:28:09 -0400211 /* current subnegotiation telopt */
212 unsigned char sb_telopt;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400213 /* length of RFC1143 queue */
214 unsigned char q_size;
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400215};
216
Sean Middleditch29144852009-03-12 23:14:47 -0400217/* initialize a telnet state tracker */
Sean Middleditch34bb0992009-03-21 00:20:44 -0400218extern void telnet_init(telnet_t *telnet, const telnet_telopt_t *telopts,
219 telnet_event_handler_t eh, unsigned char flags, void *user_data);
Sean Middleditch29144852009-03-12 23:14:47 -0400220
221/* free up any memory allocated by a state tracker */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400222extern void telnet_free(telnet_t *telnet);
Sean Middleditch29144852009-03-12 23:14:47 -0400223
Sean Middleditch29144852009-03-12 23:14:47 -0400224/* push a byte buffer into the state tracker */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400225extern void telnet_recv(telnet_t *telnet, const char *buffer,
Sean Middleditch340a51b2009-03-19 02:08:46 -0400226 size_t size);
Sean Middleditch29144852009-03-12 23:14:47 -0400227
228/* send an iac command */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400229extern void telnet_iac(telnet_t *telnet, unsigned char cmd);
Sean Middleditch29144852009-03-12 23:14:47 -0400230
Sean Middleditch2b4bfc42009-03-16 01:25:52 -0400231/* send negotiation, with RFC1143 checking.
232 * will not actually send unless necessary, but will update internal
233 * negotiation queue.
234 */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400235extern void telnet_negotiate(telnet_t *telnet, unsigned char cmd,
Sean Middleditch812358d2009-03-15 23:24:03 -0400236 unsigned char opt);
Sean Middleditch29144852009-03-12 23:14:47 -0400237
238/* send non-command data (escapes IAC bytes) */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400239extern void telnet_send(telnet_t *telnet,
Sean Middleditch340a51b2009-03-19 02:08:46 -0400240 const char *buffer, size_t size);
Sean Middleditch29144852009-03-12 23:14:47 -0400241
Sean Middleditch90e79da2009-03-19 15:17:13 -0400242/* send IAC SB followed by the telopt code */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400243extern void telnet_begin_sb(telnet_t *telnet,
Sean Middleditch90e79da2009-03-19 15:17:13 -0400244 unsigned char telopt);
245
246/* send IAC SE */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400247#define telnet_finish_sb(telnet) telnet_iac((telnet), TELNET_SE)
Sean Middleditch90e79da2009-03-19 15:17:13 -0400248
249/* shortcut for sending a complete subnegotiation buffer.
250 * equivalent to:
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400251 * telnet_begin_sb(telnet, telopt);
252 * telnet_send(telnet, buffer, size);
253 * telnet_finish_sb(telnet);
Sean Middleditch2b4bfc42009-03-16 01:25:52 -0400254 */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400255extern void telnet_subnegotiation(telnet_t *telnet, unsigned char telopt,
Sean Middleditch90e79da2009-03-19 15:17:13 -0400256 const char *buffer, size_t size);
Sean Middleditch6aef0732009-03-12 23:27:35 -0400257
Sean Middleditch124a1c22009-03-15 13:20:03 -0400258/* begin sending compressed data (server only) */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400259extern void telnet_begin_compress2(telnet_t *telnet);
Sean Middleditch124a1c22009-03-15 13:20:03 -0400260
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400261/* send formatted data with \r and \n translated, and IAC escaped */
Sean Middleditche22b4772009-03-22 16:44:40 -0400262extern int telnet_printf(telnet_t *telnet, const char *fmt, ...)
263 TELNET_GNU_PRINTF(2, 3);
Sean Middleditch4a156042009-03-16 17:10:58 -0400264
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400265/* send formatted data with just IAC escaped */
Sean Middleditche22b4772009-03-22 16:44:40 -0400266extern int telnet_printf2(telnet_t *telnet, const char *fmt, ...)
267 TELNET_GNU_PRINTF(2, 3);
268
Sean Middleditcheb950a82009-03-22 23:04:28 -0400269/* send TTYPE/ENVIRON/NEW-ENVIRON/MSSP data */
270extern void telnet_format_sb(telnet_t *telnet, unsigned char telopt,
271 size_t count, ...);
272
Sean Middleditche22b4772009-03-22 16:44:40 -0400273/* send ZMP commands */
274extern void telnet_send_zmp(telnet_t *telnet, size_t argc, const char **argv);
275extern void telnet_send_zmpv(telnet_t *telnet, ...) TELNET_GNU_SENTINEL;
Sean Middleditchd58f49f2009-03-16 12:49:35 -0400276
Sean Middleditch6aef0732009-03-12 23:27:35 -0400277#endif /* !defined(LIBTELNET_INCLUDE) */