blob: eae0a863f549ade8cebe575493b4e8a1318e496b [file] [log] [blame]
Sean Middleditch29144852009-03-12 23:14:47 -04001/*
Sean Middleditch55655962009-10-24 15:38:46 -07002 * libtelnet - TELNET protocol handling library
Sean Middleditchae39cee2009-03-22 22:47:30 -04003 *
Sean Middleditch9de15982009-03-14 03:35:49 -04004 * Sean Middleditch
5 * sean@sourcemud.org
6 *
Sean Middleditch29144852009-03-12 23:14:47 -04007 * 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 Middleditch6aef0732009-03-12 23:27:35 -040014#if !defined(LIBTELNET_INCLUDE)
Sean Middleditchaac2c122009-03-14 18:31:26 -040015#define LIBTELNET_INCLUDE 1
Sean Middleditch6aef0732009-03-12 23:27:35 -040016
Sean Middleditch991f5362010-01-22 16:12:13 -080017/* C++ support */
18#if defined(__cplusplus)
19extern "C" {
20#endif
21
Sean Middleditche22b4772009-03-22 16:44:40 -040022/* 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 Middleditch30323022009-03-14 21:45:28 -040031/* forward declarations */
Sean Middleditchf65f27d2009-03-19 02:32:04 -040032typedef struct telnet_t telnet_t;
33typedef struct telnet_event_t telnet_event_t;
Sean Middleditch34bb0992009-03-21 00:20:44 -040034typedef struct telnet_telopt_t telnet_telopt_t;
Sean Middleditch30323022009-03-14 21:45:28 -040035
Sean Middleditch29144852009-03-12 23:14:47 -040036/* telnet special values */
Sean Middleditchf65f27d2009-03-19 02:32:04 -040037#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 Middleditchf65f27d2009-03-19 02:32:04 -040043#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 Middleditch29144852009-03-12 23:14:47 -040057
58/* telnet options */
Sean Middleditchf65f27d2009-03-19 02:32:04 -040059#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 Middleditche2122b22009-03-20 23:55:09 -040099#define TELNET_TELOPT_MSSP 70
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400100#define TELNET_TELOPT_COMPRESS2 86
101#define TELNET_TELOPT_ZMP 93
102#define TELNET_TELOPT_EXOPL 255
Sean Middleditch29144852009-03-12 23:14:47 -0400103
Sean Middleditche2122b22009-03-20 23:55:09 -0400104#define TELNET_TELOPT_MCCP2 86
105
Sean Middleditch2d5c4992009-03-22 22:21:42 -0400106/* 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 Middleditch08bb05f2009-03-15 23:29:46 -0400121/* libtelnet feature flags */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400122#define TELNET_FLAG_PROXY (1<<0)
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400123
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400124#define TELNET_PFLAG_DEFLATE (1<<7)
Sean Middleditch29144852009-03-12 23:14:47 -0400125
Sean Middleditch29144852009-03-12 23:14:47 -0400126/* error codes */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400127enum 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 Middleditch29144852009-03-12 23:14:47 -0400134};
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400135typedef enum telnet_error_t telnet_error_t;
Sean Middleditch29144852009-03-12 23:14:47 -0400136
Sean Middleditch637df7f2009-03-15 12:57:32 -0400137/* event codes */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400138enum 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 Middleditch30323022009-03-14 21:45:28 -0400150};
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400151typedef enum telnet_event_type_t telnet_event_type_t;
Sean Middleditch30323022009-03-14 21:45:28 -0400152
Sean Middleditch637df7f2009-03-15 12:57:32 -0400153/* event information */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400154struct telnet_event_t {
Sean Middleditche5327da2009-03-21 00:54:50 -0400155 /* ZMP argument list */
156 const char **argv;
157 size_t argc;
Sean Middleditch637df7f2009-03-15 12:57:32 -0400158 /* data buffer: for DATA, SEND, SUBNEGOTIATION, and ERROR events */
Sean Middleditch8daf7742009-03-19 02:05:24 -0400159 const char *buffer;
Sean Middleditch340a51b2009-03-19 02:08:46 -0400160 size_t size;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400161 /* type of event */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400162 enum telnet_event_type_t type;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400163 /* IAC command */
164 unsigned char command;
165 /* telopt info: for negotiation events SUBNEGOTIATION */
166 unsigned char telopt;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400167};
168
Sean Middleditch637df7f2009-03-15 12:57:32 -0400169/* event handler declaration */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400170typedef void (*telnet_event_handler_t)(telnet_t *telnet,
171 telnet_event_t *event, void *user_data);
Sean Middleditch637df7f2009-03-15 12:57:32 -0400172
Sean Middleditch34bb0992009-03-21 00:20:44 -0400173/* telopt support table element; use telopt of -1 for end marker */
174struct telnet_telopt_t {
Sean Middleditchbfc641e2009-03-22 16:26:06 -0400175 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 Middleditch34bb0992009-03-21 00:20:44 -0400178};
179
Sean Middleditchd2466a02009-09-19 14:35:48 -0700180/* state tracker -- private data structure */
181struct telnet_t;
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400182
Sean Middleditch29144852009-03-12 23:14:47 -0400183/* initialize a telnet state tracker */
Sean Middleditchd2466a02009-09-19 14:35:48 -0700184extern telnet_t* telnet_init(const telnet_telopt_t *telopts,
Sean Middleditch34bb0992009-03-21 00:20:44 -0400185 telnet_event_handler_t eh, unsigned char flags, void *user_data);
Sean Middleditch29144852009-03-12 23:14:47 -0400186
187/* free up any memory allocated by a state tracker */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400188extern void telnet_free(telnet_t *telnet);
Sean Middleditch29144852009-03-12 23:14:47 -0400189
Sean Middleditch29144852009-03-12 23:14:47 -0400190/* push a byte buffer into the state tracker */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400191extern void telnet_recv(telnet_t *telnet, const char *buffer,
Sean Middleditch340a51b2009-03-19 02:08:46 -0400192 size_t size);
Sean Middleditch29144852009-03-12 23:14:47 -0400193
194/* send an iac command */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400195extern void telnet_iac(telnet_t *telnet, unsigned char cmd);
Sean Middleditch29144852009-03-12 23:14:47 -0400196
Sean Middleditch2b4bfc42009-03-16 01:25:52 -0400197/* send negotiation, with RFC1143 checking.
198 * will not actually send unless necessary, but will update internal
199 * negotiation queue.
200 */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400201extern void telnet_negotiate(telnet_t *telnet, unsigned char cmd,
Sean Middleditch812358d2009-03-15 23:24:03 -0400202 unsigned char opt);
Sean Middleditch29144852009-03-12 23:14:47 -0400203
204/* send non-command data (escapes IAC bytes) */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400205extern void telnet_send(telnet_t *telnet,
Sean Middleditch340a51b2009-03-19 02:08:46 -0400206 const char *buffer, size_t size);
Sean Middleditch29144852009-03-12 23:14:47 -0400207
Sean Middleditch90e79da2009-03-19 15:17:13 -0400208/* send IAC SB followed by the telopt code */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400209extern void telnet_begin_sb(telnet_t *telnet,
Sean Middleditch90e79da2009-03-19 15:17:13 -0400210 unsigned char telopt);
211
212/* send IAC SE */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400213#define telnet_finish_sb(telnet) telnet_iac((telnet), TELNET_SE)
Sean Middleditch90e79da2009-03-19 15:17:13 -0400214
215/* shortcut for sending a complete subnegotiation buffer.
216 * equivalent to:
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400217 * telnet_begin_sb(telnet, telopt);
218 * telnet_send(telnet, buffer, size);
219 * telnet_finish_sb(telnet);
Sean Middleditch2b4bfc42009-03-16 01:25:52 -0400220 */
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400221extern void telnet_subnegotiation(telnet_t *telnet, unsigned char telopt,
Sean Middleditch90e79da2009-03-19 15:17:13 -0400222 const char *buffer, size_t size);
Sean Middleditch6aef0732009-03-12 23:27:35 -0400223
Sean Middleditch124a1c22009-03-15 13:20:03 -0400224/* begin sending compressed data (server only) */
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400225extern void telnet_begin_compress2(telnet_t *telnet);
Sean Middleditch124a1c22009-03-15 13:20:03 -0400226
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400227/* send formatted data with \r and \n translated, and IAC escaped */
Sean Middleditche22b4772009-03-22 16:44:40 -0400228extern int telnet_printf(telnet_t *telnet, const char *fmt, ...)
229 TELNET_GNU_PRINTF(2, 3);
Sean Middleditch4a156042009-03-16 17:10:58 -0400230
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400231/* send formatted data with just IAC escaped */
Sean Middleditch9dcfd4b2009-10-14 16:17:46 -0700232extern int telnet_raw_printf(telnet_t *telnet, const char *fmt, ...)
Sean Middleditche22b4772009-03-22 16:44:40 -0400233 TELNET_GNU_PRINTF(2, 3);
234
Sean Middleditcheb950a82009-03-22 23:04:28 -0400235/* send TTYPE/ENVIRON/NEW-ENVIRON/MSSP data */
236extern void telnet_format_sb(telnet_t *telnet, unsigned char telopt,
237 size_t count, ...);
238
Sean Middleditche22b4772009-03-22 16:44:40 -0400239/* send ZMP commands */
240extern void telnet_send_zmp(telnet_t *telnet, size_t argc, const char **argv);
241extern void telnet_send_zmpv(telnet_t *telnet, ...) TELNET_GNU_SENTINEL;
Sean Middleditchd58f49f2009-03-16 12:49:35 -0400242
Sean Middleditch991f5362010-01-22 16:12:13 -0800243/* C++ support */
244#if defined(__cplusplus)
245} /* extern "C" */
246#endif
247
Sean Middleditch6aef0732009-03-12 23:27:35 -0400248#endif /* !defined(LIBTELNET_INCLUDE) */