blob: 70baa084f6d34efc9517df91fb8e72123e03ee18 [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 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 Middleditch892c5f12009-03-14 13:39:07 -040027For more information on the TELNET protocol, see:
28
29 http://www.faqs.org/rfcs/rfc854.html
Sean Middleditch8b788962009-03-16 01:06:27 -040030 http://www.faqs.org/rfcs/rfc1143.html
Sean Middleditch892c5f12009-03-14 13:39:07 -040031
Sean Middleditchb9e48642009-03-12 23:33:27 -040032II. LIBTELNET API
33=====================================================================
34
Sean Middleditch892c5f12009-03-14 13:39:07 -040035The libtelnet API contains several distinct parts. The first part is
36the basic initialization and deinitialization routines. The second
Sean Middleditchf65f27d2009-03-19 02:32:04 -040037part is a single function for pushing received data into the telnet
Sean Middleditch4f0c37f2009-03-20 23:08:55 -040038processor. The third part is the libtelnet output functions, which
Sean Middleditchf65f27d2009-03-19 02:32:04 -040039generate TELNET commands and ensure data is properly formatted before
40sending over the wire. The final part is the event handler
41interface.
Sean Middleditch892c5f12009-03-14 13:39:07 -040042
43IIa. Initialization
44
Sean Middleditchbfc641e2009-03-22 16:26:06 -040045 Using libtelnet requires the initialization of a telnet_t structure
46 which stores all current state for a single TELNET connection.
Sean Middleditch892c5f12009-03-14 13:39:07 -040047
Sean Middleditchbfc641e2009-03-22 16:26:06 -040048 Initializing a telnet_t structure requires several pieces of data.
49 One of these is the telopt support table, which specifies which
50 TELNET options your application supports both locally and remotely.
51 This table is comprised of telnet_telopt_t structures, one for each
52 supported option. Each entry specifies the option supported,
53 whether the option is supported locally or remotely.
54
55 struct telnet_telopt_t {
56 short telopt;
57 unsigned char us;
58 unsigned char him;
59 };
60
61 The us field denotes whether your application supporst the telopt
62 locally. It should be set to TELNET_WILL if you support it and to
63 TELNET_WONT if you don't. The him field denotes whether the telopt
64 is supported on the remote end, and should be TELNET_DO if yes and
65 TELNET_DONT if not.
66
67 When definition the telopt table you must include an end marker
68 entry, which is simply an entry with telopt set to -1. For
69 example:
70
71 static const telnet_telopt_t my_telopts[] = {
72 { TELNET_TELOPT_ECHO, TELNET_WILL, TELNET_DONT },
73 { TELNET_TELOPT_TTYPE, TELNET_WILL, TELNET_DONT },
74 { TELNET_TELOPT_COMPRESS2, TELNET_WONT, TELNET_DO },
75 { TELNET_TELOPT_ZMP, TELNET_WONT, TELNET_DO },
76 { TELNET_TELOPT_MSSP, TELNET_WONT, TELNET_DO },
77 { TELNET_TELOPT_BINARY, TELNET_WILL, TELNET_DO },
78 { TELNET_TELOPT_NAWS, TELNET_WILL, TELNET_DONT },
79 { -1, 0, 0 }
80 };
81
82 If you need to dynamically alter supported options on a
83 per-connection basis then you may use a different tables
84 (dynamically allocated if necessary) per call to telnet_init() or
85 you share a single constant table like the above example between
86 all connections if you support a fixed set of options. Most
87 applications will support only a fixed set of options.
88
89 void telnet_init(telnet_t *telnet, const telnet_telopts_t *telopts,
90 telnet_event_handler_t handler, unsigned char flags,
91 void *user_data);
Sean Middleditchf65f27d2009-03-19 02:32:04 -040092 The telnet_init() function is responsible for initializing the
93 data in a telnet_t structure. It must be called immediately after
94 establishing a connection and before any other libtelnet API calls
95 are made.
Sean Middleditch892c5f12009-03-14 13:39:07 -040096
Sean Middleditchbfc641e2009-03-22 16:26:06 -040097 The telopts field is the telopt support table as described above.
98
Sean Middleditch637df7f2009-03-15 12:57:32 -040099 The handler parameter must be a function matching the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400100 telnet_event_handler_t definition. More information about events
101 can be found in section IId.
Sean Middleditch30323022009-03-14 21:45:28 -0400102
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400103 The user_data parameter is passed to the event handler whenver it
104 is invoked. This will usually be a structure container
105 information about the connection, including a socket descriptor
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400106 for implementing TELNET_EV_SEND event handling.
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400107
Sean Middleditch08bb05f2009-03-15 23:29:46 -0400108 The flags parameter can be any of the following flag constants
109 bit-or'd together, or 0 to leave all options disabled.
110
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400111 TELNET_FLAG_PROXY
Sean Middleditchc337ba62009-03-16 16:47:27 -0400112 Operate in proxy mode. This disables the RFC1143 support and
113 enables automatic detection of COMPRESS2 streams.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400114
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400115 boid telnet_free(telnet_t *telnet);
116 Releases any internal memory allocated by libtelnet. This must be
117 called whenever a connection is closed, or you will incur memory
118 leaks.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400119
120IIb. Receiving Data
121
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400122 void telnet_recv(telnet_t *telnet,
Sean Middleditch340a51b2009-03-19 02:08:46 -0400123 const char *buffer, unsigned int size, void *user_data);
Sean Middleditch892c5f12009-03-14 13:39:07 -0400124 When your application receives data over the socket from the
125 remote end, it must pass the received bytes into this function.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400126
127 As the TELNET stream is parsed, events will be generated and
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400128 passed to the event handler given to telnet_init(). Of particular
129 interest for data receiving is the TELNET_EV_DATA event, which is
130 triggered for any regular data such as user input or server
131 process output.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400132
133IIc. Sending Data
134
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400135 All of the output functions will invoke the TELNET_EV_SEND event.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400136
137 Note: it is very important that ALL data sent to the remote end of
138 the connection be passed through libtelnet. All user input or
139 process output that you wish to send over the wire should be given
Sean Middleditch4613e682009-03-16 17:21:48 -0400140 to one of the following functions. Do NOT send or buffer
141 unprocessed output data directly!
Sean Middleditch892c5f12009-03-14 13:39:07 -0400142
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400143 void telnet_iac(telnet_t *telnet, unsigned char cmd);
Sean Middleditch892c5f12009-03-14 13:39:07 -0400144 Sends a single "simple" TELNET command, such as the GO-AHEAD
145 commands (255 249).
146
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400147 void telnet_negotiate(telnet_t *telnet, unsigned char cmd,
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400148 unsigned char opt);
149 Sends a TELNET negotiation command. The cmd parameter must be one
150 of TELNET_WILL, TELNET_DONT, TELNET_DO, or TELNET_DONT. The opt
151 parameter is the option to negotiate.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400152
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400153 Unless in PROXY mode, the RFC1143 support may delay or ellide the
154 request entirely, as appropriate. It will ignore duplicate
155 invocations, such as asking for WILL NAWS when NAWS is already on
156 or is currently awaiting response from the remote end.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400157
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400158 void telnet_send(telnet_t *telnet, const char *buffer, size_t size);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400159 Sends raw data, which would be either the process output from a
160 server or the user input from a client.
161
162 For sending regular text is may be more convenient to use
163 telnet_printf().
Sean Middleditch90e79da2009-03-19 15:17:13 -0400164
165 void telnet_begin_subnegotiation(telnet_t *telnet, unsigned char
166 telopt);
167 Sends the header for a TELNET sub-negotiation command for the
168 specified option. All send data following this command will be
169 part of the sub-negotiation data until a call is made to
170 telnet_finish_subnegotiation().
171
172 You should not use telnet_printf() for sending subnegotiation
173 data as it will perform newline translations that usually do not
174 need to be done for subnegotiation data, and may cause problems.
175
176 void telnet_finish_subnegotiation(telnet_t *telnet);
177 Sends the end marker for a TELNET sub-negotiation command. This
178 must be called after (and only after) a call has been made to
179 telnet_begin_subnegotiation() and any negotiation data has been
180 sent.
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400181
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400182 void telnet_subnegotiation(telnet_t *telnet, unsigned char telopt,
183 const char *buffer, unsigned int size);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400184 Sends a TELNET sub-negotiation command. The telopt parameter is
185 the sub-negotiation option.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400186
Sean Middleditch90e79da2009-03-19 15:17:13 -0400187 Note that this function is just a shorthand for:
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400188 telnet_begin_sb(telnet, telopt);
189 telnet_send(telnet, buffer, size);
190 telnet_end_sb(telnet);
Sean Middleditch2b4bfc42009-03-16 01:25:52 -0400191
192 For some subnegotiations that involve a lot of complex formatted
Sean Middleditch90e79da2009-03-19 15:17:13 -0400193 data to be sent, it may be easier to make calls to both
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400194 telnet_begin_sb() and telnet_finish_sb() and using telnet_send()
195 or telnet_printf2() to format the data.
Sean Middleditch2b4bfc42009-03-16 01:25:52 -0400196
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400197 NOTE: telnet_subnegotiation() does have special behavior in
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400198 PROXY mode, as in that mode this function will automatically
Sean Middleditch2b4bfc42009-03-16 01:25:52 -0400199 detect the COMPRESS2 marker and enable zlib compression.
200
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400201 int telnet_printf(telnet_t *telnet, const char *fmt, ...);
202 This functions very similarly to fprintf, except that output is
203 sent through libtelnet for processing. IAC bytes are properly
204 escaped, C newlines (\n) are translated into CR LF, and C carriage
205 returns (\r) are translated into CR NUL, all as required by
206 RFC854. The return code is the length of the formatted text.
Sean Middleditch4613e682009-03-16 17:21:48 -0400207
208 NOTE: due to an internal implementation detail, the maximum
209 lenth of the formatted text is 4096 characters.
210
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400211 int telnet_printf2(telnet_t *telnet, const char *fmt, ...);
212 Identical to telnet_printf() except that \r and \n are not
213 translated. This should be used if you are attempting to send
214 raw data inside a subnegotiation or if you have already manually
215 escaped newlines.
Sean Middleditch4613e682009-03-16 17:21:48 -0400216
Sean Middleditch637df7f2009-03-15 12:57:32 -0400217IId. Event Handling
Sean Middleditch892c5f12009-03-14 13:39:07 -0400218
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400219 libtelnet relies on an event-handling mechanism for processing the
220 parsed TELNET protocol stream as well as for buffering and sending
221 output data.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400222
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400223 When you initialize a telnet_t structure with telnet_init() you had
224 to pass in an event handler function. This function must meet the
225 following prototype:
Sean Middleditch30323022009-03-14 21:45:28 -0400226
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400227 void (telnet_t *telnet, telnet_event_t *event, void *user_data);
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400228
229 The event structure is detailed below. The user_data value is the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400230 pointer passed to telnet_init().
Sean Middleditch637df7f2009-03-15 12:57:32 -0400231
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400232 struct telnet_event_t {
Sean Middleditch340a51b2009-03-19 02:08:46 -0400233 const char *buffer;
Sean Middleditch637df7f2009-03-15 12:57:32 -0400234 unsigned int size;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400235 telnet_event_type_t type;
Sean Middleditch812358d2009-03-15 23:24:03 -0400236 unsigned char command;
237 unsigned char telopt;
238 unsigned char accept;
Sean Middleditch637df7f2009-03-15 12:57:32 -0400239 };
240
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400241 The enumeration values of telnet_event_type_t are described in
Sean Middleditch637df7f2009-03-15 12:57:32 -0400242 detail below. Whenever the the event handler is invoked, the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400243 application must look at the event->type value and do any necessary
244 processing.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400245
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400246 The only event that MUST be implemented is TELNET_EV_SEND. Most
247 applications will also always want to implement the event
248 TELNET_EV_DATA.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400249
250 Here is an example event handler implementation which includes
251 handlers for several important events.
252
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400253 void my_event_handler(telnet_t *telnet, telnet_event_t *ev,
Sean Middleditch812358d2009-03-15 23:24:03 -0400254 void *user_data) {
Sean Middleditch637df7f2009-03-15 12:57:32 -0400255 struct user_info *user = (struct user_info *)user_data;
256
257 switch (ev->type) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400258 case TELNET_EV_DATA:
Sean Middleditch637df7f2009-03-15 12:57:32 -0400259 process_user_input(user, event->buffer, event->size);
260 break;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400261 case TELNET_EV_SEND:
Sean Middleditch637df7f2009-03-15 12:57:32 -0400262 write_to_descriptor(user, event->buffer, event->size);
263 break;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400264 case TELNET_EV_ERROR:
Sean Middleditch637df7f2009-03-15 12:57:32 -0400265 fatal_error("TELNET error: %s", event->buffer);
266 break;
267 }
Sean Middleditch30323022009-03-14 21:45:28 -0400268 }
269
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400270 TELNET_EV_DATA:
271 The DATA event is triggered whenever regular data (not part of any
272 special TELNET command) is received. For a client, this will be
273 process output from the server. For a server, this will be input
274 typed by the user.
Sean Middleditch30323022009-03-14 21:45:28 -0400275
Sean Middleditch637df7f2009-03-15 12:57:32 -0400276 The event->buffer value will contain the bytes received and the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400277 event->size value will contain the number of bytes received. Note
278 that event->buffer is not NUL terminated!
Sean Middleditch30323022009-03-14 21:45:28 -0400279
Sean Middleditch637df7f2009-03-15 12:57:32 -0400280 NOTE: there is no guarantee that user input or server output
281 will be received in whole lines. If you wish to process data
282 a line at a time, you are responsible for buffering the data and
283 checking for line terminators yourself!
284
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400285 TELNET_EV_SEND:
286 This event is sent whenever libtelnet has generated data that must
287 be sent over the wire to the remove end. Generally that means
288 calling send() or adding the data to your application's output
289 buffer.
Sean Middleditch30323022009-03-14 21:45:28 -0400290
Sean Middleditch637df7f2009-03-15 12:57:32 -0400291 The event->buffer value will contain the bytes to send and the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400292 event->size value will contain the number of bytes to send. Note
293 that event->buffer is not NUL terminated, and may include NUL
294 characters in its data, so always use event->size!
Sean Middleditch30323022009-03-14 21:45:28 -0400295
Sean Middleditch637df7f2009-03-15 12:57:32 -0400296 NOTE: Your SEND event handler must send or buffer the data in
297 its raw form as provided by libtelnet. If you wish to perform
298 any kind of preprocessing on data you want to send to the other
299
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400300 TELNET_EV_IAC:
Sean Middleditch637df7f2009-03-15 12:57:32 -0400301 The IAC event is triggered whenever a simple IAC command is
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400302 received, such as the IAC EOR (end of record, also called go ahead
303 or GA) command.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400304
Sean Middleditch637df7f2009-03-15 12:57:32 -0400305 The command received is in the event->command value.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400306
Sean Middleditch637df7f2009-03-15 12:57:32 -0400307 The necessary processing depends on the specific commands; see
308 the TELNET RFC for more information.
309
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400310 TELNET_EV_WILL:
311 TELNET_EV_DO:
312 The WILL and DO events are sent when a TELNET negotiation command
313 of the same name is received.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400314
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400315 WILL events are sent by the remote end when they wish to be
316 allowed to turn an option on on their end, or in confirmation
317 after you have sent a DO command to them.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400318
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400319 DO events are sent by the remote end when they wish for you to
320 turn on an option on your end, or in confirmation after you have
321 sent a WILL command to them.
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400322
323 In either case, the TELNET option under negotiation will be in
324 event->telopt field.
325
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400326 If you support the option and wish for it to be enabled you must
327 set the event->accept field to 1, unless this event is a
328 confirmation for a previous WILL/DO command you sent to the remote
329 end. If you do not set event->field to 1 then libtelnet will send
330 a rejection command back to the other end.
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400331
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400332 libtelnet manages some of the pecularities of negotiation for you.
333 For information on libtelnet's negotiation method, see:
Sean Middleditch892c5f12009-03-14 13:39:07 -0400334
335 http://www.faqs.org/rfcs/rfc1143.html
336
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400337 Examples:
338
339 You want remote end to use TTYPE, so you send DO TTYPE.
340 Remote accepts and sends WILL TTYPE.
341
342 Remote end wants you to use SGA, so they send DO_SGA.
343 You do not support SGA and set event->accept = 0.
344
345 Remote end wants to use ZMP, so they send WILL ZMP.
346 You support ZMP, so you set event->accept = 1 and enable
347 local ZMP support.
348
349 You want to use MCCP2, so you send WILL COMPRESS2.
350 Remote end accepts and sends DO COMPRESS2.
351
352 Note that in PROXY mode libtelnet will do no processing of its
353 own for you.
354
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400355 TELNET_EV_WONT:
356 TELNET_EV_DONT:
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400357 The WONT and DONT events are sent when the remote end of the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400358 connection wishes to disable an option, when they are refusing to
359 a support an option that you have asked for, or in confirmation of
360 an option you have asked to be disabled.
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400361
362 Most commonly WONT and DONT events are sent as rejections of
363 features you requested by sending DO or WILL events. Receiving
364 these events means the TELNET option is not or will not be
365 supported by the remote end, so give up.
366
367 Sometimes WONT or DONT will be sent for TELNET options that are
368 already enabled, but the remote end wishes to stop using. You
369 cannot decline. These events are demands that must be complied
370 with. libtelnet will always send the appropriate response back
371 without consulting your application. These events are sent to
372 allow your application to disable its own use of the features.
373
374 In either case, the TELNET option under negotiation will be in
375 event->telopt field.
376
377 Note that in PROXY mode libtelnet will do no processing of its
378 own for you.
379
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400380 TELNET_EV_SUBNEGOTIATION:
Sean Middleditch637df7f2009-03-15 12:57:32 -0400381 Triggered whenever a TELNET sub-negotiation has been received.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400382 Sub-negotiations include the NAWS option for communicating
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400383 terminal size to a server, the NEW-ENVIRON and TTYPE options for
384 negotiating terminal features, and MUD-centric protocols such as
385 ZMP, MSSP, and MCCP2.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400386
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400387 The event->telopt value is the option under sub-negotiation. The
388 remaining data (if any) is passed in event->buffer and
389 event->size. Note that most subnegotiation commands can include
390 embedded NUL bytes in the subnegotiation data, and the data
391 event->buffer is not NUL terminated, so always use the event->size
392 value!
Sean Middleditch892c5f12009-03-14 13:39:07 -0400393
Sean Middleditch637df7f2009-03-15 12:57:32 -0400394 The meaning and necessary processing for subnegotiations are
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400395 defined in various TELNET RFCs and other informal specifications.
396 A subnegotiation should never be sent unless the specific option
397 has been enabled through the use of the telnet negotiation
398 feature.
Sean Middleditch8d50fe82009-03-22 22:35:17 -0400399
400 TTYPE/ENVIRON/NEW-ENVIRON/MSSP SUPPORT:
401 These telopts all use a similar format to their subnegotiation
402 requests. The data is arrnaged as a series of terms, with each
403 term beginning with a single byte type marker (a small integer in
404 the range of 0 to 3) followed by zero or more bytes until another
405 type marker or the end of the subnegotiation data.
406
407 libtelnet parses these. The number of terms found is put in the
408 ev->argc field. The terms themselves are stored as
409 NUL-terminated strings in the ev->argv array. The type byte is
410 always the first byte of these strings, e.g. ev->argv[0][0]. Due
411 to the fact that 0 is a valid type marker, remember that
412 accessing the remainder of the term's data as a string must be
413 done as &ev->argv[term_index][1].
414
415 Note that libtelnet does not support the ESC byte for ENVIRON/
416 NEW-ENVIRON. Data using escaped bytes will not be parsed
417 correctly.
Sean Middleditchb10946c2009-03-22 18:21:14 -0400418
419 ZMP SUPPORT:
420 The event->argc field is the number of ZMP parameters, including
421 the command name, that have been received. The event->argv field
422 is an array of strings, one for each ZMP parameter. The command
423 name will be in event->argv[0]. If the ZMP command could not be
424 parsed because it was malformed, event->argc will be 0 (zero) and
425 event->argv will be NULL.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400426
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400427 TELNET_EV_COMPRESS
Sean Middleditch637df7f2009-03-15 12:57:32 -0400428 The COMPRESS event notifies the app that COMPRESS2/MCCP2
429 compression has begun or ended. Only servers can send compressed
430 data, and hence only clients will receive compressed data.
Sean Middleditchb9e48642009-03-12 23:33:27 -0400431
Sean Middleditch637df7f2009-03-15 12:57:32 -0400432 The event->command value will be 1 if compression has started and
433 will be 0 if compression has ended.
Sean Middleditch16992272009-03-15 19:42:03 -0400434
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400435 TELNET_EV_WARNING
436 The WARNING event is sent whenever something has gone wrong inside
437 of libtelnet (possibly due to malformed data sent by the other
438 end) but which recovery is (likely) possible. It may be safe to
439 continue using the connection, but some data may have been lost or
440 incorrectly interpreted.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400441
442 The event->buffer value will contain a NUL terminated string
443 explaining the error, and the event->size value containers the
444 length of the string.
445
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400446 TELNET_EV_ERROR
Sean Middleditch16992272009-03-15 19:42:03 -0400447 Similar to the WARNING event, the ERROR event is sent whenever
448 something has gone wrong. ERROR events are non-recoverable,
449 however, and the application should immediately close the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400450 connection. Whatever has happened is likely going only to result
451 in garbage from libtelnet. This is most likely to happen when a
452 COMPRESS2 stream fails, but other problems can occur.
Sean Middleditch16992272009-03-15 19:42:03 -0400453
454 The event->buffer value will contain a NUL terminated string
455 explaining the error, and the event->size value containers the
456 length of the string.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400457
458III. INTEGRATING LIBTELNET WITH COMMON MUDS
Sean Middleditchb9e48642009-03-12 23:33:27 -0400459=====================================================================
460
Sean Middleditch637df7f2009-03-15 12:57:32 -0400461FIXME: fill in some notes about how to splice in libtelnet with
462common Diku/Merc/Circle/etc. MUD codebases.
Sean Middleditchb9e48642009-03-12 23:33:27 -0400463
Sean Middleditch892c5f12009-03-14 13:39:07 -0400464IV. SAFETY AND CORRECTNESS CONSIDERATIONS
Sean Middleditchb9e48642009-03-12 23:33:27 -0400465=====================================================================
466
Sean Middleditch892c5f12009-03-14 13:39:07 -0400467Your existing application may make heavy use of its own output
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400468buffering and transmission commands, including hand-made routines for
469sending TELNET commands and sub-negotiation requests. There are at
470times subtle issues that need to be handled when communication over
471the TELNET protocol, not least of which is the need to escape any
472byte value 0xFF with a special TELNET command.
Sean Middleditchb9e48642009-03-12 23:33:27 -0400473
Sean Middleditch892c5f12009-03-14 13:39:07 -0400474For these reasons, it is very important that applications making use
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400475of libtelnet always make use of the libtelnet output functions for
476all data being sent over the TELNET connection.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400477
Sean Middleditch637df7f2009-03-15 12:57:32 -0400478In particular, if you are writing a client, all user input must be
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400479passed through to telnet_send(). This also includes any input
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400480generated automatically by scripts, triggers, or macros.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400481
482For a server, any and all output -- including ANSI/VT100 escape
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400483codes, regular text, newlines, and so on -- must be passed through to
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400484telnet_send().
Sean Middleditch637df7f2009-03-15 12:57:32 -0400485
486Any TELNET commands that are to be sent must be given to one of the
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400487following: telnet_iac, telnet_negotiate, or telnet_subnegotiation().
Sean Middleditch637df7f2009-03-15 12:57:32 -0400488
489If you are attempting to enable COMPRESS2/MCCP2, you must use the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400490telnet_begin_compress2() function.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400491
Sean Middleditch892c5f12009-03-14 13:39:07 -0400492V. MCCP2 COMPRESSION
Sean Middleditchb9e48642009-03-12 23:33:27 -0400493=====================================================================
494
Sean Middleditch892c5f12009-03-14 13:39:07 -0400495The MCCP2 (COMPRESS2) TELNET extension allows for the compression of
496all traffic sent from server to client. For more information:
497
498 http://www.mudbytes.net/index.php?a=articles&s=mccp
499
Sean Middleditch637df7f2009-03-15 12:57:32 -0400500In order for libtelnet to support MCCP2, zlib must be installed and
501enabled when compiling libtelnet. Use -DHAVE_ZLIB to enable zlib
502when compiling libtelnet.c and pass -lz to the linker to link in the
503zlib shared library.
504
Sean Middleditch892c5f12009-03-14 13:39:07 -0400505libtelnet transparently supports MCCP2. For a server to support
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400506MCCP2, the application must begin negotiation of the COMPRESS2 option
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400507using telnet_negotiate(), for example:
Sean Middleditch892c5f12009-03-14 13:39:07 -0400508
Sean Middleditch4f0c37f2009-03-20 23:08:55 -0400509 telnet_negotiate(&telnet, TELNET_WILL,
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400510 TELNET_OPTION_COMPRESS2, user_data);
Sean Middleditch892c5f12009-03-14 13:39:07 -0400511
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400512If a favorable DO COMPRESS2 is sent back from the client then the
513server application can begin compression at any time by calling
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400514telnet_begin_compress2().
Sean Middleditch892c5f12009-03-14 13:39:07 -0400515
Sean Middleditch637df7f2009-03-15 12:57:32 -0400516If a connection is in PROXY mode and COMPRESS2 support is enabled
517then libtelnet will automatically detect the start of a COMPRESS2
518stream, in either the sending or receiving direction.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400519
Sean Middleditch18877352009-03-22 16:57:44 -0400520VI. ZENITH MUD PROTOCOL (ZMP) SUPPORT
521=====================================================================
522
523The Zenith MUD Protocol allows applications to send messages across
524the TELNET connection outside of the normal user input/output data
525stream. libtelnet offers some limited support for receiving and
526sending ZMP commands to make implementing a full ZMP stack easier.
527For more information on ZMP:
528
529 http://zmp.sourcemud.org/
530
531For a server to enable ZMP, it must send the WILL ZMP negotitaion:
532
533 telnet_negotiate(&telnet, TELNET_WILL, TELNET_TELOPT_ZMP);
534
535For a client to support ZMP it must include ZMP in the telopt table
536passed to telnet_init(), with the him field set to TELNET_DO:
537
538 { TELNET_TELOPT_ZMP, TELNET_WONT, TELNET_DO },
539
540Note that while ZMP is a bi-directional protocol, it is only ever
541enabled on the server end of the connection. This automatically
542enables the client to send ZMP commands. The client must never
543attempt to negotiate ZMP directly using telnet_negotiate().
544
545Once ZMP is enabled, any ZMP commands received will automatically be
Sean Middleditchb10946c2009-03-22 18:21:14 -0400546sent to the event handler function with the TELNET_EV_SUBNEGOTIATION
547event code. The command will automatically be parsed and the ZMP
548parameters will be placed in the event->argv array and the number of
549parameters will be placed in the event->argc field.
550
551NOTE: if an error occured while parsing the ZMP command because it
552was malformed, the event->argc field will be equal to 0 and the
553event->argv field will be NULL. You should always check for this
554before attempting to access the parameter array.
Sean Middleditch18877352009-03-22 16:57:44 -0400555
556To send ZMP commands to the remote end, use either telnet_send_zmp()
557or telnet_send_zmpv().
558
559 int telnet_send_zmp(telnet_t *telnet, size_t argv,
560 const char **argv);
561 Sends a ZMP command to the remote end. The argc parameter is the
562 number of ZMP parameters (including the command name!) to be sent.
563 The argv parameter is an array of strings containing the
564 parameters. The element in argv[0] is the command name itself.
565 The argv array must have at least as many elements as the value
566 argc.
567
568VII. TELNET PROXY UTILITY
Sean Middleditch892c5f12009-03-14 13:39:07 -0400569=====================================================================
570
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400571The telnet-proxy utility is a small application that serves both as a
572testbed for libtelnet and as a powerful debugging tool for TELNET
Sean Middleditch892c5f12009-03-14 13:39:07 -0400573servers and clients.
574
575To use telnet-proxy, you must first compile it using:
576
577 $ make
578
579If you do not have zlib installed and wish to disable MCCP2 support
580then you must first edit the Makefile and remove the -DHAVE_ZLIB and
581the -lz from the compile flags.
582
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400583To run telnet-proxy, you simply give it the server's host name or IP
584address, the server's port number, and the port number that
Sean Middleditchd88f1832009-03-15 01:06:17 -0400585telnet-proxy should listen on. For example, to connect to the server
586on mud.example.com port 7800 and to listen on port 5000, run:
Sean Middleditch892c5f12009-03-14 13:39:07 -0400587
Sean Middleditchd88f1832009-03-15 01:06:17 -0400588 $ ./telnet-proxy mud.example.com 7800 5000
Sean Middleditch892c5f12009-03-14 13:39:07 -0400589
590You can then connect to the host telnet-proxy is running on (e.g.
Sean Middleditchd88f1832009-03-15 01:06:17 -0400591127.0.0.1) on port 500 and you will automatically be proxied into
592mud.example.com.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400593
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400594telnet-proxy will display status information about the data passing
595through both ends of the tunnel. telnet-proxy can only support a
596single tunnel at a time. It will continue running until an error
597occurs or a terminating signal is sent to the proxy process.