blob: b643bc2984d4dbceb0a6b47ae77d173ecff260fa [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
Sean Middleditch9d2f98a2009-03-14 05:24:56 -040021 - automatic MCCP2 handling (controllable by host app)
Sean Middleditch9d2f98a2009-03-14 05:24:56 -040022 ? ZMP parsing
23 ? MSSP parsing
24 ? ENVIRON/NEW-ENVIRON parsing
25 ? telnet-status testing tool
Sean Middleditch9d2f98a2009-03-14 05:24:56 -040026
Sean Middleditchb9e48642009-03-12 23:33:27 -040027I. INTRODUCTION
28=====================================================================
29
30libtelnet provides safe and correct handling of the core TELNET
Sean Middleditch8b788962009-03-16 01:06:27 -040031protocol. In addition to the base TELNET protocol, libtelnet also
Sean Middleditchf65f27d2009-03-19 02:32:04 -040032implements the Q method of TELNET option negotiation. libtelnet can
33be used for writing servers, clients, or proxies.
Sean Middleditchb9e48642009-03-12 23:33:27 -040034
Sean Middleditch892c5f12009-03-14 13:39:07 -040035For more information on the TELNET protocol, see:
36
37 http://www.faqs.org/rfcs/rfc854.html
Sean Middleditch8b788962009-03-16 01:06:27 -040038 http://www.faqs.org/rfcs/rfc1143.html
Sean Middleditch892c5f12009-03-14 13:39:07 -040039
Sean Middleditchb9e48642009-03-12 23:33:27 -040040II. LIBTELNET API
41=====================================================================
42
Sean Middleditch892c5f12009-03-14 13:39:07 -040043The libtelnet API contains several distinct parts. The first part is
44the basic initialization and deinitialization routines. The second
Sean Middleditchf65f27d2009-03-19 02:32:04 -040045part is a single function for pushing received data into the telnet
46processor. The third part is the telnet_send_*() functions, which
47generate TELNET commands and ensure data is properly formatted before
48sending over the wire. The final part is the event handler
49interface.
Sean Middleditch892c5f12009-03-14 13:39:07 -040050
51IIa. Initialization
52
Sean Middleditchf65f27d2009-03-19 02:32:04 -040053 struct telnet_t;
Sean Middleditch892c5f12009-03-14 13:39:07 -040054 This structure represents the state of the TELNET protocol for a
Sean Middleditchf65f27d2009-03-19 02:32:04 -040055 single connection. Each connection utilizing TELNET must have its
56 own telnet_t structure, which is passed to all libtelnet API
57 calls.
Sean Middleditch892c5f12009-03-14 13:39:07 -040058
Sean Middleditchf65f27d2009-03-19 02:32:04 -040059 void telnet_init(telnet_t *telnet, telnet_event_handler_t handler,
Sean Middleditch08bb05f2009-03-15 23:29:46 -040060 unsigned char flags, void *user_data);
Sean Middleditchf65f27d2009-03-19 02:32:04 -040061 The telnet_init() function is responsible for initializing the
62 data in a telnet_t structure. It must be called immediately after
63 establishing a connection and before any other libtelnet API calls
64 are made.
Sean Middleditch892c5f12009-03-14 13:39:07 -040065
Sean Middleditch637df7f2009-03-15 12:57:32 -040066 The handler parameter must be a function matching the
Sean Middleditchf65f27d2009-03-19 02:32:04 -040067 telnet_event_handler_t definition. More information about events
68 can be found in section IId.
Sean Middleditch30323022009-03-14 21:45:28 -040069
Sean Middleditch9f79cc52009-03-15 13:39:24 -040070 The user_data parameter is passed to the event handler whenver it
71 is invoked. This will usually be a structure container
72 information about the connection, including a socket descriptor
Sean Middleditchf65f27d2009-03-19 02:32:04 -040073 for implementing TELNET_EV_SEND event handling.
Sean Middleditch9f79cc52009-03-15 13:39:24 -040074
Sean Middleditch08bb05f2009-03-15 23:29:46 -040075 The flags parameter can be any of the following flag constants
76 bit-or'd together, or 0 to leave all options disabled.
77
Sean Middleditchf65f27d2009-03-19 02:32:04 -040078 TELNET_FLAG_PROXY
Sean Middleditchc337ba62009-03-16 16:47:27 -040079 Operate in proxy mode. This disables the RFC1143 support and
80 enables automatic detection of COMPRESS2 streams.
Sean Middleditch892c5f12009-03-14 13:39:07 -040081
Sean Middleditchf65f27d2009-03-19 02:32:04 -040082 boid telnet_free(telnet_t *telnet);
83 Releases any internal memory allocated by libtelnet. This must be
84 called whenever a connection is closed, or you will incur memory
85 leaks.
Sean Middleditch892c5f12009-03-14 13:39:07 -040086
87IIb. Receiving Data
88
Sean Middleditchf65f27d2009-03-19 02:32:04 -040089 void telnet_push(telnet_t *telnet,
Sean Middleditch340a51b2009-03-19 02:08:46 -040090 const char *buffer, unsigned int size, void *user_data);
Sean Middleditch892c5f12009-03-14 13:39:07 -040091 When your application receives data over the socket from the
92 remote end, it must pass the received bytes into this function.
Sean Middleditch637df7f2009-03-15 12:57:32 -040093
94 As the TELNET stream is parsed, events will be generated and
Sean Middleditchf65f27d2009-03-19 02:32:04 -040095 passed to the event handler given to telnet_init(). Of particular
96 interest for data receiving is the TELNET_EV_DATA event, which is
97 triggered for any regular data such as user input or server
98 process output.
Sean Middleditch892c5f12009-03-14 13:39:07 -040099
100IIc. Sending Data
101
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400102 All of the telnet_send_*() functions will invoke the TELNET_EV_SEND
103 event.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400104
105 Note: it is very important that ALL data sent to the remote end of
106 the connection be passed through libtelnet. All user input or
107 process output that you wish to send over the wire should be given
Sean Middleditch4613e682009-03-16 17:21:48 -0400108 to one of the following functions. Do NOT send or buffer
109 unprocessed output data directly!
Sean Middleditch892c5f12009-03-14 13:39:07 -0400110
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400111 void telnet_send_command(telnet_t *telnet, unsigned char cmd);
Sean Middleditch892c5f12009-03-14 13:39:07 -0400112 Sends a single "simple" TELNET command, such as the GO-AHEAD
113 commands (255 249).
114
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400115 void telnet_send_negotiate(telnet_t *telnet, unsigned char cmd,
116 unsigned char opt);
117 Sends a TELNET negotiation command. The cmd parameter must be one
118 of TELNET_WILL, TELNET_DONT, TELNET_DO, or TELNET_DONT. The opt
119 parameter is the option to negotiate.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400120
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400121 Unless in PROXY mode, the RFC1143 support may delay or ellide the
122 request entirely, as appropriate. It will ignore duplicate
123 invocations, such as asking for WILL NAWS when NAWS is already on
124 or is currently awaiting response from the remote end.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400125
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400126 void telnet_send_data(telnet_t *telnet, const char *buffer,
Sean Middleditch97a8cb22009-03-16 16:51:41 -0400127 unsigned int size);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400128 Sends raw data, which would be either the process output from a
129 server or the user input from a client.
130
131 For sending regular text is may be more convenient to use
132 telnet_printf().
Sean Middleditch90e79da2009-03-19 15:17:13 -0400133
134 void telnet_begin_subnegotiation(telnet_t *telnet, unsigned char
135 telopt);
136 Sends the header for a TELNET sub-negotiation command for the
137 specified option. All send data following this command will be
138 part of the sub-negotiation data until a call is made to
139 telnet_finish_subnegotiation().
140
141 You should not use telnet_printf() for sending subnegotiation
142 data as it will perform newline translations that usually do not
143 need to be done for subnegotiation data, and may cause problems.
144
145 void telnet_finish_subnegotiation(telnet_t *telnet);
146 Sends the end marker for a TELNET sub-negotiation command. This
147 must be called after (and only after) a call has been made to
148 telnet_begin_subnegotiation() and any negotiation data has been
149 sent.
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400150
151 void telnet_send_subnegotiation(telnet_t *telnet,
152 unsigned char telopt, const char *buffer, unsigned int size);
153 Sends a TELNET sub-negotiation command. The telopt parameter is
154 the sub-negotiation option.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400155
Sean Middleditch90e79da2009-03-19 15:17:13 -0400156 Note that this function is just a shorthand for:
157 telnet_begin_subnegotiation(telnet, telopt);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400158 telnet_send_data(telnet, buffer, size);
Sean Middleditch90e79da2009-03-19 15:17:13 -0400159 telnet_end_subnegotiation(telnet);
Sean Middleditch2b4bfc42009-03-16 01:25:52 -0400160
161 For some subnegotiations that involve a lot of complex formatted
Sean Middleditch90e79da2009-03-19 15:17:13 -0400162 data to be sent, it may be easier to make calls to both
163 telnet_begin_negotiation() and telnet_end_subnegotiation() and
164 using telnet_send_data() or telnet_printf2() to format the data.
Sean Middleditch2b4bfc42009-03-16 01:25:52 -0400165
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400166 NOTE: telnet_send_subnegotiation() does have special behavior in
167 PROXY mode, as in that mode this function will automatically
Sean Middleditch2b4bfc42009-03-16 01:25:52 -0400168 detect the COMPRESS2 marker and enable zlib compression.
169
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400170 int telnet_printf(telnet_t *telnet, const char *fmt, ...);
171 This functions very similarly to fprintf, except that output is
172 sent through libtelnet for processing. IAC bytes are properly
173 escaped, C newlines (\n) are translated into CR LF, and C carriage
174 returns (\r) are translated into CR NUL, all as required by
175 RFC854. The return code is the length of the formatted text.
Sean Middleditch4613e682009-03-16 17:21:48 -0400176
177 NOTE: due to an internal implementation detail, the maximum
178 lenth of the formatted text is 4096 characters.
179
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400180 int telnet_printf2(telnet_t *telnet, const char *fmt, ...);
181 Identical to telnet_printf() except that \r and \n are not
182 translated. This should be used if you are attempting to send
183 raw data inside a subnegotiation or if you have already manually
184 escaped newlines.
Sean Middleditch4613e682009-03-16 17:21:48 -0400185
Sean Middleditch637df7f2009-03-15 12:57:32 -0400186IId. Event Handling
Sean Middleditch892c5f12009-03-14 13:39:07 -0400187
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400188 libtelnet relies on an event-handling mechanism for processing the
189 parsed TELNET protocol stream as well as for buffering and sending
190 output data.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400191
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400192 When you initialize a telnet_t structure with telnet_init() you had
193 to pass in an event handler function. This function must meet the
194 following prototype:
Sean Middleditch30323022009-03-14 21:45:28 -0400195
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400196 void (telnet_t *telnet, telnet_event_t *event, void *user_data);
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400197
198 The event structure is detailed below. The user_data value is the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400199 pointer passed to telnet_init().
Sean Middleditch637df7f2009-03-15 12:57:32 -0400200
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400201 struct telnet_event_t {
Sean Middleditch340a51b2009-03-19 02:08:46 -0400202 const char *buffer;
Sean Middleditch637df7f2009-03-15 12:57:32 -0400203 unsigned int size;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400204 telnet_event_type_t type;
Sean Middleditch812358d2009-03-15 23:24:03 -0400205 unsigned char command;
206 unsigned char telopt;
207 unsigned char accept;
Sean Middleditch637df7f2009-03-15 12:57:32 -0400208 };
209
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400210 The enumeration values of telnet_event_type_t are described in
Sean Middleditch637df7f2009-03-15 12:57:32 -0400211 detail below. Whenever the the event handler is invoked, the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400212 application must look at the event->type value and do any necessary
213 processing.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400214
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400215 The only event that MUST be implemented is TELNET_EV_SEND. Most
216 applications will also always want to implement the event
217 TELNET_EV_DATA.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400218
219 Here is an example event handler implementation which includes
220 handlers for several important events.
221
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400222 void my_event_handler(telnet_t *telnet, telnet_event_t *ev,
Sean Middleditch812358d2009-03-15 23:24:03 -0400223 void *user_data) {
Sean Middleditch637df7f2009-03-15 12:57:32 -0400224 struct user_info *user = (struct user_info *)user_data;
225
226 switch (ev->type) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400227 case TELNET_EV_DATA:
Sean Middleditch637df7f2009-03-15 12:57:32 -0400228 process_user_input(user, event->buffer, event->size);
229 break;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400230 case TELNET_EV_SEND:
Sean Middleditch637df7f2009-03-15 12:57:32 -0400231 write_to_descriptor(user, event->buffer, event->size);
232 break;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400233 case TELNET_EV_ERROR:
Sean Middleditch637df7f2009-03-15 12:57:32 -0400234 fatal_error("TELNET error: %s", event->buffer);
235 break;
236 }
Sean Middleditch30323022009-03-14 21:45:28 -0400237 }
238
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400239 TELNET_EV_DATA:
240 The DATA event is triggered whenever regular data (not part of any
241 special TELNET command) is received. For a client, this will be
242 process output from the server. For a server, this will be input
243 typed by the user.
Sean Middleditch30323022009-03-14 21:45:28 -0400244
Sean Middleditch637df7f2009-03-15 12:57:32 -0400245 The event->buffer value will contain the bytes received and the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400246 event->size value will contain the number of bytes received. Note
247 that event->buffer is not NUL terminated!
Sean Middleditch30323022009-03-14 21:45:28 -0400248
Sean Middleditch637df7f2009-03-15 12:57:32 -0400249 NOTE: there is no guarantee that user input or server output
250 will be received in whole lines. If you wish to process data
251 a line at a time, you are responsible for buffering the data and
252 checking for line terminators yourself!
253
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400254 TELNET_EV_SEND:
255 This event is sent whenever libtelnet has generated data that must
256 be sent over the wire to the remove end. Generally that means
257 calling send() or adding the data to your application's output
258 buffer.
Sean Middleditch30323022009-03-14 21:45:28 -0400259
Sean Middleditch637df7f2009-03-15 12:57:32 -0400260 The event->buffer value will contain the bytes to send and the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400261 event->size value will contain the number of bytes to send. Note
262 that event->buffer is not NUL terminated, and may include NUL
263 characters in its data, so always use event->size!
Sean Middleditch30323022009-03-14 21:45:28 -0400264
Sean Middleditch637df7f2009-03-15 12:57:32 -0400265 NOTE: Your SEND event handler must send or buffer the data in
266 its raw form as provided by libtelnet. If you wish to perform
267 any kind of preprocessing on data you want to send to the other
268
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400269 TELNET_EV_IAC:
Sean Middleditch637df7f2009-03-15 12:57:32 -0400270 The IAC event is triggered whenever a simple IAC command is
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400271 received, such as the IAC EOR (end of record, also called go ahead
272 or GA) command.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400273
Sean Middleditch637df7f2009-03-15 12:57:32 -0400274 The command received is in the event->command value.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400275
Sean Middleditch637df7f2009-03-15 12:57:32 -0400276 The necessary processing depends on the specific commands; see
277 the TELNET RFC for more information.
278
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400279 TELNET_EV_WILL:
280 TELNET_EV_DO:
281 The WILL and DO events are sent when a TELNET negotiation command
282 of the same name is received.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400283
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400284 WILL events are sent by the remote end when they wish to be
285 allowed to turn an option on on their end, or in confirmation
286 after you have sent a DO command to them.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400287
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400288 DO events are sent by the remote end when they wish for you to
289 turn on an option on your end, or in confirmation after you have
290 sent a WILL command to them.
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400291
292 In either case, the TELNET option under negotiation will be in
293 event->telopt field.
294
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400295 If you support the option and wish for it to be enabled you must
296 set the event->accept field to 1, unless this event is a
297 confirmation for a previous WILL/DO command you sent to the remote
298 end. If you do not set event->field to 1 then libtelnet will send
299 a rejection command back to the other end.
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400300
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400301 libtelnet manages some of the pecularities of negotiation for you.
302 For information on libtelnet's negotiation method, see:
Sean Middleditch892c5f12009-03-14 13:39:07 -0400303
304 http://www.faqs.org/rfcs/rfc1143.html
305
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400306 Examples:
307
308 You want remote end to use TTYPE, so you send DO TTYPE.
309 Remote accepts and sends WILL TTYPE.
310
311 Remote end wants you to use SGA, so they send DO_SGA.
312 You do not support SGA and set event->accept = 0.
313
314 Remote end wants to use ZMP, so they send WILL ZMP.
315 You support ZMP, so you set event->accept = 1 and enable
316 local ZMP support.
317
318 You want to use MCCP2, so you send WILL COMPRESS2.
319 Remote end accepts and sends DO COMPRESS2.
320
321 Note that in PROXY mode libtelnet will do no processing of its
322 own for you.
323
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400324 TELNET_EV_WONT:
325 TELNET_EV_DONT:
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400326 The WONT and DONT events are sent when the remote end of the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400327 connection wishes to disable an option, when they are refusing to
328 a support an option that you have asked for, or in confirmation of
329 an option you have asked to be disabled.
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400330
331 Most commonly WONT and DONT events are sent as rejections of
332 features you requested by sending DO or WILL events. Receiving
333 these events means the TELNET option is not or will not be
334 supported by the remote end, so give up.
335
336 Sometimes WONT or DONT will be sent for TELNET options that are
337 already enabled, but the remote end wishes to stop using. You
338 cannot decline. These events are demands that must be complied
339 with. libtelnet will always send the appropriate response back
340 without consulting your application. These events are sent to
341 allow your application to disable its own use of the features.
342
343 In either case, the TELNET option under negotiation will be in
344 event->telopt field.
345
346 Note that in PROXY mode libtelnet will do no processing of its
347 own for you.
348
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400349 TELNET_EV_SUBNEGOTIATION:
Sean Middleditch637df7f2009-03-15 12:57:32 -0400350 Triggered whenever a TELNET sub-negotiation has been received.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400351 Sub-negotiations include the NAWS option for communicating
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400352 terminal size to a server, the NEW-ENVIRON and TTYPE options for
353 negotiating terminal features, and MUD-centric protocols such as
354 ZMP, MSSP, and MCCP2.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400355
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400356 The event->telopt value is the option under sub-negotiation. The
357 remaining data (if any) is passed in event->buffer and
358 event->size. Note that most subnegotiation commands can include
359 embedded NUL bytes in the subnegotiation data, and the data
360 event->buffer is not NUL terminated, so always use the event->size
361 value!
Sean Middleditch892c5f12009-03-14 13:39:07 -0400362
Sean Middleditch637df7f2009-03-15 12:57:32 -0400363 The meaning and necessary processing for subnegotiations are
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400364 defined in various TELNET RFCs and other informal specifications.
365 A subnegotiation should never be sent unless the specific option
366 has been enabled through the use of the telnet negotiation
367 feature.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400368
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400369 TELNET_EV_COMPRESS
Sean Middleditch637df7f2009-03-15 12:57:32 -0400370 The COMPRESS event notifies the app that COMPRESS2/MCCP2
371 compression has begun or ended. Only servers can send compressed
372 data, and hence only clients will receive compressed data.
Sean Middleditchb9e48642009-03-12 23:33:27 -0400373
Sean Middleditch637df7f2009-03-15 12:57:32 -0400374 The event->command value will be 1 if compression has started and
375 will be 0 if compression has ended.
Sean Middleditch16992272009-03-15 19:42:03 -0400376
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400377 TELNET_EV_WARNING
378 The WARNING event is sent whenever something has gone wrong inside
379 of libtelnet (possibly due to malformed data sent by the other
380 end) but which recovery is (likely) possible. It may be safe to
381 continue using the connection, but some data may have been lost or
382 incorrectly interpreted.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400383
384 The event->buffer value will contain a NUL terminated string
385 explaining the error, and the event->size value containers the
386 length of the string.
387
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400388 TELNET_EV_ERROR
Sean Middleditch16992272009-03-15 19:42:03 -0400389 Similar to the WARNING event, the ERROR event is sent whenever
390 something has gone wrong. ERROR events are non-recoverable,
391 however, and the application should immediately close the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400392 connection. Whatever has happened is likely going only to result
393 in garbage from libtelnet. This is most likely to happen when a
394 COMPRESS2 stream fails, but other problems can occur.
Sean Middleditch16992272009-03-15 19:42:03 -0400395
396 The event->buffer value will contain a NUL terminated string
397 explaining the error, and the event->size value containers the
398 length of the string.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400399
400III. INTEGRATING LIBTELNET WITH COMMON MUDS
Sean Middleditchb9e48642009-03-12 23:33:27 -0400401=====================================================================
402
Sean Middleditch637df7f2009-03-15 12:57:32 -0400403FIXME: fill in some notes about how to splice in libtelnet with
404common Diku/Merc/Circle/etc. MUD codebases.
Sean Middleditchb9e48642009-03-12 23:33:27 -0400405
Sean Middleditch892c5f12009-03-14 13:39:07 -0400406IV. SAFETY AND CORRECTNESS CONSIDERATIONS
Sean Middleditchb9e48642009-03-12 23:33:27 -0400407=====================================================================
408
Sean Middleditch892c5f12009-03-14 13:39:07 -0400409Your existing application may make heavy use of its own output
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400410buffering and transmission commands, including hand-made routines for
411sending TELNET commands and sub-negotiation requests. There are at
412times subtle issues that need to be handled when communication over
413the TELNET protocol, not least of which is the need to escape any
414byte value 0xFF with a special TELNET command.
Sean Middleditchb9e48642009-03-12 23:33:27 -0400415
Sean Middleditch892c5f12009-03-14 13:39:07 -0400416For these reasons, it is very important that applications making use
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400417of libtelnet always make use of the telnet_send_*() family of
Sean Middleditch892c5f12009-03-14 13:39:07 -0400418functions for all data being sent over the TELNET connection.
419
Sean Middleditch637df7f2009-03-15 12:57:32 -0400420In particular, if you are writing a client, all user input must be
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400421passed through to telnet_send_data(). This also includes any input
422generated automatically by scripts, triggers, or macros.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400423
424For a server, any and all output -- including ANSI/VT100 escape
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400425codes, regular text, newlines, and so on -- must be passed through to
426telnet_send_data().
Sean Middleditch637df7f2009-03-15 12:57:32 -0400427
428Any TELNET commands that are to be sent must be given to one of the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400429following: telnet_send_command, telnet_send_negotiate, or
430telnet_send_subnegotiation().
Sean Middleditch637df7f2009-03-15 12:57:32 -0400431
432If you are attempting to enable COMPRESS2/MCCP2, you must use the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400433telnet_begin_compress2() function.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400434
Sean Middleditch892c5f12009-03-14 13:39:07 -0400435V. MCCP2 COMPRESSION
Sean Middleditchb9e48642009-03-12 23:33:27 -0400436=====================================================================
437
Sean Middleditch892c5f12009-03-14 13:39:07 -0400438The MCCP2 (COMPRESS2) TELNET extension allows for the compression of
439all traffic sent from server to client. For more information:
440
441 http://www.mudbytes.net/index.php?a=articles&s=mccp
442
Sean Middleditch637df7f2009-03-15 12:57:32 -0400443In order for libtelnet to support MCCP2, zlib must be installed and
444enabled when compiling libtelnet. Use -DHAVE_ZLIB to enable zlib
445when compiling libtelnet.c and pass -lz to the linker to link in the
446zlib shared library.
447
Sean Middleditch892c5f12009-03-14 13:39:07 -0400448libtelnet transparently supports MCCP2. For a server to support
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400449MCCP2, the application must begin negotiation of the COMPRESS2 option
450using telnet_send_negotiate(), for example:
Sean Middleditch892c5f12009-03-14 13:39:07 -0400451
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400452 telnet_send_negotiate(&telnet, TELNET_WILL,
453 TELNET_OPTION_COMPRESS2, user_data);
Sean Middleditch892c5f12009-03-14 13:39:07 -0400454
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400455If a favorable DO COMPRESS2 is sent back from the client then the
456server application can begin compression at any time by calling
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400457telnet_begin_compress2().
Sean Middleditch892c5f12009-03-14 13:39:07 -0400458
Sean Middleditch637df7f2009-03-15 12:57:32 -0400459If a connection is in PROXY mode and COMPRESS2 support is enabled
460then libtelnet will automatically detect the start of a COMPRESS2
461stream, in either the sending or receiving direction.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400462
463VI. TELNET PROXY UTILITY
464=====================================================================
465
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400466The telnet-proxy utility is a small application that serves both as a
467testbed for libtelnet and as a powerful debugging tool for TELNET
Sean Middleditch892c5f12009-03-14 13:39:07 -0400468servers and clients.
469
470To use telnet-proxy, you must first compile it using:
471
472 $ make
473
474If you do not have zlib installed and wish to disable MCCP2 support
475then you must first edit the Makefile and remove the -DHAVE_ZLIB and
476the -lz from the compile flags.
477
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400478To run telnet-proxy, you simply give it the server's host name or IP
479address, the server's port number, and the port number that
Sean Middleditchd88f1832009-03-15 01:06:17 -0400480telnet-proxy should listen on. For example, to connect to the server
481on mud.example.com port 7800 and to listen on port 5000, run:
Sean Middleditch892c5f12009-03-14 13:39:07 -0400482
Sean Middleditchd88f1832009-03-15 01:06:17 -0400483 $ ./telnet-proxy mud.example.com 7800 5000
Sean Middleditch892c5f12009-03-14 13:39:07 -0400484
485You can then connect to the host telnet-proxy is running on (e.g.
Sean Middleditchd88f1832009-03-15 01:06:17 -0400486127.0.0.1) on port 500 and you will automatically be proxied into
487mud.example.com.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400488
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400489telnet-proxy will display status information about the data passing
490through both ends of the tunnel. telnet-proxy can only support a
491single tunnel at a time. It will continue running until an error
492occurs or a terminating signal is sent to the proxy process.