blob: c9f25832949ac250b85a0c483f0e7dab56537396 [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 Middleditch30323022009-03-14 21:45:28 -040015/* forward declarations */
Sean Middleditchf65f27d2009-03-19 02:32:04 -040016typedef struct telnet_t telnet_t;
17typedef struct telnet_event_t telnet_event_t;
Sean Middleditch34bb0992009-03-21 00:20:44 -040018typedef struct telnet_telopt_t telnet_telopt_t;
Sean Middleditch30323022009-03-14 21:45:28 -040019
Sean Middleditch29144852009-03-12 23:14:47 -040020/* telnet special values */
Sean Middleditchf65f27d2009-03-19 02:32:04 -040021#define TELNET_IAC 255
22#define TELNET_DONT 254
23#define TELNET_DO 253
24#define TELNET_WONT 252
25#define TELNET_WILL 251
26#define TELNET_SB 250
27#define TELNET_SB 250
28#define TELNET_GA 249
29#define TELNET_EL 248
30#define TELNET_EC 247
31#define TELNET_AYT 246
32#define TELNET_AO 245
33#define TELNET_IP 244
34#define TELNET_BREAK 243
35#define TELNET_DM 242
36#define TELNET_NOP 241
37#define TELNET_SE 240
38#define TELNET_EOR 239
39#define TELNET_ABORT 238
40#define TELNET_SUSP 237
41#define TELNET_EOF 236
Sean Middleditch29144852009-03-12 23:14:47 -040042
43/* telnet options */
Sean Middleditchf65f27d2009-03-19 02:32:04 -040044#define TELNET_TELOPT_BINARY 0
45#define TELNET_TELOPT_ECHO 1
46#define TELNET_TELOPT_RCP 2
47#define TELNET_TELOPT_SGA 3
48#define TELNET_TELOPT_NAMS 4
49#define TELNET_TELOPT_STATUS 5
50#define TELNET_TELOPT_TM 6
51#define TELNET_TELOPT_RCTE 7
52#define TELNET_TELOPT_NAOL 8
53#define TELNET_TELOPT_NAOP 9
54#define TELNET_TELOPT_NAOCRD 10
55#define TELNET_TELOPT_NAOHTS 11
56#define TELNET_TELOPT_NAOHTD 12
57#define TELNET_TELOPT_NAOFFD 13
58#define TELNET_TELOPT_NAOVTS 14
59#define TELNET_TELOPT_NAOVTD 15
60#define TELNET_TELOPT_NAOLFD 16
61#define TELNET_TELOPT_XASCII 17
62#define TELNET_TELOPT_LOGOUT 18
63#define TELNET_TELOPT_BM 19
64#define TELNET_TELOPT_DET 20
65#define TELNET_TELOPT_SUPDUP 21
66#define TELNET_TELOPT_SUPDUPOUTPUT 22
67#define TELNET_TELOPT_SNDLOC 23
68#define TELNET_TELOPT_TTYPE 24
69#define TELNET_TELOPT_EOR 25
70#define TELNET_TELOPT_TUID 26
71#define TELNET_TELOPT_OUTMRK 27
72#define TELNET_TELOPT_TTYLOC 28
73#define TELNET_TELOPT_3270REGIME 29
74#define TELNET_TELOPT_X3PAD 30
75#define TELNET_TELOPT_NAWS 31
76#define TELNET_TELOPT_TSPEED 32
77#define TELNET_TELOPT_LFLOW 33
78#define TELNET_TELOPT_LINEMODE 34
79#define TELNET_TELOPT_XDISPLOC 35
80#define TELNET_TELOPT_ENVIRON 36
81#define TELNET_TELOPT_AUTHENTICATION 37
82#define TELNET_TELOPT_ENCRYPT 38
83#define TELNET_TELOPT_NEW_ENVIRON 39
Sean Middleditche2122b22009-03-20 23:55:09 -040084#define TELNET_TELOPT_MSSP 70
Sean Middleditchf65f27d2009-03-19 02:32:04 -040085#define TELNET_TELOPT_COMPRESS2 86
86#define TELNET_TELOPT_ZMP 93
87#define TELNET_TELOPT_EXOPL 255
Sean Middleditch29144852009-03-12 23:14:47 -040088
Sean Middleditche2122b22009-03-20 23:55:09 -040089#define TELNET_TELOPT_MCCP2 86
90
Sean Middleditch08bb05f2009-03-15 23:29:46 -040091/* libtelnet feature flags */
Sean Middleditchf65f27d2009-03-19 02:32:04 -040092#define TELNET_FLAG_PROXY (1<<0)
Sean Middleditch61f8eb62009-03-14 04:57:27 -040093
Sean Middleditchf65f27d2009-03-19 02:32:04 -040094#define TELNET_PFLAG_DEFLATE (1<<7)
Sean Middleditch29144852009-03-12 23:14:47 -040095
96/* telnet states */
Sean Middleditchf65f27d2009-03-19 02:32:04 -040097enum telnet_state_t {
98 TELNET_STATE_DATA = 0,
99 TELNET_STATE_IAC,
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400100 TELNET_STATE_WILL,
101 TELNET_STATE_WONT,
Sean Middleditch447d3ad2009-03-20 23:22:37 -0400102 TELNET_STATE_DO,
103 TELNET_STATE_DONT,
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400104 TELNET_STATE_SB,
105 TELNET_STATE_SB_DATA,
106 TELNET_STATE_SB_DATA_IAC
Sean Middleditch29144852009-03-12 23:14:47 -0400107};
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400108typedef enum telnet_state_t telnet_state_t;
Sean Middleditch29144852009-03-12 23:14:47 -0400109
110/* error codes */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400111enum telnet_error_t {
112 TELNET_EOK = 0,
113 TELNET_EBADVAL, /* invalid parameter, or API misuse */
114 TELNET_ENOMEM, /* memory allocation failure */
115 TELNET_EOVERFLOW, /* data exceeds buffer size */
116 TELNET_EPROTOCOL, /* invalid sequence of special bytes */
117 TELNET_ECOMPRESS /* error handling compressed streams */
Sean Middleditch29144852009-03-12 23:14:47 -0400118};
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400119typedef enum telnet_error_t telnet_error_t;
Sean Middleditch29144852009-03-12 23:14:47 -0400120
Sean Middleditch637df7f2009-03-15 12:57:32 -0400121/* event codes */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400122enum telnet_event_type_t {
123 TELNET_EV_DATA = 0,
124 TELNET_EV_SEND,
125 TELNET_EV_IAC,
126 TELNET_EV_WILL,
127 TELNET_EV_WONT,
128 TELNET_EV_DO,
129 TELNET_EV_DONT,
130 TELNET_EV_SUBNEGOTIATION,
131 TELNET_EV_COMPRESS,
132 TELNET_EV_WARNING,
133 TELNET_EV_ERROR
Sean Middleditch30323022009-03-14 21:45:28 -0400134};
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400135typedef enum telnet_event_type_t telnet_event_type_t;
Sean Middleditch30323022009-03-14 21:45:28 -0400136
Sean Middleditch637df7f2009-03-15 12:57:32 -0400137/* event information */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400138struct telnet_event_t {
Sean Middleditch637df7f2009-03-15 12:57:32 -0400139 /* data buffer: for DATA, SEND, SUBNEGOTIATION, and ERROR events */
Sean Middleditch8daf7742009-03-19 02:05:24 -0400140 const char *buffer;
Sean Middleditch340a51b2009-03-19 02:08:46 -0400141 size_t size;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400142 /* type of event */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400143 enum telnet_event_type_t type;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400144 /* IAC command */
145 unsigned char command;
146 /* telopt info: for negotiation events SUBNEGOTIATION */
147 unsigned char telopt;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400148};
149
Sean Middleditch637df7f2009-03-15 12:57:32 -0400150/* event handler declaration */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400151typedef void (*telnet_event_handler_t)(telnet_t *telnet,
152 telnet_event_t *event, void *user_data);
Sean Middleditch637df7f2009-03-15 12:57:32 -0400153
Sean Middleditch34bb0992009-03-21 00:20:44 -0400154/* telopt support table element; use telopt of -1 for end marker */
155struct telnet_telopt_t {
156 short telopt;
157 short us:1, him:1;
158};
159
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400160/* state tracker */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400161struct telnet_t {
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400162 /* user data */
163 void *ud;
Sean Middleditch34bb0992009-03-21 00:20:44 -0400164 /* telopt support table */
165 const telnet_telopt_t *telopts;
Sean Middleditch637df7f2009-03-15 12:57:32 -0400166 /* event handler */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400167 telnet_event_handler_t eh;
Sean Middleditch9de15982009-03-14 03:35:49 -0400168#ifdef HAVE_ZLIB
Sean Middleditch30323022009-03-14 21:45:28 -0400169 /* zlib (mccp2) compression */
Sean Middleditchfbe93e32009-03-15 23:39:31 -0400170 z_stream *z;
Sean Middleditch9de15982009-03-14 03:35:49 -0400171#endif
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400172 /* RFC1143 option negotiation states */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400173 struct telnet_rfc1143_t *q;
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400174 /* sub-request buffer */
Sean Middleditch8daf7742009-03-19 02:05:24 -0400175 char *buffer;
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400176 /* current size of the buffer */
Sean Middleditch340a51b2009-03-19 02:08:46 -0400177 size_t buffer_size;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400178 /* current buffer write position (also length of buffer data) */
Sean Middleditch340a51b2009-03-19 02:08:46 -0400179 size_t buffer_pos;
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400180 /* current state */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400181 enum telnet_state_t state;
Sean Middleditch08bb05f2009-03-15 23:29:46 -0400182 /* option flags */
183 unsigned char flags;
Sean Middleditchda0e6952009-03-15 13:28:09 -0400184 /* current subnegotiation telopt */
185 unsigned char sb_telopt;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400186 /* length of RFC1143 queue */
187 unsigned char q_size;
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400188};
189
Sean Middleditch29144852009-03-12 23:14:47 -0400190/* initialize a telnet state tracker */
Sean Middleditch34bb0992009-03-21 00:20:44 -0400191extern void telnet_init(telnet_t *telnet, const telnet_telopt_t *telopts,
192 telnet_event_handler_t eh, unsigned char flags, void *user_data);
Sean Middleditch29144852009-03-12 23:14:47 -0400193
194/* free up any memory allocated by a state tracker */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400195extern void telnet_free(telnet_t *telnet);
Sean Middleditch29144852009-03-12 23:14:47 -0400196
Sean Middleditch29144852009-03-12 23:14:47 -0400197/* push a byte buffer into the state tracker */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400198extern void telnet_recv(telnet_t *telnet, const char *buffer,
Sean Middleditch340a51b2009-03-19 02:08:46 -0400199 size_t size);
Sean Middleditch29144852009-03-12 23:14:47 -0400200
201/* send an iac command */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400202extern void telnet_iac(telnet_t *telnet, unsigned char cmd);
Sean Middleditch29144852009-03-12 23:14:47 -0400203
Sean Middleditch2b4bfc42009-03-16 01:25:52 -0400204/* send negotiation, with RFC1143 checking.
205 * will not actually send unless necessary, but will update internal
206 * negotiation queue.
207 */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400208extern void telnet_negotiate(telnet_t *telnet, unsigned char cmd,
Sean Middleditch812358d2009-03-15 23:24:03 -0400209 unsigned char opt);
Sean Middleditch29144852009-03-12 23:14:47 -0400210
211/* send non-command data (escapes IAC bytes) */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400212extern void telnet_send(telnet_t *telnet,
Sean Middleditch340a51b2009-03-19 02:08:46 -0400213 const char *buffer, size_t size);
Sean Middleditch29144852009-03-12 23:14:47 -0400214
Sean Middleditch90e79da2009-03-19 15:17:13 -0400215/* send IAC SB followed by the telopt code */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400216extern void telnet_begin_sb(telnet_t *telnet,
Sean Middleditch90e79da2009-03-19 15:17:13 -0400217 unsigned char telopt);
218
219/* send IAC SE */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400220#define telnet_finish_sb(telnet) telnet_iac((telnet), TELNET_SE)
Sean Middleditch90e79da2009-03-19 15:17:13 -0400221
222/* shortcut for sending a complete subnegotiation buffer.
223 * equivalent to:
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400224 * telnet_begin_sb(telnet, telopt);
225 * telnet_send(telnet, buffer, size);
226 * telnet_finish_sb(telnet);
Sean Middleditch2b4bfc42009-03-16 01:25:52 -0400227 */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400228extern void telnet_subnegotiation(telnet_t *telnet, unsigned char telopt,
Sean Middleditch90e79da2009-03-19 15:17:13 -0400229 const char *buffer, size_t size);
Sean Middleditch6aef0732009-03-12 23:27:35 -0400230
Sean Middleditch124a1c22009-03-15 13:20:03 -0400231/* begin sending compressed data (server only) */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400232extern void telnet_begin_compress2(telnet_t *telnet);
Sean Middleditch124a1c22009-03-15 13:20:03 -0400233
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400234/* printf type checking feature in GCC and some other compilers */
Sean Middleditchb7bf8f32009-03-19 18:18:22 -0400235#if __GNUC__
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400236# define TELNET_GNU_PRINTF(f,a) __attribute__((printf(f, a)))
Sean Middleditchd58f49f2009-03-16 12:49:35 -0400237#else
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400238# define TELNET_GNU_PRINTF(f,a)
Sean Middleditchd58f49f2009-03-16 12:49:35 -0400239#endif
240
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400241/* send formatted data with \r and \n translated, and IAC escaped */
242extern int telnet_printf(telnet_t *telnet, const char *fmt, ...);
Sean Middleditch4a156042009-03-16 17:10:58 -0400243
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400244/* send formatted data with just IAC escaped */
245extern int telnet_printf2(telnet_t *telnet, const char *fmt, ...);
Sean Middleditchd58f49f2009-03-16 12:49:35 -0400246
Sean Middleditch6aef0732009-03-12 23:27:35 -0400247#endif /* !defined(LIBTELNET_INCLUDE) */