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