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