blob: c3bd6dee6ee9a3a21e9539ddc970365738f59fef [file] [log] [blame]
Sean Middleditchb9e48642009-03-12 23:33:27 -04001=====================================================================
2 libtelnet - TELNET protocol handling library
3=====================================================================
4
Sean Middleditch892c5f12009-03-14 13:39:07 -04005 http://github.com/elanthis/libtelnet
6
7 Sean Middleditch
8 sean@sourcemud.org
Sean Middleditchb9e48642009-03-12 23:33:27 -04009
10---------------------------------------------------------------------
11The author or authors of this code dedicate any and all copyright
12interest in this code to the public domain. We make this dedication
13for the benefit of the public at large and to the detriment of our
14heirs and successors. We intend this dedication to be an overt act of
15relinquishment in perpetuity of all present and future rights to this
16code under copyright law.
17---------------------------------------------------------------------
18
Sean Middleditch9d2f98a2009-03-14 05:24:56 -040019*** TODO ***
20
21 - RFC 1143 option negotiation algorithm
22 - automatic MCCP2 handling (controllable by host app)
23 - efficient one-byte sub-requests
24 ? MCCP1
25 ? ZMP parsing
26 ? MSSP parsing
27 ? ENVIRON/NEW-ENVIRON parsing
28 ? telnet-status testing tool
29 ? few options to make telnet-proxy even more useful
30
Sean Middleditchb9e48642009-03-12 23:33:27 -040031I. INTRODUCTION
32=====================================================================
33
34libtelnet provides safe and correct handling of the core TELNET
35protocol. It does not include any "smarts," and all use of the
36protocol (such as deciding which options to support, enabling
37and disabling options, or processing subrequests) must be implemented
38by the application author.
39
Sean Middleditch892c5f12009-03-14 13:39:07 -040040For more information on the TELNET protocol, see:
41
42 http://www.faqs.org/rfcs/rfc854.html
43
Sean Middleditchb9e48642009-03-12 23:33:27 -040044II. LIBTELNET API
45=====================================================================
46
Sean Middleditch892c5f12009-03-14 13:39:07 -040047The libtelnet API contains several distinct parts. The first part is
48the basic initialization and deinitialization routines. The second
49part is a single function for pushing received data into the
50libtelnet processor. The third part is the libtelnet_send_*()
51functions, which generate TELNET commands and ensure data is properly
52formatted before sending over the wire. The final part is the
Sean Middleditch30323022009-03-14 21:45:28 -040053callback structure libtelnet_cb_t.
Sean Middleditch892c5f12009-03-14 13:39:07 -040054
55IIa. Initialization
56
57 struct libtelnet_t;
58 This structure represents the state of the TELNET protocol for a
59 single connection. Each connection utilizing TELNET must have
60 its own libtelnet_t structure, which is passed to all libtelnet
61 API calls.
62
Sean Middleditch30323022009-03-14 21:45:28 -040063 struct libtelnet_cb_t;
64 An instance of this structure must be initialized and have all
65 mandatory and desired optional callbacks set. See section IId
66 for more information.
67
Sean Middleditch892c5f12009-03-14 13:39:07 -040068 void libtelnet_init(struct libtelnet_t *telnet,
Sean Middleditch30323022009-03-14 21:45:28 -040069 struct libtelnet_cb_t *cb, enum libtelnet_mode_t mode);
Sean Middleditch892c5f12009-03-14 13:39:07 -040070 The libtelnet_init() function is responsible for initializing
71 the data in a libtelnet_t structure. It must be called
72 immediately after establishing a connection and before any other
73 libtelnet API calls are made.
74
Sean Middleditch30323022009-03-14 21:45:28 -040075 The cb parameter must be a pointer to a fully initialized
76 instance of libtelnet_cb_t. A single instance of the structure
77 can be shared between any number of libtelnet_t instances.
78
Sean Middleditch892c5f12009-03-14 13:39:07 -040079 The mode parameter must be one of LIBTELNET_MODE_SERVER or
80 LIBTELNET_MODE_CLIENT. These slightly alter the behavior of
81 libtelnet in certain instances. If you are implementing a
82 TELNET server, use the SERVER mode. If you are implementing a
83 client, use the CLIENT mode.
84
85 boid libtelnet_free(struct libtelnet_t *telnet);
86 Releases any internal memory allocated by libtelnet. This must
87 be called whenever a connection is closed, or you will incur
88 memory leaks.
89
90IIb. Receiving Data
91
92 void libtelnet_push(struct libtelnet_t *telnet,
93 unsigned char *buffer, unsigned int size, void *user_data);
94 When your application receives data over the socket from the
95 remote end, it must pass the received bytes into this function.
96 Callback functions will be invoked as the buffer is processed,
97 and the user_data parameter will be passed to each callback.
98
99IIc. Sending Data
100
101 Note that all of the libtelnet_send_*() functions will invoke
Sean Middleditch30323022009-03-14 21:45:28 -0400102 the send callback function attached to the libtelnet_t instance.
103 The user_data parameter to each of these functions is passed
104 through to the callback.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400105
106 void libtelnet_send_command(struct libtelnet_t *telnet,
107 unsigned char cmd, void *user_data);
108 Sends a single "simple" TELNET command, such as the GO-AHEAD
109 commands (255 249).
110
111 void libtelnet_send_negotiate(struct libtelnet_t *telnet,
112 unsigned char cmd, unsigned char opt, void *user_data);
113 Sends a TELNET negotiation command. The cmd parameter must be
114 one of LIBTELNET_WILL, LIBTELNET_DONT, LIBTELNET_DO, or
115 LIBTELNET_DONT. The opt parameter is the option to
116 negotiate.
117
118 void libtelnet_send_data(struct libtelnet_t *telnet,
119 unsigned char *buffer, unsigned int size, void *user_data);
120 Sends raw data, which would be either the process output from
121 a server or the user input from a client.
122
123 void libtelnet_send_subnegotiation(struct libtelnet_t *telnet,
124 unsigned char opt, unsigned char *buffer, unsigned int size,
125 void *user_data);
126 Sends a TELNET sub-negotiation command. The opt parameter
127 is the sub-negotiation option.
128
129IId. Callbacks
130
Sean Middleditch30323022009-03-14 21:45:28 -0400131 The libtelnet_cb_t structure containers a number of callback
132 entry points. Of these, only the send and data callbacks are
133 absolutely required. All others are optional. The declarations
134 below show the signature of the callback functions.
Sean Middleditch892c5f12009-03-14 13:39:07 -0400135
Sean Middleditch30323022009-03-14 21:45:28 -0400136 An example of initializing a libtelnet_cb_t structure:
137
138 /* illustrative data callback */
139 void my_data_cb(libtelnet_t *telnet, unsigned char *buffer,
140 unsigned int size, void *user_data) {
141 /* print number of bytes received and then show the
142 * whole buffer */
143 printf("RECV(%d): %.*s\n", size, size, buffer);
144 }
145
146 /* illustrative variable definitions */
147 libtelnet_t conn;
148 libtelnet_cb_t callbacks;
149
150 /* clear all callbacks and set just the ones we want */
151 memset(&callbacks, 0, sizeof(callbacks));
152 callbacks->send = my_send_cb;
153 callbacks->data = my_data_cb;
154
155 /* initialize the connection with our callbacks */
156 libtelnet_init(&conn, &callbacks, LIBTELNET_MODE_SERVER);
157
158 Remember that a single libtelnet_cb_t structure can be shared
159 between any number of libtelnet_t instances. There is no reason
160 to make multiple copies of the data if all of your connections
161 use the same callback functions.
162
163 void libtelnet_cb_t->data(struct libtelnet_t *telnet,
Sean Middleditch892c5f12009-03-14 13:39:07 -0400164 unsigned char *buffer, unsigned int size, void *user_data);
165 Regular data has been received by the remote end. For a server,
166 this would be input typed by the client; for a client, this is
167 process output generated by the server.
168
169 Note that data is not line-buffered by libtelnet. A single
170 line of input may be broken into pieces and given to
171 consecutive calls to libtelnet_data_cb(). If you are doing
172 line-based processing of data, it is your responsibility to
173 buffer data and find the line breaks.
174
Sean Middleditch30323022009-03-14 21:45:28 -0400175 void libtelnet_cb_t->send(struct libtelnet_t *telnet,
Sean Middleditch892c5f12009-03-14 13:39:07 -0400176 unsigned char *buffer, unsigned int size, void *user_data);
177 This is called whenever libtelnet has generated output to be
178 send to the remote end of the connection. In most cases this
179 will be a simple wrapper arround your applications network
180 output buffering/transmission code.
181
182 You can pass socket information through the user_data
183 parameter to libtelnet calls so that it is available in this
184 callback.
185
Sean Middleditch30323022009-03-14 21:45:28 -0400186 void libtelnet_cb_t->command(struct libtelnet_t *telnet,
Sean Middleditch892c5f12009-03-14 13:39:07 -0400187 unsigned char cmd, void *user_data);
188 Called whenever a "simpler" TELNET command has arrived, such
189 as GO-AHEAD commands (255 249). The necessary processing
190 depends on the specific commands; see the TELNET RFC for
191 more information.
192
Sean Middleditch30323022009-03-14 21:45:28 -0400193 void libtelnet_cb_t->negotiate(struct libtelnet_t *telnet,
Sean Middleditch892c5f12009-03-14 13:39:07 -0400194 unsigned char cmd, unsigned char opt, void *user_data);
195 This function is called whenever a TELNET negotiation command
196 has been received. The cmd parameter will be one of
197 LIBTELNET_WILL, LIBTELNET_WONT, LIBTELNET_DO, or LIBTELNET_DONT.
198 The opt parameter is the option being negotiated.
199
200 libtelnet does not currently manage negotiation for you. For
201 best practice in implementing TELNET negotiation, see:
202
203 http://www.faqs.org/rfcs/rfc1143.html
204
Sean Middleditch30323022009-03-14 21:45:28 -0400205 void libtelnet_cb_t->subnegotiation(struct libtelnet_t *telnet,
Sean Middleditch892c5f12009-03-14 13:39:07 -0400206 unsigned char opt, unsigned char *data, unsigned int size,
207 void *user_data);
208 Called whenever a TELNET sub-negotiation has been received.
209 Sub-negotiations include the NAWS option for communicating
210 terminal size to a server, the NEW-ENVIRON and TTYPE options
211 for negotiating terminal features, and MUD-centric protocols
212 such as ZMP, MSSP, and MCCP2.
213
214 The opt parameter is the option under sub-negotiation. The
215 remaining data (if any) is passed in the buffer.
216
Sean Middleditch30323022009-03-14 21:45:28 -0400217 void libtelnet_cb_t->compress(struct libtelnet_t *telnet,
Sean Middleditch892c5f12009-03-14 13:39:07 -0400218 char enabled, void *user_data);
219 The callback is invoked whenever the COMPRESS2 (MCCP2)
220 feature is enabled or disabled. For servers, this is called
221 immediately after beginning compression after a client accepts
222 the COMPRESS2 option. For clients, this is called immediately
223 after a compress stream begin or ends.
224
225 The enabled parameter is 1 if compression has begun and 0 if
226 compression has ended.
Sean Middleditchb9e48642009-03-12 23:33:27 -0400227
228III. INTEGRATING LIBTELNET
229=====================================================================
230
231FIXME: fill in notes about implementing the libtelnet_*_cb functions
232
Sean Middleditch892c5f12009-03-14 13:39:07 -0400233IV. SAFETY AND CORRECTNESS CONSIDERATIONS
Sean Middleditchb9e48642009-03-12 23:33:27 -0400234=====================================================================
235
Sean Middleditch892c5f12009-03-14 13:39:07 -0400236Your existing application may make heavy use of its own output
237buffering and transmission commands, including hand-made routines
238for sending TELNET commands and sub-negotiation requests. There are
239at times subtle issues that need to be handled when communication
240over the TELNET protocol, not least of which is the need to escape
241any byte value 0xFF with a special TELNET command.
Sean Middleditchb9e48642009-03-12 23:33:27 -0400242
Sean Middleditch892c5f12009-03-14 13:39:07 -0400243For these reasons, it is very important that applications making use
244of libtelnet always make use of the libtelnet_send_*() family of
245functions for all data being sent over the TELNET connection.
246
247V. MCCP2 COMPRESSION
Sean Middleditchb9e48642009-03-12 23:33:27 -0400248=====================================================================
249
Sean Middleditch892c5f12009-03-14 13:39:07 -0400250The MCCP2 (COMPRESS2) TELNET extension allows for the compression of
251all traffic sent from server to client. For more information:
252
253 http://www.mudbytes.net/index.php?a=articles&s=mccp
254
255libtelnet transparently supports MCCP2. For a server to support
256MCCP2, the application must begin negotiation of the COMPRESS2
257option using libtelnet_send_negotiate(), for example:
258
259 libtelnet_send_negotiate(&telnet, LIBTELNET_WILL,
260 LIBTELNET_OPTION_COMPRESS2, user_data);
261
262libtelnet will automatically detect if the client responds favoribly
263and will begin compressing data. For clients, no action must be
264taken, as libtelnet will automatically handle the requests.
265
266NOTE: libtelnet will still invoke the callback functions for
267negotiation and sub-negotiation commands relating to MCCP2. Do not
268respond to these.
269
270In order for libtelnet to support MCCP2, zlib must be installed and
271enabled when compiling libtelnet. Use -DHAVE_ZLIB to enable zlib
272when compiling libtelnet.c and pass -lz to the linker to link in the
273zlib shared library.
274
275VI. TELNET PROXY UTILITY
276=====================================================================
277
278The telnet-proxy utility is a small application that serves both as
279a testbed for libtelnet and as a powerful debugging tool for TELNET
280servers and clients.
281
282To use telnet-proxy, you must first compile it using:
283
284 $ make
285
286If you do not have zlib installed and wish to disable MCCP2 support
287then you must first edit the Makefile and remove the -DHAVE_ZLIB and
288the -lz from the compile flags.
289
290To run telnet-proxy, you simply give it the server's IP address
291(telnet-proxy does not support hostname resolution, nor IPv6), the
292server's port number, and the port number that telnet-proxy should
293listen on. For example, to connect to the server on 209.85.171.100
294port 7800 and to listen on port 5000, run:
295
296 $ ./telnet-proxy 209.85.171.100 7800 5000
297
298You can then connect to the host telnet-proxy is running on (e.g.
299127.0.0.1, or the machine's public IP) on port 500 and you will
300automatically be proxied into 209.85.171.100.
301
302telnet-proxy will display status information about the data
303passing through both ends of the tunnel. Once either end
304disconnects, telnet-proxy will close. telnet-proxy can only
305support a single tunnel at a time and must be restarted for each
306connection.