Sean Middleditch | b9e4864 | 2009-03-12 23:33:27 -0400 | [diff] [blame] | 1 | ===================================================================== |
| 2 | libtelnet - TELNET protocol handling library |
| 3 | ===================================================================== |
| 4 | |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 5 | http://github.com/elanthis/libtelnet |
| 6 | |
| 7 | Sean Middleditch |
| 8 | sean@sourcemud.org |
Sean Middleditch | b9e4864 | 2009-03-12 23:33:27 -0400 | [diff] [blame] | 9 | |
| 10 | --------------------------------------------------------------------- |
| 11 | The author or authors of this code dedicate any and all copyright |
| 12 | interest in this code to the public domain. We make this dedication |
| 13 | for the benefit of the public at large and to the detriment of our |
| 14 | heirs and successors. We intend this dedication to be an overt act of |
| 15 | relinquishment in perpetuity of all present and future rights to this |
| 16 | code under copyright law. |
| 17 | --------------------------------------------------------------------- |
| 18 | |
Sean Middleditch | 9d2f98a | 2009-03-14 05:24:56 -0400 | [diff] [blame] | 19 | *** TODO *** |
| 20 | |
Sean Middleditch | 9d2f98a | 2009-03-14 05:24:56 -0400 | [diff] [blame] | 21 | - automatic MCCP2 handling (controllable by host app) |
Sean Middleditch | 9d2f98a | 2009-03-14 05:24:56 -0400 | [diff] [blame] | 22 | ? ZMP parsing |
| 23 | ? MSSP parsing |
| 24 | ? ENVIRON/NEW-ENVIRON parsing |
| 25 | ? telnet-status testing tool |
Sean Middleditch | 9d2f98a | 2009-03-14 05:24:56 -0400 | [diff] [blame] | 26 | |
Sean Middleditch | b9e4864 | 2009-03-12 23:33:27 -0400 | [diff] [blame] | 27 | I. INTRODUCTION |
| 28 | ===================================================================== |
| 29 | |
| 30 | libtelnet provides safe and correct handling of the core TELNET |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 31 | protocol. In addition to the base TELNET protocol, libtelnet also |
| 32 | implements the Q method of TELNET option negotiation. libtelnet |
| 33 | can be used for writing servers, clients, or proxies. |
Sean Middleditch | b9e4864 | 2009-03-12 23:33:27 -0400 | [diff] [blame] | 34 | |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 35 | For more information on the TELNET protocol, see: |
| 36 | |
| 37 | http://www.faqs.org/rfcs/rfc854.html |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 38 | http://www.faqs.org/rfcs/rfc1143.html |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 39 | |
Sean Middleditch | b9e4864 | 2009-03-12 23:33:27 -0400 | [diff] [blame] | 40 | II. LIBTELNET API |
| 41 | ===================================================================== |
| 42 | |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 43 | The libtelnet API contains several distinct parts. The first part is |
| 44 | the basic initialization and deinitialization routines. The second |
| 45 | part is a single function for pushing received data into the |
| 46 | libtelnet processor. The third part is the libtelnet_send_*() |
| 47 | functions, which generate TELNET commands and ensure data is properly |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 48 | formatted before sending over the wire. The final part is the event |
| 49 | handler interface. |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 50 | |
| 51 | IIa. Initialization |
| 52 | |
| 53 | struct libtelnet_t; |
| 54 | This structure represents the state of the TELNET protocol for a |
| 55 | single connection. Each connection utilizing TELNET must have |
| 56 | its own libtelnet_t structure, which is passed to all libtelnet |
| 57 | API calls. |
| 58 | |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 59 | void libtelnet_init(libtelnet_t *telnet, libtelnet_event_handler_t handler, |
Sean Middleditch | 08bb05f | 2009-03-15 23:29:46 -0400 | [diff] [blame] | 60 | unsigned char flags, void *user_data); |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 61 | The libtelnet_init() function is responsible for initializing |
| 62 | the data in a libtelnet_t structure. It must be called |
| 63 | immediately after establishing a connection and before any other |
| 64 | libtelnet API calls are made. |
| 65 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 66 | The handler parameter must be a function matching the |
| 67 | libtelnet_event_handler_t definition. More information about |
| 68 | events can be found in section IId. |
Sean Middleditch | 3032302 | 2009-03-14 21:45:28 -0400 | [diff] [blame] | 69 | |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 70 | 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 |
| 73 | for implementing LIBTELNET_EV_SEND event handling. |
| 74 | |
Sean Middleditch | 08bb05f | 2009-03-15 23:29:46 -0400 | [diff] [blame] | 75 | 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 Middleditch | c337ba6 | 2009-03-16 16:47:27 -0400 | [diff] [blame] | 78 | LIBTELNET_FLAG_PROXY |
| 79 | Operate in proxy mode. This disables the RFC1143 support and |
| 80 | enables automatic detection of COMPRESS2 streams. |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 81 | |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 82 | boid libtelnet_free(libtelnet_t *telnet); |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 83 | Releases any internal memory allocated by libtelnet. This must |
| 84 | be called whenever a connection is closed, or you will incur |
| 85 | memory leaks. |
| 86 | |
| 87 | IIb. Receiving Data |
| 88 | |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 89 | void libtelnet_push(libtelnet_t *telnet, |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 90 | const char *buffer, unsigned int size, void *user_data); |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 91 | When your application receives data over the socket from the |
| 92 | remote end, it must pass the received bytes into this function. |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 93 | |
| 94 | As the TELNET stream is parsed, events will be generated and |
| 95 | passed to the event handler given to libtelnet_init(). Of |
| 96 | particular interest for data receiving is the LIBTELNET_EV_DATA |
| 97 | event, which is triggered for any regular data such as user |
| 98 | input or server process output. |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 99 | |
| 100 | IIc. Sending Data |
| 101 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 102 | All of the libtelnet_send_*() functions will invoke the |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 103 | LIBTELNET_EV_SEND event. |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 104 | |
| 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 Middleditch | 4613e68 | 2009-03-16 17:21:48 -0400 | [diff] [blame] | 108 | to one of the following functions. Do NOT send or buffer |
| 109 | unprocessed output data directly! |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 110 | |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 111 | void libtelnet_send_command(libtelnet_t *telnet, unsigned char cmd); |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 112 | Sends a single "simple" TELNET command, such as the GO-AHEAD |
| 113 | commands (255 249). |
| 114 | |
Sean Middleditch | 2b4bfc4 | 2009-03-16 01:25:52 -0400 | [diff] [blame] | 115 | void libtelnet_send_command(libtelnet_t *telnet, unsigned char cmd, |
| 116 | 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 Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 120 | void libtelnet_send_negotiate(libtelnet_t *telnet, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 121 | unsigned char cmd, unsigned char opt); |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 122 | Sends a TELNET negotiation command. The cmd parameter must be |
| 123 | one of LIBTELNET_WILL, LIBTELNET_DONT, LIBTELNET_DO, or |
| 124 | LIBTELNET_DONT. The opt parameter is the option to |
| 125 | negotiate. |
| 126 | |
Sean Middleditch | 97a8cb2 | 2009-03-16 16:51:41 -0400 | [diff] [blame] | 127 | void libtelnet_send_data(libtelnet_t *telnet, |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 128 | const char *buffer, unsigned int size); |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 129 | Sends raw data, which would be either the process output from |
| 130 | a server or the user input from a client. |
| 131 | |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 132 | void libtelnet_send_subnegotiation(libtelnet_t *telnet, |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 133 | unsigned char telopt, const char *buffer, |
Sean Middleditch | 97a8cb2 | 2009-03-16 16:51:41 -0400 | [diff] [blame] | 134 | unsigned int size); |
Sean Middleditch | 2b4bfc4 | 2009-03-16 01:25:52 -0400 | [diff] [blame] | 135 | Sends a TELNET sub-negotiation command. The telopt parameter |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 136 | is the sub-negotiation option. |
| 137 | |
Sean Middleditch | 2b4bfc4 | 2009-03-16 01:25:52 -0400 | [diff] [blame] | 138 | Note that the above function is just a shorthand for: |
| 139 | libtelnet_send_telopt(telnet, LIBTELNET_SB, telopt); |
| 140 | libtelnet_send_data(telnet, buffer, size); |
| 141 | libtelnet_send_command(telnet, LIBTELNET_SE); |
| 142 | |
| 143 | For some subnegotiations that involve a lot of complex formatted |
| 144 | data to be sent, it may be easier to manually send the SB telopt |
| 145 | header and SE footer around mulitple calls to send_data. |
| 146 | |
Sean Middleditch | 4613e68 | 2009-03-16 17:21:48 -0400 | [diff] [blame] | 147 | NOTE: libtelnet_send_subnegotiation() does have special behavior |
| 148 | in PROXY mode, as in that mode this function will automatically |
Sean Middleditch | 2b4bfc4 | 2009-03-16 01:25:52 -0400 | [diff] [blame] | 149 | detect the COMPRESS2 marker and enable zlib compression. |
| 150 | |
Sean Middleditch | 4613e68 | 2009-03-16 17:21:48 -0400 | [diff] [blame] | 151 | int libtelnet_printf(libtelnet_t *telnet, const char *fmt, ...); |
| 152 | This functions very similarly to fprintf, except that output |
| 153 | is sent through libtelnet for processing. This is equivalent |
| 154 | to using snprintf() to format data into a buffer and then |
| 155 | sending the buffer to libtelnet_send_data(). The return code |
| 156 | is the length of the formatted text. |
| 157 | |
| 158 | NOTE: due to an internal implementation detail, the maximum |
| 159 | lenth of the formatted text is 4096 characters. |
| 160 | |
| 161 | int libtelnet_printf2(libtelnet_T *telnet, const char *fmt, ...); |
| 162 | Identical to libtelnet_print(), except that this variant will |
| 163 | also translate C newlines (\n) into a CRLF and translates |
| 164 | carriage returns (\r) into CRNUL, as required by TELNET. |
| 165 | |
| 166 | NOTE: this function should only be used for regular data such |
| 167 | as user input (in client applications) or process output (in |
| 168 | server applications). If you are formatting data that is part |
| 169 | of a subnegotiation, you should always use libtelnet_printf() |
| 170 | instead, as you will rarely want newline translation inside of |
| 171 | subnegotiations. |
| 172 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 173 | IId. Event Handling |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 174 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 175 | libtelnet relies on an event-handling mechanism for processing |
| 176 | the parsed TELNET protocol stream as well as for buffering and |
| 177 | sending output data. |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 178 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 179 | When you initialize a libtelnet_t structure with libtelnet_init() |
| 180 | you had to pass in an event handler function. This function must |
| 181 | meet the following prototype: |
Sean Middleditch | 3032302 | 2009-03-14 21:45:28 -0400 | [diff] [blame] | 182 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 183 | void (libtelnet_t *telnet, libtelnet_event_t *event, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 184 | void *user_data); |
| 185 | |
| 186 | The event structure is detailed below. The user_data value is the |
| 187 | pointer passed to libtelnet_init(). |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 188 | |
| 189 | struct libtelnet_event_t { |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 190 | const char *buffer; |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 191 | unsigned int size; |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 192 | libtelnet_event_type_t type; |
| 193 | unsigned char command; |
| 194 | unsigned char telopt; |
| 195 | unsigned char accept; |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | The enumeration values of libtelnet_event_type_t are described in |
| 199 | detail below. Whenever the the event handler is invoked, the |
| 200 | application must look at the event->type value and do any |
| 201 | necessary processing. |
| 202 | |
| 203 | The only event that MUST be implemented is LIBTELNET_EV_SEND. |
| 204 | Most applications will also always want to implement the event |
| 205 | LIBTELNET_EV_DATA. |
| 206 | |
| 207 | Here is an example event handler implementation which includes |
| 208 | handlers for several important events. |
| 209 | |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 210 | void my_event_handler(libtelnet_t *telnet, libtelnet_event_t *ev, |
| 211 | void *user_data) { |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 212 | struct user_info *user = (struct user_info *)user_data; |
| 213 | |
| 214 | switch (ev->type) { |
| 215 | case LIBTELNET_EV_DATA: |
| 216 | process_user_input(user, event->buffer, event->size); |
| 217 | break; |
| 218 | case LIBTELNET_EV_SEND: |
| 219 | write_to_descriptor(user, event->buffer, event->size); |
| 220 | break; |
| 221 | case LIBTELNET_EV_ERROR: |
| 222 | fatal_error("TELNET error: %s", event->buffer); |
| 223 | break; |
| 224 | } |
Sean Middleditch | 3032302 | 2009-03-14 21:45:28 -0400 | [diff] [blame] | 225 | } |
| 226 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 227 | LIBTELNET_EV_DATA: |
| 228 | The DATA event is triggered whenever regular data (not part of |
| 229 | any special TELNET command) is received. For a client, this |
| 230 | will be process output from the server. For a server, this will |
| 231 | be input typed by the user. |
Sean Middleditch | 3032302 | 2009-03-14 21:45:28 -0400 | [diff] [blame] | 232 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 233 | The event->buffer value will contain the bytes received and the |
| 234 | event->size value will contain the number of bytes received. |
| 235 | Note that event->buffer is not NUL terminated! |
Sean Middleditch | 3032302 | 2009-03-14 21:45:28 -0400 | [diff] [blame] | 236 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 237 | NOTE: there is no guarantee that user input or server output |
| 238 | will be received in whole lines. If you wish to process data |
| 239 | a line at a time, you are responsible for buffering the data and |
| 240 | checking for line terminators yourself! |
| 241 | |
| 242 | LIBTELNET_EV_SEND: |
| 243 | This event is sent whenever libtelnet has generated data that |
| 244 | must be sent over the wire to the remove end. Generally that |
| 245 | means calling send() or adding the data to your application's |
| 246 | output buffer. |
Sean Middleditch | 3032302 | 2009-03-14 21:45:28 -0400 | [diff] [blame] | 247 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 248 | The event->buffer value will contain the bytes to send and the |
| 249 | event->size value will contain the number of bytes to send. |
| 250 | Note that event->buffer is not NUL terminated, and may include |
| 251 | NUL characters in its data, so always use event->size! |
Sean Middleditch | 3032302 | 2009-03-14 21:45:28 -0400 | [diff] [blame] | 252 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 253 | NOTE: Your SEND event handler must send or buffer the data in |
| 254 | its raw form as provided by libtelnet. If you wish to perform |
| 255 | any kind of preprocessing on data you want to send to the other |
| 256 | |
| 257 | LIBTELNET_EV_IAC: |
| 258 | The IAC event is triggered whenever a simple IAC command is |
| 259 | received, such as the IAC EOR (end of record, also called |
| 260 | go ahead or GA) command. |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 261 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 262 | The command received is in the event->command value. |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 263 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 264 | The necessary processing depends on the specific commands; see |
| 265 | the TELNET RFC for more information. |
| 266 | |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 267 | LIBTELNET_EV_WILL: |
| 268 | LIBTELNET_EV_DO: |
| 269 | The WILL and DO events are sent when a TELNET negotiation |
| 270 | command of the same name is received. |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 271 | |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 272 | WILL events are sent by the remote end when they wish to be |
| 273 | allowed to turn an option on on their end, or in confirmation |
| 274 | after you have sent a DO command to them. |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 275 | |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 276 | DO events are sent by the remote end when they wish for you |
| 277 | to turn on an option on your end, or in confirmation after you |
| 278 | have sent a WILL command to them. |
| 279 | |
| 280 | In either case, the TELNET option under negotiation will be in |
| 281 | event->telopt field. |
| 282 | |
| 283 | If you support the option and wish for it to be enabled you |
| 284 | must set the event->accept field to 1, unless this event is |
| 285 | a confirmation for a previous WILL/DO command you sent to the |
| 286 | remote end. If you do not set event->field to 1 then |
| 287 | libtelnet will send a rejection command back to the other end. |
| 288 | |
| 289 | libtelnet manages some of the pecularities of negotiation for |
| 290 | you. For information on libtelnet's negotiation method, see: |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 291 | |
| 292 | http://www.faqs.org/rfcs/rfc1143.html |
| 293 | |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 294 | Examples: |
| 295 | |
| 296 | You want remote end to use TTYPE, so you send DO TTYPE. |
| 297 | Remote accepts and sends WILL TTYPE. |
| 298 | |
| 299 | Remote end wants you to use SGA, so they send DO_SGA. |
| 300 | You do not support SGA and set event->accept = 0. |
| 301 | |
| 302 | Remote end wants to use ZMP, so they send WILL ZMP. |
| 303 | You support ZMP, so you set event->accept = 1 and enable |
| 304 | local ZMP support. |
| 305 | |
| 306 | You want to use MCCP2, so you send WILL COMPRESS2. |
| 307 | Remote end accepts and sends DO COMPRESS2. |
| 308 | |
| 309 | Note that in PROXY mode libtelnet will do no processing of its |
| 310 | own for you. |
| 311 | |
| 312 | LIBTELNET_EV_WONT: |
| 313 | LIBTELNET_EV_DONT: |
| 314 | The WONT and DONT events are sent when the remote end of the |
| 315 | connection wishes to disable an option, when they are |
| 316 | refusing to a support an option that you have asked for, or |
| 317 | in confirmation of an option you have asked to be disabled. |
| 318 | |
| 319 | Most commonly WONT and DONT events are sent as rejections of |
| 320 | features you requested by sending DO or WILL events. Receiving |
| 321 | these events means the TELNET option is not or will not be |
| 322 | supported by the remote end, so give up. |
| 323 | |
| 324 | Sometimes WONT or DONT will be sent for TELNET options that are |
| 325 | already enabled, but the remote end wishes to stop using. You |
| 326 | cannot decline. These events are demands that must be complied |
| 327 | with. libtelnet will always send the appropriate response back |
| 328 | without consulting your application. These events are sent to |
| 329 | allow your application to disable its own use of the features. |
| 330 | |
| 331 | In either case, the TELNET option under negotiation will be in |
| 332 | event->telopt field. |
| 333 | |
| 334 | Note that in PROXY mode libtelnet will do no processing of its |
| 335 | own for you. |
| 336 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 337 | LIBTELNET_EV_SUBNEGOTIATION: |
| 338 | Triggered whenever a TELNET sub-negotiation has been received. |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 339 | Sub-negotiations include the NAWS option for communicating |
| 340 | terminal size to a server, the NEW-ENVIRON and TTYPE options |
| 341 | for negotiating terminal features, and MUD-centric protocols |
| 342 | such as ZMP, MSSP, and MCCP2. |
| 343 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 344 | The event->telopt value is the option under sub-negotiation. |
| 345 | The remaining data (if any) is passed in event->buffer and |
| 346 | event->size. Note that most subnegotiation commands can |
| 347 | include embedded NUL bytes in the subnegotiation data, and |
| 348 | the data event->buffer is not NUL terminated, so always use |
| 349 | the event->size value! |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 350 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 351 | The meaning and necessary processing for subnegotiations are |
| 352 | defined in various TELNET RFCs and other informal |
| 353 | specifications. A subnegotiation should never be sent unless |
| 354 | the specific option has been enabled through the use of the |
| 355 | telnet negotiation feature. |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 356 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 357 | LIBTELNET_EV_COMPRESS |
| 358 | The COMPRESS event notifies the app that COMPRESS2/MCCP2 |
| 359 | compression has begun or ended. Only servers can send compressed |
| 360 | data, and hence only clients will receive compressed data. |
Sean Middleditch | b9e4864 | 2009-03-12 23:33:27 -0400 | [diff] [blame] | 361 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 362 | The event->command value will be 1 if compression has started and |
| 363 | will be 0 if compression has ended. |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 364 | |
| 365 | LIBTELNET_EV_WARNING |
| 366 | The WARNING event is sent whenever something has gone wrong |
| 367 | inside of libtelnet (possibly due to malformed data sent by the |
| 368 | other end) but which recovery is (likely) possible. It may be |
| 369 | safe to continue using the connection, but some data may have |
| 370 | been lost or incorrectly interpreted. |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 371 | |
| 372 | The event->buffer value will contain a NUL terminated string |
| 373 | explaining the error, and the event->size value containers the |
| 374 | length of the string. |
| 375 | |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 376 | LIBTELNET_EV_ERROR |
| 377 | Similar to the WARNING event, the ERROR event is sent whenever |
| 378 | something has gone wrong. ERROR events are non-recoverable, |
| 379 | however, and the application should immediately close the |
| 380 | connection. Whatever has happened is likely going only to |
| 381 | result in garbage from libtelnet. This is most likely to |
| 382 | happen when a COMPRESS2 stream fails, but other problems can |
| 383 | occur. |
| 384 | |
| 385 | The event->buffer value will contain a NUL terminated string |
| 386 | explaining the error, and the event->size value containers the |
| 387 | length of the string. |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 388 | |
| 389 | III. INTEGRATING LIBTELNET WITH COMMON MUDS |
Sean Middleditch | b9e4864 | 2009-03-12 23:33:27 -0400 | [diff] [blame] | 390 | ===================================================================== |
| 391 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 392 | FIXME: fill in some notes about how to splice in libtelnet with |
| 393 | common Diku/Merc/Circle/etc. MUD codebases. |
Sean Middleditch | b9e4864 | 2009-03-12 23:33:27 -0400 | [diff] [blame] | 394 | |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 395 | IV. SAFETY AND CORRECTNESS CONSIDERATIONS |
Sean Middleditch | b9e4864 | 2009-03-12 23:33:27 -0400 | [diff] [blame] | 396 | ===================================================================== |
| 397 | |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 398 | Your existing application may make heavy use of its own output |
| 399 | buffering and transmission commands, including hand-made routines |
| 400 | for sending TELNET commands and sub-negotiation requests. There are |
| 401 | at times subtle issues that need to be handled when communication |
| 402 | over the TELNET protocol, not least of which is the need to escape |
| 403 | any byte value 0xFF with a special TELNET command. |
Sean Middleditch | b9e4864 | 2009-03-12 23:33:27 -0400 | [diff] [blame] | 404 | |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 405 | For these reasons, it is very important that applications making use |
| 406 | of libtelnet always make use of the libtelnet_send_*() family of |
| 407 | functions for all data being sent over the TELNET connection. |
| 408 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 409 | In particular, if you are writing a client, all user input must be |
| 410 | passed through to libtelnet_send_data(). This also includes any |
| 411 | input generated automatically by scripts, triggers, or macros. |
| 412 | |
| 413 | For a server, any and all output -- including ANSI/VT100 escape |
| 414 | codes, regular text, newlines, and so on -- must be passed through |
| 415 | to libtelnet_send_data(). |
| 416 | |
| 417 | Any TELNET commands that are to be sent must be given to one of the |
| 418 | following: libtelnet_send_command, libtelnet_send_negotiate, or |
| 419 | libtelnet_send_subnegotiation(). |
| 420 | |
| 421 | If you are attempting to enable COMPRESS2/MCCP2, you must use the |
| 422 | libtelnet_begin_compress2() function. |
| 423 | |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 424 | V. MCCP2 COMPRESSION |
Sean Middleditch | b9e4864 | 2009-03-12 23:33:27 -0400 | [diff] [blame] | 425 | ===================================================================== |
| 426 | |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 427 | The MCCP2 (COMPRESS2) TELNET extension allows for the compression of |
| 428 | all traffic sent from server to client. For more information: |
| 429 | |
| 430 | http://www.mudbytes.net/index.php?a=articles&s=mccp |
| 431 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 432 | In order for libtelnet to support MCCP2, zlib must be installed and |
| 433 | enabled when compiling libtelnet. Use -DHAVE_ZLIB to enable zlib |
| 434 | when compiling libtelnet.c and pass -lz to the linker to link in the |
| 435 | zlib shared library. |
| 436 | |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 437 | libtelnet transparently supports MCCP2. For a server to support |
| 438 | MCCP2, the application must begin negotiation of the COMPRESS2 |
| 439 | option using libtelnet_send_negotiate(), for example: |
| 440 | |
| 441 | libtelnet_send_negotiate(&telnet, LIBTELNET_WILL, |
| 442 | LIBTELNET_OPTION_COMPRESS2, user_data); |
| 443 | |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 444 | If a favorable DO COMPRESS2 is sent back from the client then the |
| 445 | server application can begin compression at any time by calling |
| 446 | libtelnet_begin_compress2(). |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 447 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 448 | If a connection is in PROXY mode and COMPRESS2 support is enabled |
| 449 | then libtelnet will automatically detect the start of a COMPRESS2 |
| 450 | stream, in either the sending or receiving direction. |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 451 | |
| 452 | VI. TELNET PROXY UTILITY |
| 453 | ===================================================================== |
| 454 | |
| 455 | The telnet-proxy utility is a small application that serves both as |
| 456 | a testbed for libtelnet and as a powerful debugging tool for TELNET |
| 457 | servers and clients. |
| 458 | |
| 459 | To use telnet-proxy, you must first compile it using: |
| 460 | |
| 461 | $ make |
| 462 | |
| 463 | If you do not have zlib installed and wish to disable MCCP2 support |
| 464 | then you must first edit the Makefile and remove the -DHAVE_ZLIB and |
| 465 | the -lz from the compile flags. |
| 466 | |
Sean Middleditch | d88f183 | 2009-03-15 01:06:17 -0400 | [diff] [blame] | 467 | To run telnet-proxy, you simply give it the server's host name or |
| 468 | IP address, the server's port number, and the port number that |
| 469 | telnet-proxy should listen on. For example, to connect to the server |
| 470 | on mud.example.com port 7800 and to listen on port 5000, run: |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 471 | |
Sean Middleditch | d88f183 | 2009-03-15 01:06:17 -0400 | [diff] [blame] | 472 | $ ./telnet-proxy mud.example.com 7800 5000 |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 473 | |
| 474 | You can then connect to the host telnet-proxy is running on (e.g. |
Sean Middleditch | d88f183 | 2009-03-15 01:06:17 -0400 | [diff] [blame] | 475 | 127.0.0.1) on port 500 and you will automatically be proxied into |
| 476 | mud.example.com. |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 477 | |
| 478 | telnet-proxy will display status information about the data |
Sean Middleditch | aefcd0c | 2009-03-15 13:16:44 -0400 | [diff] [blame] | 479 | passing through both ends of the tunnel. telnet-proxy can only |
| 480 | support a single tunnel at a time. It will continue running until |
| 481 | an error occurs or a terminating signal is sent to the proxy |
| 482 | process. |