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 |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 32 | implements the Q method of TELNET option negotiation. libtelnet can |
| 33 | 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 |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 45 | part is a single function for pushing received data into the telnet |
| 46 | processor. The third part is the telnet_send_*() functions, which |
| 47 | generate TELNET commands and ensure data is properly formatted before |
| 48 | sending over the wire. The final part is the event handler |
| 49 | interface. |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 50 | |
| 51 | IIa. Initialization |
| 52 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 53 | struct telnet_t; |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 54 | This structure represents the state of the TELNET protocol for a |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 55 | single connection. Each connection utilizing TELNET must have its |
| 56 | own telnet_t structure, which is passed to all libtelnet API |
| 57 | calls. |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 58 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 59 | void telnet_init(telnet_t *telnet, telnet_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 | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 61 | 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 Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 65 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 66 | The handler parameter must be a function matching the |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 67 | telnet_event_handler_t definition. More information about events |
| 68 | 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 |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 73 | for implementing TELNET_EV_SEND event handling. |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 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 | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 78 | TELNET_FLAG_PROXY |
Sean Middleditch | c337ba6 | 2009-03-16 16:47:27 -0400 | [diff] [blame] | 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 | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 82 | 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 Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 86 | |
| 87 | IIb. Receiving Data |
| 88 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 89 | void telnet_push(telnet_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 |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 95 | 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 Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 99 | |
| 100 | IIc. Sending Data |
| 101 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 102 | All of the telnet_send_*() functions will invoke the TELNET_EV_SEND |
| 103 | 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 | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 111 | void telnet_send_command(telnet_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 | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 115 | 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 Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 120 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 121 | 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 Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 125 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 126 | void telnet_send_data(telnet_t *telnet, const char *buffer, |
Sean Middleditch | 97a8cb2 | 2009-03-16 16:51:41 -0400 | [diff] [blame] | 127 | unsigned int size); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 128 | 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 Middleditch | 90e79da | 2009-03-19 15:17:13 -0400 | [diff] [blame^] | 133 | |
| 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 Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 150 | |
| 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 Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 155 | |
Sean Middleditch | 90e79da | 2009-03-19 15:17:13 -0400 | [diff] [blame^] | 156 | Note that this function is just a shorthand for: |
| 157 | telnet_begin_subnegotiation(telnet, telopt); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 158 | telnet_send_data(telnet, buffer, size); |
Sean Middleditch | 90e79da | 2009-03-19 15:17:13 -0400 | [diff] [blame^] | 159 | telnet_end_subnegotiation(telnet); |
Sean Middleditch | 2b4bfc4 | 2009-03-16 01:25:52 -0400 | [diff] [blame] | 160 | |
| 161 | For some subnegotiations that involve a lot of complex formatted |
Sean Middleditch | 90e79da | 2009-03-19 15:17:13 -0400 | [diff] [blame^] | 162 | 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 Middleditch | 2b4bfc4 | 2009-03-16 01:25:52 -0400 | [diff] [blame] | 165 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 166 | NOTE: telnet_send_subnegotiation() does have special behavior in |
| 167 | PROXY mode, as in that mode this function will automatically |
Sean Middleditch | 2b4bfc4 | 2009-03-16 01:25:52 -0400 | [diff] [blame] | 168 | detect the COMPRESS2 marker and enable zlib compression. |
| 169 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 170 | 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 Middleditch | 4613e68 | 2009-03-16 17:21:48 -0400 | [diff] [blame] | 176 | |
| 177 | NOTE: due to an internal implementation detail, the maximum |
| 178 | lenth of the formatted text is 4096 characters. |
| 179 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 180 | 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 Middleditch | 4613e68 | 2009-03-16 17:21:48 -0400 | [diff] [blame] | 185 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 186 | IId. Event Handling |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 187 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 188 | 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 Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 191 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 192 | 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 Middleditch | 3032302 | 2009-03-14 21:45:28 -0400 | [diff] [blame] | 195 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 196 | void (telnet_t *telnet, telnet_event_t *event, void *user_data); |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 197 | |
| 198 | The event structure is detailed below. The user_data value is the |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 199 | pointer passed to telnet_init(). |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 200 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 201 | struct telnet_event_t { |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 202 | const char *buffer; |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 203 | unsigned int size; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 204 | telnet_event_type_t type; |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 205 | unsigned char command; |
| 206 | unsigned char telopt; |
| 207 | unsigned char accept; |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 208 | }; |
| 209 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 210 | The enumeration values of telnet_event_type_t are described in |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 211 | detail below. Whenever the the event handler is invoked, the |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 212 | application must look at the event->type value and do any necessary |
| 213 | processing. |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 214 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 215 | 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 Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 218 | |
| 219 | Here is an example event handler implementation which includes |
| 220 | handlers for several important events. |
| 221 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 222 | void my_event_handler(telnet_t *telnet, telnet_event_t *ev, |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 223 | void *user_data) { |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 224 | struct user_info *user = (struct user_info *)user_data; |
| 225 | |
| 226 | switch (ev->type) { |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 227 | case TELNET_EV_DATA: |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 228 | process_user_input(user, event->buffer, event->size); |
| 229 | break; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 230 | case TELNET_EV_SEND: |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 231 | write_to_descriptor(user, event->buffer, event->size); |
| 232 | break; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 233 | case TELNET_EV_ERROR: |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 234 | fatal_error("TELNET error: %s", event->buffer); |
| 235 | break; |
| 236 | } |
Sean Middleditch | 3032302 | 2009-03-14 21:45:28 -0400 | [diff] [blame] | 237 | } |
| 238 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 239 | 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 Middleditch | 3032302 | 2009-03-14 21:45:28 -0400 | [diff] [blame] | 244 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 245 | The event->buffer value will contain the bytes received and the |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 246 | event->size value will contain the number of bytes received. Note |
| 247 | that event->buffer is not NUL terminated! |
Sean Middleditch | 3032302 | 2009-03-14 21:45:28 -0400 | [diff] [blame] | 248 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 249 | 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 Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 254 | 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 Middleditch | 3032302 | 2009-03-14 21:45:28 -0400 | [diff] [blame] | 259 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 260 | The event->buffer value will contain the bytes to send and the |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 261 | 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 Middleditch | 3032302 | 2009-03-14 21:45:28 -0400 | [diff] [blame] | 264 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 265 | 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 Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 269 | TELNET_EV_IAC: |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 270 | The IAC event is triggered whenever a simple IAC command is |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 271 | received, such as the IAC EOR (end of record, also called go ahead |
| 272 | or GA) command. |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 273 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 274 | The command received is in the event->command value. |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 275 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 276 | The necessary processing depends on the specific commands; see |
| 277 | the TELNET RFC for more information. |
| 278 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 279 | 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 Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 283 | |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 284 | 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 Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 287 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 288 | 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 Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 291 | |
| 292 | In either case, the TELNET option under negotiation will be in |
| 293 | event->telopt field. |
| 294 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 295 | 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 Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 300 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 301 | libtelnet manages some of the pecularities of negotiation for you. |
| 302 | For information on libtelnet's negotiation method, see: |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 303 | |
| 304 | http://www.faqs.org/rfcs/rfc1143.html |
| 305 | |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 306 | 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 Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 324 | TELNET_EV_WONT: |
| 325 | TELNET_EV_DONT: |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 326 | The WONT and DONT events are sent when the remote end of the |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 327 | 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 Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 330 | |
| 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 Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 349 | TELNET_EV_SUBNEGOTIATION: |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 350 | Triggered whenever a TELNET sub-negotiation has been received. |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 351 | Sub-negotiations include the NAWS option for communicating |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 352 | 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 Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 355 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 356 | 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 Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 362 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 363 | The meaning and necessary processing for subnegotiations are |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 364 | 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 Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 368 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 369 | TELNET_EV_COMPRESS |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 370 | 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 Middleditch | b9e4864 | 2009-03-12 23:33:27 -0400 | [diff] [blame] | 373 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 374 | The event->command value will be 1 if compression has started and |
| 375 | will be 0 if compression has ended. |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 376 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 377 | 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 Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 383 | |
| 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 Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 388 | TELNET_EV_ERROR |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 389 | 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 Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 392 | 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 Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 395 | |
| 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 Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 399 | |
| 400 | III. INTEGRATING LIBTELNET WITH COMMON MUDS |
Sean Middleditch | b9e4864 | 2009-03-12 23:33:27 -0400 | [diff] [blame] | 401 | ===================================================================== |
| 402 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 403 | FIXME: fill in some notes about how to splice in libtelnet with |
| 404 | common Diku/Merc/Circle/etc. MUD codebases. |
Sean Middleditch | b9e4864 | 2009-03-12 23:33:27 -0400 | [diff] [blame] | 405 | |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 406 | IV. SAFETY AND CORRECTNESS CONSIDERATIONS |
Sean Middleditch | b9e4864 | 2009-03-12 23:33:27 -0400 | [diff] [blame] | 407 | ===================================================================== |
| 408 | |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 409 | Your existing application may make heavy use of its own output |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 410 | buffering and transmission commands, including hand-made routines for |
| 411 | sending TELNET commands and sub-negotiation requests. There are at |
| 412 | times subtle issues that need to be handled when communication over |
| 413 | the TELNET protocol, not least of which is the need to escape any |
| 414 | byte value 0xFF with a special TELNET command. |
Sean Middleditch | b9e4864 | 2009-03-12 23:33:27 -0400 | [diff] [blame] | 415 | |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 416 | For these reasons, it is very important that applications making use |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 417 | of libtelnet always make use of the telnet_send_*() family of |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 418 | functions for all data being sent over the TELNET connection. |
| 419 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 420 | In particular, if you are writing a client, all user input must be |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 421 | passed through to telnet_send_data(). This also includes any input |
| 422 | generated automatically by scripts, triggers, or macros. |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 423 | |
| 424 | For a server, any and all output -- including ANSI/VT100 escape |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 425 | codes, regular text, newlines, and so on -- must be passed through to |
| 426 | telnet_send_data(). |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 427 | |
| 428 | Any TELNET commands that are to be sent must be given to one of the |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 429 | following: telnet_send_command, telnet_send_negotiate, or |
| 430 | telnet_send_subnegotiation(). |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 431 | |
| 432 | If you are attempting to enable COMPRESS2/MCCP2, you must use the |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 433 | telnet_begin_compress2() function. |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 434 | |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 435 | V. MCCP2 COMPRESSION |
Sean Middleditch | b9e4864 | 2009-03-12 23:33:27 -0400 | [diff] [blame] | 436 | ===================================================================== |
| 437 | |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 438 | The MCCP2 (COMPRESS2) TELNET extension allows for the compression of |
| 439 | all traffic sent from server to client. For more information: |
| 440 | |
| 441 | http://www.mudbytes.net/index.php?a=articles&s=mccp |
| 442 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 443 | In order for libtelnet to support MCCP2, zlib must be installed and |
| 444 | enabled when compiling libtelnet. Use -DHAVE_ZLIB to enable zlib |
| 445 | when compiling libtelnet.c and pass -lz to the linker to link in the |
| 446 | zlib shared library. |
| 447 | |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 448 | libtelnet transparently supports MCCP2. For a server to support |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 449 | MCCP2, the application must begin negotiation of the COMPRESS2 option |
| 450 | using telnet_send_negotiate(), for example: |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 451 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 452 | telnet_send_negotiate(&telnet, TELNET_WILL, |
| 453 | TELNET_OPTION_COMPRESS2, user_data); |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 454 | |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 455 | If a favorable DO COMPRESS2 is sent back from the client then the |
| 456 | server application can begin compression at any time by calling |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 457 | telnet_begin_compress2(). |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 458 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 459 | If a connection is in PROXY mode and COMPRESS2 support is enabled |
| 460 | then libtelnet will automatically detect the start of a COMPRESS2 |
| 461 | stream, in either the sending or receiving direction. |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 462 | |
| 463 | VI. TELNET PROXY UTILITY |
| 464 | ===================================================================== |
| 465 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 466 | The telnet-proxy utility is a small application that serves both as a |
| 467 | testbed for libtelnet and as a powerful debugging tool for TELNET |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 468 | servers and clients. |
| 469 | |
| 470 | To use telnet-proxy, you must first compile it using: |
| 471 | |
| 472 | $ make |
| 473 | |
| 474 | If you do not have zlib installed and wish to disable MCCP2 support |
| 475 | then you must first edit the Makefile and remove the -DHAVE_ZLIB and |
| 476 | the -lz from the compile flags. |
| 477 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 478 | To run telnet-proxy, you simply give it the server's host name or IP |
| 479 | address, the server's port number, and the port number that |
Sean Middleditch | d88f183 | 2009-03-15 01:06:17 -0400 | [diff] [blame] | 480 | telnet-proxy should listen on. For example, to connect to the server |
| 481 | 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] | 482 | |
Sean Middleditch | d88f183 | 2009-03-15 01:06:17 -0400 | [diff] [blame] | 483 | $ ./telnet-proxy mud.example.com 7800 5000 |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 484 | |
| 485 | 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] | 486 | 127.0.0.1) on port 500 and you will automatically be proxied into |
| 487 | mud.example.com. |
Sean Middleditch | 892c5f1 | 2009-03-14 13:39:07 -0400 | [diff] [blame] | 488 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 489 | telnet-proxy will display status information about the data passing |
| 490 | through both ends of the tunnel. telnet-proxy can only support a |
| 491 | single tunnel at a time. It will continue running until an error |
| 492 | occurs or a terminating signal is sent to the proxy process. |