blob: 0ef18a85eebe887f05e9abfe672e41209c350cef [file] [log] [blame]
Sean Middleditchb9e48642009-03-12 23:33:27 -04001=====================================================================
Sean Middleditch55655962009-10-24 15:38:46 -07002 libtelnet - TELNET protocol handling library
Sean Middleditchb9e48642009-03-12 23:33:27 -04003=====================================================================
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 Middleditchb9e48642009-03-12 23:33:27 -040019I. INTRODUCTION
20=====================================================================
21
22libtelnet provides safe and correct handling of the core TELNET
Sean Middleditch8b788962009-03-16 01:06:27 -040023protocol. In addition to the base TELNET protocol, libtelnet also
Sean Middleditchf65f27d2009-03-19 02:32:04 -040024implements the Q method of TELNET option negotiation. libtelnet can
25be used for writing servers, clients, or proxies.
Sean Middleditchb9e48642009-03-12 23:33:27 -040026
Sean Middleditch94d67e82009-03-22 22:38:02 -040027For more information on the TELNET standards supported by libtelnet,
28visit the following websites:
Sean Middleditch892c5f12009-03-14 13:39:07 -040029
30 http://www.faqs.org/rfcs/rfc854.html
Sean Middleditch94d67e82009-03-22 22:38:02 -040031 http://www.faqs.org/rfcs/rfc855.html
32 http://www.faqs.org/rfcs/rfc1091.html
Sean Middleditch8b788962009-03-16 01:06:27 -040033 http://www.faqs.org/rfcs/rfc1143.html
Sean Middleditch94d67e82009-03-22 22:38:02 -040034 http://www.faqs.org/rfcs/rfc1408.html
35 http://www.faqs.org/rfcs/rfc1572.html
Sean Middleditch892c5f12009-03-14 13:39:07 -040036
Sean Middleditchb9e48642009-03-12 23:33:27 -040037II. LIBTELNET API
38=====================================================================
39
Sean Middleditch892c5f12009-03-14 13:39:07 -040040The libtelnet API contains several distinct parts. The first part is
41the basic initialization and deinitialization routines. The second
Sean Middleditchf65f27d2009-03-19 02:32:04 -040042part is a single function for pushing received data into the telnet
Sean Middleditch4f0c37f2009-03-20 23:08:55 -040043processor. The third part is the libtelnet output functions, which
Sean Middleditchf65f27d2009-03-19 02:32:04 -040044generate TELNET commands and ensure data is properly formatted before
45sending over the wire. The final part is the event handler
46interface.
Sean Middleditch892c5f12009-03-14 13:39:07 -040047
48IIa. Initialization
49
Sean Middleditchbfc641e2009-03-22 16:26:06 -040050 Using libtelnet requires the initialization of a telnet_t structure
51 which stores all current state for a single TELNET connection.
Sean Middleditch892c5f12009-03-14 13:39:07 -040052
Sean Middleditchbfc641e2009-03-22 16:26:06 -040053 Initializing a telnet_t structure requires several pieces of data.
54 One of these is the telopt support table, which specifies which
55 TELNET options your application supports both locally and remotely.
56 This table is comprised of telnet_telopt_t structures, one for each
57 supported option. Each entry specifies the option supported,
58 whether the option is supported locally or remotely.
59
60 struct telnet_telopt_t {
61 short telopt;
62 unsigned char us;
63 unsigned char him;
64 };
65
66 The us field denotes whether your application supporst the telopt
67 locally. It should be set to TELNET_WILL if you support it and to
68 TELNET_WONT if you don't. The him field denotes whether the telopt
69 is supported on the remote end, and should be TELNET_DO if yes and
70 TELNET_DONT if not.
71
72 When definition the telopt table you must include an end marker
73 entry, which is simply an entry with telopt set to -1. For
74 example:
75
76 static const telnet_telopt_t my_telopts[] = {
77 { TELNET_TELOPT_ECHO, TELNET_WILL, TELNET_DONT },
78 { TELNET_TELOPT_TTYPE, TELNET_WILL, TELNET_DONT },
79 { TELNET_TELOPT_COMPRESS2, TELNET_WONT, TELNET_DO },
80 { TELNET_TELOPT_ZMP, TELNET_WONT, TELNET_DO },
81 { TELNET_TELOPT_MSSP, TELNET_WONT, TELNET_DO },
82 { TELNET_TELOPT_BINARY, TELNET_WILL, TELNET_DO },
83 { TELNET_TELOPT_NAWS, TELNET_WILL, TELNET_DONT },
84 { -1, 0, 0 }
85 };
86
87 If you need to dynamically alter supported options on a
88 per-connection basis then you may use a different tables
89 (dynamically allocated if necessary) per call to telnet_init() or
90 you share a single constant table like the above example between
91 all connections if you support a fixed set of options. Most
92 applications will support only a fixed set of options.
93
Sean Middleditchbe230ee2009-09-19 14:39:58 -070094 telnet_t *telnet_init(const telnet_telopts_t *telopts,
Sean Middleditchbfc641e2009-03-22 16:26:06 -040095 telnet_event_handler_t handler, unsigned char flags,
96 void *user_data);
Sean Middleditchbe230ee2009-09-19 14:39:58 -070097 The telnet_init() function is responsible for allocating memory
98 and initializing the data in a telnet_t structure. It must be
99 called immediately after establishing a connection and before any
100 other libtelnet API calls are made.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400101
Sean Middleditchbfc641e2009-03-22 16:26:06 -0400102 The telopts field is the telopt support table as described above.
103
Sean Middleditch637df7f2009-03-15 12:57:32 -0400104 The handler parameter must be a function matching the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400105 telnet_event_handler_t definition. More information about events
106 can be found in section IId.
Sean Middleditch30323022009-03-14 21:45:28 -0400107
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400108 The user_data parameter is passed to the event handler whenver it
109 is invoked. This will usually be a structure container
110 information about the connection, including a socket descriptor
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400111 for implementing TELNET_EV_SEND event handling.
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400112
Sean Middleditch08bb05f2009-03-15 23:29:46 -0400113 The flags parameter can be any of the following flag constants
114 bit-or'd together, or 0 to leave all options disabled.
115
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400116 TELNET_FLAG_PROXY
Sean Middleditchc337ba62009-03-16 16:47:27 -0400117 Operate in proxy mode. This disables the RFC1143 support and
118 enables automatic detection of COMPRESS2 streams.
Sean Middleditchbe230ee2009-09-19 14:39:58 -0700119
120 If telnet_init() fails to allocate the required memory, the
121 returned pointer will be zero.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400122
Sean Middleditchbe230ee2009-09-19 14:39:58 -0700123 void telnet_free(telnet_t *telnet);
124 Releases any internal memory allocated by libtelnet for the given
125 telnet pointer. This must be called whenever a connection is
126 closed, or you will incur memory leaks. The pointer passed in may
127 no longer be used afterwards.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400128
129IIb. Receiving Data
130
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400131 void telnet_recv(telnet_t *telnet,
Sean Middleditch340a51b2009-03-19 02:08:46 -0400132 const char *buffer, unsigned int size, void *user_data);
Sean Middleditch892c5f12009-03-14 13:39:07 -0400133 When your application receives data over the socket from the
134 remote end, it must pass the received bytes into this function.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400135
136 As the TELNET stream is parsed, events will be generated and
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400137 passed to the event handler given to telnet_init(). Of particular
138 interest for data receiving is the TELNET_EV_DATA event, which is
139 triggered for any regular data such as user input or server
140 process output.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400141
142IIc. Sending Data
143
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400144 All of the output functions will invoke the TELNET_EV_SEND event.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400145
146 Note: it is very important that ALL data sent to the remote end of
147 the connection be passed through libtelnet. All user input or
148 process output that you wish to send over the wire should be given
Sean Middleditch4613e682009-03-16 17:21:48 -0400149 to one of the following functions. Do NOT send or buffer
150 unprocessed output data directly!
Sean Middleditch892c5f12009-03-14 13:39:07 -0400151
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400152 void telnet_iac(telnet_t *telnet, unsigned char cmd);
Sean Middleditch892c5f12009-03-14 13:39:07 -0400153 Sends a single "simple" TELNET command, such as the GO-AHEAD
154 commands (255 249).
155
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400156 void telnet_negotiate(telnet_t *telnet, unsigned char cmd,
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400157 unsigned char opt);
158 Sends a TELNET negotiation command. The cmd parameter must be one
159 of TELNET_WILL, TELNET_DONT, TELNET_DO, or TELNET_DONT. The opt
160 parameter is the option to negotiate.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400161
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400162 Unless in PROXY mode, the RFC1143 support may delay or ellide the
163 request entirely, as appropriate. It will ignore duplicate
164 invocations, such as asking for WILL NAWS when NAWS is already on
165 or is currently awaiting response from the remote end.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400166
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400167 void telnet_send(telnet_t *telnet, const char *buffer, size_t size);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400168 Sends raw data, which would be either the process output from a
169 server or the user input from a client.
170
171 For sending regular text is may be more convenient to use
172 telnet_printf().
Sean Middleditch90e79da2009-03-19 15:17:13 -0400173
174 void telnet_begin_subnegotiation(telnet_t *telnet, unsigned char
175 telopt);
176 Sends the header for a TELNET sub-negotiation command for the
177 specified option. All send data following this command will be
178 part of the sub-negotiation data until a call is made to
179 telnet_finish_subnegotiation().
180
181 You should not use telnet_printf() for sending subnegotiation
182 data as it will perform newline translations that usually do not
183 need to be done for subnegotiation data, and may cause problems.
184
185 void telnet_finish_subnegotiation(telnet_t *telnet);
186 Sends the end marker for a TELNET sub-negotiation command. This
187 must be called after (and only after) a call has been made to
188 telnet_begin_subnegotiation() and any negotiation data has been
189 sent.
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400190
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400191 void telnet_subnegotiation(telnet_t *telnet, unsigned char telopt,
192 const char *buffer, unsigned int size);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400193 Sends a TELNET sub-negotiation command. The telopt parameter is
194 the sub-negotiation option.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400195
Sean Middleditch90e79da2009-03-19 15:17:13 -0400196 Note that this function is just a shorthand for:
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400197 telnet_begin_sb(telnet, telopt);
198 telnet_send(telnet, buffer, size);
199 telnet_end_sb(telnet);
Sean Middleditch2b4bfc42009-03-16 01:25:52 -0400200
201 For some subnegotiations that involve a lot of complex formatted
Sean Middleditch90e79da2009-03-19 15:17:13 -0400202 data to be sent, it may be easier to make calls to both
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400203 telnet_begin_sb() and telnet_finish_sb() and using telnet_send()
204 or telnet_printf2() to format the data.
Sean Middleditch2b4bfc42009-03-16 01:25:52 -0400205
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400206 NOTE: telnet_subnegotiation() does have special behavior in
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400207 PROXY mode, as in that mode this function will automatically
Sean Middleditch2b4bfc42009-03-16 01:25:52 -0400208 detect the COMPRESS2 marker and enable zlib compression.
209
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400210 int telnet_printf(telnet_t *telnet, const char *fmt, ...);
211 This functions very similarly to fprintf, except that output is
212 sent through libtelnet for processing. IAC bytes are properly
213 escaped, C newlines (\n) are translated into CR LF, and C carriage
214 returns (\r) are translated into CR NUL, all as required by
215 RFC854. The return code is the length of the formatted text.
Sean Middleditch4613e682009-03-16 17:21:48 -0400216
217 NOTE: due to an internal implementation detail, the maximum
218 lenth of the formatted text is 4096 characters.
219
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400220 int telnet_printf2(telnet_t *telnet, const char *fmt, ...);
221 Identical to telnet_printf() except that \r and \n are not
222 translated. This should be used if you are attempting to send
223 raw data inside a subnegotiation or if you have already manually
224 escaped newlines.
Sean Middleditcheb950a82009-03-22 23:04:28 -0400225
226 void telnet_format_sb(telnet_t *telnet, unsigned char telopt,
227 size_t count, ...);
228 This is a helper function for sending the specially formatted
229 data used in the TTYPE, ENVIRON/NEW-ENVIRON, and MSSP telopt
230 subnegotiations.
231
232 The variadic arguments must be given as a series of pairs of
233 markers and strings. The markers are different for each telopt;
234 they are defined in libtelnet.h and include:
235
236 /* TTYPE markers */
237 #define TELNET_TTYPE_IS 0
238 #define TELNET_TTYPE_SEND 1
239
240 /* ENVIRON/NEW-ENVIRON markers */
241 #define TELNET_ENVIRON_IS 0
242 #define TELNET_ENVIRON_SEND 1
243 #define TELNET_ENVIRON_INFO 2
244 #define TELNET_ENVIRON_VAR 0
245 #define TELNET_ENVIRON_VALUE 1
246 #define TELNET_ENVIRON_ESC 2
247 #define TELNET_ENVIRON_USERVAR 3
248
249 /* MSSP markers */
250 #define TELNET_MSSP_VAR 1
251 #define TELNET_MSSP_VAL 2
252
253 So to send a TTYPE subnegotiation from the server (just an IS
254 command), you would use:
255
256 telnet_format_sb(&telnet, TELNET_TELOPT_TTYPE, 1,
257 TELNET_TTYPE_SEND);
258
259 The client response for an xterm-compatible terminal would be:
260
261 telnet_format_sb(&telnet, TELNET_TELOPT_TTYPE, 1,
262 TELNET_TTYPE_IS, "xterm");
263
264 For more information on the meaning of the markers and strings,
265 please refer to the specific RFC for the telopt in question.
Sean Middleditch4613e682009-03-16 17:21:48 -0400266
Sean Middleditch637df7f2009-03-15 12:57:32 -0400267IId. Event Handling
Sean Middleditch892c5f12009-03-14 13:39:07 -0400268
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400269 libtelnet relies on an event-handling mechanism for processing the
270 parsed TELNET protocol stream as well as for buffering and sending
271 output data.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400272
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400273 When you initialize a telnet_t structure with telnet_init() you had
274 to pass in an event handler function. This function must meet the
275 following prototype:
Sean Middleditch30323022009-03-14 21:45:28 -0400276
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400277 void (telnet_t *telnet, telnet_event_t *event, void *user_data);
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400278
279 The event structure is detailed below. The user_data value is the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400280 pointer passed to telnet_init().
Sean Middleditch637df7f2009-03-15 12:57:32 -0400281
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400282 struct telnet_event_t {
Sean Middleditch340a51b2009-03-19 02:08:46 -0400283 const char *buffer;
Sean Middleditch637df7f2009-03-15 12:57:32 -0400284 unsigned int size;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400285 telnet_event_type_t type;
Sean Middleditch812358d2009-03-15 23:24:03 -0400286 unsigned char command;
287 unsigned char telopt;
288 unsigned char accept;
Sean Middleditch637df7f2009-03-15 12:57:32 -0400289 };
290
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400291 The enumeration values of telnet_event_type_t are described in
Sean Middleditch637df7f2009-03-15 12:57:32 -0400292 detail below. Whenever the the event handler is invoked, the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400293 application must look at the event->type value and do any necessary
294 processing.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400295
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400296 The only event that MUST be implemented is TELNET_EV_SEND. Most
297 applications will also always want to implement the event
298 TELNET_EV_DATA.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400299
300 Here is an example event handler implementation which includes
301 handlers for several important events.
302
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400303 void my_event_handler(telnet_t *telnet, telnet_event_t *ev,
Sean Middleditch812358d2009-03-15 23:24:03 -0400304 void *user_data) {
Sean Middleditch637df7f2009-03-15 12:57:32 -0400305 struct user_info *user = (struct user_info *)user_data;
306
307 switch (ev->type) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400308 case TELNET_EV_DATA:
Sean Middleditch637df7f2009-03-15 12:57:32 -0400309 process_user_input(user, event->buffer, event->size);
310 break;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400311 case TELNET_EV_SEND:
Sean Middleditch637df7f2009-03-15 12:57:32 -0400312 write_to_descriptor(user, event->buffer, event->size);
313 break;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400314 case TELNET_EV_ERROR:
Sean Middleditch637df7f2009-03-15 12:57:32 -0400315 fatal_error("TELNET error: %s", event->buffer);
316 break;
317 }
Sean Middleditch30323022009-03-14 21:45:28 -0400318 }
319
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400320 TELNET_EV_DATA:
321 The DATA event is triggered whenever regular data (not part of any
322 special TELNET command) is received. For a client, this will be
323 process output from the server. For a server, this will be input
324 typed by the user.
Sean Middleditch30323022009-03-14 21:45:28 -0400325
Sean Middleditch637df7f2009-03-15 12:57:32 -0400326 The event->buffer value will contain the bytes received and the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400327 event->size value will contain the number of bytes received. Note
328 that event->buffer is not NUL terminated!
Sean Middleditch30323022009-03-14 21:45:28 -0400329
Sean Middleditch637df7f2009-03-15 12:57:32 -0400330 NOTE: there is no guarantee that user input or server output
331 will be received in whole lines. If you wish to process data
332 a line at a time, you are responsible for buffering the data and
333 checking for line terminators yourself!
334
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400335 TELNET_EV_SEND:
336 This event is sent whenever libtelnet has generated data that must
337 be sent over the wire to the remove end. Generally that means
338 calling send() or adding the data to your application's output
339 buffer.
Sean Middleditch30323022009-03-14 21:45:28 -0400340
Sean Middleditch637df7f2009-03-15 12:57:32 -0400341 The event->buffer value will contain the bytes to send and the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400342 event->size value will contain the number of bytes to send. Note
343 that event->buffer is not NUL terminated, and may include NUL
344 characters in its data, so always use event->size!
Sean Middleditch30323022009-03-14 21:45:28 -0400345
Sean Middleditch637df7f2009-03-15 12:57:32 -0400346 NOTE: Your SEND event handler must send or buffer the data in
347 its raw form as provided by libtelnet. If you wish to perform
348 any kind of preprocessing on data you want to send to the other
349
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400350 TELNET_EV_IAC:
Sean Middleditch637df7f2009-03-15 12:57:32 -0400351 The IAC event is triggered whenever a simple IAC command is
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400352 received, such as the IAC EOR (end of record, also called go ahead
353 or GA) command.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400354
Sean Middleditch637df7f2009-03-15 12:57:32 -0400355 The command received is in the event->command value.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400356
Sean Middleditch637df7f2009-03-15 12:57:32 -0400357 The necessary processing depends on the specific commands; see
358 the TELNET RFC for more information.
359
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400360 TELNET_EV_WILL:
361 TELNET_EV_DO:
362 The WILL and DO events are sent when a TELNET negotiation command
363 of the same name is received.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400364
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400365 WILL events are sent by the remote end when they wish to be
366 allowed to turn an option on on their end, or in confirmation
367 after you have sent a DO command to them.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400368
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400369 DO events are sent by the remote end when they wish for you to
370 turn on an option on your end, or in confirmation after you have
371 sent a WILL command to them.
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400372
373 In either case, the TELNET option under negotiation will be in
374 event->telopt field.
375
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400376 If you support the option and wish for it to be enabled you must
377 set the event->accept field to 1, unless this event is a
378 confirmation for a previous WILL/DO command you sent to the remote
379 end. If you do not set event->field to 1 then libtelnet will send
380 a rejection command back to the other end.
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400381
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400382 libtelnet manages some of the pecularities of negotiation for you.
383 For information on libtelnet's negotiation method, see:
Sean Middleditch892c5f12009-03-14 13:39:07 -0400384
385 http://www.faqs.org/rfcs/rfc1143.html
386
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400387 Examples:
388
389 You want remote end to use TTYPE, so you send DO TTYPE.
390 Remote accepts and sends WILL TTYPE.
391
392 Remote end wants you to use SGA, so they send DO_SGA.
393 You do not support SGA and set event->accept = 0.
394
395 Remote end wants to use ZMP, so they send WILL ZMP.
396 You support ZMP, so you set event->accept = 1 and enable
397 local ZMP support.
398
399 You want to use MCCP2, so you send WILL COMPRESS2.
400 Remote end accepts and sends DO COMPRESS2.
401
402 Note that in PROXY mode libtelnet will do no processing of its
403 own for you.
404
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400405 TELNET_EV_WONT:
406 TELNET_EV_DONT:
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400407 The WONT and DONT events are sent when the remote end of the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400408 connection wishes to disable an option, when they are refusing to
409 a support an option that you have asked for, or in confirmation of
410 an option you have asked to be disabled.
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400411
412 Most commonly WONT and DONT events are sent as rejections of
413 features you requested by sending DO or WILL events. Receiving
414 these events means the TELNET option is not or will not be
415 supported by the remote end, so give up.
416
417 Sometimes WONT or DONT will be sent for TELNET options that are
418 already enabled, but the remote end wishes to stop using. You
419 cannot decline. These events are demands that must be complied
420 with. libtelnet will always send the appropriate response back
421 without consulting your application. These events are sent to
422 allow your application to disable its own use of the features.
423
424 In either case, the TELNET option under negotiation will be in
425 event->telopt field.
426
427 Note that in PROXY mode libtelnet will do no processing of its
428 own for you.
429
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400430 TELNET_EV_SUBNEGOTIATION:
Sean Middleditch637df7f2009-03-15 12:57:32 -0400431 Triggered whenever a TELNET sub-negotiation has been received.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400432 Sub-negotiations include the NAWS option for communicating
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400433 terminal size to a server, the NEW-ENVIRON and TTYPE options for
434 negotiating terminal features, and MUD-centric protocols such as
435 ZMP, MSSP, and MCCP2.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400436
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400437 The event->telopt value is the option under sub-negotiation. The
438 remaining data (if any) is passed in event->buffer and
439 event->size. Note that most subnegotiation commands can include
440 embedded NUL bytes in the subnegotiation data, and the data
441 event->buffer is not NUL terminated, so always use the event->size
442 value!
Sean Middleditch892c5f12009-03-14 13:39:07 -0400443
Sean Middleditch637df7f2009-03-15 12:57:32 -0400444 The meaning and necessary processing for subnegotiations are
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400445 defined in various TELNET RFCs and other informal specifications.
446 A subnegotiation should never be sent unless the specific option
447 has been enabled through the use of the telnet negotiation
448 feature.
Sean Middleditch8d50fe82009-03-22 22:35:17 -0400449
450 TTYPE/ENVIRON/NEW-ENVIRON/MSSP SUPPORT:
451 These telopts all use a similar format to their subnegotiation
452 requests. The data is arrnaged as a series of terms, with each
453 term beginning with a single byte type marker (a small integer in
454 the range of 0 to 3) followed by zero or more bytes until another
455 type marker or the end of the subnegotiation data.
456
457 libtelnet parses these. The number of terms found is put in the
458 ev->argc field. The terms themselves are stored as
459 NUL-terminated strings in the ev->argv array. The type byte is
460 always the first byte of these strings, e.g. ev->argv[0][0]. Due
461 to the fact that 0 is a valid type marker, remember that
462 accessing the remainder of the term's data as a string must be
463 done as &ev->argv[term_index][1].
464
465 Note that libtelnet does not support the ESC byte for ENVIRON/
466 NEW-ENVIRON. Data using escaped bytes will not be parsed
467 correctly.
Sean Middleditchb10946c2009-03-22 18:21:14 -0400468
469 ZMP SUPPORT:
470 The event->argc field is the number of ZMP parameters, including
471 the command name, that have been received. The event->argv field
472 is an array of strings, one for each ZMP parameter. The command
473 name will be in event->argv[0]. If the ZMP command could not be
474 parsed because it was malformed, event->argc will be 0 (zero) and
475 event->argv will be NULL.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400476
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400477 TELNET_EV_COMPRESS
Sean Middleditch637df7f2009-03-15 12:57:32 -0400478 The COMPRESS event notifies the app that COMPRESS2/MCCP2
479 compression has begun or ended. Only servers can send compressed
480 data, and hence only clients will receive compressed data.
Sean Middleditchb9e48642009-03-12 23:33:27 -0400481
Sean Middleditch637df7f2009-03-15 12:57:32 -0400482 The event->command value will be 1 if compression has started and
483 will be 0 if compression has ended.
Sean Middleditch16992272009-03-15 19:42:03 -0400484
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400485 TELNET_EV_WARNING
486 The WARNING event is sent whenever something has gone wrong inside
487 of libtelnet (possibly due to malformed data sent by the other
488 end) but which recovery is (likely) possible. It may be safe to
489 continue using the connection, but some data may have been lost or
490 incorrectly interpreted.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400491
492 The event->buffer value will contain a NUL terminated string
493 explaining the error, and the event->size value containers the
494 length of the string.
495
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400496 TELNET_EV_ERROR
Sean Middleditch16992272009-03-15 19:42:03 -0400497 Similar to the WARNING event, the ERROR event is sent whenever
498 something has gone wrong. ERROR events are non-recoverable,
499 however, and the application should immediately close the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400500 connection. Whatever has happened is likely going only to result
501 in garbage from libtelnet. This is most likely to happen when a
502 COMPRESS2 stream fails, but other problems can occur.
Sean Middleditch16992272009-03-15 19:42:03 -0400503
504 The event->buffer value will contain a NUL terminated string
505 explaining the error, and the event->size value containers the
506 length of the string.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400507
508III. INTEGRATING LIBTELNET WITH COMMON MUDS
Sean Middleditchb9e48642009-03-12 23:33:27 -0400509=====================================================================
510
Sean Middleditch637df7f2009-03-15 12:57:32 -0400511FIXME: fill in some notes about how to splice in libtelnet with
512common Diku/Merc/Circle/etc. MUD codebases.
Sean Middleditchb9e48642009-03-12 23:33:27 -0400513
Sean Middleditch892c5f12009-03-14 13:39:07 -0400514IV. SAFETY AND CORRECTNESS CONSIDERATIONS
Sean Middleditchb9e48642009-03-12 23:33:27 -0400515=====================================================================
516
Sean Middleditch892c5f12009-03-14 13:39:07 -0400517Your existing application may make heavy use of its own output
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400518buffering and transmission commands, including hand-made routines for
519sending TELNET commands and sub-negotiation requests. There are at
520times subtle issues that need to be handled when communication over
521the TELNET protocol, not least of which is the need to escape any
522byte value 0xFF with a special TELNET command.
Sean Middleditchb9e48642009-03-12 23:33:27 -0400523
Sean Middleditch892c5f12009-03-14 13:39:07 -0400524For these reasons, it is very important that applications making use
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400525of libtelnet always make use of the libtelnet output functions for
526all data being sent over the TELNET connection.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400527
Sean Middleditch637df7f2009-03-15 12:57:32 -0400528In particular, if you are writing a client, all user input must be
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400529passed through to telnet_send(). This also includes any input
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400530generated automatically by scripts, triggers, or macros.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400531
532For a server, any and all output -- including ANSI/VT100 escape
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400533codes, regular text, newlines, and so on -- must be passed through to
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400534telnet_send().
Sean Middleditch637df7f2009-03-15 12:57:32 -0400535
536Any TELNET commands that are to be sent must be given to one of the
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400537following: telnet_iac, telnet_negotiate, or telnet_subnegotiation().
Sean Middleditch637df7f2009-03-15 12:57:32 -0400538
539If you are attempting to enable COMPRESS2/MCCP2, you must use the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400540telnet_begin_compress2() function.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400541
Sean Middleditch892c5f12009-03-14 13:39:07 -0400542V. MCCP2 COMPRESSION
Sean Middleditchb9e48642009-03-12 23:33:27 -0400543=====================================================================
544
Sean Middleditch892c5f12009-03-14 13:39:07 -0400545The MCCP2 (COMPRESS2) TELNET extension allows for the compression of
546all traffic sent from server to client. For more information:
547
548 http://www.mudbytes.net/index.php?a=articles&s=mccp
549
Sean Middleditch637df7f2009-03-15 12:57:32 -0400550In order for libtelnet to support MCCP2, zlib must be installed and
551enabled when compiling libtelnet. Use -DHAVE_ZLIB to enable zlib
552when compiling libtelnet.c and pass -lz to the linker to link in the
553zlib shared library.
554
Sean Middleditch892c5f12009-03-14 13:39:07 -0400555libtelnet transparently supports MCCP2. For a server to support
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400556MCCP2, the application must begin negotiation of the COMPRESS2 option
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400557using telnet_negotiate(), for example:
Sean Middleditch892c5f12009-03-14 13:39:07 -0400558
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400559 telnet_negotiate(&telnet, TELNET_WILL,
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400560 TELNET_OPTION_COMPRESS2, user_data);
Sean Middleditch892c5f12009-03-14 13:39:07 -0400561
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400562If a favorable DO COMPRESS2 is sent back from the client then the
563server application can begin compression at any time by calling
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400564telnet_begin_compress2().
Sean Middleditch892c5f12009-03-14 13:39:07 -0400565
Sean Middleditch637df7f2009-03-15 12:57:32 -0400566If a connection is in PROXY mode and COMPRESS2 support is enabled
567then libtelnet will automatically detect the start of a COMPRESS2
568stream, in either the sending or receiving direction.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400569
Sean Middleditch18877352009-03-22 16:57:44 -0400570VI. ZENITH MUD PROTOCOL (ZMP) SUPPORT
571=====================================================================
572
573The Zenith MUD Protocol allows applications to send messages across
574the TELNET connection outside of the normal user input/output data
575stream. libtelnet offers some limited support for receiving and
576sending ZMP commands to make implementing a full ZMP stack easier.
577For more information on ZMP:
578
579 http://zmp.sourcemud.org/
580
581For a server to enable ZMP, it must send the WILL ZMP negotitaion:
582
583 telnet_negotiate(&telnet, TELNET_WILL, TELNET_TELOPT_ZMP);
584
585For a client to support ZMP it must include ZMP in the telopt table
586passed to telnet_init(), with the him field set to TELNET_DO:
587
588 { TELNET_TELOPT_ZMP, TELNET_WONT, TELNET_DO },
589
590Note that while ZMP is a bi-directional protocol, it is only ever
591enabled on the server end of the connection. This automatically
592enables the client to send ZMP commands. The client must never
593attempt to negotiate ZMP directly using telnet_negotiate().
594
595Once ZMP is enabled, any ZMP commands received will automatically be
Sean Middleditchb10946c2009-03-22 18:21:14 -0400596sent to the event handler function with the TELNET_EV_SUBNEGOTIATION
597event code. The command will automatically be parsed and the ZMP
598parameters will be placed in the event->argv array and the number of
599parameters will be placed in the event->argc field.
600
601NOTE: if an error occured while parsing the ZMP command because it
602was malformed, the event->argc field will be equal to 0 and the
603event->argv field will be NULL. You should always check for this
604before attempting to access the parameter array.
Sean Middleditch18877352009-03-22 16:57:44 -0400605
606To send ZMP commands to the remote end, use either telnet_send_zmp()
607or telnet_send_zmpv().
608
609 int telnet_send_zmp(telnet_t *telnet, size_t argv,
610 const char **argv);
611 Sends a ZMP command to the remote end. The argc parameter is the
612 number of ZMP parameters (including the command name!) to be sent.
613 The argv parameter is an array of strings containing the
614 parameters. The element in argv[0] is the command name itself.
615 The argv array must have at least as many elements as the value
616 argc.
617
618VII. TELNET PROXY UTILITY
Sean Middleditch892c5f12009-03-14 13:39:07 -0400619=====================================================================
620
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400621The telnet-proxy utility is a small application that serves both as a
622testbed for libtelnet and as a powerful debugging tool for TELNET
Sean Middleditch892c5f12009-03-14 13:39:07 -0400623servers and clients.
624
625To use telnet-proxy, you must first compile it using:
626
627 $ make
628
629If you do not have zlib installed and wish to disable MCCP2 support
630then you must first edit the Makefile and remove the -DHAVE_ZLIB and
631the -lz from the compile flags.
632
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400633To run telnet-proxy, you simply give it the server's host name or IP
634address, the server's port number, and the port number that
Sean Middleditchd88f1832009-03-15 01:06:17 -0400635telnet-proxy should listen on. For example, to connect to the server
636on mud.example.com port 7800 and to listen on port 5000, run:
Sean Middleditch892c5f12009-03-14 13:39:07 -0400637
Sean Middleditchd88f1832009-03-15 01:06:17 -0400638 $ ./telnet-proxy mud.example.com 7800 5000
Sean Middleditch892c5f12009-03-14 13:39:07 -0400639
640You can then connect to the host telnet-proxy is running on (e.g.
Sean Middleditchd88f1832009-03-15 01:06:17 -0400641127.0.0.1) on port 500 and you will automatically be proxied into
642mud.example.com.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400643
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400644telnet-proxy will display status information about the data passing
645through both ends of the tunnel. telnet-proxy can only support a
646single tunnel at a time. It will continue running until an error
647occurs or a terminating signal is sent to the proxy process.