blob: 81af3cd1871f59b62d6dca700e120a8667b2b59b [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_telopt(telnet_t *telnet, unsigned char cmd,
Sean Middleditch2b4bfc42009-03-16 01:25:52 -0400116 unsigned char telopt);
117 Sends a TELNET command with an option code following. This is
118 only useful for the WILL, WONT, DO, DONT, and SB commands.
119
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400120 void telnet_send_negotiate(telnet_t *telnet, unsigned char cmd,
121 unsigned char opt);
122 Sends a TELNET negotiation command. The cmd parameter must be one
123 of TELNET_WILL, TELNET_DONT, TELNET_DO, or TELNET_DONT. The opt
124 parameter is the option to negotiate.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400125
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400126 Unless in PROXY mode, the RFC1143 support may delay or ellide the
127 request entirely, as appropriate. It will ignore duplicate
128 invocations, such as asking for WILL NAWS when NAWS is already on
129 or is currently awaiting response from the remote end.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400130
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400131 void telnet_send_data(telnet_t *telnet, const char *buffer,
Sean Middleditch97a8cb22009-03-16 16:51:41 -0400132 unsigned int size);
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400133 Sends raw data, which would be either the process output from a
134 server or the user input from a client.
135
136 For sending regular text is may be more convenient to use
137 telnet_printf().
138
139 void telnet_send_subnegotiation(telnet_t *telnet,
140 unsigned char telopt, const char *buffer, unsigned int size);
141 Sends a TELNET sub-negotiation command. The telopt parameter is
142 the sub-negotiation option.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400143
Sean Middleditch2b4bfc42009-03-16 01:25:52 -0400144 Note that the above function is just a shorthand for:
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400145 telnet_send_telopt(telnet, TELNET_SB, telopt);
146 telnet_send_data(telnet, buffer, size);
147 telnet_send_command(telnet, TELNET_SE);
Sean Middleditch2b4bfc42009-03-16 01:25:52 -0400148
149 For some subnegotiations that involve a lot of complex formatted
150 data to be sent, it may be easier to manually send the SB telopt
151 header and SE footer around mulitple calls to send_data.
152
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400153 NOTE: telnet_send_subnegotiation() does have special behavior in
154 PROXY mode, as in that mode this function will automatically
Sean Middleditch2b4bfc42009-03-16 01:25:52 -0400155 detect the COMPRESS2 marker and enable zlib compression.
156
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400157 int telnet_printf(telnet_t *telnet, const char *fmt, ...);
158 This functions very similarly to fprintf, except that output is
159 sent through libtelnet for processing. IAC bytes are properly
160 escaped, C newlines (\n) are translated into CR LF, and C carriage
161 returns (\r) are translated into CR NUL, all as required by
162 RFC854. The return code is the length of the formatted text.
Sean Middleditch4613e682009-03-16 17:21:48 -0400163
164 NOTE: due to an internal implementation detail, the maximum
165 lenth of the formatted text is 4096 characters.
166
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400167 int telnet_printf2(telnet_t *telnet, const char *fmt, ...);
168 Identical to telnet_printf() except that \r and \n are not
169 translated. This should be used if you are attempting to send
170 raw data inside a subnegotiation or if you have already manually
171 escaped newlines.
Sean Middleditch4613e682009-03-16 17:21:48 -0400172
Sean Middleditch637df7f2009-03-15 12:57:32 -0400173IId. Event Handling
Sean Middleditch892c5f12009-03-14 13:39:07 -0400174
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400175 libtelnet relies on an event-handling mechanism for processing the
176 parsed TELNET protocol stream as well as for buffering and sending
177 output data.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400178
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400179 When you initialize a telnet_t structure with telnet_init() you had
180 to pass in an event handler function. This function must meet the
181 following prototype:
Sean Middleditch30323022009-03-14 21:45:28 -0400182
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400183 void (telnet_t *telnet, telnet_event_t *event, void *user_data);
Sean Middleditch9f79cc52009-03-15 13:39:24 -0400184
185 The event structure is detailed below. The user_data value is the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400186 pointer passed to telnet_init().
Sean Middleditch637df7f2009-03-15 12:57:32 -0400187
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400188 struct telnet_event_t {
Sean Middleditch340a51b2009-03-19 02:08:46 -0400189 const char *buffer;
Sean Middleditch637df7f2009-03-15 12:57:32 -0400190 unsigned int size;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400191 telnet_event_type_t type;
Sean Middleditch812358d2009-03-15 23:24:03 -0400192 unsigned char command;
193 unsigned char telopt;
194 unsigned char accept;
Sean Middleditch637df7f2009-03-15 12:57:32 -0400195 };
196
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400197 The enumeration values of telnet_event_type_t are described in
Sean Middleditch637df7f2009-03-15 12:57:32 -0400198 detail below. Whenever the the event handler is invoked, the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400199 application must look at the event->type value and do any necessary
200 processing.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400201
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400202 The only event that MUST be implemented is TELNET_EV_SEND. Most
203 applications will also always want to implement the event
204 TELNET_EV_DATA.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400205
206 Here is an example event handler implementation which includes
207 handlers for several important events.
208
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400209 void my_event_handler(telnet_t *telnet, telnet_event_t *ev,
Sean Middleditch812358d2009-03-15 23:24:03 -0400210 void *user_data) {
Sean Middleditch637df7f2009-03-15 12:57:32 -0400211 struct user_info *user = (struct user_info *)user_data;
212
213 switch (ev->type) {
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400214 case TELNET_EV_DATA:
Sean Middleditch637df7f2009-03-15 12:57:32 -0400215 process_user_input(user, event->buffer, event->size);
216 break;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400217 case TELNET_EV_SEND:
Sean Middleditch637df7f2009-03-15 12:57:32 -0400218 write_to_descriptor(user, event->buffer, event->size);
219 break;
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400220 case TELNET_EV_ERROR:
Sean Middleditch637df7f2009-03-15 12:57:32 -0400221 fatal_error("TELNET error: %s", event->buffer);
222 break;
223 }
Sean Middleditch30323022009-03-14 21:45:28 -0400224 }
225
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400226 TELNET_EV_DATA:
227 The DATA event is triggered whenever regular data (not part of any
228 special TELNET command) is received. For a client, this will be
229 process output from the server. For a server, this will be input
230 typed by the user.
Sean Middleditch30323022009-03-14 21:45:28 -0400231
Sean Middleditch637df7f2009-03-15 12:57:32 -0400232 The event->buffer value will contain the bytes received and the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400233 event->size value will contain the number of bytes received. Note
234 that event->buffer is not NUL terminated!
Sean Middleditch30323022009-03-14 21:45:28 -0400235
Sean Middleditch637df7f2009-03-15 12:57:32 -0400236 NOTE: there is no guarantee that user input or server output
237 will be received in whole lines. If you wish to process data
238 a line at a time, you are responsible for buffering the data and
239 checking for line terminators yourself!
240
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400241 TELNET_EV_SEND:
242 This event is sent whenever libtelnet has generated data that must
243 be sent over the wire to the remove end. Generally that means
244 calling send() or adding the data to your application's output
245 buffer.
Sean Middleditch30323022009-03-14 21:45:28 -0400246
Sean Middleditch637df7f2009-03-15 12:57:32 -0400247 The event->buffer value will contain the bytes to send and the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400248 event->size value will contain the number of bytes to send. Note
249 that event->buffer is not NUL terminated, and may include NUL
250 characters in its data, so always use event->size!
Sean Middleditch30323022009-03-14 21:45:28 -0400251
Sean Middleditch637df7f2009-03-15 12:57:32 -0400252 NOTE: Your SEND event handler must send or buffer the data in
253 its raw form as provided by libtelnet. If you wish to perform
254 any kind of preprocessing on data you want to send to the other
255
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400256 TELNET_EV_IAC:
Sean Middleditch637df7f2009-03-15 12:57:32 -0400257 The IAC event is triggered whenever a simple IAC command is
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400258 received, such as the IAC EOR (end of record, also called go ahead
259 or GA) command.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400260
Sean Middleditch637df7f2009-03-15 12:57:32 -0400261 The command received is in the event->command value.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400262
Sean Middleditch637df7f2009-03-15 12:57:32 -0400263 The necessary processing depends on the specific commands; see
264 the TELNET RFC for more information.
265
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400266 TELNET_EV_WILL:
267 TELNET_EV_DO:
268 The WILL and DO events are sent when a TELNET negotiation command
269 of the same name is received.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400270
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400271 WILL events are sent by the remote end when they wish to be
272 allowed to turn an option on on their end, or in confirmation
273 after you have sent a DO command to them.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400274
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400275 DO events are sent by the remote end when they wish for you to
276 turn on an option on your end, or in confirmation after you have
277 sent a WILL command to them.
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400278
279 In either case, the TELNET option under negotiation will be in
280 event->telopt field.
281
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400282 If you support the option and wish for it to be enabled you must
283 set the event->accept field to 1, unless this event is a
284 confirmation for a previous WILL/DO command you sent to the remote
285 end. If you do not set event->field to 1 then libtelnet will send
286 a rejection command back to the other end.
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400287
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400288 libtelnet manages some of the pecularities of negotiation for you.
289 For information on libtelnet's negotiation method, see:
Sean Middleditch892c5f12009-03-14 13:39:07 -0400290
291 http://www.faqs.org/rfcs/rfc1143.html
292
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400293 Examples:
294
295 You want remote end to use TTYPE, so you send DO TTYPE.
296 Remote accepts and sends WILL TTYPE.
297
298 Remote end wants you to use SGA, so they send DO_SGA.
299 You do not support SGA and set event->accept = 0.
300
301 Remote end wants to use ZMP, so they send WILL ZMP.
302 You support ZMP, so you set event->accept = 1 and enable
303 local ZMP support.
304
305 You want to use MCCP2, so you send WILL COMPRESS2.
306 Remote end accepts and sends DO COMPRESS2.
307
308 Note that in PROXY mode libtelnet will do no processing of its
309 own for you.
310
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400311 TELNET_EV_WONT:
312 TELNET_EV_DONT:
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400313 The WONT and DONT events are sent when the remote end of the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400314 connection wishes to disable an option, when they are refusing to
315 a support an option that you have asked for, or in confirmation of
316 an option you have asked to be disabled.
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400317
318 Most commonly WONT and DONT events are sent as rejections of
319 features you requested by sending DO or WILL events. Receiving
320 these events means the TELNET option is not or will not be
321 supported by the remote end, so give up.
322
323 Sometimes WONT or DONT will be sent for TELNET options that are
324 already enabled, but the remote end wishes to stop using. You
325 cannot decline. These events are demands that must be complied
326 with. libtelnet will always send the appropriate response back
327 without consulting your application. These events are sent to
328 allow your application to disable its own use of the features.
329
330 In either case, the TELNET option under negotiation will be in
331 event->telopt field.
332
333 Note that in PROXY mode libtelnet will do no processing of its
334 own for you.
335
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400336 TELNET_EV_SUBNEGOTIATION:
Sean Middleditch637df7f2009-03-15 12:57:32 -0400337 Triggered whenever a TELNET sub-negotiation has been received.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400338 Sub-negotiations include the NAWS option for communicating
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400339 terminal size to a server, the NEW-ENVIRON and TTYPE options for
340 negotiating terminal features, and MUD-centric protocols such as
341 ZMP, MSSP, and MCCP2.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400342
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400343 The event->telopt value is the option under sub-negotiation. The
344 remaining data (if any) is passed in event->buffer and
345 event->size. Note that most subnegotiation commands can include
346 embedded NUL bytes in the subnegotiation data, and the data
347 event->buffer is not NUL terminated, so always use the event->size
348 value!
Sean Middleditch892c5f12009-03-14 13:39:07 -0400349
Sean Middleditch637df7f2009-03-15 12:57:32 -0400350 The meaning and necessary processing for subnegotiations are
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400351 defined in various TELNET RFCs and other informal specifications.
352 A subnegotiation should never be sent unless the specific option
353 has been enabled through the use of the telnet negotiation
354 feature.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400355
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400356 TELNET_EV_COMPRESS
Sean Middleditch637df7f2009-03-15 12:57:32 -0400357 The COMPRESS event notifies the app that COMPRESS2/MCCP2
358 compression has begun or ended. Only servers can send compressed
359 data, and hence only clients will receive compressed data.
Sean Middleditchb9e48642009-03-12 23:33:27 -0400360
Sean Middleditch637df7f2009-03-15 12:57:32 -0400361 The event->command value will be 1 if compression has started and
362 will be 0 if compression has ended.
Sean Middleditch16992272009-03-15 19:42:03 -0400363
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400364 TELNET_EV_WARNING
365 The WARNING event is sent whenever something has gone wrong inside
366 of libtelnet (possibly due to malformed data sent by the other
367 end) but which recovery is (likely) possible. It may be safe to
368 continue using the connection, but some data may have been lost or
369 incorrectly interpreted.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400370
371 The event->buffer value will contain a NUL terminated string
372 explaining the error, and the event->size value containers the
373 length of the string.
374
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400375 TELNET_EV_ERROR
Sean Middleditch16992272009-03-15 19:42:03 -0400376 Similar to the WARNING event, the ERROR event is sent whenever
377 something has gone wrong. ERROR events are non-recoverable,
378 however, and the application should immediately close the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400379 connection. Whatever has happened is likely going only to result
380 in garbage from libtelnet. This is most likely to happen when a
381 COMPRESS2 stream fails, but other problems can occur.
Sean Middleditch16992272009-03-15 19:42:03 -0400382
383 The event->buffer value will contain a NUL terminated string
384 explaining the error, and the event->size value containers the
385 length of the string.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400386
387III. INTEGRATING LIBTELNET WITH COMMON MUDS
Sean Middleditchb9e48642009-03-12 23:33:27 -0400388=====================================================================
389
Sean Middleditch637df7f2009-03-15 12:57:32 -0400390FIXME: fill in some notes about how to splice in libtelnet with
391common Diku/Merc/Circle/etc. MUD codebases.
Sean Middleditchb9e48642009-03-12 23:33:27 -0400392
Sean Middleditch892c5f12009-03-14 13:39:07 -0400393IV. SAFETY AND CORRECTNESS CONSIDERATIONS
Sean Middleditchb9e48642009-03-12 23:33:27 -0400394=====================================================================
395
Sean Middleditch892c5f12009-03-14 13:39:07 -0400396Your existing application may make heavy use of its own output
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400397buffering and transmission commands, including hand-made routines for
398sending TELNET commands and sub-negotiation requests. There are at
399times subtle issues that need to be handled when communication over
400the TELNET protocol, not least of which is the need to escape any
401byte value 0xFF with a special TELNET command.
Sean Middleditchb9e48642009-03-12 23:33:27 -0400402
Sean Middleditch892c5f12009-03-14 13:39:07 -0400403For these reasons, it is very important that applications making use
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400404of libtelnet always make use of the telnet_send_*() family of
Sean Middleditch892c5f12009-03-14 13:39:07 -0400405functions for all data being sent over the TELNET connection.
406
Sean Middleditch637df7f2009-03-15 12:57:32 -0400407In particular, if you are writing a client, all user input must be
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400408passed through to telnet_send_data(). This also includes any input
409generated automatically by scripts, triggers, or macros.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400410
411For a server, any and all output -- including ANSI/VT100 escape
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400412codes, regular text, newlines, and so on -- must be passed through to
413telnet_send_data().
Sean Middleditch637df7f2009-03-15 12:57:32 -0400414
415Any TELNET commands that are to be sent must be given to one of the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400416following: telnet_send_command, telnet_send_negotiate, or
417telnet_send_subnegotiation().
Sean Middleditch637df7f2009-03-15 12:57:32 -0400418
419If you are attempting to enable COMPRESS2/MCCP2, you must use the
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400420telnet_begin_compress2() function.
Sean Middleditch637df7f2009-03-15 12:57:32 -0400421
Sean Middleditch892c5f12009-03-14 13:39:07 -0400422V. MCCP2 COMPRESSION
Sean Middleditchb9e48642009-03-12 23:33:27 -0400423=====================================================================
424
Sean Middleditch892c5f12009-03-14 13:39:07 -0400425The MCCP2 (COMPRESS2) TELNET extension allows for the compression of
426all traffic sent from server to client. For more information:
427
428 http://www.mudbytes.net/index.php?a=articles&s=mccp
429
Sean Middleditch637df7f2009-03-15 12:57:32 -0400430In order for libtelnet to support MCCP2, zlib must be installed and
431enabled when compiling libtelnet. Use -DHAVE_ZLIB to enable zlib
432when compiling libtelnet.c and pass -lz to the linker to link in the
433zlib shared library.
434
Sean Middleditch892c5f12009-03-14 13:39:07 -0400435libtelnet transparently supports MCCP2. For a server to support
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400436MCCP2, the application must begin negotiation of the COMPRESS2 option
437using telnet_send_negotiate(), for example:
Sean Middleditch892c5f12009-03-14 13:39:07 -0400438
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400439 telnet_send_negotiate(&telnet, TELNET_WILL,
440 TELNET_OPTION_COMPRESS2, user_data);
Sean Middleditch892c5f12009-03-14 13:39:07 -0400441
Sean Middleditch5b5bc922009-03-15 23:02:10 -0400442If a favorable DO COMPRESS2 is sent back from the client then the
443server application can begin compression at any time by calling
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400444telnet_begin_compress2().
Sean Middleditch892c5f12009-03-14 13:39:07 -0400445
Sean Middleditch637df7f2009-03-15 12:57:32 -0400446If a connection is in PROXY mode and COMPRESS2 support is enabled
447then libtelnet will automatically detect the start of a COMPRESS2
448stream, in either the sending or receiving direction.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400449
450VI. TELNET PROXY UTILITY
451=====================================================================
452
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400453The telnet-proxy utility is a small application that serves both as a
454testbed for libtelnet and as a powerful debugging tool for TELNET
Sean Middleditch892c5f12009-03-14 13:39:07 -0400455servers and clients.
456
457To use telnet-proxy, you must first compile it using:
458
459 $ make
460
461If you do not have zlib installed and wish to disable MCCP2 support
462then you must first edit the Makefile and remove the -DHAVE_ZLIB and
463the -lz from the compile flags.
464
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400465To run telnet-proxy, you simply give it the server's host name or IP
466address, the server's port number, and the port number that
Sean Middleditchd88f1832009-03-15 01:06:17 -0400467telnet-proxy should listen on. For example, to connect to the server
468on mud.example.com port 7800 and to listen on port 5000, run:
Sean Middleditch892c5f12009-03-14 13:39:07 -0400469
Sean Middleditchd88f1832009-03-15 01:06:17 -0400470 $ ./telnet-proxy mud.example.com 7800 5000
Sean Middleditch892c5f12009-03-14 13:39:07 -0400471
472You can then connect to the host telnet-proxy is running on (e.g.
Sean Middleditchd88f1832009-03-15 01:06:17 -0400473127.0.0.1) on port 500 and you will automatically be proxied into
474mud.example.com.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400475
Sean Middleditchf65f27d2009-03-19 02:32:04 -0400476telnet-proxy will display status information about the data passing
477through both ends of the tunnel. telnet-proxy can only support a
478single tunnel at a time. It will continue running until an error
479occurs or a terminating signal is sent to the proxy process.