blob: 1bb793d722f64b7193fa4ee644211b81f4d59a37 [file] [log] [blame]
Sean Middleditchb9e48642009-03-12 23:33:27 -04001=====================================================================
2 libtelnet - TELNET protocol handling library
3=====================================================================
4
Sean Middleditch892c5f12009-03-14 13:39:07 -04005 http://github.com/elanthis/libtelnet
6
7 Sean Middleditch
8 sean@sourcemud.org
Sean Middleditchb9e48642009-03-12 23:33:27 -04009
10---------------------------------------------------------------------
11The author or authors of this code dedicate any and all copyright
12interest in this code to the public domain. We make this dedication
13for the benefit of the public at large and to the detriment of our
14heirs and successors. We intend this dedication to be an overt act of
15relinquishment in perpetuity of all present and future rights to this
16code under copyright law.
17---------------------------------------------------------------------
18
Sean Middleditch9d2f98a2009-03-14 05:24:56 -040019*** TODO ***
20
21 - RFC 1143 option negotiation algorithm
22 - automatic MCCP2 handling (controllable by host app)
Sean Middleditch9d2f98a2009-03-14 05:24:56 -040023 ? ZMP parsing
24 ? MSSP parsing
25 ? ENVIRON/NEW-ENVIRON parsing
26 ? telnet-status testing tool
Sean Middleditch9d2f98a2009-03-14 05:24:56 -040027
Sean Middleditchb9e48642009-03-12 23:33:27 -040028I. INTRODUCTION
29=====================================================================
30
31libtelnet provides safe and correct handling of the core TELNET
Sean Middleditch8b788962009-03-16 01:06:27 -040032protocol. In addition to the base TELNET protocol, libtelnet also
33implements the Q method of TELNET option negotiation. libtelnet
34can be used for writing servers, clients, or proxies.
Sean Middleditchb9e48642009-03-12 23:33:27 -040035
Sean Middleditch892c5f12009-03-14 13:39:07 -040036For more information on the TELNET protocol, see:
37
38 http://www.faqs.org/rfcs/rfc854.html
Sean Middleditch8b788962009-03-16 01:06:27 -040039 http://www.faqs.org/rfcs/rfc1143.html
Sean Middleditch892c5f12009-03-14 13:39:07 -040040
Sean Middleditchb9e48642009-03-12 23:33:27 -040041II. LIBTELNET API
42=====================================================================
43
Sean Middleditch892c5f12009-03-14 13:39:07 -040044The libtelnet API contains several distinct parts. The first part is
45the basic initialization and deinitialization routines. The second
46part is a single function for pushing received data into the
47libtelnet processor. The third part is the libtelnet_send_*()
48functions, which generate TELNET commands and ensure data is properly
Sean Middleditch637df7f2009-03-15 12:57:32 -040049formatted before sending over the wire. The final part is the event
50handler interface.
Sean Middleditch892c5f12009-03-14 13:39:07 -040051
52IIa. Initialization
53
54 struct libtelnet_t;
55 This structure represents the state of the TELNET protocol for a
56 single connection. Each connection utilizing TELNET must have
57 its own libtelnet_t structure, which is passed to all libtelnet
58 API calls.
59
Sean Middleditch812358d2009-03-15 23:24:03 -040060 void libtelnet_init(libtelnet_t *telnet, libtelnet_event_handler_t handler,
Sean Middleditch08bb05f2009-03-15 23:29:46 -040061 unsigned char flags, void *user_data);
Sean Middleditch892c5f12009-03-14 13:39:07 -040062 The libtelnet_init() function is responsible for initializing
63 the data in a libtelnet_t structure. It must be called
64 immediately after establishing a connection and before any other
65 libtelnet API calls are made.
66
Sean Middleditch637df7f2009-03-15 12:57:32 -040067 The handler parameter must be a function matching the
68 libtelnet_event_handler_t definition. More information about
69 events can be found in section IId.
Sean Middleditch30323022009-03-14 21:45:28 -040070
Sean Middleditch9f79cc52009-03-15 13:39:24 -040071 The user_data parameter is passed to the event handler whenver it
72 is invoked. This will usually be a structure container
73 information about the connection, including a socket descriptor
74 for implementing LIBTELNET_EV_SEND event handling.
75
Sean Middleditch08bb05f2009-03-15 23:29:46 -040076 The flags parameter can be any of the following flag constants
77 bit-or'd together, or 0 to leave all options disabled.
78
79 LIBTELNET_FLAG_PROXY - operate in proxy mode
Sean Middleditch892c5f12009-03-14 13:39:07 -040080
Sean Middleditch812358d2009-03-15 23:24:03 -040081 boid libtelnet_free(libtelnet_t *telnet);
Sean Middleditch892c5f12009-03-14 13:39:07 -040082 Releases any internal memory allocated by libtelnet. This must
83 be called whenever a connection is closed, or you will incur
84 memory leaks.
85
86IIb. Receiving Data
87
Sean Middleditch812358d2009-03-15 23:24:03 -040088 void libtelnet_push(libtelnet_t *telnet,
Sean Middleditch892c5f12009-03-14 13:39:07 -040089 unsigned char *buffer, unsigned int size, void *user_data);
90 When your application receives data over the socket from the
91 remote end, it must pass the received bytes into this function.
Sean Middleditch637df7f2009-03-15 12:57:32 -040092
93 As the TELNET stream is parsed, events will be generated and
94 passed to the event handler given to libtelnet_init(). Of
95 particular interest for data receiving is the LIBTELNET_EV_DATA
96 event, which is triggered for any regular data such as user
97 input or server process output.
Sean Middleditch892c5f12009-03-14 13:39:07 -040098
99IIc. Sending Data
100
Sean Middleditch637df7f2009-03-15 12:57:32 -0400101 All of the libtelnet_send_*() functions will invoke the
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400102 LIBTELNET_EV_SEND event.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400103
104 Note: it is very important that ALL data sent to the remote end of
105 the connection be passed through libtelnet. All user input or
106 process output that you wish to send over the wire should be given
107 to libtelnet_send_data(). Do NOT send or buffer unprocessed output
108 data directly!
Sean Middleditch892c5f12009-03-14 13:39:07 -0400109
Sean Middleditch812358d2009-03-15 23:24:03 -0400110 void libtelnet_send_command(libtelnet_t *telnet, unsigned char cmd);
Sean Middleditch892c5f12009-03-14 13:39:07 -0400111 Sends a single "simple" TELNET command, such as the GO-AHEAD
112 commands (255 249).
113
Sean Middleditch812358d2009-03-15 23:24:03 -0400114 void libtelnet_send_negotiate(libtelnet_t *telnet,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400115 unsigned char cmd, unsigned char opt);
Sean Middleditch892c5f12009-03-14 13:39:07 -0400116 Sends a TELNET negotiation command. The cmd parameter must be
117 one of LIBTELNET_WILL, LIBTELNET_DONT, LIBTELNET_DO, or
118 LIBTELNET_DONT. The opt parameter is the option to
119 negotiate.
120
Sean Middleditch812358d2009-03-15 23:24:03 -0400121 void libtelnet_send_data(libtelnet_t *telnet, unsigned char *buffer,
122 unsigned int size);
Sean Middleditch892c5f12009-03-14 13:39:07 -0400123 Sends raw data, which would be either the process output from
124 a server or the user input from a client.
125
Sean Middleditch812358d2009-03-15 23:24:03 -0400126 void libtelnet_send_subnegotiation(libtelnet_t *telnet,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400127 unsigned char opt, unsigned char *buffer, unsigned int size);
Sean Middleditch892c5f12009-03-14 13:39:07 -0400128 Sends a TELNET sub-negotiation command. The opt parameter
129 is the sub-negotiation option.
130
Sean Middleditch637df7f2009-03-15 12:57:32 -0400131IId. Event Handling
Sean Middleditch892c5f12009-03-14 13:39:07 -0400132
Sean Middleditch637df7f2009-03-15 12:57:32 -0400133 libtelnet relies on an event-handling mechanism for processing
134 the parsed TELNET protocol stream as well as for buffering and
135 sending output data.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400136
Sean Middleditch637df7f2009-03-15 12:57:32 -0400137 When you initialize a libtelnet_t structure with libtelnet_init()
138 you had to pass in an event handler function. This function must
139 meet the following prototype:
Sean Middleditch30323022009-03-14 21:45:28 -0400140
Sean Middleditch637df7f2009-03-15 12:57:32 -0400141 void (libtelnet_t *telnet, libtelnet_event_t *event,
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400142 void *user_data);
143
144 The event structure is detailed below. The user_data value is the
145 pointer passed to libtelnet_init().
Sean Middleditch637df7f2009-03-15 12:57:32 -0400146
147 struct libtelnet_event_t {
Sean Middleditch637df7f2009-03-15 12:57:32 -0400148 unsigned char *buffer;
149 unsigned int size;
Sean Middleditch812358d2009-03-15 23:24:03 -0400150 libtelnet_event_type_t type;
151 unsigned char command;
152 unsigned char telopt;
153 unsigned char accept;
Sean Middleditch637df7f2009-03-15 12:57:32 -0400154 };
155
156 The enumeration values of libtelnet_event_type_t are described in
157 detail below. Whenever the the event handler is invoked, the
158 application must look at the event->type value and do any
159 necessary processing.
160
161 The only event that MUST be implemented is LIBTELNET_EV_SEND.
162 Most applications will also always want to implement the event
163 LIBTELNET_EV_DATA.
164
165 Here is an example event handler implementation which includes
166 handlers for several important events.
167
Sean Middleditch812358d2009-03-15 23:24:03 -0400168 void my_event_handler(libtelnet_t *telnet, libtelnet_event_t *ev,
169 void *user_data) {
Sean Middleditch637df7f2009-03-15 12:57:32 -0400170 struct user_info *user = (struct user_info *)user_data;
171
172 switch (ev->type) {
173 case LIBTELNET_EV_DATA:
174 process_user_input(user, event->buffer, event->size);
175 break;
176 case LIBTELNET_EV_SEND:
177 write_to_descriptor(user, event->buffer, event->size);
178 break;
179 case LIBTELNET_EV_ERROR:
180 fatal_error("TELNET error: %s", event->buffer);
181 break;
182 }
Sean Middleditch30323022009-03-14 21:45:28 -0400183 }
184
Sean Middleditch637df7f2009-03-15 12:57:32 -0400185 LIBTELNET_EV_DATA:
186 The DATA event is triggered whenever regular data (not part of
187 any special TELNET command) is received. For a client, this
188 will be process output from the server. For a server, this will
189 be input typed by the user.
Sean Middleditch30323022009-03-14 21:45:28 -0400190
Sean Middleditch637df7f2009-03-15 12:57:32 -0400191 The event->buffer value will contain the bytes received and the
192 event->size value will contain the number of bytes received.
193 Note that event->buffer is not NUL terminated!
Sean Middleditch30323022009-03-14 21:45:28 -0400194
Sean Middleditch637df7f2009-03-15 12:57:32 -0400195 NOTE: there is no guarantee that user input or server output
196 will be received in whole lines. If you wish to process data
197 a line at a time, you are responsible for buffering the data and
198 checking for line terminators yourself!
199
200 LIBTELNET_EV_SEND:
201 This event is sent whenever libtelnet has generated data that
202 must be sent over the wire to the remove end. Generally that
203 means calling send() or adding the data to your application's
204 output buffer.
Sean Middleditch30323022009-03-14 21:45:28 -0400205
Sean Middleditch637df7f2009-03-15 12:57:32 -0400206 The event->buffer value will contain the bytes to send and the
207 event->size value will contain the number of bytes to send.
208 Note that event->buffer is not NUL terminated, and may include
209 NUL characters in its data, so always use event->size!
Sean Middleditch30323022009-03-14 21:45:28 -0400210
Sean Middleditch637df7f2009-03-15 12:57:32 -0400211 NOTE: Your SEND event handler must send or buffer the data in
212 its raw form as provided by libtelnet. If you wish to perform
213 any kind of preprocessing on data you want to send to the other
214
215 LIBTELNET_EV_IAC:
216 The IAC event is triggered whenever a simple IAC command is
217 received, such as the IAC EOR (end of record, also called
218 go ahead or GA) command.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400219
Sean Middleditch637df7f2009-03-15 12:57:32 -0400220 The command received is in the event->command value.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400221
Sean Middleditch637df7f2009-03-15 12:57:32 -0400222 The necessary processing depends on the specific commands; see
223 the TELNET RFC for more information.
224
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400225 LIBTELNET_EV_WILL:
226 LIBTELNET_EV_DO:
227 The WILL and DO events are sent when a TELNET negotiation
228 command of the same name is received.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400229
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400230 WILL events are sent by the remote end when they wish to be
231 allowed to turn an option on on their end, or in confirmation
232 after you have sent a DO command to them.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400233
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400234 DO events are sent by the remote end when they wish for you
235 to turn on an option on your end, or in confirmation after you
236 have sent a WILL command to them.
237
238 In either case, the TELNET option under negotiation will be in
239 event->telopt field.
240
241 If you support the option and wish for it to be enabled you
242 must set the event->accept field to 1, unless this event is
243 a confirmation for a previous WILL/DO command you sent to the
244 remote end. If you do not set event->field to 1 then
245 libtelnet will send a rejection command back to the other end.
246
247 libtelnet manages some of the pecularities of negotiation for
248 you. For information on libtelnet's negotiation method, see:
Sean Middleditch892c5f12009-03-14 13:39:07 -0400249
250 http://www.faqs.org/rfcs/rfc1143.html
251
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400252 Examples:
253
254 You want remote end to use TTYPE, so you send DO TTYPE.
255 Remote accepts and sends WILL TTYPE.
256
257 Remote end wants you to use SGA, so they send DO_SGA.
258 You do not support SGA and set event->accept = 0.
259
260 Remote end wants to use ZMP, so they send WILL ZMP.
261 You support ZMP, so you set event->accept = 1 and enable
262 local ZMP support.
263
264 You want to use MCCP2, so you send WILL COMPRESS2.
265 Remote end accepts and sends DO COMPRESS2.
266
267 Note that in PROXY mode libtelnet will do no processing of its
268 own for you.
269
270 LIBTELNET_EV_WONT:
271 LIBTELNET_EV_DONT:
272 The WONT and DONT events are sent when the remote end of the
273 connection wishes to disable an option, when they are
274 refusing to a support an option that you have asked for, or
275 in confirmation of an option you have asked to be disabled.
276
277 Most commonly WONT and DONT events are sent as rejections of
278 features you requested by sending DO or WILL events. Receiving
279 these events means the TELNET option is not or will not be
280 supported by the remote end, so give up.
281
282 Sometimes WONT or DONT will be sent for TELNET options that are
283 already enabled, but the remote end wishes to stop using. You
284 cannot decline. These events are demands that must be complied
285 with. libtelnet will always send the appropriate response back
286 without consulting your application. These events are sent to
287 allow your application to disable its own use of the features.
288
289 In either case, the TELNET option under negotiation will be in
290 event->telopt field.
291
292 Note that in PROXY mode libtelnet will do no processing of its
293 own for you.
294
Sean Middleditch637df7f2009-03-15 12:57:32 -0400295 LIBTELNET_EV_SUBNEGOTIATION:
296 Triggered whenever a TELNET sub-negotiation has been received.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400297 Sub-negotiations include the NAWS option for communicating
298 terminal size to a server, the NEW-ENVIRON and TTYPE options
299 for negotiating terminal features, and MUD-centric protocols
300 such as ZMP, MSSP, and MCCP2.
301
Sean Middleditch637df7f2009-03-15 12:57:32 -0400302 The event->telopt value is the option under sub-negotiation.
303 The remaining data (if any) is passed in event->buffer and
304 event->size. Note that most subnegotiation commands can
305 include embedded NUL bytes in the subnegotiation data, and
306 the data event->buffer is not NUL terminated, so always use
307 the event->size value!
Sean Middleditch892c5f12009-03-14 13:39:07 -0400308
Sean Middleditch637df7f2009-03-15 12:57:32 -0400309 The meaning and necessary processing for subnegotiations are
310 defined in various TELNET RFCs and other informal
311 specifications. A subnegotiation should never be sent unless
312 the specific option has been enabled through the use of the
313 telnet negotiation feature.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400314
Sean Middleditch637df7f2009-03-15 12:57:32 -0400315 LIBTELNET_EV_COMPRESS
316 The COMPRESS event notifies the app that COMPRESS2/MCCP2
317 compression has begun or ended. Only servers can send compressed
318 data, and hence only clients will receive compressed data.
Sean Middleditchb9e48642009-03-12 23:33:27 -0400319
Sean Middleditch637df7f2009-03-15 12:57:32 -0400320 The event->command value will be 1 if compression has started and
321 will be 0 if compression has ended.
Sean Middleditch16992272009-03-15 19:42:03 -0400322
323 LIBTELNET_EV_WARNING
324 The WARNING event is sent whenever something has gone wrong
325 inside of libtelnet (possibly due to malformed data sent by the
326 other end) but which recovery is (likely) possible. It may be
327 safe to continue using the connection, but some data may have
328 been lost or incorrectly interpreted.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400329
330 The event->buffer value will contain a NUL terminated string
331 explaining the error, and the event->size value containers the
332 length of the string.
333
Sean Middleditch16992272009-03-15 19:42:03 -0400334 LIBTELNET_EV_ERROR
335 Similar to the WARNING event, the ERROR event is sent whenever
336 something has gone wrong. ERROR events are non-recoverable,
337 however, and the application should immediately close the
338 connection. Whatever has happened is likely going only to
339 result in garbage from libtelnet. This is most likely to
340 happen when a COMPRESS2 stream fails, but other problems can
341 occur.
342
343 The event->buffer value will contain a NUL terminated string
344 explaining the error, and the event->size value containers the
345 length of the string.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400346
347III. INTEGRATING LIBTELNET WITH COMMON MUDS
Sean Middleditchb9e48642009-03-12 23:33:27 -0400348=====================================================================
349
Sean Middleditch637df7f2009-03-15 12:57:32 -0400350FIXME: fill in some notes about how to splice in libtelnet with
351common Diku/Merc/Circle/etc. MUD codebases.
Sean Middleditchb9e48642009-03-12 23:33:27 -0400352
Sean Middleditch892c5f12009-03-14 13:39:07 -0400353IV. SAFETY AND CORRECTNESS CONSIDERATIONS
Sean Middleditchb9e48642009-03-12 23:33:27 -0400354=====================================================================
355
Sean Middleditch892c5f12009-03-14 13:39:07 -0400356Your existing application may make heavy use of its own output
357buffering and transmission commands, including hand-made routines
358for sending TELNET commands and sub-negotiation requests. There are
359at times subtle issues that need to be handled when communication
360over the TELNET protocol, not least of which is the need to escape
361any byte value 0xFF with a special TELNET command.
Sean Middleditchb9e48642009-03-12 23:33:27 -0400362
Sean Middleditch892c5f12009-03-14 13:39:07 -0400363For these reasons, it is very important that applications making use
364of libtelnet always make use of the libtelnet_send_*() family of
365functions for all data being sent over the TELNET connection.
366
Sean Middleditch637df7f2009-03-15 12:57:32 -0400367In particular, if you are writing a client, all user input must be
368passed through to libtelnet_send_data(). This also includes any
369input generated automatically by scripts, triggers, or macros.
370
371For a server, any and all output -- including ANSI/VT100 escape
372codes, regular text, newlines, and so on -- must be passed through
373to libtelnet_send_data().
374
375Any TELNET commands that are to be sent must be given to one of the
376following: libtelnet_send_command, libtelnet_send_negotiate, or
377libtelnet_send_subnegotiation().
378
379If you are attempting to enable COMPRESS2/MCCP2, you must use the
380libtelnet_begin_compress2() function.
381
Sean Middleditch892c5f12009-03-14 13:39:07 -0400382V. MCCP2 COMPRESSION
Sean Middleditchb9e48642009-03-12 23:33:27 -0400383=====================================================================
384
Sean Middleditch892c5f12009-03-14 13:39:07 -0400385The MCCP2 (COMPRESS2) TELNET extension allows for the compression of
386all traffic sent from server to client. For more information:
387
388 http://www.mudbytes.net/index.php?a=articles&s=mccp
389
Sean Middleditch637df7f2009-03-15 12:57:32 -0400390In order for libtelnet to support MCCP2, zlib must be installed and
391enabled when compiling libtelnet. Use -DHAVE_ZLIB to enable zlib
392when compiling libtelnet.c and pass -lz to the linker to link in the
393zlib shared library.
394
Sean Middleditch892c5f12009-03-14 13:39:07 -0400395libtelnet transparently supports MCCP2. For a server to support
396MCCP2, the application must begin negotiation of the COMPRESS2
397option using libtelnet_send_negotiate(), for example:
398
399 libtelnet_send_negotiate(&telnet, LIBTELNET_WILL,
400 LIBTELNET_OPTION_COMPRESS2, user_data);
401
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400402If a favorable DO COMPRESS2 is sent back from the client then the
403server application can begin compression at any time by calling
404libtelnet_begin_compress2().
Sean Middleditch892c5f12009-03-14 13:39:07 -0400405
Sean Middleditch637df7f2009-03-15 12:57:32 -0400406If a connection is in PROXY mode and COMPRESS2 support is enabled
407then libtelnet will automatically detect the start of a COMPRESS2
408stream, in either the sending or receiving direction.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400409
410VI. TELNET PROXY UTILITY
411=====================================================================
412
413The telnet-proxy utility is a small application that serves both as
414a testbed for libtelnet and as a powerful debugging tool for TELNET
415servers and clients.
416
417To use telnet-proxy, you must first compile it using:
418
419 $ make
420
421If you do not have zlib installed and wish to disable MCCP2 support
422then you must first edit the Makefile and remove the -DHAVE_ZLIB and
423the -lz from the compile flags.
424
Sean Middleditchd88f1832009-03-15 01:06:17 -0400425To run telnet-proxy, you simply give it the server's host name or
426IP address, the server's port number, and the port number that
427telnet-proxy should listen on. For example, to connect to the server
428on mud.example.com port 7800 and to listen on port 5000, run:
Sean Middleditch892c5f12009-03-14 13:39:07 -0400429
Sean Middleditchd88f1832009-03-15 01:06:17 -0400430 $ ./telnet-proxy mud.example.com 7800 5000
Sean Middleditch892c5f12009-03-14 13:39:07 -0400431
432You can then connect to the host telnet-proxy is running on (e.g.
Sean Middleditchd88f1832009-03-15 01:06:17 -0400433127.0.0.1) on port 500 and you will automatically be proxied into
434mud.example.com.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400435
436telnet-proxy will display status information about the data
Sean Middleditchaefcd0c2009-03-15 13:16:44 -0400437passing through both ends of the tunnel. telnet-proxy can only
438support a single tunnel at a time. It will continue running until
439an error occurs or a terminating signal is sent to the proxy
440process.