Sean Middleditch | 6aef073 | 2009-03-12 23:27:35 -0400 | [diff] [blame] | 1 | /* |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 2 | * Sean Middleditch |
| 3 | * sean@sourcemud.org |
| 4 | * |
Sean Middleditch | 6aef073 | 2009-03-12 23:27:35 -0400 | [diff] [blame] | 5 | * The author or authors of this code dedicate any and all copyright interest |
| 6 | * in this code to the public domain. We make this dedication for the benefit |
| 7 | * of the public at large and to the detriment of our heirs and successors. We |
| 8 | * intend this dedication to be an overt act of relinquishment in perpetuity of |
| 9 | * all present and future rights to this code under copyright law. |
| 10 | */ |
| 11 | |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 12 | #include <malloc.h> |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 13 | #include <string.h> |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 14 | #include <stdio.h> |
| 15 | #include <errno.h> |
| 16 | #include <string.h> |
| 17 | #include <stdarg.h> |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 18 | |
| 19 | #ifdef HAVE_ZLIB |
| 20 | #include "zlib.h" |
| 21 | #endif |
| 22 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 23 | #include "libtelnet.h" |
| 24 | |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 25 | /* RFC1143 state names */ |
| 26 | #define RFC1143_NO 0x00 |
| 27 | #define RFC1143_YES 0x01 |
| 28 | |
| 29 | #define RFC1143_WANT 0x02 |
| 30 | #define RFC1143_OP 0x04 |
| 31 | |
| 32 | #define RFC1143_WANTNO (RFC1143_WANT|RFC1143_YES) |
| 33 | #define RFC1143_WANTYES (RFC1143_WANT|RFC1143_NO) |
| 34 | #define RFC1143_WANTNO_OP (RFC1143_WANTNO|RFC1143_OP) |
| 35 | #define RFC1143_WANTYES_OP (RFC1143_WANTYES|RFC1143_OP) |
| 36 | |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 37 | /* buffer sizes */ |
| 38 | static const unsigned int _buffer_sizes[] = { |
| 39 | 0, |
| 40 | 512, |
| 41 | 2048, |
| 42 | 8192, |
| 43 | 16384, |
| 44 | }; |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 45 | static const unsigned int _buffer_sizes_count = sizeof(_buffer_sizes) / |
| 46 | sizeof(_buffer_sizes[0]); |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 47 | |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 48 | /* event dispatch helper; return value is value of the accept field of the |
| 49 | * event struct after dispatch; used for the funky REQUEST event */ |
Sean Middleditch | 35b95be | 2009-03-15 23:46:31 -0400 | [diff] [blame] | 50 | static int _event(libtelnet_t *telnet, libtelnet_event_type_t type, |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 51 | unsigned char command, unsigned char telopt, unsigned char *buffer, |
| 52 | unsigned int size) { |
| 53 | libtelnet_event_t ev; |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 54 | ev.buffer = buffer; |
| 55 | ev.size = size; |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 56 | ev.type = type; |
| 57 | ev.command = command; |
| 58 | ev.telopt = telopt; |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 59 | ev.accept = 0; |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 60 | |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 61 | telnet->eh(telnet, &ev, telnet->ud); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 62 | |
| 63 | return ev.accept; |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 64 | } |
| 65 | |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 66 | /* error generation function */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 67 | static libtelnet_error_t _error(libtelnet_t *telnet, unsigned line, |
| 68 | const char* func, libtelnet_error_t err, int fatal, const char *fmt, |
| 69 | ...) { |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 70 | char buffer[512]; |
| 71 | va_list va; |
| 72 | |
| 73 | /* format error intro */ |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 74 | snprintf(buffer, sizeof(buffer), "%s:%u in %s: ", __FILE__, line, func); |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 75 | |
| 76 | va_start(va, fmt); |
| 77 | vsnprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), |
| 78 | fmt, va); |
| 79 | va_end(va); |
| 80 | |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 81 | _event(telnet, fatal ? LIBTELNET_EV_ERROR : LIBTELNET_EV_WARNING, err, |
Sean Middleditch | 5cf6666 | 2009-03-15 20:02:14 -0400 | [diff] [blame] | 82 | 0, (unsigned char *)buffer, strlen(buffer)); |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 83 | |
| 84 | return err; |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 85 | } |
| 86 | |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 87 | /* initialize the zlib box for a telnet box; if deflate is non-zero, it |
| 88 | * initializes zlib for delating (compression), otherwise for inflating |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 89 | * (decompression). returns LIBTELNET_EOK on success, something else on |
| 90 | * failure. |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 91 | */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 92 | libtelnet_error_t _init_zlib(libtelnet_t *telnet, int deflate, int err_fatal) { |
| 93 | z_stream *z; |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 94 | int rs; |
| 95 | |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 96 | /* if compression is already enabled, fail loudly */ |
| 97 | if (telnet->z != 0) |
| 98 | return _error(telnet, __LINE__, __func__, LIBTELNET_EBADVAL, |
| 99 | err_fatal, "cannot initialize compression twice"); |
| 100 | |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 101 | /* allocate zstream box */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 102 | if ((z= (z_stream *)calloc(1, sizeof(z_stream))) == 0) |
| 103 | return _error(telnet, __LINE__, __func__, LIBTELNET_ENOMEM, err_fatal, |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 104 | "malloc() failed: %s", strerror(errno)); |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 105 | |
| 106 | /* initialize */ |
| 107 | if (deflate) { |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 108 | if ((rs = deflateInit(z, Z_DEFAULT_COMPRESSION)) != Z_OK) { |
| 109 | free(z); |
| 110 | return _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, |
| 111 | err_fatal, "deflateInit() failed: %s", zError(rs)); |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 112 | } |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 113 | telnet->flags |= LIBTELNET_PFLAG_DEFLATE; |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 114 | } else { |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 115 | if ((rs = inflateInit(z)) != Z_OK) { |
| 116 | free(z); |
| 117 | return _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, |
| 118 | err_fatal, "inflateInit() failed: %s", zError(rs)); |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 119 | } |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 120 | telnet->flags &= ~LIBTELNET_PFLAG_DEFLATE; |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 121 | } |
| 122 | |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 123 | telnet->z = z; |
| 124 | |
| 125 | return LIBTELNET_EOK; |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 126 | } |
| 127 | |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 128 | /* push bytes out, compressing them first if need be */ |
| 129 | static void _send(libtelnet_t *telnet, unsigned char *buffer, |
| 130 | unsigned int size) { |
| 131 | #ifdef HAVE_ZLIB |
| 132 | /* if we have a deflate (compression) zlib box, use it */ |
Sean Middleditch | 823bc00 | 2009-03-16 01:09:26 -0400 | [diff] [blame] | 133 | if (telnet->z != 0 && telnet->flags & LIBTELNET_PFLAG_DEFLATE) { |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 134 | unsigned char deflate_buffer[1024]; |
| 135 | int rs; |
| 136 | |
| 137 | /* initialize z state */ |
| 138 | telnet->z->next_in = buffer; |
| 139 | telnet->z->avail_in = size; |
| 140 | telnet->z->next_out = deflate_buffer; |
| 141 | telnet->z->avail_out = sizeof(deflate_buffer); |
| 142 | |
| 143 | /* deflate until buffer exhausted and all output is produced */ |
| 144 | while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) { |
| 145 | /* compress */ |
| 146 | if ((rs = deflate(telnet->z, Z_SYNC_FLUSH)) != Z_OK) { |
| 147 | _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, 1, |
| 148 | "deflate() failed: %s", zError(rs)); |
| 149 | deflateEnd(telnet->z); |
| 150 | free(telnet->z); |
| 151 | telnet->z = 0; |
| 152 | break; |
| 153 | } |
| 154 | |
| 155 | _event(telnet, LIBTELNET_EV_SEND, 0, 0, deflate_buffer, |
| 156 | sizeof(deflate_buffer) - telnet->z->avail_out); |
| 157 | |
| 158 | /* prepare output buffer for next run */ |
| 159 | telnet->z->next_out = deflate_buffer; |
| 160 | telnet->z->avail_out = sizeof(deflate_buffer); |
| 161 | } |
| 162 | |
| 163 | /* COMPRESS2 is not negotiated, just send */ |
| 164 | } else |
| 165 | #endif /* HAVE_ZLIB */ |
| 166 | _event(telnet, LIBTELNET_EV_SEND, 0, 0, buffer, size); |
| 167 | } |
| 168 | |
| 169 | /* retrieve RFC1143 option state */ |
| 170 | libtelnet_rfc1143_t _get_rfc1143(libtelnet_t *telnet, unsigned char telopt) { |
| 171 | static const libtelnet_rfc1143_t empty = { 0, 0, 0}; |
| 172 | int i; |
| 173 | |
| 174 | /* search for entry */ |
| 175 | for (i = 0; i != telnet->q_size; ++i) |
| 176 | if (telnet->q[i].telopt == telopt) |
| 177 | return telnet->q[i]; |
| 178 | |
| 179 | /* not found, return empty value */ |
| 180 | return empty; |
| 181 | } |
| 182 | |
| 183 | /* save RFC1143 option state */ |
| 184 | void _set_rfc1143(libtelnet_t *telnet, libtelnet_rfc1143_t q) { |
| 185 | libtelnet_rfc1143_t *qtmp; |
| 186 | int i; |
| 187 | |
| 188 | /* search for entry */ |
| 189 | for (i = 0; i != telnet->q_size; ++i) { |
| 190 | if (telnet->q[i].telopt == q.telopt) { |
| 191 | telnet->q[i] = q; |
| 192 | return; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /* we're going to need to track state for it, so grow the queue |
| 197 | * and put the telopt into it; bail on allocation error |
| 198 | */ |
| 199 | if ((qtmp = (libtelnet_rfc1143_t *)malloc(sizeof( |
| 200 | libtelnet_rfc1143_t) * (telnet->q_size + 1))) == 0) { |
| 201 | _error(telnet, __LINE__, __func__, LIBTELNET_ENOMEM, 0, |
| 202 | "malloc() failed: %s", strerror(errno)); |
| 203 | return; |
| 204 | } |
| 205 | telnet->q = qtmp; |
| 206 | telnet->q[telnet->q_size++] = q; |
| 207 | } |
| 208 | |
| 209 | /* send a negotiation without going through the RFC1143 checks */ |
| 210 | static void _send_negotiate(libtelnet_t *telnet, unsigned char cmd, |
| 211 | unsigned char opt) { |
| 212 | unsigned char bytes[3] = { LIBTELNET_IAC, cmd, opt }; |
| 213 | _send(telnet, bytes, 3); |
| 214 | } |
| 215 | |
| 216 | /* negotiation handling magic for RFC1143 */ |
| 217 | static void _negotiate(libtelnet_t *telnet, unsigned char cmd, |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 218 | unsigned char telopt) { |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 219 | libtelnet_rfc1143_t q; |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 220 | |
| 221 | /* in PROXY mode, just pass it thru and do nothing */ |
Sean Middleditch | 562257e | 2009-03-15 23:56:25 -0400 | [diff] [blame] | 222 | if (telnet->flags & LIBTELNET_FLAG_PROXY) { |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 223 | switch (cmd) { |
| 224 | case LIBTELNET_WILL: |
Sean Middleditch | 562257e | 2009-03-15 23:56:25 -0400 | [diff] [blame] | 225 | _event(telnet, LIBTELNET_EV_WILL, 0, telopt, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 226 | break; |
| 227 | case LIBTELNET_WONT: |
Sean Middleditch | 562257e | 2009-03-15 23:56:25 -0400 | [diff] [blame] | 228 | _event(telnet, LIBTELNET_EV_WONT, 0, telopt, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 229 | break; |
| 230 | case LIBTELNET_DO: |
Sean Middleditch | 562257e | 2009-03-15 23:56:25 -0400 | [diff] [blame] | 231 | _event(telnet, LIBTELNET_EV_DO, 0, telopt, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 232 | break; |
| 233 | case LIBTELNET_DONT: |
Sean Middleditch | 562257e | 2009-03-15 23:56:25 -0400 | [diff] [blame] | 234 | _event(telnet, LIBTELNET_EV_DONT, 0, telopt, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 235 | break; |
| 236 | } |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | /* lookup the current state of the option */ |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 241 | q = _get_rfc1143(telnet, telopt); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 242 | |
| 243 | /* start processing... */ |
| 244 | switch (cmd) { |
| 245 | /* request to enable option on remote end or confirm DO */ |
| 246 | case LIBTELNET_WILL: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 247 | switch (q.him) { |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 248 | case RFC1143_NO: |
| 249 | if (_event(telnet, LIBTELNET_EV_WILL, cmd, telopt, 0, 0) == 1) { |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 250 | q.him = RFC1143_YES; |
| 251 | _set_rfc1143(telnet, q); |
| 252 | _send_negotiate(telnet, LIBTELNET_DO, telopt); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 253 | } else |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 254 | _send_negotiate(telnet, LIBTELNET_DONT, telopt); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 255 | break; |
| 256 | case RFC1143_YES: |
| 257 | break; |
| 258 | case RFC1143_WANTNO: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 259 | q.him = RFC1143_NO; |
| 260 | _set_rfc1143(telnet, q); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 261 | _error(telnet, __LINE__, __func__, LIBTELNET_EPROTOCOL, 0, |
| 262 | "DONT answered by WILL"); |
| 263 | break; |
| 264 | case RFC1143_WANTNO_OP: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 265 | q.him = RFC1143_YES; |
| 266 | _set_rfc1143(telnet, q); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 267 | _error(telnet, __LINE__, __func__, LIBTELNET_EPROTOCOL, 0, |
| 268 | "DONT answered by WILL"); |
| 269 | break; |
| 270 | case RFC1143_WANTYES: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 271 | q.him = RFC1143_YES; |
| 272 | _set_rfc1143(telnet, q); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 273 | break; |
| 274 | case RFC1143_WANTYES_OP: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 275 | q.him = RFC1143_WANTNO; |
| 276 | _set_rfc1143(telnet, q); |
| 277 | _send_negotiate(telnet, LIBTELNET_DONT, telopt); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 278 | break; |
| 279 | } |
| 280 | break; |
| 281 | |
| 282 | /* request to disable option on remote end, confirm DONT, reject DO */ |
| 283 | case LIBTELNET_WONT: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 284 | switch (q.him) { |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 285 | case RFC1143_NO: |
| 286 | break; |
| 287 | case RFC1143_YES: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 288 | q.him = RFC1143_NO; |
| 289 | _set_rfc1143(telnet, q); |
| 290 | _send_negotiate(telnet, LIBTELNET_DONT, telopt); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 291 | _event(telnet, LIBTELNET_EV_WONT, 0, telopt, |
| 292 | 0, 0); |
| 293 | break; |
| 294 | case RFC1143_WANTNO: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 295 | q.him = RFC1143_NO; |
| 296 | _set_rfc1143(telnet, q); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 297 | _event(telnet, LIBTELNET_EV_WONT, 0, telopt, |
| 298 | 0, 0); |
| 299 | break; |
| 300 | case RFC1143_WANTNO_OP: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 301 | q.him = RFC1143_WANTYES; |
| 302 | _set_rfc1143(telnet, q); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 303 | _event(telnet, LIBTELNET_EV_DO, 0, telopt, |
| 304 | 0, 0); |
| 305 | break; |
| 306 | case RFC1143_WANTYES: |
| 307 | case RFC1143_WANTYES_OP: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 308 | q.him = RFC1143_NO; |
| 309 | _set_rfc1143(telnet, q); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 310 | break; |
| 311 | } |
| 312 | break; |
| 313 | |
| 314 | /* request to enable option on local end or confirm WILL */ |
| 315 | case LIBTELNET_DO: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 316 | switch (q.us) { |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 317 | case RFC1143_NO: |
| 318 | if (_event(telnet, LIBTELNET_EV_DO, cmd, telopt, 0, 0) == 1) { |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 319 | q.us = RFC1143_YES; |
| 320 | _set_rfc1143(telnet, q); |
| 321 | _send_negotiate(telnet, LIBTELNET_WILL, telopt); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 322 | } else |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 323 | _send_negotiate(telnet, LIBTELNET_WONT, telopt); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 324 | break; |
| 325 | case RFC1143_YES: |
| 326 | break; |
| 327 | case RFC1143_WANTNO: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 328 | q.us = RFC1143_NO; |
| 329 | _set_rfc1143(telnet, q); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 330 | _error(telnet, __LINE__, __func__, LIBTELNET_EPROTOCOL, 0, |
| 331 | "WONT answered by DO"); |
| 332 | break; |
| 333 | case RFC1143_WANTNO_OP: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 334 | q.us = RFC1143_YES; |
| 335 | _set_rfc1143(telnet, q); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 336 | _error(telnet, __LINE__, __func__, LIBTELNET_EPROTOCOL, 0, |
| 337 | "WONT answered by DO"); |
| 338 | break; |
| 339 | case RFC1143_WANTYES: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 340 | q.us = RFC1143_YES; |
| 341 | _set_rfc1143(telnet, q); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 342 | break; |
| 343 | case RFC1143_WANTYES_OP: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 344 | q.us = RFC1143_WANTNO; |
| 345 | _set_rfc1143(telnet, q); |
| 346 | _send_negotiate(telnet, LIBTELNET_WONT, telopt); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 347 | break; |
| 348 | } |
| 349 | break; |
| 350 | |
| 351 | /* request to disable option on local end, confirm WONT, reject WILL */ |
| 352 | case LIBTELNET_DONT: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 353 | switch (q.us) { |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 354 | case RFC1143_NO: |
| 355 | break; |
| 356 | case RFC1143_YES: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 357 | q.us = RFC1143_NO; |
| 358 | _set_rfc1143(telnet, q); |
| 359 | _send_negotiate(telnet, LIBTELNET_WONT, telopt); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 360 | _event(telnet, LIBTELNET_EV_DONT, 0, telopt, 0, 0); |
| 361 | break; |
| 362 | case RFC1143_WANTNO: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 363 | q.us = RFC1143_NO; |
| 364 | _set_rfc1143(telnet, q); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 365 | _event(telnet, LIBTELNET_EV_WONT, 0, telopt, 0, 0); |
| 366 | break; |
| 367 | case RFC1143_WANTNO_OP: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 368 | q.us = RFC1143_WANTYES; |
| 369 | _set_rfc1143(telnet, q); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 370 | _event(telnet, LIBTELNET_EV_WILL, 0, telopt, 0, 0); |
| 371 | break; |
| 372 | case RFC1143_WANTYES: |
| 373 | case RFC1143_WANTYES_OP: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 374 | q.us = RFC1143_NO; |
| 375 | _set_rfc1143(telnet, q); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 376 | break; |
| 377 | } |
| 378 | break; |
| 379 | } |
| 380 | } |
| 381 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 382 | /* initialize a telnet state tracker */ |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 383 | void libtelnet_init(libtelnet_t *telnet, libtelnet_event_handler_t eh, |
Sean Middleditch | 08bb05f | 2009-03-15 23:29:46 -0400 | [diff] [blame] | 384 | unsigned char flags, void *user_data) { |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 385 | memset(telnet, 0, sizeof(libtelnet_t)); |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 386 | telnet->ud = user_data; |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 387 | telnet->eh = eh; |
Sean Middleditch | 08bb05f | 2009-03-15 23:29:46 -0400 | [diff] [blame] | 388 | telnet->flags = flags; |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /* free up any memory allocated by a state tracker */ |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 392 | void libtelnet_free(libtelnet_t *telnet) { |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 393 | /* free sub-request buffer */ |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 394 | if (telnet->buffer != 0) { |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 395 | free(telnet->buffer); |
| 396 | telnet->buffer = 0; |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 397 | telnet->buffer_size = 0; |
| 398 | telnet->buffer_pos = 0; |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 399 | } |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 400 | |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 401 | /* free zlib box */ |
| 402 | if (telnet->z != 0) { |
| 403 | if (telnet->flags & LIBTELNET_PFLAG_DEFLATE) |
| 404 | deflateEnd(telnet->z); |
| 405 | else |
| 406 | inflateEnd(telnet->z); |
| 407 | free(telnet->z); |
| 408 | telnet->z = 0; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 409 | } |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 410 | |
| 411 | /* free RFC1143 queue */ |
| 412 | if (telnet->q) { |
| 413 | free(telnet->q); |
| 414 | telnet->q = 0; |
| 415 | telnet->q_size = 0; |
| 416 | } |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 417 | } |
| 418 | |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 419 | /* push a byte into the telnet buffer */ |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 420 | static libtelnet_error_t _buffer_byte(libtelnet_t *telnet, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 421 | unsigned char byte) { |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 422 | unsigned char *new_buffer; |
| 423 | int i; |
| 424 | |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 425 | /* check if we're out of room */ |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 426 | if (telnet->buffer_pos == telnet->buffer_size) { |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 427 | /* find the next buffer size */ |
| 428 | for (i = 0; i != _buffer_sizes_count; ++i) { |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 429 | if (_buffer_sizes[i] == telnet->buffer_size) |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 430 | break; |
| 431 | } |
| 432 | |
| 433 | /* overflow -- can't grow any more */ |
| 434 | if (i >= _buffer_sizes_count - 1) { |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 435 | _error(telnet, __LINE__, __func__, LIBTELNET_EOVERFLOW, 0, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 436 | "subnegotiation buffer size limit reached"); |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 437 | libtelnet_free(telnet); |
Sean Middleditch | f66a7ee | 2009-03-15 11:54:07 -0400 | [diff] [blame] | 438 | return LIBTELNET_EOVERFLOW; |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 439 | } |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 440 | |
| 441 | /* (re)allocate buffer */ |
| 442 | new_buffer = (unsigned char *)realloc(telnet->buffer, |
| 443 | _buffer_sizes[i + 1]); |
| 444 | if (new_buffer == 0) { |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 445 | _error(telnet, __LINE__, __func__, LIBTELNET_ENOMEM, 0, |
| 446 | "realloc() failed"); |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 447 | libtelnet_free(telnet); |
Sean Middleditch | f66a7ee | 2009-03-15 11:54:07 -0400 | [diff] [blame] | 448 | return LIBTELNET_ENOMEM; |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | telnet->buffer = new_buffer; |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 452 | telnet->buffer_size = _buffer_sizes[i + 1]; |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | /* push the byte, all set */ |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 456 | telnet->buffer[telnet->buffer_pos++] = byte; |
Sean Middleditch | f66a7ee | 2009-03-15 11:54:07 -0400 | [diff] [blame] | 457 | return LIBTELNET_EOK; |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 458 | } |
| 459 | |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 460 | static void _process(libtelnet_t *telnet, unsigned char *buffer, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 461 | unsigned int size) { |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 462 | unsigned char byte; |
| 463 | unsigned int i, start; |
| 464 | for (i = start = 0; i != size; ++i) { |
| 465 | byte = buffer[i]; |
| 466 | switch (telnet->state) { |
| 467 | /* regular data */ |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 468 | case LIBTELNET_STATE_DATA: |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 469 | /* on an IAC byte, pass through all pending bytes and |
| 470 | * switch states */ |
| 471 | if (byte == LIBTELNET_IAC) { |
| 472 | if (i != start) |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 473 | _event(telnet, LIBTELNET_EV_DATA, 0, 0, &buffer[start], |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 474 | i - start); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 475 | telnet->state = LIBTELNET_STATE_IAC; |
| 476 | } |
| 477 | break; |
| 478 | |
| 479 | /* IAC command */ |
| 480 | case LIBTELNET_STATE_IAC: |
| 481 | switch (byte) { |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 482 | /* subnegotiation */ |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 483 | case LIBTELNET_SB: |
| 484 | telnet->state = LIBTELNET_STATE_SB; |
| 485 | break; |
| 486 | /* negotiation commands */ |
| 487 | case LIBTELNET_WILL: |
| 488 | telnet->state = LIBTELNET_STATE_WILL; |
| 489 | break; |
| 490 | case LIBTELNET_WONT: |
| 491 | telnet->state = LIBTELNET_STATE_WONT; |
| 492 | break; |
| 493 | case LIBTELNET_DO: |
| 494 | telnet->state = LIBTELNET_STATE_DO; |
| 495 | break; |
| 496 | case LIBTELNET_DONT: |
| 497 | telnet->state = LIBTELNET_STATE_DONT; |
| 498 | break; |
| 499 | /* IAC escaping */ |
| 500 | case LIBTELNET_IAC: |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 501 | _event(telnet, LIBTELNET_EV_DATA, 0, 0, &byte, 1); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 502 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 503 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 504 | break; |
| 505 | /* some other command */ |
| 506 | default: |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 507 | _event(telnet, LIBTELNET_EV_IAC, byte, 0, 0, 0); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 508 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 509 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 510 | } |
| 511 | break; |
| 512 | |
| 513 | /* negotiation commands */ |
| 514 | case LIBTELNET_STATE_DO: |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 515 | _negotiate(telnet, LIBTELNET_DO, byte); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 516 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 517 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 518 | break; |
| 519 | case LIBTELNET_STATE_DONT: |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 520 | _negotiate(telnet, LIBTELNET_DONT, byte); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 521 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 522 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 523 | break; |
| 524 | case LIBTELNET_STATE_WILL: |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 525 | _negotiate(telnet, LIBTELNET_WILL, byte); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 526 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 527 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 528 | break; |
| 529 | case LIBTELNET_STATE_WONT: |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 530 | _negotiate(telnet, LIBTELNET_WONT, byte); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 531 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 532 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 533 | break; |
| 534 | |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame] | 535 | /* subnegotiation -- determine subnegotiation telopt */ |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 536 | case LIBTELNET_STATE_SB: |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame] | 537 | telnet->sb_telopt = byte; |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 538 | telnet->buffer_pos = 0; |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame] | 539 | telnet->state = LIBTELNET_STATE_SB_DATA; |
| 540 | break; |
| 541 | |
| 542 | /* subnegotiation -- buffer bytes until end request */ |
| 543 | case LIBTELNET_STATE_SB_DATA: |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 544 | /* IAC command in subnegotiation -- either IAC SE or IAC IAC */ |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 545 | if (byte == LIBTELNET_IAC) { |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame] | 546 | telnet->state = LIBTELNET_STATE_SB_DATA_IAC; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 547 | /* buffer the byte, or bail if we can't */ |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 548 | } else if (_buffer_byte(telnet, byte) != LIBTELNET_EOK) { |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 549 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 550 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 551 | } |
| 552 | break; |
| 553 | |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 554 | /* IAC escaping inside a subnegotiation */ |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame] | 555 | case LIBTELNET_STATE_SB_DATA_IAC: |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 556 | switch (byte) { |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 557 | /* end subnegotiation */ |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 558 | case LIBTELNET_SE: |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 559 | /* return to default state */ |
| 560 | start = i + 1; |
| 561 | telnet->state = LIBTELNET_STATE_DATA; |
| 562 | |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 563 | /* invoke callback */ |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 564 | _event(telnet, LIBTELNET_EV_SUBNEGOTIATION, 0, |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 565 | telnet->sb_telopt, telnet->buffer, telnet->buffer_pos); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 566 | |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 567 | #ifdef HAVE_ZLIB |
Sean Middleditch | 3df17f9 | 2009-03-16 01:12:16 -0400 | [diff] [blame^] | 568 | /* received COMPRESS2 begin marker, setup our zlib box and |
| 569 | * start handling the compressed stream if it's not already. |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 570 | */ |
Sean Middleditch | 3df17f9 | 2009-03-16 01:12:16 -0400 | [diff] [blame^] | 571 | if (telnet->sb_telopt == LIBTELNET_TELOPT_COMPRESS2) { |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 572 | if (_init_zlib(telnet, 0, 1) != LIBTELNET_EOK) |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 573 | break; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 574 | |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 575 | /* notify app that compression was enabled */ |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 576 | _event(telnet, LIBTELNET_EV_COMPRESS, 1, 0, 0, 0); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 577 | |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 578 | /* any remaining bytes in the buffer are compressed. |
| 579 | * we have to re-invoke libtelnet_push to get those |
| 580 | * bytes inflated and abort trying to process the |
| 581 | * remaining compressed bytes in the current _process |
| 582 | * buffer argument |
| 583 | */ |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 584 | libtelnet_push(telnet, &buffer[start], size - start); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 585 | return; |
| 586 | } |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 587 | #endif /* HAVE_ZLIB */ |
| 588 | |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 589 | break; |
| 590 | /* escaped IAC byte */ |
| 591 | case LIBTELNET_IAC: |
| 592 | /* push IAC into buffer */ |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 593 | if (_buffer_byte(telnet, LIBTELNET_IAC) != |
Sean Middleditch | f66a7ee | 2009-03-15 11:54:07 -0400 | [diff] [blame] | 594 | LIBTELNET_EOK) { |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 595 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 596 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 597 | } else { |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame] | 598 | telnet->state = LIBTELNET_STATE_SB_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 599 | } |
| 600 | break; |
| 601 | /* something else -- protocol error */ |
| 602 | default: |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 603 | _error(telnet, __LINE__, __func__, LIBTELNET_EPROTOCOL, 0, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 604 | "unexpected byte after IAC inside SB: %d", |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 605 | byte); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 606 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 607 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 608 | break; |
| 609 | } |
| 610 | break; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | /* pass through any remaining bytes */ |
Sean Middleditch | d30fd57 | 2009-03-14 12:55:17 -0400 | [diff] [blame] | 615 | if (telnet->state == LIBTELNET_STATE_DATA && i != start) |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 616 | _event(telnet, LIBTELNET_EV_DATA, 0, 0, buffer + start, i - start); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 617 | } |
| 618 | |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 619 | /* push a bytes into the state tracker */ |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 620 | void libtelnet_push(libtelnet_t *telnet, unsigned char *buffer, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 621 | unsigned int size) { |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 622 | #ifdef HAVE_ZLIB |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 623 | /* if we have an inflate (decompression) zlib stream, use it */ |
Sean Middleditch | 4611d91 | 2009-03-16 00:53:57 -0400 | [diff] [blame] | 624 | if (telnet->z != 0 && !(telnet->flags & LIBTELNET_PFLAG_DEFLATE)) { |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 625 | unsigned char inflate_buffer[4096]; |
| 626 | int rs; |
| 627 | |
| 628 | /* initialize zlib state */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 629 | telnet->z->next_in = buffer; |
| 630 | telnet->z->avail_in = size; |
| 631 | telnet->z->next_out = inflate_buffer; |
| 632 | telnet->z->avail_out = sizeof(inflate_buffer); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 633 | |
| 634 | /* inflate until buffer exhausted and all output is produced */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 635 | while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) { |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 636 | /* reset output buffer */ |
| 637 | |
| 638 | /* decompress */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 639 | rs = inflate(telnet->z, Z_SYNC_FLUSH); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 640 | |
| 641 | /* process the decompressed bytes on success */ |
| 642 | if (rs == Z_OK || rs == Z_STREAM_END) |
| 643 | _process(telnet, inflate_buffer, sizeof(inflate_buffer) - |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 644 | telnet->z->avail_out); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 645 | else |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 646 | _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, 1, |
| 647 | "inflate() failed: %s", zError(rs)); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 648 | |
| 649 | /* prepare output buffer for next run */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 650 | telnet->z->next_out = inflate_buffer; |
| 651 | telnet->z->avail_out = sizeof(inflate_buffer); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 652 | |
| 653 | /* on error (or on end of stream) disable further inflation */ |
| 654 | if (rs != Z_OK) { |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 655 | _event(telnet, LIBTELNET_EV_COMPRESS, 0, 0, 0, 0); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 656 | |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 657 | inflateEnd(telnet->z); |
| 658 | free(telnet->z); |
| 659 | telnet->z = 0; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 660 | break; |
| 661 | } |
| 662 | } |
| 663 | |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 664 | /* COMPRESS2 is not negotiated, just process */ |
| 665 | } else |
| 666 | #endif /* HAVE_ZLIB */ |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 667 | _process(telnet, buffer, size); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 668 | } |
| 669 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 670 | /* send an iac command */ |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 671 | void libtelnet_send_command(libtelnet_t *telnet, unsigned char cmd) { |
Sean Middleditch | f9cebec | 2009-03-13 20:17:31 -0400 | [diff] [blame] | 672 | unsigned char bytes[2] = { LIBTELNET_IAC, cmd }; |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 673 | _send(telnet, bytes, 2); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | /* send negotiation */ |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 677 | void libtelnet_send_negotiate(libtelnet_t *telnet, unsigned char cmd, |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 678 | unsigned char telopt) { |
| 679 | libtelnet_rfc1143_t q; |
| 680 | |
| 681 | /* if we're in proxy mode, just send it now */ |
| 682 | if (telnet->flags & LIBTELNET_FLAG_PROXY) { |
| 683 | unsigned char bytes[3] = { LIBTELNET_IAC, cmd, telopt }; |
| 684 | _send(telnet, bytes, 3); |
| 685 | return; |
| 686 | } |
| 687 | |
| 688 | /* get current option states */ |
| 689 | q = _get_rfc1143(telnet, telopt); |
| 690 | |
| 691 | switch (cmd) { |
| 692 | /* advertise willingess to support an option */ |
| 693 | case LIBTELNET_WILL: |
| 694 | switch (q.us) { |
| 695 | case RFC1143_NO: |
| 696 | q.us = RFC1143_WANTYES; |
| 697 | _set_rfc1143(telnet, q); |
| 698 | _negotiate(telnet, LIBTELNET_WILL, telopt); |
| 699 | break; |
| 700 | case RFC1143_YES: |
| 701 | break; |
| 702 | case RFC1143_WANTNO: |
| 703 | q.us = RFC1143_WANTNO_OP; |
| 704 | _set_rfc1143(telnet, q); |
| 705 | break; |
| 706 | case RFC1143_WANTYES: |
| 707 | break; |
| 708 | case RFC1143_WANTNO_OP: |
| 709 | break; |
| 710 | case RFC1143_WANTYES_OP: |
| 711 | q.us = RFC1143_WANTYES; |
| 712 | _set_rfc1143(telnet, q); |
| 713 | break; |
| 714 | } |
| 715 | break; |
| 716 | |
| 717 | /* force turn-off of locally enabled option */ |
| 718 | case LIBTELNET_WONT: |
| 719 | switch (q.us) { |
| 720 | case RFC1143_NO: |
| 721 | break; |
| 722 | case RFC1143_YES: |
| 723 | q.us = RFC1143_WANTNO; |
| 724 | _set_rfc1143(telnet, q); |
| 725 | _negotiate(telnet, LIBTELNET_WONT, telopt); |
| 726 | break; |
| 727 | case RFC1143_WANTNO: |
| 728 | break; |
| 729 | case RFC1143_WANTYES: |
| 730 | q.us = RFC1143_WANTYES_OP; |
| 731 | _set_rfc1143(telnet, q); |
| 732 | break; |
| 733 | case RFC1143_WANTNO_OP: |
| 734 | q.us = RFC1143_WANTNO; |
| 735 | _set_rfc1143(telnet, q); |
| 736 | break; |
| 737 | case RFC1143_WANTYES_OP: |
| 738 | break; |
| 739 | } |
| 740 | break; |
| 741 | |
| 742 | /* ask remote end to enable an option */ |
| 743 | case LIBTELNET_DO: |
| 744 | switch (q.him) { |
| 745 | case RFC1143_NO: |
| 746 | q.him = RFC1143_WANTYES; |
| 747 | _set_rfc1143(telnet, q); |
| 748 | _negotiate(telnet, LIBTELNET_DO, telopt); |
| 749 | break; |
| 750 | case RFC1143_YES: |
| 751 | break; |
| 752 | case RFC1143_WANTNO: |
| 753 | q.him = RFC1143_WANTNO_OP; |
| 754 | _set_rfc1143(telnet, q); |
| 755 | break; |
| 756 | case RFC1143_WANTYES: |
| 757 | break; |
| 758 | case RFC1143_WANTNO_OP: |
| 759 | break; |
| 760 | case RFC1143_WANTYES_OP: |
| 761 | q.him = RFC1143_WANTYES; |
| 762 | _set_rfc1143(telnet, q); |
| 763 | break; |
| 764 | } |
| 765 | break; |
| 766 | |
| 767 | /* demand remote end disable an option */ |
| 768 | case LIBTELNET_DONT: |
| 769 | switch (q.him) { |
| 770 | case RFC1143_NO: |
| 771 | break; |
| 772 | case RFC1143_YES: |
| 773 | q.him = RFC1143_WANTNO; |
| 774 | _set_rfc1143(telnet, q); |
| 775 | _negotiate(telnet, LIBTELNET_DONT, telopt); |
| 776 | break; |
| 777 | case RFC1143_WANTNO: |
| 778 | break; |
| 779 | case RFC1143_WANTYES: |
| 780 | q.him = RFC1143_WANTYES_OP; |
| 781 | _set_rfc1143(telnet, q); |
| 782 | break; |
| 783 | case RFC1143_WANTNO_OP: |
| 784 | q.him = RFC1143_WANTNO; |
| 785 | _set_rfc1143(telnet, q); |
| 786 | break; |
| 787 | case RFC1143_WANTYES_OP: |
| 788 | break; |
| 789 | } |
| 790 | break; |
| 791 | } |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | /* send non-command data (escapes IAC bytes) */ |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 795 | void libtelnet_send_data(libtelnet_t *telnet, unsigned char *buffer, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 796 | unsigned int size) { |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 797 | unsigned int i, l; |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 798 | for (l = i = 0; i != size; ++i) { |
| 799 | /* dump prior portion of text, send escaped bytes */ |
| 800 | if (buffer[i] == LIBTELNET_IAC) { |
| 801 | /* dump prior text if any */ |
| 802 | if (i != l) |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 803 | _send(telnet, buffer + l, i - l); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 804 | l = i + 1; |
| 805 | |
| 806 | /* send escape */ |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 807 | libtelnet_send_command(telnet, LIBTELNET_IAC); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 808 | } |
| 809 | } |
| 810 | |
| 811 | /* send whatever portion of buffer is left */ |
| 812 | if (i != l) |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 813 | _send(telnet, buffer + l, i - l); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | /* send sub-request */ |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 817 | void libtelnet_send_subnegotiation(libtelnet_t *telnet, unsigned char opt, |
| 818 | unsigned char *buffer, unsigned int size) { |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 819 | libtelnet_send_command(telnet, LIBTELNET_SB); |
| 820 | libtelnet_send_data(telnet, &opt, 1); |
| 821 | libtelnet_send_data(telnet, buffer, size); |
| 822 | libtelnet_send_command(telnet, LIBTELNET_SE); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 823 | |
| 824 | #ifdef HAVE_ZLIB |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 825 | /* if we're a proxy and we just sent the COMPRESS2 marker, we must |
| 826 | * make sure all further data is compressed if not already. |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 827 | */ |
Sean Middleditch | 08bb05f | 2009-03-15 23:29:46 -0400 | [diff] [blame] | 828 | if (telnet->flags & LIBTELNET_FLAG_PROXY && |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 829 | telnet->z == 0 && |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 830 | opt == LIBTELNET_TELOPT_COMPRESS2) { |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 831 | |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 832 | if (_init_zlib(telnet, 1, 1) != LIBTELNET_EOK) |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 833 | return; |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 834 | |
| 835 | /* notify app that compression was enabled */ |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 836 | _event(telnet, LIBTELNET_EV_COMPRESS, 1, 0, 0, 0); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 837 | } |
| 838 | #endif /* HAVE_ZLIB */ |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 839 | } |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 840 | |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 841 | void libtelnet_begin_compress2(libtelnet_t *telnet) { |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 842 | #ifdef HAVE_ZLIB |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 843 | unsigned char compress2[] = { LIBTELNET_IAC, LIBTELNET_SB, |
| 844 | LIBTELNET_TELOPT_COMPRESS2, LIBTELNET_IAC, LIBTELNET_SE }; |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 845 | |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 846 | /* attempt to create output stream first, bail if we can't */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 847 | if (_init_zlib(telnet, 1, 0) != LIBTELNET_EOK) |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 848 | return; |
| 849 | |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 850 | /* send compression marker. we send directly to the event handler |
| 851 | * instead of passing through _send because _send would result in |
| 852 | * the compress marker itself being compressed. |
| 853 | */ |
| 854 | _event(telnet, LIBTELNET_EV_SEND, 0, 0, compress2, sizeof(compress2)); |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 855 | #endif /* HAVE_ZLIB */ |
| 856 | } |