blob: 6c872b812afffae31d8ff919900078f2744f6297 [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 */
16struct libtelnet_t;
17struct libtelnet_cb_t;
18
Sean Middleditch29144852009-03-12 23:14:47 -040019/* telnet special values */
20#define LIBTELNET_IAC 255
21#define LIBTELNET_DONT 254
22#define LIBTELNET_DO 253
23#define LIBTELNET_WONT 252
24#define LIBTELNET_WILL 251
25#define LIBTELNET_SB 250
Sean Middleditchc04224b2009-03-14 18:30:57 -040026#define LIBTELNET_SB 250
27#define LIBTELNET_GA 249
28#define LIBTELNET_EL 248
29#define LIBTELNET_EC 247
30#define LIBTELNET_AYT 246
31#define LIBTELNET_AO 245
32#define LIBTELNET_IP 244
33#define LIBTELNET_BREAK 243
34#define LIBTELNET_DM 242
35#define LIBTELNET_NOP 241
Sean Middleditch29144852009-03-12 23:14:47 -040036#define LIBTELNET_SE 240
Sean Middleditchc04224b2009-03-14 18:30:57 -040037#define LIBTELNET_EOR 239
38#define LIBTELNET_ABORT 238
39#define LIBTELNET_SUSP 237
40#define LIBTELNET_EOF 236
Sean Middleditch29144852009-03-12 23:14:47 -040041
42/* telnet options */
Sean Middleditchc04224b2009-03-14 18:30:57 -040043#define LIBTELNET_TELOPT_BINARY 0
44#define LIBTELNET_TELOPT_ECHO 1
45#define LIBTELNET_TELOPT_RCP 2
46#define LIBTELNET_TELOPT_SGA 3
47#define LIBTELNET_TELOPT_NAMS 4
48#define LIBTELNET_TELOPT_STATUS 5
49#define LIBTELNET_TELOPT_TM 6
50#define LIBTELNET_TELOPT_RCTE 7
51#define LIBTELNET_TELOPT_NAOL 8
52#define LIBTELNET_TELOPT_NAOP 9
53#define LIBTELNET_TELOPT_NAOCRD 10
54#define LIBTELNET_TELOPT_NAOHTS 11
55#define LIBTELNET_TELOPT_NAOHTD 12
56#define LIBTELNET_TELOPT_NAOFFD 13
57#define LIBTELNET_TELOPT_NAOVTS 14
58#define LIBTELNET_TELOPT_NAOVTD 15
59#define LIBTELNET_TELOPT_NAOLFD 16
60#define LIBTELNET_TELOPT_XASCII 17
61#define LIBTELNET_TELOPT_LOGOUT 18
62#define LIBTELNET_TELOPT_BM 19
63#define LIBTELNET_TELOPT_DET 20
64#define LIBTELNET_TELOPT_SUPDUP 21
65#define LIBTELNET_TELOPT_SUPDUPOUTPUT 22
66#define LIBTELNET_TELOPT_SNDLOC 23
67#define LIBTELNET_TELOPT_TTYPE 24
68#define LIBTELNET_TELOPT_EOR 25
69#define LIBTELNET_TELOPT_TUID 26
70#define LIBTELNET_TELOPT_OUTMRK 27
71#define LIBTELNET_TELOPT_TTYLOC 28
72#define LIBTELNET_TELOPT_3270REGIME 29
73#define LIBTELNET_TELOPT_X3PAD 30
74#define LIBTELNET_TELOPT_NAWS 31
75#define LIBTELNET_TELOPT_TSPEED 32
76#define LIBTELNET_TELOPT_LFLOW 33
77#define LIBTELNET_TELOPT_LINEMODE 34
78#define LIBTELNET_TELOPT_XDISPLOC 35
79#define LIBTELNET_TELOPT_ENVIRON 36
80#define LIBTELNET_TELOPT_AUTHENTICATION 37
81#define LIBTELNET_TELOPT_ENCRYPT 38
82#define LIBTELNET_TELOPT_NEW_ENVIRON 39
83#define LIBTELNET_TELOPT_COMPRESS 85
84#define LIBTELNET_TELOPT_COMPRESS2 86
85#define LIBTELNET_TELOPT_ZMP 93
86#define LIBTELNET_TELOPT_EXOPL 255
Sean Middleditch29144852009-03-12 23:14:47 -040087
Sean Middleditch61f8eb62009-03-14 04:57:27 -040088/* libtelnet modes */
89enum libtelnet_mode_t {
90 LIBTELNET_MODE_SERVER = 0,
Sean Middleditch72cc9642009-03-15 11:50:36 -040091 LIBTELNET_MODE_CLIENT,
92 LIBTELNET_MODE_PROXY
Sean Middleditch61f8eb62009-03-14 04:57:27 -040093};
94
Sean Middleditch29144852009-03-12 23:14:47 -040095/* telnet states */
96enum libtelnet_state_t {
Sean Middleditch9de15982009-03-14 03:35:49 -040097 LIBTELNET_STATE_DATA = 0,
Sean Middleditch29144852009-03-12 23:14:47 -040098 LIBTELNET_STATE_IAC,
99 LIBTELNET_STATE_DO,
100 LIBTELNET_STATE_DONT,
101 LIBTELNET_STATE_WILL,
102 LIBTELNET_STATE_WONT,
103 LIBTELNET_STATE_SB,
Sean Middleditchda0e6952009-03-15 13:28:09 -0400104 LIBTELNET_STATE_SB_DATA,
105 LIBTELNET_STATE_SB_DATA_IAC
Sean Middleditch29144852009-03-12 23:14:47 -0400106};
107
108/* error codes */
109enum libtelnet_error_t {
Sean Middleditchf66a7ee2009-03-15 11:54:07 -0400110 LIBTELNET_EOK = 0,
Sean Middleditch16992272009-03-15 19:42:03 -0400111 LIBTELNET_EBADVAL, /* invalid parameter, or API misuse */
Sean Middleditchf66a7ee2009-03-15 11:54:07 -0400112 LIBTELNET_ENOMEM, /* memory allocation failure */
113 LIBTELNET_EOVERFLOW, /* data exceeds buffer size */
114 LIBTELNET_EPROTOCOL, /* invalid sequence of special bytes */
Sean Middleditch16992272009-03-15 19:42:03 -0400115 LIBTELNET_ECOMPRESS /* error handling compressed streams */
Sean Middleditch29144852009-03-12 23:14:47 -0400116};
117
Sean Middleditch637df7f2009-03-15 12:57:32 -0400118/* event codes */
119enum libtelnet_event_type_t {
120 LIBTELNET_EV_DATA = 0,
121 LIBTELNET_EV_SEND,
122 LIBTELNET_EV_IAC,
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400123 LIBTELNET_EV_WILL,
124 LIBTELNET_EV_WONT,
125 LIBTELNET_EV_DO,
126 LIBTELNET_EV_DONT,
Sean Middleditch637df7f2009-03-15 12:57:32 -0400127 LIBTELNET_EV_SUBNEGOTIATION,
128 LIBTELNET_EV_COMPRESS,
Sean Middleditch16992272009-03-15 19:42:03 -0400129 LIBTELNET_EV_WARNING,
Sean Middleditch637df7f2009-03-15 12:57:32 -0400130 LIBTELNET_EV_ERROR
Sean Middleditch30323022009-03-14 21:45:28 -0400131};
132
Sean Middleditch637df7f2009-03-15 12:57:32 -0400133/* event information */
134struct libtelnet_event_t {
Sean Middleditch637df7f2009-03-15 12:57:32 -0400135 /* data buffer: for DATA, SEND, SUBNEGOTIATION, and ERROR events */
136 unsigned char *buffer;
137 unsigned int size;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400138 /* type of event */
139 enum libtelnet_event_type_t type;
140 /* IAC command */
141 unsigned char command;
142 /* telopt info: for negotiation events SUBNEGOTIATION */
143 unsigned char telopt;
144 /* accept status: for WILL and DO events */
145 unsigned char accept;
146};
147
148/* option negotiation state (RFC 1143) */
149struct libtelnet_rfc1143_t {
150 unsigned char telopt;
151 char us:4, him:4;
Sean Middleditch637df7f2009-03-15 12:57:32 -0400152};
153
154/* event handler declaration */
155typedef void (*libtelnet_event_handler_t)(struct libtelnet_t *telnet,
156 struct libtelnet_event_t *event, void *user_data);
157
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400158/* state tracker */
159struct libtelnet_t {
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400160 /* user data */
161 void *ud;
Sean Middleditch637df7f2009-03-15 12:57:32 -0400162 /* event handler */
163 libtelnet_event_handler_t eh;
Sean Middleditch9de15982009-03-14 03:35:49 -0400164#ifdef HAVE_ZLIB
Sean Middleditch30323022009-03-14 21:45:28 -0400165 /* zlib (mccp2) compression */
Sean Middleditch72cc9642009-03-15 11:50:36 -0400166 z_stream *z_deflate;
167 z_stream *z_inflate;
Sean Middleditch9de15982009-03-14 03:35:49 -0400168#endif
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400169 /* RFC1143 option negotiation states */
170 struct libtelnet_rfc1143_t *q;
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400171 /* sub-request buffer */
172 unsigned char *buffer;
173 /* current size of the buffer */
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400174 unsigned int buffer_size;
175 /* current buffer write position (also length of buffer data) */
176 unsigned int buffer_pos;
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400177 /* current state */
178 enum libtelnet_state_t state;
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400179 /* processing mode */
180 enum libtelnet_mode_t mode;
Sean Middleditchda0e6952009-03-15 13:28:09 -0400181 /* current subnegotiation telopt */
182 unsigned char sb_telopt;
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400183 /* length of RFC1143 queue */
184 unsigned char q_size;
Sean Middleditch4d9444d2009-03-13 22:48:05 -0400185};
186
Sean Middleditch29144852009-03-12 23:14:47 -0400187/* initialize a telnet state tracker */
Sean Middleditch61f8eb62009-03-14 04:57:27 -0400188extern void libtelnet_init(struct libtelnet_t *telnet,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400189 libtelnet_event_handler_t eh, enum libtelnet_mode_t mode,
190 void *user_data);
Sean Middleditch29144852009-03-12 23:14:47 -0400191
192/* free up any memory allocated by a state tracker */
Sean Middleditch51ad6792009-03-13 20:15:59 -0400193extern void libtelnet_free(struct libtelnet_t *telnet);
Sean Middleditch29144852009-03-12 23:14:47 -0400194
Sean Middleditch29144852009-03-12 23:14:47 -0400195/* push a byte buffer into the state tracker */
Sean Middleditch8b5e2b12009-03-13 23:39:18 -0400196extern void libtelnet_push(struct libtelnet_t *telnet,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400197 unsigned char *buffer, unsigned int size);
Sean Middleditch29144852009-03-12 23:14:47 -0400198
199/* send an iac command */
Sean Middleditchb1e452e2009-03-12 23:26:34 -0400200extern void libtelnet_send_command(struct libtelnet_t *telnet,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400201 unsigned char cmd);
Sean Middleditch29144852009-03-12 23:14:47 -0400202
203/* send negotiation */
Sean Middleditchb1e452e2009-03-12 23:26:34 -0400204extern void libtelnet_send_negotiate(struct libtelnet_t *telnet,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400205 unsigned char cmd, unsigned char opt);
Sean Middleditch29144852009-03-12 23:14:47 -0400206
207/* send non-command data (escapes IAC bytes) */
Sean Middleditchb1e452e2009-03-12 23:26:34 -0400208extern void libtelnet_send_data(struct libtelnet_t *telnet,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400209 unsigned char *buffer, unsigned int size);
Sean Middleditch29144852009-03-12 23:14:47 -0400210
211/* send sub-request */
Sean Middleditch6b372882009-03-14 13:06:47 -0400212extern void libtelnet_send_subnegotiation(struct libtelnet_t *telnet,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400213 unsigned char opt, unsigned char *buffer, unsigned int size);
Sean Middleditch6aef0732009-03-12 23:27:35 -0400214
Sean Middleditch124a1c22009-03-15 13:20:03 -0400215/* begin sending compressed data (server only) */
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400216extern void libtelnet_begin_compress2(struct libtelnet_t *telnet);
Sean Middleditch124a1c22009-03-15 13:20:03 -0400217
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400218/* return the status of a specific TELNET option on our end (US) */
219extern int libtelnet_get_telopt_local(struct libtelnet_t *telnet,
220 unsigned char telopt);
221
222/* return the status of a specific TELNET option on remote end (HIM) */
223extern int libtelnet_get_telopt_remote(struct libtelnet_t *telnet,
224 unsigned char telopt);
225
Sean Middleditch6aef0732009-03-12 23:27:35 -0400226#endif /* !defined(LIBTELNET_INCLUDE) */