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 */ |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 38 | static const size_t _buffer_sizes[] = { |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 39 | 0, |
| 40 | 512, |
| 41 | 2048, |
| 42 | 8192, |
| 43 | 16384, |
| 44 | }; |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 45 | static const size_t _buffer_sizes_count = sizeof(_buffer_sizes) / |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 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 | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 50 | static int _event(telnet_t *telnet, telnet_event_type_t type, |
Sean Middleditch | 97a8cb2 | 2009-03-16 16:51:41 -0400 | [diff] [blame] | 51 | unsigned char command, unsigned char telopt, |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 52 | const char *buffer, size_t size) { |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 53 | telnet_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 | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 67 | static telnet_error_t _error(telnet_t *telnet, unsigned line, |
| 68 | const char* func, telnet_error_t err, int fatal, const char *fmt, |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 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 | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 81 | _event(telnet, fatal ? TELNET_EV_ERROR : TELNET_EV_WARNING, err, |
Sean Middleditch | 8daf774 | 2009-03-19 02:05:24 -0400 | [diff] [blame] | 82 | 0, (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 | e5b4759 | 2009-03-16 17:37:43 -0400 | [diff] [blame] | 87 | #ifdef HAVE_ZLIB |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 88 | /* initialize the zlib box for a telnet box; if deflate is non-zero, it |
| 89 | * initializes zlib for delating (compression), otherwise for inflating |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 90 | * (decompression). returns TELNET_EOK on success, something else on |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 91 | * failure. |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 92 | */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 93 | telnet_error_t _init_zlib(telnet_t *telnet, int deflate, int err_fatal) { |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 94 | z_stream *z; |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 95 | int rs; |
| 96 | |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 97 | /* if compression is already enabled, fail loudly */ |
| 98 | if (telnet->z != 0) |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 99 | return _error(telnet, __LINE__, __func__, TELNET_EBADVAL, |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 100 | err_fatal, "cannot initialize compression twice"); |
| 101 | |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 102 | /* allocate zstream box */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 103 | if ((z= (z_stream *)calloc(1, sizeof(z_stream))) == 0) |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 104 | return _error(telnet, __LINE__, __func__, TELNET_ENOMEM, err_fatal, |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 105 | "malloc() failed: %s", strerror(errno)); |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 106 | |
| 107 | /* initialize */ |
| 108 | if (deflate) { |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 109 | if ((rs = deflateInit(z, Z_DEFAULT_COMPRESSION)) != Z_OK) { |
| 110 | free(z); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 111 | return _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS, |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 112 | err_fatal, "deflateInit() failed: %s", zError(rs)); |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 113 | } |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 114 | telnet->flags |= TELNET_PFLAG_DEFLATE; |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 115 | } else { |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 116 | if ((rs = inflateInit(z)) != Z_OK) { |
| 117 | free(z); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 118 | return _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS, |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 119 | err_fatal, "inflateInit() failed: %s", zError(rs)); |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 120 | } |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 121 | telnet->flags &= ~TELNET_PFLAG_DEFLATE; |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 122 | } |
| 123 | |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 124 | telnet->z = z; |
| 125 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 126 | return TELNET_EOK; |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 127 | } |
Sean Middleditch | e5b4759 | 2009-03-16 17:37:43 -0400 | [diff] [blame] | 128 | #endif |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 129 | |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 130 | /* push bytes out, compressing them first if need be */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 131 | static void _send(telnet_t *telnet, const char *buffer, |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 132 | size_t size) { |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 133 | #ifdef HAVE_ZLIB |
| 134 | /* if we have a deflate (compression) zlib box, use it */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 135 | if (telnet->z != 0 && telnet->flags & TELNET_PFLAG_DEFLATE) { |
Sean Middleditch | 8daf774 | 2009-03-19 02:05:24 -0400 | [diff] [blame] | 136 | char deflate_buffer[1024]; |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 137 | int rs; |
| 138 | |
| 139 | /* initialize z state */ |
Sean Middleditch | 97a8cb2 | 2009-03-16 16:51:41 -0400 | [diff] [blame] | 140 | telnet->z->next_in = (unsigned char *)buffer; |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 141 | telnet->z->avail_in = size; |
Sean Middleditch | 8daf774 | 2009-03-19 02:05:24 -0400 | [diff] [blame] | 142 | telnet->z->next_out = (unsigned char *)deflate_buffer; |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 143 | telnet->z->avail_out = sizeof(deflate_buffer); |
| 144 | |
| 145 | /* deflate until buffer exhausted and all output is produced */ |
| 146 | while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) { |
| 147 | /* compress */ |
| 148 | if ((rs = deflate(telnet->z, Z_SYNC_FLUSH)) != Z_OK) { |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 149 | _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS, 1, |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 150 | "deflate() failed: %s", zError(rs)); |
| 151 | deflateEnd(telnet->z); |
| 152 | free(telnet->z); |
| 153 | telnet->z = 0; |
| 154 | break; |
| 155 | } |
| 156 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 157 | _event(telnet, TELNET_EV_SEND, 0, 0, deflate_buffer, |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 158 | sizeof(deflate_buffer) - telnet->z->avail_out); |
| 159 | |
| 160 | /* prepare output buffer for next run */ |
Sean Middleditch | 8daf774 | 2009-03-19 02:05:24 -0400 | [diff] [blame] | 161 | telnet->z->next_out = (unsigned char *)deflate_buffer; |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 162 | telnet->z->avail_out = sizeof(deflate_buffer); |
| 163 | } |
| 164 | |
| 165 | /* COMPRESS2 is not negotiated, just send */ |
| 166 | } else |
| 167 | #endif /* HAVE_ZLIB */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 168 | _event(telnet, TELNET_EV_SEND, 0, 0, buffer, size); |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /* retrieve RFC1143 option state */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 172 | telnet_rfc1143_t _get_rfc1143(telnet_t *telnet, unsigned char telopt) { |
Sean Middleditch | 4987f9f | 2009-03-19 12:51:40 -0400 | [diff] [blame] | 173 | const telnet_rfc1143_t empty = { telopt, 0, 0}; |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 174 | int i; |
| 175 | |
| 176 | /* search for entry */ |
| 177 | for (i = 0; i != telnet->q_size; ++i) |
| 178 | if (telnet->q[i].telopt == telopt) |
| 179 | return telnet->q[i]; |
| 180 | |
| 181 | /* not found, return empty value */ |
| 182 | return empty; |
| 183 | } |
| 184 | |
| 185 | /* save RFC1143 option state */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 186 | void _set_rfc1143(telnet_t *telnet, telnet_rfc1143_t q) { |
| 187 | telnet_rfc1143_t *qtmp; |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 188 | int i; |
| 189 | |
| 190 | /* search for entry */ |
| 191 | for (i = 0; i != telnet->q_size; ++i) { |
| 192 | if (telnet->q[i].telopt == q.telopt) { |
| 193 | telnet->q[i] = q; |
| 194 | return; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /* we're going to need to track state for it, so grow the queue |
| 199 | * and put the telopt into it; bail on allocation error |
| 200 | */ |
Sean Middleditch | 5dea1f7 | 2009-03-19 12:45:34 -0400 | [diff] [blame] | 201 | if ((qtmp = (telnet_rfc1143_t *)realloc(telnet->q, sizeof( |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 202 | telnet_rfc1143_t) * (telnet->q_size + 1))) == 0) { |
| 203 | _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0, |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 204 | "malloc() failed: %s", strerror(errno)); |
| 205 | return; |
| 206 | } |
| 207 | telnet->q = qtmp; |
| 208 | telnet->q[telnet->q_size++] = q; |
| 209 | } |
| 210 | |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 211 | /* negotiation handling magic for RFC1143 */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 212 | static void _negotiate(telnet_t *telnet, unsigned char cmd, |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 213 | unsigned char telopt) { |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 214 | telnet_rfc1143_t q; |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 215 | |
| 216 | /* in PROXY mode, just pass it thru and do nothing */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 217 | if (telnet->flags & TELNET_FLAG_PROXY) { |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 218 | switch (cmd) { |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 219 | case TELNET_WILL: |
| 220 | _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 221 | break; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 222 | case TELNET_WONT: |
| 223 | _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 224 | break; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 225 | case TELNET_DO: |
| 226 | _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 227 | break; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 228 | case TELNET_DONT: |
| 229 | _event(telnet, TELNET_EV_DONT, 0, telopt, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 230 | break; |
| 231 | } |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | /* lookup the current state of the option */ |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 236 | q = _get_rfc1143(telnet, telopt); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 237 | |
| 238 | /* start processing... */ |
| 239 | switch (cmd) { |
| 240 | /* request to enable option on remote end or confirm DO */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 241 | case TELNET_WILL: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 242 | switch (q.him) { |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 243 | case RFC1143_NO: |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 244 | if (_event(telnet, TELNET_EV_WILL, cmd, telopt, 0, 0) == 1) { |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 245 | q.him = RFC1143_YES; |
| 246 | _set_rfc1143(telnet, q); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 247 | telnet_send_telopt(telnet, TELNET_DO, telopt); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 248 | } else |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 249 | telnet_send_telopt(telnet, TELNET_DONT, telopt); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 250 | break; |
| 251 | case RFC1143_YES: |
| 252 | break; |
| 253 | case RFC1143_WANTNO: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 254 | q.him = RFC1143_NO; |
| 255 | _set_rfc1143(telnet, q); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 256 | _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0, |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 257 | "DONT answered by WILL"); |
| 258 | break; |
| 259 | case RFC1143_WANTNO_OP: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 260 | q.him = RFC1143_YES; |
| 261 | _set_rfc1143(telnet, q); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 262 | _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0, |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 263 | "DONT answered by WILL"); |
| 264 | break; |
| 265 | case RFC1143_WANTYES: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 266 | q.him = RFC1143_YES; |
| 267 | _set_rfc1143(telnet, q); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 268 | break; |
| 269 | case RFC1143_WANTYES_OP: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 270 | q.him = RFC1143_WANTNO; |
| 271 | _set_rfc1143(telnet, q); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 272 | telnet_send_telopt(telnet, TELNET_DONT, telopt); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 273 | break; |
| 274 | } |
| 275 | break; |
| 276 | |
| 277 | /* request to disable option on remote end, confirm DONT, reject DO */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 278 | case TELNET_WONT: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 279 | switch (q.him) { |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 280 | case RFC1143_NO: |
| 281 | break; |
| 282 | case RFC1143_YES: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 283 | q.him = RFC1143_NO; |
| 284 | _set_rfc1143(telnet, q); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 285 | telnet_send_telopt(telnet, TELNET_DONT, telopt); |
| 286 | _event(telnet, TELNET_EV_WONT, 0, telopt, |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 287 | 0, 0); |
| 288 | break; |
| 289 | case RFC1143_WANTNO: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 290 | q.him = RFC1143_NO; |
| 291 | _set_rfc1143(telnet, q); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 292 | _event(telnet, TELNET_EV_WONT, 0, telopt, |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 293 | 0, 0); |
| 294 | break; |
| 295 | case RFC1143_WANTNO_OP: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 296 | q.him = RFC1143_WANTYES; |
| 297 | _set_rfc1143(telnet, q); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 298 | _event(telnet, TELNET_EV_DO, 0, telopt, |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 299 | 0, 0); |
| 300 | break; |
| 301 | case RFC1143_WANTYES: |
| 302 | case RFC1143_WANTYES_OP: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 303 | q.him = RFC1143_NO; |
| 304 | _set_rfc1143(telnet, q); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 305 | break; |
| 306 | } |
| 307 | break; |
| 308 | |
| 309 | /* request to enable option on local end or confirm WILL */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 310 | case TELNET_DO: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 311 | switch (q.us) { |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 312 | case RFC1143_NO: |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 313 | if (_event(telnet, TELNET_EV_DO, cmd, telopt, 0, 0) == 1) { |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 314 | q.us = RFC1143_YES; |
| 315 | _set_rfc1143(telnet, q); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 316 | telnet_send_telopt(telnet, TELNET_WILL, telopt); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 317 | } else |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 318 | telnet_send_telopt(telnet, TELNET_WONT, telopt); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 319 | break; |
| 320 | case RFC1143_YES: |
| 321 | break; |
| 322 | case RFC1143_WANTNO: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 323 | q.us = RFC1143_NO; |
| 324 | _set_rfc1143(telnet, q); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 325 | _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0, |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 326 | "WONT answered by DO"); |
| 327 | break; |
| 328 | case RFC1143_WANTNO_OP: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 329 | q.us = RFC1143_YES; |
| 330 | _set_rfc1143(telnet, q); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 331 | _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0, |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 332 | "WONT answered by DO"); |
| 333 | break; |
| 334 | case RFC1143_WANTYES: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 335 | q.us = RFC1143_YES; |
| 336 | _set_rfc1143(telnet, q); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 337 | break; |
| 338 | case RFC1143_WANTYES_OP: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 339 | q.us = RFC1143_WANTNO; |
| 340 | _set_rfc1143(telnet, q); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 341 | telnet_send_telopt(telnet, TELNET_WONT, telopt); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 342 | break; |
| 343 | } |
| 344 | break; |
| 345 | |
| 346 | /* request to disable option on local end, confirm WONT, reject WILL */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 347 | case TELNET_DONT: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 348 | switch (q.us) { |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 349 | case RFC1143_NO: |
| 350 | break; |
| 351 | case RFC1143_YES: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 352 | q.us = RFC1143_NO; |
| 353 | _set_rfc1143(telnet, q); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 354 | telnet_send_telopt(telnet, TELNET_WONT, telopt); |
| 355 | _event(telnet, TELNET_EV_DONT, 0, telopt, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 356 | break; |
| 357 | case RFC1143_WANTNO: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 358 | q.us = RFC1143_NO; |
| 359 | _set_rfc1143(telnet, q); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 360 | _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 361 | break; |
| 362 | case RFC1143_WANTNO_OP: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 363 | q.us = RFC1143_WANTYES; |
| 364 | _set_rfc1143(telnet, q); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 365 | _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 366 | break; |
| 367 | case RFC1143_WANTYES: |
| 368 | case RFC1143_WANTYES_OP: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 369 | q.us = RFC1143_NO; |
| 370 | _set_rfc1143(telnet, q); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 371 | break; |
| 372 | } |
| 373 | break; |
| 374 | } |
| 375 | } |
| 376 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 377 | /* initialize a telnet state tracker */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 378 | void telnet_init(telnet_t *telnet, telnet_event_handler_t eh, |
Sean Middleditch | 08bb05f | 2009-03-15 23:29:46 -0400 | [diff] [blame] | 379 | unsigned char flags, void *user_data) { |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 380 | memset(telnet, 0, sizeof(telnet_t)); |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 381 | telnet->ud = user_data; |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 382 | telnet->eh = eh; |
Sean Middleditch | 08bb05f | 2009-03-15 23:29:46 -0400 | [diff] [blame] | 383 | telnet->flags = flags; |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /* free up any memory allocated by a state tracker */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 387 | void telnet_free(telnet_t *telnet) { |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 388 | /* free sub-request buffer */ |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 389 | if (telnet->buffer != 0) { |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 390 | free(telnet->buffer); |
| 391 | telnet->buffer = 0; |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 392 | telnet->buffer_size = 0; |
| 393 | telnet->buffer_pos = 0; |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 394 | } |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 395 | |
Sean Middleditch | e5b4759 | 2009-03-16 17:37:43 -0400 | [diff] [blame] | 396 | #ifdef HAVE_ZLIB |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 397 | /* free zlib box */ |
| 398 | if (telnet->z != 0) { |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 399 | if (telnet->flags & TELNET_PFLAG_DEFLATE) |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 400 | deflateEnd(telnet->z); |
| 401 | else |
| 402 | inflateEnd(telnet->z); |
| 403 | free(telnet->z); |
| 404 | telnet->z = 0; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 405 | } |
Sean Middleditch | e5b4759 | 2009-03-16 17:37:43 -0400 | [diff] [blame] | 406 | #endif |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 407 | |
| 408 | /* free RFC1143 queue */ |
| 409 | if (telnet->q) { |
| 410 | free(telnet->q); |
| 411 | telnet->q = 0; |
| 412 | telnet->q_size = 0; |
| 413 | } |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 414 | } |
| 415 | |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 416 | /* push a byte into the telnet buffer */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 417 | static telnet_error_t _buffer_byte(telnet_t *telnet, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 418 | unsigned char byte) { |
Sean Middleditch | 8daf774 | 2009-03-19 02:05:24 -0400 | [diff] [blame] | 419 | char *new_buffer; |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 420 | size_t i; |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 421 | |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 422 | /* check if we're out of room */ |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 423 | if (telnet->buffer_pos == telnet->buffer_size) { |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 424 | /* find the next buffer size */ |
| 425 | for (i = 0; i != _buffer_sizes_count; ++i) { |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 426 | if (_buffer_sizes[i] == telnet->buffer_size) |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 427 | break; |
| 428 | } |
| 429 | |
| 430 | /* overflow -- can't grow any more */ |
| 431 | if (i >= _buffer_sizes_count - 1) { |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 432 | _error(telnet, __LINE__, __func__, TELNET_EOVERFLOW, 0, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 433 | "subnegotiation buffer size limit reached"); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 434 | telnet_free(telnet); |
| 435 | return TELNET_EOVERFLOW; |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 436 | } |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 437 | |
| 438 | /* (re)allocate buffer */ |
Sean Middleditch | 8daf774 | 2009-03-19 02:05:24 -0400 | [diff] [blame] | 439 | new_buffer = (char *)realloc(telnet->buffer, |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 440 | _buffer_sizes[i + 1]); |
| 441 | if (new_buffer == 0) { |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 442 | _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0, |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 443 | "realloc() failed"); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 444 | telnet_free(telnet); |
| 445 | return TELNET_ENOMEM; |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | telnet->buffer = new_buffer; |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 449 | telnet->buffer_size = _buffer_sizes[i + 1]; |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /* push the byte, all set */ |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 453 | telnet->buffer[telnet->buffer_pos++] = byte; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 454 | return TELNET_EOK; |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 455 | } |
| 456 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 457 | static void _process(telnet_t *telnet, const char *buffer, |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 458 | size_t size) { |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 459 | unsigned char byte; |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 460 | size_t i, start; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 461 | for (i = start = 0; i != size; ++i) { |
| 462 | byte = buffer[i]; |
| 463 | switch (telnet->state) { |
| 464 | /* regular data */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 465 | case TELNET_STATE_DATA: |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 466 | /* on an IAC byte, pass through all pending bytes and |
| 467 | * switch states */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 468 | if (byte == TELNET_IAC) { |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 469 | if (i != start) |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 470 | _event(telnet, TELNET_EV_DATA, 0, 0, &buffer[start], |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 471 | i - start); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 472 | telnet->state = TELNET_STATE_IAC; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 473 | } |
| 474 | break; |
| 475 | |
| 476 | /* IAC command */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 477 | case TELNET_STATE_IAC: |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 478 | switch (byte) { |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 479 | /* subnegotiation */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 480 | case TELNET_SB: |
| 481 | telnet->state = TELNET_STATE_SB; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 482 | break; |
| 483 | /* negotiation commands */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 484 | case TELNET_WILL: |
| 485 | telnet->state = TELNET_STATE_WILL; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 486 | break; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 487 | case TELNET_WONT: |
| 488 | telnet->state = TELNET_STATE_WONT; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 489 | break; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 490 | case TELNET_DO: |
| 491 | telnet->state = TELNET_STATE_DO; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 492 | break; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 493 | case TELNET_DONT: |
| 494 | telnet->state = TELNET_STATE_DONT; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 495 | break; |
| 496 | /* IAC escaping */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 497 | case TELNET_IAC: |
| 498 | _event(telnet, TELNET_EV_DATA, 0, 0, (char*)&byte, 1); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 499 | start = i + 1; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 500 | telnet->state = TELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 501 | break; |
| 502 | /* some other command */ |
| 503 | default: |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 504 | _event(telnet, TELNET_EV_IAC, byte, 0, 0, 0); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 505 | start = i + 1; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 506 | telnet->state = TELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 507 | } |
| 508 | break; |
| 509 | |
| 510 | /* negotiation commands */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 511 | case TELNET_STATE_DO: |
| 512 | _negotiate(telnet, TELNET_DO, byte); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 513 | start = i + 1; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 514 | telnet->state = TELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 515 | break; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 516 | case TELNET_STATE_DONT: |
| 517 | _negotiate(telnet, TELNET_DONT, byte); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 518 | start = i + 1; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 519 | telnet->state = TELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 520 | break; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 521 | case TELNET_STATE_WILL: |
| 522 | _negotiate(telnet, TELNET_WILL, byte); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 523 | start = i + 1; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 524 | telnet->state = TELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 525 | break; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 526 | case TELNET_STATE_WONT: |
| 527 | _negotiate(telnet, TELNET_WONT, byte); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 528 | start = i + 1; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 529 | telnet->state = TELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 530 | break; |
| 531 | |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame] | 532 | /* subnegotiation -- determine subnegotiation telopt */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 533 | case TELNET_STATE_SB: |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame] | 534 | telnet->sb_telopt = byte; |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 535 | telnet->buffer_pos = 0; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 536 | telnet->state = TELNET_STATE_SB_DATA; |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame] | 537 | break; |
| 538 | |
| 539 | /* subnegotiation -- buffer bytes until end request */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 540 | case TELNET_STATE_SB_DATA: |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 541 | /* IAC command in subnegotiation -- either IAC SE or IAC IAC */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 542 | if (byte == TELNET_IAC) { |
| 543 | telnet->state = TELNET_STATE_SB_DATA_IAC; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 544 | /* buffer the byte, or bail if we can't */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 545 | } else if (_buffer_byte(telnet, byte) != TELNET_EOK) { |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 546 | start = i + 1; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 547 | telnet->state = TELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 548 | } |
| 549 | break; |
| 550 | |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 551 | /* IAC escaping inside a subnegotiation */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 552 | case TELNET_STATE_SB_DATA_IAC: |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 553 | switch (byte) { |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 554 | /* end subnegotiation */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 555 | case TELNET_SE: |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 556 | /* return to default state */ |
| 557 | start = i + 1; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 558 | telnet->state = TELNET_STATE_DATA; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 559 | |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 560 | /* invoke callback */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 561 | _event(telnet, TELNET_EV_SUBNEGOTIATION, 0, |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 562 | telnet->sb_telopt, telnet->buffer, telnet->buffer_pos); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 563 | |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 564 | #ifdef HAVE_ZLIB |
Sean Middleditch | 3df17f9 | 2009-03-16 01:12:16 -0400 | [diff] [blame] | 565 | /* received COMPRESS2 begin marker, setup our zlib box and |
| 566 | * start handling the compressed stream if it's not already. |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 567 | */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 568 | if (telnet->sb_telopt == TELNET_TELOPT_COMPRESS2) { |
| 569 | if (_init_zlib(telnet, 0, 1) != TELNET_EOK) |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 570 | break; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 571 | |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 572 | /* notify app that compression was enabled */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 573 | _event(telnet, TELNET_EV_COMPRESS, 1, 0, 0, 0); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 574 | |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 575 | /* any remaining bytes in the buffer are compressed. |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 576 | * we have to re-invoke telnet_push to get those |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 577 | * bytes inflated and abort trying to process the |
| 578 | * remaining compressed bytes in the current _process |
| 579 | * buffer argument |
| 580 | */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 581 | telnet_push(telnet, &buffer[start], size - start); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 582 | return; |
| 583 | } |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 584 | #endif /* HAVE_ZLIB */ |
| 585 | |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 586 | break; |
| 587 | /* escaped IAC byte */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 588 | case TELNET_IAC: |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 589 | /* push IAC into buffer */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 590 | if (_buffer_byte(telnet, TELNET_IAC) != |
| 591 | TELNET_EOK) { |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 592 | start = i + 1; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 593 | telnet->state = TELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 594 | } else { |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 595 | telnet->state = TELNET_STATE_SB_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 596 | } |
| 597 | break; |
| 598 | /* something else -- protocol error */ |
| 599 | default: |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 600 | _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 601 | "unexpected byte after IAC inside SB: %d", |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 602 | byte); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 603 | start = i + 1; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 604 | telnet->state = TELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 605 | break; |
| 606 | } |
| 607 | break; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | /* pass through any remaining bytes */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 612 | if (telnet->state == TELNET_STATE_DATA && i != start) |
| 613 | _event(telnet, TELNET_EV_DATA, 0, 0, buffer + start, i - start); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 614 | } |
| 615 | |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 616 | /* push a bytes into the state tracker */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 617 | void telnet_push(telnet_t *telnet, const char *buffer, |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 618 | size_t size) { |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 619 | #ifdef HAVE_ZLIB |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 620 | /* if we have an inflate (decompression) zlib stream, use it */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 621 | if (telnet->z != 0 && !(telnet->flags & TELNET_PFLAG_DEFLATE)) { |
Sean Middleditch | 8daf774 | 2009-03-19 02:05:24 -0400 | [diff] [blame] | 622 | char inflate_buffer[4096]; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 623 | int rs; |
| 624 | |
| 625 | /* initialize zlib state */ |
Sean Middleditch | 8daf774 | 2009-03-19 02:05:24 -0400 | [diff] [blame] | 626 | telnet->z->next_in = (unsigned char*)buffer; |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 627 | telnet->z->avail_in = size; |
Sean Middleditch | 8daf774 | 2009-03-19 02:05:24 -0400 | [diff] [blame] | 628 | telnet->z->next_out = (unsigned char *)inflate_buffer; |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 629 | telnet->z->avail_out = sizeof(inflate_buffer); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 630 | |
| 631 | /* inflate until buffer exhausted and all output is produced */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 632 | while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) { |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 633 | /* reset output buffer */ |
| 634 | |
| 635 | /* decompress */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 636 | rs = inflate(telnet->z, Z_SYNC_FLUSH); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 637 | |
| 638 | /* process the decompressed bytes on success */ |
| 639 | if (rs == Z_OK || rs == Z_STREAM_END) |
| 640 | _process(telnet, inflate_buffer, sizeof(inflate_buffer) - |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 641 | telnet->z->avail_out); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 642 | else |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 643 | _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS, 1, |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 644 | "inflate() failed: %s", zError(rs)); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 645 | |
| 646 | /* prepare output buffer for next run */ |
Sean Middleditch | 8daf774 | 2009-03-19 02:05:24 -0400 | [diff] [blame] | 647 | telnet->z->next_out = (unsigned char *)inflate_buffer; |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 648 | telnet->z->avail_out = sizeof(inflate_buffer); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 649 | |
| 650 | /* on error (or on end of stream) disable further inflation */ |
| 651 | if (rs != Z_OK) { |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 652 | _event(telnet, TELNET_EV_COMPRESS, 0, 0, 0, 0); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 653 | |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 654 | inflateEnd(telnet->z); |
| 655 | free(telnet->z); |
| 656 | telnet->z = 0; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 657 | break; |
| 658 | } |
| 659 | } |
| 660 | |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 661 | /* COMPRESS2 is not negotiated, just process */ |
| 662 | } else |
| 663 | #endif /* HAVE_ZLIB */ |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 664 | _process(telnet, buffer, size); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 665 | } |
| 666 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 667 | /* send an iac command */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 668 | void telnet_send_command(telnet_t *telnet, unsigned char cmd) { |
| 669 | char bytes[2] = { TELNET_IAC, cmd }; |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 670 | _send(telnet, bytes, 2); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 671 | } |
| 672 | |
Sean Middleditch | 2b4bfc4 | 2009-03-16 01:25:52 -0400 | [diff] [blame] | 673 | /* send an iac command with telopt */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 674 | void telnet_send_telopt(telnet_t *telnet, unsigned char cmd, |
Sean Middleditch | 2b4bfc4 | 2009-03-16 01:25:52 -0400 | [diff] [blame] | 675 | unsigned char telopt) { |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 676 | char bytes[3] = { TELNET_IAC, cmd, telopt }; |
Sean Middleditch | 2b4bfc4 | 2009-03-16 01:25:52 -0400 | [diff] [blame] | 677 | _send(telnet, bytes, 3); |
| 678 | } |
| 679 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 680 | /* send negotiation */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 681 | void telnet_send_negotiate(telnet_t *telnet, unsigned char cmd, |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 682 | unsigned char telopt) { |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 683 | telnet_rfc1143_t q; |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 684 | |
| 685 | /* if we're in proxy mode, just send it now */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 686 | if (telnet->flags & TELNET_FLAG_PROXY) { |
| 687 | char bytes[3] = { TELNET_IAC, cmd, telopt }; |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 688 | _send(telnet, bytes, 3); |
| 689 | return; |
| 690 | } |
| 691 | |
| 692 | /* get current option states */ |
| 693 | q = _get_rfc1143(telnet, telopt); |
| 694 | |
| 695 | switch (cmd) { |
| 696 | /* advertise willingess to support an option */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 697 | case TELNET_WILL: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 698 | switch (q.us) { |
| 699 | case RFC1143_NO: |
| 700 | q.us = RFC1143_WANTYES; |
| 701 | _set_rfc1143(telnet, q); |
Sean Middleditch | 1688da1 | 2009-03-19 13:02:09 -0400 | [diff] [blame^] | 702 | telnet_send_telopt(telnet, TELNET_WILL, telopt); |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 703 | break; |
| 704 | case RFC1143_YES: |
| 705 | break; |
| 706 | case RFC1143_WANTNO: |
| 707 | q.us = RFC1143_WANTNO_OP; |
| 708 | _set_rfc1143(telnet, q); |
| 709 | break; |
| 710 | case RFC1143_WANTYES: |
| 711 | break; |
| 712 | case RFC1143_WANTNO_OP: |
| 713 | break; |
| 714 | case RFC1143_WANTYES_OP: |
| 715 | q.us = RFC1143_WANTYES; |
| 716 | _set_rfc1143(telnet, q); |
| 717 | break; |
| 718 | } |
| 719 | break; |
| 720 | |
| 721 | /* force turn-off of locally enabled option */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 722 | case TELNET_WONT: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 723 | switch (q.us) { |
| 724 | case RFC1143_NO: |
| 725 | break; |
| 726 | case RFC1143_YES: |
| 727 | q.us = RFC1143_WANTNO; |
| 728 | _set_rfc1143(telnet, q); |
Sean Middleditch | 1688da1 | 2009-03-19 13:02:09 -0400 | [diff] [blame^] | 729 | telnet_send_telopt(telnet, TELNET_WONT, telopt); |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 730 | break; |
| 731 | case RFC1143_WANTNO: |
| 732 | break; |
| 733 | case RFC1143_WANTYES: |
| 734 | q.us = RFC1143_WANTYES_OP; |
| 735 | _set_rfc1143(telnet, q); |
| 736 | break; |
| 737 | case RFC1143_WANTNO_OP: |
| 738 | q.us = RFC1143_WANTNO; |
| 739 | _set_rfc1143(telnet, q); |
| 740 | break; |
| 741 | case RFC1143_WANTYES_OP: |
| 742 | break; |
| 743 | } |
| 744 | break; |
| 745 | |
| 746 | /* ask remote end to enable an option */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 747 | case TELNET_DO: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 748 | switch (q.him) { |
| 749 | case RFC1143_NO: |
| 750 | q.him = RFC1143_WANTYES; |
| 751 | _set_rfc1143(telnet, q); |
Sean Middleditch | 1688da1 | 2009-03-19 13:02:09 -0400 | [diff] [blame^] | 752 | telnet_send_telopt(telnet, TELNET_DO, telopt); |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 753 | break; |
| 754 | case RFC1143_YES: |
| 755 | break; |
| 756 | case RFC1143_WANTNO: |
| 757 | q.him = RFC1143_WANTNO_OP; |
| 758 | _set_rfc1143(telnet, q); |
| 759 | break; |
| 760 | case RFC1143_WANTYES: |
| 761 | break; |
| 762 | case RFC1143_WANTNO_OP: |
| 763 | break; |
| 764 | case RFC1143_WANTYES_OP: |
| 765 | q.him = RFC1143_WANTYES; |
| 766 | _set_rfc1143(telnet, q); |
| 767 | break; |
| 768 | } |
| 769 | break; |
| 770 | |
| 771 | /* demand remote end disable an option */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 772 | case TELNET_DONT: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 773 | switch (q.him) { |
| 774 | case RFC1143_NO: |
| 775 | break; |
| 776 | case RFC1143_YES: |
| 777 | q.him = RFC1143_WANTNO; |
| 778 | _set_rfc1143(telnet, q); |
Sean Middleditch | 1688da1 | 2009-03-19 13:02:09 -0400 | [diff] [blame^] | 779 | telnet_send_telopt(telnet, TELNET_DONT, telopt); |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 780 | break; |
| 781 | case RFC1143_WANTNO: |
| 782 | break; |
| 783 | case RFC1143_WANTYES: |
| 784 | q.him = RFC1143_WANTYES_OP; |
| 785 | _set_rfc1143(telnet, q); |
| 786 | break; |
| 787 | case RFC1143_WANTNO_OP: |
| 788 | q.him = RFC1143_WANTNO; |
| 789 | _set_rfc1143(telnet, q); |
| 790 | break; |
| 791 | case RFC1143_WANTYES_OP: |
| 792 | break; |
| 793 | } |
| 794 | break; |
| 795 | } |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | /* send non-command data (escapes IAC bytes) */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 799 | void telnet_send_data(telnet_t *telnet, const char *buffer, |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 800 | size_t size) { |
| 801 | size_t i, l; |
Sean Middleditch | c337ba6 | 2009-03-16 16:47:27 -0400 | [diff] [blame] | 802 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 803 | for (l = i = 0; i != size; ++i) { |
| 804 | /* dump prior portion of text, send escaped bytes */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 805 | if (buffer[i] == TELNET_IAC) { |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 806 | /* dump prior text if any */ |
| 807 | if (i != l) |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 808 | _send(telnet, buffer + l, i - l); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 809 | l = i + 1; |
| 810 | |
| 811 | /* send escape */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 812 | telnet_send_command(telnet, TELNET_IAC); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | |
| 816 | /* send whatever portion of buffer is left */ |
| 817 | if (i != l) |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 818 | _send(telnet, buffer + l, i - l); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | /* send sub-request */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 822 | void telnet_send_subnegotiation(telnet_t *telnet, unsigned char telopt, |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 823 | const char *buffer, size_t size) { |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 824 | telnet_send_telopt(telnet, TELNET_SB, telopt); |
| 825 | telnet_send_data(telnet, buffer, size); |
| 826 | telnet_send_command(telnet, TELNET_SE); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 827 | |
| 828 | #ifdef HAVE_ZLIB |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 829 | /* if we're a proxy and we just sent the COMPRESS2 marker, we must |
| 830 | * make sure all further data is compressed if not already. |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 831 | */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 832 | if (telnet->flags & TELNET_FLAG_PROXY && |
| 833 | telopt == TELNET_TELOPT_COMPRESS2) { |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 834 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 835 | if (_init_zlib(telnet, 1, 1) != TELNET_EOK) |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 836 | return; |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 837 | |
| 838 | /* notify app that compression was enabled */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 839 | _event(telnet, TELNET_EV_COMPRESS, 1, 0, 0, 0); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 840 | } |
| 841 | #endif /* HAVE_ZLIB */ |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 842 | } |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 843 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 844 | void telnet_begin_compress2(telnet_t *telnet) { |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 845 | #ifdef HAVE_ZLIB |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 846 | static const char compress2[] = { TELNET_IAC, TELNET_SB, |
| 847 | TELNET_TELOPT_COMPRESS2, TELNET_IAC, TELNET_SE }; |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 848 | |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 849 | /* attempt to create output stream first, bail if we can't */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 850 | if (_init_zlib(telnet, 1, 0) != TELNET_EOK) |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 851 | return; |
| 852 | |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 853 | /* send compression marker. we send directly to the event handler |
| 854 | * instead of passing through _send because _send would result in |
| 855 | * the compress marker itself being compressed. |
| 856 | */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 857 | _event(telnet, TELNET_EV_SEND, 0, 0, compress2, sizeof(compress2)); |
Sean Middleditch | 2b4bfc4 | 2009-03-16 01:25:52 -0400 | [diff] [blame] | 858 | |
| 859 | /* notify app that compression was successfully enabled */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 860 | _event(telnet, TELNET_EV_COMPRESS, 1, 0, 0, 0); |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 861 | #endif /* HAVE_ZLIB */ |
| 862 | } |
Sean Middleditch | d58f49f | 2009-03-16 12:49:35 -0400 | [diff] [blame] | 863 | |
Sean Middleditch | 4a15604 | 2009-03-16 17:10:58 -0400 | [diff] [blame] | 864 | /* send formatted data with \r and \n translation in addition to IAC IAC */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 865 | int telnet_printf(telnet_t *telnet, const char *fmt, ...) { |
Sean Middleditch | 8daf774 | 2009-03-19 02:05:24 -0400 | [diff] [blame] | 866 | static const char CRLF[] = { '\r', '\n' }; |
| 867 | static const char CRNUL[] = { '\r', '\0' }; |
Sean Middleditch | 4a15604 | 2009-03-16 17:10:58 -0400 | [diff] [blame] | 868 | char buffer[4096]; |
| 869 | va_list va; |
| 870 | int rs, i, l; |
| 871 | |
| 872 | /* format */ |
| 873 | va_start(va, fmt); |
| 874 | rs = vsnprintf(buffer, sizeof(buffer), fmt, va); |
| 875 | va_end(va); |
| 876 | |
| 877 | /* send */ |
| 878 | for (l = i = 0; i != rs; ++i) { |
| 879 | /* special characters */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 880 | if (buffer[i] == TELNET_IAC || buffer[i] == '\r' || |
Sean Middleditch | 4a15604 | 2009-03-16 17:10:58 -0400 | [diff] [blame] | 881 | buffer[i] == '\n') { |
| 882 | /* dump prior portion of text */ |
| 883 | if (i != l) |
Sean Middleditch | 8daf774 | 2009-03-19 02:05:24 -0400 | [diff] [blame] | 884 | _send(telnet, (char *)buffer + l, i - l); |
Sean Middleditch | 4a15604 | 2009-03-16 17:10:58 -0400 | [diff] [blame] | 885 | l = i + 1; |
| 886 | |
| 887 | /* IAC -> IAC IAC */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 888 | if (buffer[i] == TELNET_IAC) |
| 889 | telnet_send_command(telnet, TELNET_IAC); |
Sean Middleditch | 4a15604 | 2009-03-16 17:10:58 -0400 | [diff] [blame] | 890 | /* automatic translation of \r -> CRNUL */ |
| 891 | else if (buffer[i] == '\r') |
| 892 | _send(telnet, CRNUL, 2); |
| 893 | /* automatic translation of \n -> CRLF */ |
| 894 | else if (buffer[i] == '\n') |
| 895 | _send(telnet, CRLF, 2); |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | /* send whatever portion of buffer is left */ |
| 900 | if (i != l) |
Sean Middleditch | 8daf774 | 2009-03-19 02:05:24 -0400 | [diff] [blame] | 901 | _send(telnet, (char *)buffer + l, i - l); |
Sean Middleditch | 4a15604 | 2009-03-16 17:10:58 -0400 | [diff] [blame] | 902 | |
| 903 | return rs; |
| 904 | } |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 905 | |
| 906 | /* send formatted data through telnet_send_data */ |
| 907 | int telnet_printf2(telnet_t *telnet, const char *fmt, ...) { |
| 908 | char buffer[4096]; |
| 909 | va_list va; |
| 910 | int rs; |
| 911 | |
| 912 | /* format */ |
| 913 | va_start(va, fmt); |
| 914 | rs = vsnprintf(buffer, sizeof(buffer), fmt, va); |
| 915 | va_end(va); |
| 916 | |
| 917 | /* send */ |
| 918 | telnet_send_data(telnet, (char *)buffer, rs); |
| 919 | |
| 920 | return rs; |
| 921 | } |