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