Sean Middleditch | 6aef073 | 2009-03-12 23:27:35 -0400 | [diff] [blame] | 1 | /* |
Sean Middleditch | ae39cee | 2009-03-22 22:47:30 -0400 | [diff] [blame] | 2 | * libtelnet 0.9 |
| 3 | * |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 4 | * Sean Middleditch |
| 5 | * sean@sourcemud.org |
| 6 | * |
Sean Middleditch | 6aef073 | 2009-03-12 23:27:35 -0400 | [diff] [blame] | 7 | * The author or authors of this code dedicate any and all copyright interest |
| 8 | * in this code to the public domain. We make this dedication for the benefit |
| 9 | * of the public at large and to the detriment of our heirs and successors. We |
| 10 | * intend this dedication to be an overt act of relinquishment in perpetuity of |
| 11 | * all present and future rights to this code under copyright law. |
| 12 | */ |
| 13 | |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 14 | #include <malloc.h> |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 15 | #include <string.h> |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 16 | #include <stdio.h> |
| 17 | #include <errno.h> |
| 18 | #include <string.h> |
| 19 | #include <stdarg.h> |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 20 | |
Sean Middleditch | 2d5c499 | 2009-03-22 22:21:42 -0400 | [diff] [blame] | 21 | #ifdef HAVE_ALLOCA |
| 22 | #include <alloca.h> |
| 23 | #endif |
| 24 | |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 25 | #ifdef HAVE_ZLIB |
Sean Middleditch | 2d5c499 | 2009-03-22 22:21:42 -0400 | [diff] [blame] | 26 | #include <zlib.h> |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 27 | #endif |
| 28 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 29 | #include "libtelnet.h" |
| 30 | |
Sean Middleditch | 9b07e92 | 2009-03-19 18:18:44 -0400 | [diff] [blame] | 31 | /* inlinable functions */ |
| 32 | #if __GNUC__ || __STDC_VERSION__ >= 199901L |
Sean Middleditch | 53a8661 | 2009-03-22 17:43:29 -0400 | [diff] [blame] | 33 | # define INLINE __inline__ |
Sean Middleditch | 9b07e92 | 2009-03-19 18:18:44 -0400 | [diff] [blame] | 34 | #else |
| 35 | # define INLINE |
| 36 | #endif |
| 37 | |
Sean Middleditch | d2466a0 | 2009-09-19 14:35:48 -0700 | [diff] [blame^] | 38 | /* telnet state */ |
| 39 | struct telnet_t { |
| 40 | /* user data */ |
| 41 | void *ud; |
| 42 | /* telopt support table */ |
| 43 | const telnet_telopt_t *telopts; |
| 44 | /* event handler */ |
| 45 | telnet_event_handler_t eh; |
| 46 | #ifdef HAVE_ZLIB |
| 47 | /* zlib (mccp2) compression */ |
| 48 | z_stream *z; |
| 49 | #endif |
| 50 | /* RFC1143 option negotiation states */ |
| 51 | struct telnet_rfc1143_t *q; |
| 52 | /* sub-request buffer */ |
| 53 | char *buffer; |
| 54 | /* current size of the buffer */ |
| 55 | size_t buffer_size; |
| 56 | /* current buffer write position (also length of buffer data) */ |
| 57 | size_t buffer_pos; |
| 58 | /* current state */ |
| 59 | enum telnet_state_t state; |
| 60 | /* option flags */ |
| 61 | unsigned char flags; |
| 62 | /* current subnegotiation telopt */ |
| 63 | unsigned char sb_telopt; |
| 64 | /* length of RFC1143 queue */ |
| 65 | unsigned char q_size; |
| 66 | }; |
| 67 | |
Sean Middleditch | 90e79da | 2009-03-19 15:17:13 -0400 | [diff] [blame] | 68 | /* RFC1143 option negotiation state */ |
| 69 | typedef struct telnet_rfc1143_t { |
| 70 | unsigned char telopt; |
| 71 | char us:4, him:4; |
| 72 | } telnet_rfc1143_t; |
| 73 | |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 74 | /* RFC1143 state names */ |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 75 | #define Q_NO 0 |
| 76 | #define Q_YES 1 |
| 77 | #define Q_WANTNO 2 |
| 78 | #define Q_WANTYES 3 |
| 79 | #define Q_WANTNO_OP 4 |
| 80 | #define Q_WANTYES_OP 5 |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 81 | |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 82 | /* buffer sizes */ |
Sean Middleditch | 8c11179 | 2009-03-22 22:44:38 -0400 | [diff] [blame] | 83 | static const size_t _buffer_sizes[] = { 0, 512, 2048, 8192, 16384, }; |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 84 | static const size_t _buffer_sizes_count = sizeof(_buffer_sizes) / |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 85 | sizeof(_buffer_sizes[0]); |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 86 | |
Sean Middleditch | 34bb099 | 2009-03-21 00:20:44 -0400 | [diff] [blame] | 87 | /* event dispatch helper */ |
| 88 | static INLINE void _event(telnet_t *telnet, telnet_event_type_t type, |
Sean Middleditch | 97a8cb2 | 2009-03-16 16:51:41 -0400 | [diff] [blame] | 89 | unsigned char command, unsigned char telopt, |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 90 | const char *buffer, size_t size, const char **argv, size_t argc) { |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 91 | telnet_event_t ev; |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 92 | ev.argv = argv; |
| 93 | ev.argc = argc; |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 94 | ev.buffer = buffer; |
| 95 | ev.size = size; |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 96 | ev.type = type; |
| 97 | ev.command = command; |
| 98 | ev.telopt = telopt; |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 99 | |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 100 | telnet->eh(telnet, &ev, telnet->ud); |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 101 | } |
| 102 | |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 103 | /* error generation function */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 104 | static telnet_error_t _error(telnet_t *telnet, unsigned line, |
| 105 | const char* func, telnet_error_t err, int fatal, const char *fmt, |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 106 | ...) { |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 107 | char buffer[512]; |
| 108 | va_list va; |
| 109 | |
| 110 | /* format error intro */ |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 111 | snprintf(buffer, sizeof(buffer), "%s:%u in %s: ", __FILE__, line, func); |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 112 | |
Sean Middleditch | b43c10c | 2009-03-20 23:21:02 -0400 | [diff] [blame] | 113 | /* format informational text */ |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 114 | va_start(va, fmt); |
| 115 | vsnprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), |
| 116 | fmt, va); |
| 117 | va_end(va); |
| 118 | |
Sean Middleditch | b43c10c | 2009-03-20 23:21:02 -0400 | [diff] [blame] | 119 | /* send error event to the user */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 120 | _event(telnet, fatal ? TELNET_EV_ERROR : TELNET_EV_WARNING, err, |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 121 | 0, buffer, strlen(buffer), 0, 0); |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 122 | |
| 123 | return err; |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 124 | } |
| 125 | |
Sean Middleditch | e5b4759 | 2009-03-16 17:37:43 -0400 | [diff] [blame] | 126 | #ifdef HAVE_ZLIB |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 127 | /* initialize the zlib box for a telnet box; if deflate is non-zero, it |
| 128 | * initializes zlib for delating (compression), otherwise for inflating |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 129 | * (decompression). returns TELNET_EOK on success, something else on |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 130 | * failure. |
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_error_t _init_zlib(telnet_t *telnet, int deflate, int err_fatal) { |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 133 | z_stream *z; |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 134 | int rs; |
| 135 | |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 136 | /* if compression is already enabled, fail loudly */ |
| 137 | if (telnet->z != 0) |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 138 | return _error(telnet, __LINE__, __func__, TELNET_EBADVAL, |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 139 | err_fatal, "cannot initialize compression twice"); |
| 140 | |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 141 | /* allocate zstream box */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 142 | if ((z= (z_stream *)calloc(1, sizeof(z_stream))) == 0) |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 143 | return _error(telnet, __LINE__, __func__, TELNET_ENOMEM, err_fatal, |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 144 | "malloc() failed: %s", strerror(errno)); |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 145 | |
| 146 | /* initialize */ |
| 147 | if (deflate) { |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 148 | if ((rs = deflateInit(z, Z_DEFAULT_COMPRESSION)) != Z_OK) { |
| 149 | free(z); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 150 | return _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS, |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 151 | err_fatal, "deflateInit() failed: %s", zError(rs)); |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 152 | } |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 153 | telnet->flags |= TELNET_PFLAG_DEFLATE; |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 154 | } else { |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 155 | if ((rs = inflateInit(z)) != Z_OK) { |
| 156 | free(z); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 157 | return _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS, |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 158 | err_fatal, "inflateInit() failed: %s", zError(rs)); |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 159 | } |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 160 | telnet->flags &= ~TELNET_PFLAG_DEFLATE; |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 161 | } |
| 162 | |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 163 | telnet->z = z; |
| 164 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 165 | return TELNET_EOK; |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 166 | } |
Sean Middleditch | e5b4759 | 2009-03-16 17:37:43 -0400 | [diff] [blame] | 167 | #endif |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 168 | |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 169 | /* push bytes out, compressing them first if need be */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 170 | static void _send(telnet_t *telnet, const char *buffer, |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 171 | size_t size) { |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 172 | #ifdef HAVE_ZLIB |
| 173 | /* if we have a deflate (compression) zlib box, use it */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 174 | if (telnet->z != 0 && telnet->flags & TELNET_PFLAG_DEFLATE) { |
Sean Middleditch | 8daf774 | 2009-03-19 02:05:24 -0400 | [diff] [blame] | 175 | char deflate_buffer[1024]; |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 176 | int rs; |
| 177 | |
| 178 | /* initialize z state */ |
Sean Middleditch | 97a8cb2 | 2009-03-16 16:51:41 -0400 | [diff] [blame] | 179 | telnet->z->next_in = (unsigned char *)buffer; |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 180 | telnet->z->avail_in = size; |
Sean Middleditch | 8daf774 | 2009-03-19 02:05:24 -0400 | [diff] [blame] | 181 | telnet->z->next_out = (unsigned char *)deflate_buffer; |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 182 | telnet->z->avail_out = sizeof(deflate_buffer); |
| 183 | |
| 184 | /* deflate until buffer exhausted and all output is produced */ |
| 185 | while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) { |
| 186 | /* compress */ |
| 187 | if ((rs = deflate(telnet->z, Z_SYNC_FLUSH)) != Z_OK) { |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 188 | _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS, 1, |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 189 | "deflate() failed: %s", zError(rs)); |
| 190 | deflateEnd(telnet->z); |
| 191 | free(telnet->z); |
| 192 | telnet->z = 0; |
| 193 | break; |
| 194 | } |
| 195 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 196 | _event(telnet, TELNET_EV_SEND, 0, 0, deflate_buffer, |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 197 | sizeof(deflate_buffer) - telnet->z->avail_out, 0, 0); |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 198 | |
| 199 | /* prepare output buffer for next run */ |
Sean Middleditch | 8daf774 | 2009-03-19 02:05:24 -0400 | [diff] [blame] | 200 | telnet->z->next_out = (unsigned char *)deflate_buffer; |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 201 | telnet->z->avail_out = sizeof(deflate_buffer); |
| 202 | } |
| 203 | |
| 204 | /* COMPRESS2 is not negotiated, just send */ |
| 205 | } else |
| 206 | #endif /* HAVE_ZLIB */ |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 207 | _event(telnet, TELNET_EV_SEND, 0, 0, buffer, size, 0, 0); |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 208 | } |
| 209 | |
Sean Middleditch | 34bb099 | 2009-03-21 00:20:44 -0400 | [diff] [blame] | 210 | /* check if we support a particular telopt; if us is non-zero, we |
| 211 | * check if we (local) supports it, otherwise we check if he (remote) |
| 212 | * supports it. return non-zero if supported, zero if not supported. |
| 213 | */ |
| 214 | static INLINE int _check_telopt(telnet_t *telnet, unsigned char telopt, |
| 215 | int us) { |
| 216 | int i; |
| 217 | |
| 218 | /* if we have no telopts table, we obviously don't support it */ |
| 219 | if (telnet->telopts == 0) |
| 220 | return 0; |
| 221 | |
| 222 | /* loop unti found or end marker (us and him both 0) */ |
Sean Middleditch | bfc641e | 2009-03-22 16:26:06 -0400 | [diff] [blame] | 223 | for (i = 0; telnet->telopts[i].telopt != -1; ++i) { |
| 224 | if (telnet->telopts[i].telopt == telopt) { |
| 225 | if (us && telnet->telopts[i].us == TELNET_WILL) |
| 226 | return 1; |
| 227 | else if (!us && telnet->telopts[i].him == TELNET_DO) |
| 228 | return 1; |
| 229 | else |
| 230 | return 0; |
| 231 | } |
| 232 | } |
Sean Middleditch | 34bb099 | 2009-03-21 00:20:44 -0400 | [diff] [blame] | 233 | |
| 234 | /* not found, so not supported */ |
| 235 | return 0; |
| 236 | } |
| 237 | |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 238 | /* retrieve RFC1143 option state */ |
Sean Middleditch | 9b07e92 | 2009-03-19 18:18:44 -0400 | [diff] [blame] | 239 | static INLINE telnet_rfc1143_t _get_rfc1143(telnet_t *telnet, |
| 240 | unsigned char telopt) { |
Sean Middleditch | 4987f9f | 2009-03-19 12:51:40 -0400 | [diff] [blame] | 241 | const telnet_rfc1143_t empty = { telopt, 0, 0}; |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 242 | int i; |
| 243 | |
| 244 | /* search for entry */ |
| 245 | for (i = 0; i != telnet->q_size; ++i) |
| 246 | if (telnet->q[i].telopt == telopt) |
| 247 | return telnet->q[i]; |
| 248 | |
| 249 | /* not found, return empty value */ |
| 250 | return empty; |
| 251 | } |
| 252 | |
| 253 | /* save RFC1143 option state */ |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 254 | static INLINE void _set_rfc1143(telnet_t *telnet, unsigned char telopt, |
| 255 | char us, char him) { |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 256 | telnet_rfc1143_t *qtmp; |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 257 | int i; |
| 258 | |
| 259 | /* search for entry */ |
| 260 | for (i = 0; i != telnet->q_size; ++i) { |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 261 | if (telnet->q[i].telopt == telopt) { |
| 262 | telnet->q[i].us = us; |
| 263 | telnet->q[i].him = him; |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 264 | return; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /* we're going to need to track state for it, so grow the queue |
Sean Middleditch | 1e80f52 | 2009-03-22 18:24:18 -0400 | [diff] [blame] | 269 | * by 4 (four) elements and put the telopt into it; bail on allocation |
| 270 | * error. we go by four because it seems like a reasonable guess as |
| 271 | * to the number of enabled options for most simple code, and it |
| 272 | * allows for an acceptable number of reallocations for complex code. |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 273 | */ |
Sean Middleditch | a63ff7f | 2009-04-03 01:27:06 -0400 | [diff] [blame] | 274 | if ((qtmp = (telnet_rfc1143_t *)realloc(telnet->q, |
| 275 | sizeof(telnet_rfc1143_t) * (telnet->q_size + 4))) == 0) { |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 276 | _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0, |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 277 | "malloc() failed: %s", strerror(errno)); |
| 278 | return; |
| 279 | } |
Sean Middleditch | 2d5c499 | 2009-03-22 22:21:42 -0400 | [diff] [blame] | 280 | memset(&qtmp[telnet->q_size], 0, sizeof(telnet_rfc1143_t) * 4); |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 281 | telnet->q = qtmp; |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 282 | telnet->q[telnet->q_size].telopt = telopt; |
| 283 | telnet->q[telnet->q_size].us = us; |
| 284 | telnet->q[telnet->q_size].him = him; |
Sean Middleditch | 2d5c499 | 2009-03-22 22:21:42 -0400 | [diff] [blame] | 285 | telnet->q_size += 4; |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 286 | } |
| 287 | |
Sean Middleditch | 90e79da | 2009-03-19 15:17:13 -0400 | [diff] [blame] | 288 | /* send negotiation bytes */ |
Sean Middleditch | 9b07e92 | 2009-03-19 18:18:44 -0400 | [diff] [blame] | 289 | static INLINE void _send_negotiate(telnet_t *telnet, unsigned char cmd, |
Sean Middleditch | 90e79da | 2009-03-19 15:17:13 -0400 | [diff] [blame] | 290 | unsigned char telopt) { |
| 291 | char bytes[3] = { TELNET_IAC, cmd, telopt }; |
| 292 | _send(telnet, bytes, 3); |
| 293 | } |
| 294 | |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 295 | /* negotiation handling magic for RFC1143 */ |
Sean Middleditch | 330c261 | 2009-03-20 23:29:17 -0400 | [diff] [blame] | 296 | static void _negotiate(telnet_t *telnet, unsigned char telopt) { |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 297 | telnet_rfc1143_t q; |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 298 | |
| 299 | /* in PROXY mode, just pass it thru and do nothing */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 300 | if (telnet->flags & TELNET_FLAG_PROXY) { |
Sean Middleditch | 330c261 | 2009-03-20 23:29:17 -0400 | [diff] [blame] | 301 | switch ((int)telnet->state) { |
| 302 | case TELNET_STATE_WILL: |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 303 | _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 304 | break; |
Sean Middleditch | 330c261 | 2009-03-20 23:29:17 -0400 | [diff] [blame] | 305 | case TELNET_STATE_WONT: |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 306 | _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 307 | break; |
Sean Middleditch | 330c261 | 2009-03-20 23:29:17 -0400 | [diff] [blame] | 308 | case TELNET_STATE_DO: |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 309 | _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 310 | break; |
Sean Middleditch | 330c261 | 2009-03-20 23:29:17 -0400 | [diff] [blame] | 311 | case TELNET_STATE_DONT: |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 312 | _event(telnet, TELNET_EV_DONT, 0, telopt, 0, 0, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 313 | break; |
| 314 | } |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | /* lookup the current state of the option */ |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 319 | q = _get_rfc1143(telnet, telopt); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 320 | |
| 321 | /* start processing... */ |
Sean Middleditch | 330c261 | 2009-03-20 23:29:17 -0400 | [diff] [blame] | 322 | switch ((int)telnet->state) { |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 323 | /* request to enable option on remote end or confirm DO */ |
Sean Middleditch | 330c261 | 2009-03-20 23:29:17 -0400 | [diff] [blame] | 324 | case TELNET_STATE_WILL: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 325 | switch (q.him) { |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 326 | case Q_NO: |
Sean Middleditch | 34bb099 | 2009-03-21 00:20:44 -0400 | [diff] [blame] | 327 | if (_check_telopt(telnet, telopt, 0)) { |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 328 | _set_rfc1143(telnet, telopt, q.us, Q_YES); |
Sean Middleditch | 90e79da | 2009-03-19 15:17:13 -0400 | [diff] [blame] | 329 | _send_negotiate(telnet, TELNET_DO, telopt); |
Sean Middleditch | 94738a4 | 2009-03-22 16:33:22 -0400 | [diff] [blame] | 330 | _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 331 | } else |
Sean Middleditch | 90e79da | 2009-03-19 15:17:13 -0400 | [diff] [blame] | 332 | _send_negotiate(telnet, TELNET_DONT, telopt); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 333 | break; |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 334 | case Q_WANTNO: |
| 335 | _set_rfc1143(telnet, telopt, q.us, Q_NO); |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 336 | _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0, 0, 0); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 337 | _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0, |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 338 | "DONT answered by WILL"); |
| 339 | break; |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 340 | case Q_WANTNO_OP: |
| 341 | _set_rfc1143(telnet, telopt, q.us, Q_YES); |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 342 | _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0, 0, 0); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 343 | _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0, |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 344 | "DONT answered by WILL"); |
| 345 | break; |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 346 | case Q_WANTYES: |
| 347 | _set_rfc1143(telnet, telopt, q.us, Q_YES); |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 348 | _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 349 | break; |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 350 | case Q_WANTYES_OP: |
| 351 | _set_rfc1143(telnet, telopt, q.us, Q_WANTNO); |
Sean Middleditch | 90e79da | 2009-03-19 15:17:13 -0400 | [diff] [blame] | 352 | _send_negotiate(telnet, TELNET_DONT, telopt); |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 353 | _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 354 | break; |
| 355 | } |
| 356 | break; |
| 357 | |
| 358 | /* request to disable option on remote end, confirm DONT, reject DO */ |
Sean Middleditch | 330c261 | 2009-03-20 23:29:17 -0400 | [diff] [blame] | 359 | case TELNET_STATE_WONT: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 360 | switch (q.him) { |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 361 | case Q_YES: |
| 362 | _set_rfc1143(telnet, telopt, q.us, Q_NO); |
Sean Middleditch | 90e79da | 2009-03-19 15:17:13 -0400 | [diff] [blame] | 363 | _send_negotiate(telnet, TELNET_DONT, telopt); |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 364 | _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0, 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: |
| 367 | _set_rfc1143(telnet, telopt, q.us, Q_NO); |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 368 | _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0, 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_WANTNO_OP: |
| 371 | _set_rfc1143(telnet, telopt, q.us, Q_WANTYES); |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 372 | _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 373 | break; |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 374 | case Q_WANTYES: |
| 375 | case Q_WANTYES_OP: |
| 376 | _set_rfc1143(telnet, telopt, q.us, Q_NO); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 377 | break; |
| 378 | } |
| 379 | break; |
| 380 | |
| 381 | /* request to enable option on local end or confirm WILL */ |
Sean Middleditch | 330c261 | 2009-03-20 23:29:17 -0400 | [diff] [blame] | 382 | case TELNET_STATE_DO: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 383 | switch (q.us) { |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 384 | case Q_NO: |
Sean Middleditch | 34bb099 | 2009-03-21 00:20:44 -0400 | [diff] [blame] | 385 | if (_check_telopt(telnet, telopt, 1)) { |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 386 | _set_rfc1143(telnet, telopt, Q_YES, q.him); |
Sean Middleditch | 90e79da | 2009-03-19 15:17:13 -0400 | [diff] [blame] | 387 | _send_negotiate(telnet, TELNET_WILL, telopt); |
Sean Middleditch | 94738a4 | 2009-03-22 16:33:22 -0400 | [diff] [blame] | 388 | _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 389 | } else |
Sean Middleditch | 90e79da | 2009-03-19 15:17:13 -0400 | [diff] [blame] | 390 | _send_negotiate(telnet, TELNET_WONT, telopt); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 391 | break; |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 392 | case Q_WANTNO: |
| 393 | _set_rfc1143(telnet, telopt, Q_NO, q.him); |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 394 | _event(telnet, TELNET_EV_DONT, 0, telopt, 0, 0, 0, 0); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 395 | _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0, |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 396 | "WONT answered by DO"); |
| 397 | break; |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 398 | case Q_WANTNO_OP: |
| 399 | _set_rfc1143(telnet, telopt, Q_YES, q.him); |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 400 | _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0, 0, 0); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 401 | _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0, |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 402 | "WONT answered by DO"); |
| 403 | break; |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 404 | case Q_WANTYES: |
| 405 | _set_rfc1143(telnet, telopt, Q_YES, q.him); |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 406 | _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 407 | break; |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 408 | case Q_WANTYES_OP: |
| 409 | _set_rfc1143(telnet, telopt, Q_WANTNO, q.him); |
Sean Middleditch | 90e79da | 2009-03-19 15:17:13 -0400 | [diff] [blame] | 410 | _send_negotiate(telnet, TELNET_WONT, telopt); |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 411 | _event(telnet, TELNET_EV_DO, 0, telopt, 0, 0, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 412 | break; |
| 413 | } |
| 414 | break; |
| 415 | |
| 416 | /* request to disable option on local end, confirm WONT, reject WILL */ |
Sean Middleditch | 330c261 | 2009-03-20 23:29:17 -0400 | [diff] [blame] | 417 | case TELNET_STATE_DONT: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 418 | switch (q.us) { |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 419 | case Q_YES: |
| 420 | _set_rfc1143(telnet, telopt, Q_NO, q.him); |
Sean Middleditch | 90e79da | 2009-03-19 15:17:13 -0400 | [diff] [blame] | 421 | _send_negotiate(telnet, TELNET_WONT, telopt); |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 422 | _event(telnet, TELNET_EV_DONT, 0, telopt, 0, 0, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 423 | break; |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 424 | case Q_WANTNO: |
| 425 | _set_rfc1143(telnet, telopt, Q_NO, q.him); |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 426 | _event(telnet, TELNET_EV_WONT, 0, telopt, 0, 0, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 427 | break; |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 428 | case Q_WANTNO_OP: |
| 429 | _set_rfc1143(telnet, telopt, Q_WANTYES, q.him); |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 430 | _event(telnet, TELNET_EV_WILL, 0, telopt, 0, 0, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 431 | break; |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 432 | case Q_WANTYES: |
| 433 | case Q_WANTYES_OP: |
| 434 | _set_rfc1143(telnet, telopt, Q_NO, q.him); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 435 | break; |
| 436 | } |
| 437 | break; |
| 438 | } |
| 439 | } |
| 440 | |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 441 | /* process a subnegotiation buffer; return non-zero if the current buffer |
| 442 | * must be aborted and reprocessed due to COMPRESS2 being activated |
| 443 | */ |
| 444 | static int _subnegotiate(telnet_t *telnet) { |
Sean Middleditch | b10946c | 2009-03-22 18:21:14 -0400 | [diff] [blame] | 445 | switch (telnet->sb_telopt) { |
| 446 | #ifdef HAVE_ZLIB |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 447 | /* received COMPRESS2 begin marker, setup our zlib box and |
| 448 | * start handling the compressed stream if it's not already. |
| 449 | */ |
Sean Middleditch | b10946c | 2009-03-22 18:21:14 -0400 | [diff] [blame] | 450 | case TELNET_TELOPT_COMPRESS2: |
| 451 | if (telnet->sb_telopt == TELNET_TELOPT_COMPRESS2) { |
| 452 | if (_init_zlib(telnet, 0, 1) != TELNET_EOK) |
Sean Middleditch | 8c11179 | 2009-03-22 22:44:38 -0400 | [diff] [blame] | 453 | return 0; |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 454 | |
Sean Middleditch | b10946c | 2009-03-22 18:21:14 -0400 | [diff] [blame] | 455 | /* standard SB notification */ |
| 456 | _event(telnet, TELNET_EV_SUBNEGOTIATION, 0, telnet->sb_telopt, |
| 457 | telnet->buffer, telnet->buffer_pos, 0, 0); |
| 458 | |
| 459 | /* notify app that compression was enabled */ |
| 460 | _event(telnet, TELNET_EV_COMPRESS, 1, 0, 0, 0, 0, 0); |
| 461 | return 1; |
| 462 | } |
Sean Middleditch | 8c11179 | 2009-03-22 22:44:38 -0400 | [diff] [blame] | 463 | return 0; |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 464 | #endif /* HAVE_ZLIB */ |
Sean Middleditch | 2d5c499 | 2009-03-22 22:21:42 -0400 | [diff] [blame] | 465 | #ifdef HAVE_ALLOCA |
Sean Middleditch | 8c11179 | 2009-03-22 22:44:38 -0400 | [diff] [blame] | 466 | |
Sean Middleditch | b10946c | 2009-03-22 18:21:14 -0400 | [diff] [blame] | 467 | /* ZMP command */ |
Sean Middleditch | 8c11179 | 2009-03-22 22:44:38 -0400 | [diff] [blame] | 468 | case TELNET_TELOPT_ZMP: { |
| 469 | const char **argv, *c; |
| 470 | size_t i, argc; |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 471 | /* make sure this is a valid ZMP buffer */ |
Sean Middleditch | b10946c | 2009-03-22 18:21:14 -0400 | [diff] [blame] | 472 | if (telnet->buffer_pos == 0 || |
| 473 | telnet->buffer[telnet->buffer_pos - 1] != 0) { |
| 474 | _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0, |
| 475 | "incomplete ZMP frame"); |
| 476 | _event(telnet, TELNET_EV_SUBNEGOTIATION, 0, telnet->sb_telopt, |
| 477 | telnet->buffer, telnet->buffer_pos, 0, 0); |
Sean Middleditch | 8c11179 | 2009-03-22 22:44:38 -0400 | [diff] [blame] | 478 | return 0; |
Sean Middleditch | b10946c | 2009-03-22 18:21:14 -0400 | [diff] [blame] | 479 | } |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 480 | |
| 481 | /* count arguments */ |
Sean Middleditch | b10946c | 2009-03-22 18:21:14 -0400 | [diff] [blame] | 482 | for (argc = 0, c = telnet->buffer; c != telnet->buffer + |
| 483 | telnet->buffer_pos; ++argc) |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 484 | c += strlen(c) + 1; |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 485 | |
| 486 | /* allocate argument array, bail on error */ |
Sean Middleditch | 2d5c499 | 2009-03-22 22:21:42 -0400 | [diff] [blame] | 487 | if ((argv = (const char **)alloca(sizeof(char *) * argc)) == 0) { |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 488 | _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0, |
Sean Middleditch | 2d5c499 | 2009-03-22 22:21:42 -0400 | [diff] [blame] | 489 | "alloca() failed: %s", strerror(errno)); |
Sean Middleditch | b10946c | 2009-03-22 18:21:14 -0400 | [diff] [blame] | 490 | _event(telnet, TELNET_EV_SUBNEGOTIATION, 0, telnet->sb_telopt, |
| 491 | telnet->buffer, telnet->buffer_pos, 0, 0); |
Sean Middleditch | 8c11179 | 2009-03-22 22:44:38 -0400 | [diff] [blame] | 492 | return 0; |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | /* populate argument array */ |
| 496 | for (i = 0, c = telnet->buffer; i != argc; ++i) { |
| 497 | argv[i] = c; |
| 498 | c += strlen(c) + 1; |
| 499 | } |
| 500 | |
Sean Middleditch | b10946c | 2009-03-22 18:21:14 -0400 | [diff] [blame] | 501 | /* invoke event with our arguments */ |
| 502 | _event(telnet, TELNET_EV_SUBNEGOTIATION, 0, telnet->sb_telopt, |
| 503 | telnet->buffer, telnet->buffer_pos, argv, argc); |
Sean Middleditch | 8c11179 | 2009-03-22 22:44:38 -0400 | [diff] [blame] | 504 | return 0; |
| 505 | } |
| 506 | |
Sean Middleditch | 2d5c499 | 2009-03-22 22:21:42 -0400 | [diff] [blame] | 507 | /* any of a number of commands that use the form <BYTE>data<BYTE>data, |
| 508 | * including TTYPE, ENVIRON, NEW-ENVIRON, and MSSP |
| 509 | */ |
| 510 | case TELNET_TELOPT_TTYPE: |
| 511 | case TELNET_TELOPT_ENVIRON: |
| 512 | case TELNET_TELOPT_NEW_ENVIRON: |
Sean Middleditch | 8c11179 | 2009-03-22 22:44:38 -0400 | [diff] [blame] | 513 | case TELNET_TELOPT_MSSP: { |
| 514 | char **argv, *c, *l; |
| 515 | size_t i, argc; |
| 516 | |
Sean Middleditch | 2d5c499 | 2009-03-22 22:21:42 -0400 | [diff] [blame] | 517 | /* if we have no data, just pass it through */ |
| 518 | if (telnet->buffer_pos == 0) { |
| 519 | _event(telnet, TELNET_EV_SUBNEGOTIATION, 0, telnet->sb_telopt, |
| 520 | telnet->buffer, telnet->buffer_pos, 0, 0); |
Sean Middleditch | 8c11179 | 2009-03-22 22:44:38 -0400 | [diff] [blame] | 521 | return 0; |
Sean Middleditch | 2d5c499 | 2009-03-22 22:21:42 -0400 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | /* very first byte must be in range 0-3 */ |
| 525 | if ((unsigned)telnet->buffer[0] > 3) { |
| 526 | _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0, |
| 527 | "telopt %d subneg has invalid data", telnet->sb_telopt); |
| 528 | _event(telnet, TELNET_EV_SUBNEGOTIATION, 0, telnet->sb_telopt, |
| 529 | telnet->buffer, telnet->buffer_pos, 0, 0); |
Sean Middleditch | 8c11179 | 2009-03-22 22:44:38 -0400 | [diff] [blame] | 530 | return 0; |
Sean Middleditch | 2d5c499 | 2009-03-22 22:21:42 -0400 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /* count arguments; each argument is preceded by a byte in the |
| 534 | * range 0-3, so just count those. |
| 535 | * NOTE: we don't support the ENVIRON/NEW-ENVIRON ESC handling |
| 536 | * properly at all. guess that's a FIXME. |
| 537 | */ |
| 538 | for (argc = 0, i = 0; i != telnet->buffer_pos; ++i) |
| 539 | if ((unsigned)telnet->buffer[i] <= 3) |
| 540 | ++argc; |
| 541 | |
| 542 | /* allocate argument array, bail on error */ |
Sean Middleditch | 8c11179 | 2009-03-22 22:44:38 -0400 | [diff] [blame] | 543 | if ((argv = (char **)alloca(sizeof(char *) * argc)) == 0) { |
Sean Middleditch | 2d5c499 | 2009-03-22 22:21:42 -0400 | [diff] [blame] | 544 | _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0, |
| 545 | "alloca() failed: %s", strerror(errno)); |
| 546 | _event(telnet, TELNET_EV_SUBNEGOTIATION, 0, telnet->sb_telopt, |
| 547 | telnet->buffer, telnet->buffer_pos, 0, 0); |
Sean Middleditch | 8c11179 | 2009-03-22 22:44:38 -0400 | [diff] [blame] | 548 | return 0; |
Sean Middleditch | 2d5c499 | 2009-03-22 22:21:42 -0400 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | /* allocate strings in argument array */ |
| 552 | for (i = 0, l = telnet->buffer; i != argc; ++i) { |
Sean Middleditch | 8c11179 | 2009-03-22 22:44:38 -0400 | [diff] [blame] | 553 | /* search for end marker */ |
Sean Middleditch | 2d5c499 | 2009-03-22 22:21:42 -0400 | [diff] [blame] | 554 | c = l + 1; |
| 555 | while (c != telnet->buffer + telnet->buffer_pos && |
| 556 | (unsigned)*c > 3) |
| 557 | ++c; |
Sean Middleditch | 2d5c499 | 2009-03-22 22:21:42 -0400 | [diff] [blame] | 558 | |
Sean Middleditch | 8c11179 | 2009-03-22 22:44:38 -0400 | [diff] [blame] | 559 | /* allocate space; bail on error */ |
| 560 | if ((argv[i] = (char *)alloca(c - l + 1)) == 0) { |
| 561 | _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0, |
| 562 | "alloca() failed: %s", strerror(errno)); |
| 563 | _event(telnet, TELNET_EV_SUBNEGOTIATION, 0, telnet->sb_telopt, |
| 564 | telnet->buffer, telnet->buffer_pos, 0, 0); |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | /* copy data */ |
| 569 | memcpy(argv[i], l, c - l); |
| 570 | argv[i][c - l] = 0; |
| 571 | |
| 572 | /* prepare for next loop */ |
Sean Middleditch | 2d5c499 | 2009-03-22 22:21:42 -0400 | [diff] [blame] | 573 | l = c; |
| 574 | } |
| 575 | |
| 576 | /* invoke event with our arguments */ |
| 577 | _event(telnet, TELNET_EV_SUBNEGOTIATION, 0, telnet->sb_telopt, |
Sean Middleditch | 8c11179 | 2009-03-22 22:44:38 -0400 | [diff] [blame] | 578 | telnet->buffer, telnet->buffer_pos, (const char **)argv, argc); |
| 579 | return 0; |
| 580 | } |
Sean Middleditch | 2d5c499 | 2009-03-22 22:21:42 -0400 | [diff] [blame] | 581 | #endif /* HAVE_ALLOCA */ |
Sean Middleditch | 8c11179 | 2009-03-22 22:44:38 -0400 | [diff] [blame] | 582 | |
Sean Middleditch | b10946c | 2009-03-22 18:21:14 -0400 | [diff] [blame] | 583 | /* other generic subnegotiation */ |
| 584 | default: |
| 585 | _event(telnet, TELNET_EV_SUBNEGOTIATION, 0, telnet->sb_telopt, |
| 586 | telnet->buffer, telnet->buffer_pos, 0, 0); |
Sean Middleditch | 8c11179 | 2009-03-22 22:44:38 -0400 | [diff] [blame] | 587 | return 0; |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 588 | } |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 589 | } |
| 590 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 591 | /* initialize a telnet state tracker */ |
Sean Middleditch | d2466a0 | 2009-09-19 14:35:48 -0700 | [diff] [blame^] | 592 | telnet_t *telnet_init(const telnet_telopt_t *telopts, |
Sean Middleditch | 34bb099 | 2009-03-21 00:20:44 -0400 | [diff] [blame] | 593 | telnet_event_handler_t eh, unsigned char flags, void *user_data) { |
Sean Middleditch | d2466a0 | 2009-09-19 14:35:48 -0700 | [diff] [blame^] | 594 | /* allocate structure */ |
| 595 | struct telnet_t *telnet = (telnet_t*)calloc(1, sizeof(telnet_t)); |
| 596 | if (telnet == 0) |
| 597 | return 0; |
| 598 | |
| 599 | /* initialize data */ |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 600 | telnet->ud = user_data; |
Sean Middleditch | 34bb099 | 2009-03-21 00:20:44 -0400 | [diff] [blame] | 601 | telnet->telopts = telopts; |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 602 | telnet->eh = eh; |
Sean Middleditch | 08bb05f | 2009-03-15 23:29:46 -0400 | [diff] [blame] | 603 | telnet->flags = flags; |
Sean Middleditch | d2466a0 | 2009-09-19 14:35:48 -0700 | [diff] [blame^] | 604 | |
| 605 | return telnet; |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | /* free up any memory allocated by a state tracker */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 609 | void telnet_free(telnet_t *telnet) { |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 610 | /* free sub-request buffer */ |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 611 | if (telnet->buffer != 0) { |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 612 | free(telnet->buffer); |
| 613 | telnet->buffer = 0; |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 614 | telnet->buffer_size = 0; |
| 615 | telnet->buffer_pos = 0; |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 616 | } |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 617 | |
Sean Middleditch | e5b4759 | 2009-03-16 17:37:43 -0400 | [diff] [blame] | 618 | #ifdef HAVE_ZLIB |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 619 | /* free zlib box */ |
| 620 | if (telnet->z != 0) { |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 621 | if (telnet->flags & TELNET_PFLAG_DEFLATE) |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 622 | deflateEnd(telnet->z); |
| 623 | else |
| 624 | inflateEnd(telnet->z); |
| 625 | free(telnet->z); |
| 626 | telnet->z = 0; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 627 | } |
Sean Middleditch | e5b4759 | 2009-03-16 17:37:43 -0400 | [diff] [blame] | 628 | #endif |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 629 | |
| 630 | /* free RFC1143 queue */ |
| 631 | if (telnet->q) { |
| 632 | free(telnet->q); |
| 633 | telnet->q = 0; |
| 634 | telnet->q_size = 0; |
| 635 | } |
Sean Middleditch | d2466a0 | 2009-09-19 14:35:48 -0700 | [diff] [blame^] | 636 | |
| 637 | /* free the telnet structure itself */ |
| 638 | free(telnet); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 639 | } |
| 640 | |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 641 | /* push a byte into the telnet buffer */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 642 | static telnet_error_t _buffer_byte(telnet_t *telnet, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 643 | unsigned char byte) { |
Sean Middleditch | 8daf774 | 2009-03-19 02:05:24 -0400 | [diff] [blame] | 644 | char *new_buffer; |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 645 | size_t i; |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 646 | |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 647 | /* check if we're out of room */ |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 648 | if (telnet->buffer_pos == telnet->buffer_size) { |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 649 | /* find the next buffer size */ |
| 650 | for (i = 0; i != _buffer_sizes_count; ++i) { |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 651 | if (_buffer_sizes[i] == telnet->buffer_size) |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 652 | break; |
| 653 | } |
| 654 | |
| 655 | /* overflow -- can't grow any more */ |
| 656 | if (i >= _buffer_sizes_count - 1) { |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 657 | _error(telnet, __LINE__, __func__, TELNET_EOVERFLOW, 0, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 658 | "subnegotiation buffer size limit reached"); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 659 | return TELNET_EOVERFLOW; |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 660 | } |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 661 | |
| 662 | /* (re)allocate buffer */ |
Sean Middleditch | b43c10c | 2009-03-20 23:21:02 -0400 | [diff] [blame] | 663 | new_buffer = (char *)realloc(telnet->buffer, _buffer_sizes[i + 1]); |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 664 | if (new_buffer == 0) { |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 665 | _error(telnet, __LINE__, __func__, TELNET_ENOMEM, 0, |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 666 | "realloc() failed"); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 667 | return TELNET_ENOMEM; |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | telnet->buffer = new_buffer; |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 671 | telnet->buffer_size = _buffer_sizes[i + 1]; |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | /* push the byte, all set */ |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 675 | telnet->buffer[telnet->buffer_pos++] = byte; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 676 | return TELNET_EOK; |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 677 | } |
| 678 | |
Sean Middleditch | 2d5f4bf | 2009-03-20 23:32:47 -0400 | [diff] [blame] | 679 | static void _process(telnet_t *telnet, const char *buffer, size_t size) { |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 680 | unsigned char byte; |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 681 | size_t i, start; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 682 | for (i = start = 0; i != size; ++i) { |
| 683 | byte = buffer[i]; |
| 684 | switch (telnet->state) { |
| 685 | /* regular data */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 686 | case TELNET_STATE_DATA: |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 687 | /* on an IAC byte, pass through all pending bytes and |
| 688 | * switch states */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 689 | if (byte == TELNET_IAC) { |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 690 | if (i != start) |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 691 | _event(telnet, TELNET_EV_DATA, 0, 0, &buffer[start], |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 692 | i - start, 0, 0); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 693 | telnet->state = TELNET_STATE_IAC; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 694 | } |
| 695 | break; |
| 696 | |
| 697 | /* IAC command */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 698 | case TELNET_STATE_IAC: |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 699 | switch (byte) { |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 700 | /* subnegotiation */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 701 | case TELNET_SB: |
| 702 | telnet->state = TELNET_STATE_SB; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 703 | break; |
| 704 | /* negotiation commands */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 705 | case TELNET_WILL: |
| 706 | telnet->state = TELNET_STATE_WILL; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 707 | break; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 708 | case TELNET_WONT: |
| 709 | telnet->state = TELNET_STATE_WONT; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 710 | break; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 711 | case TELNET_DO: |
| 712 | telnet->state = TELNET_STATE_DO; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 713 | break; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 714 | case TELNET_DONT: |
| 715 | telnet->state = TELNET_STATE_DONT; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 716 | break; |
| 717 | /* IAC escaping */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 718 | case TELNET_IAC: |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 719 | _event(telnet, TELNET_EV_DATA, 0, 0, (char*)&byte, 1, 0, 0); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 720 | start = i + 1; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 721 | telnet->state = TELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 722 | break; |
| 723 | /* some other command */ |
| 724 | default: |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 725 | _event(telnet, TELNET_EV_IAC, byte, 0, 0, 0, 0, 0); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 726 | start = i + 1; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 727 | telnet->state = TELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 728 | } |
| 729 | break; |
| 730 | |
| 731 | /* negotiation commands */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 732 | case TELNET_STATE_WILL: |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 733 | case TELNET_STATE_WONT: |
Sean Middleditch | 447d3ad | 2009-03-20 23:22:37 -0400 | [diff] [blame] | 734 | case TELNET_STATE_DO: |
Sean Middleditch | 447d3ad | 2009-03-20 23:22:37 -0400 | [diff] [blame] | 735 | case TELNET_STATE_DONT: |
Sean Middleditch | 330c261 | 2009-03-20 23:29:17 -0400 | [diff] [blame] | 736 | _negotiate(telnet, byte); |
Sean Middleditch | 447d3ad | 2009-03-20 23:22:37 -0400 | [diff] [blame] | 737 | start = i + 1; |
| 738 | telnet->state = TELNET_STATE_DATA; |
| 739 | break; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 740 | |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame] | 741 | /* subnegotiation -- determine subnegotiation telopt */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 742 | case TELNET_STATE_SB: |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame] | 743 | telnet->sb_telopt = byte; |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 744 | telnet->buffer_pos = 0; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 745 | telnet->state = TELNET_STATE_SB_DATA; |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame] | 746 | break; |
| 747 | |
| 748 | /* subnegotiation -- buffer bytes until end request */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 749 | case TELNET_STATE_SB_DATA: |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 750 | /* IAC command in subnegotiation -- either IAC SE or IAC IAC */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 751 | if (byte == TELNET_IAC) { |
| 752 | telnet->state = TELNET_STATE_SB_DATA_IAC; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 753 | /* buffer the byte, or bail if we can't */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 754 | } else if (_buffer_byte(telnet, byte) != TELNET_EOK) { |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 755 | start = i + 1; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 756 | telnet->state = TELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 757 | } |
| 758 | break; |
| 759 | |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 760 | /* IAC escaping inside a subnegotiation */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 761 | case TELNET_STATE_SB_DATA_IAC: |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 762 | switch (byte) { |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 763 | /* end subnegotiation */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 764 | case TELNET_SE: |
Sean Middleditch | 8c56735 | 2009-03-21 01:07:18 -0400 | [diff] [blame] | 765 | /* return to default state */ |
| 766 | start = i + 1; |
| 767 | telnet->state = TELNET_STATE_DATA; |
| 768 | |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 769 | /* process subnegotiation */ |
| 770 | if (_subnegotiate(telnet) != 0) { |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 771 | /* any remaining bytes in the buffer are compressed. |
Sean Middleditch | 4f0c37f | 2009-03-20 23:08:55 -0400 | [diff] [blame] | 772 | * we have to re-invoke telnet_recv to get those |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 773 | * bytes inflated and abort trying to process the |
| 774 | * remaining compressed bytes in the current _process |
| 775 | * buffer argument |
| 776 | */ |
Sean Middleditch | 8c56735 | 2009-03-21 01:07:18 -0400 | [diff] [blame] | 777 | telnet_recv(telnet, &buffer[start], size - start); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 778 | return; |
| 779 | } |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 780 | break; |
| 781 | /* escaped IAC byte */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 782 | case TELNET_IAC: |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 783 | /* push IAC into buffer */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 784 | if (_buffer_byte(telnet, TELNET_IAC) != |
| 785 | TELNET_EOK) { |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 786 | start = i + 1; |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 787 | telnet->state = TELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 788 | } else { |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 789 | telnet->state = TELNET_STATE_SB_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 790 | } |
| 791 | break; |
Sean Middleditch | 2d5f4bf | 2009-03-20 23:32:47 -0400 | [diff] [blame] | 792 | /* something else -- protocol error. attempt to process |
| 793 | * content in subnegotiation buffer, then evaluate the |
| 794 | * given command as an IAC code. |
| 795 | */ |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 796 | default: |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 797 | _error(telnet, __LINE__, __func__, TELNET_EPROTOCOL, 0, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 798 | "unexpected byte after IAC inside SB: %d", |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 799 | byte); |
Sean Middleditch | 2d5f4bf | 2009-03-20 23:32:47 -0400 | [diff] [blame] | 800 | |
Sean Middleditch | f6daa85 | 2009-03-21 01:16:20 -0400 | [diff] [blame] | 801 | /* enter IAC state */ |
Sean Middleditch | 3f96c5c | 2009-03-21 01:11:46 -0400 | [diff] [blame] | 802 | start = i + 1; |
Sean Middleditch | f6daa85 | 2009-03-21 01:16:20 -0400 | [diff] [blame] | 803 | telnet->state = TELNET_STATE_IAC; |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 804 | |
| 805 | /* process subnegotiation; see comment in |
| 806 | * TELNET_STATE_SB_DATA_IAC about invoking telnet_recv() |
| 807 | */ |
| 808 | if (_subnegotiate(telnet) != 0) { |
Sean Middleditch | 3f96c5c | 2009-03-21 01:11:46 -0400 | [diff] [blame] | 809 | telnet_recv(telnet, &buffer[start], size - start); |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 810 | return; |
Sean Middleditch | 3f96c5c | 2009-03-21 01:11:46 -0400 | [diff] [blame] | 811 | } else { |
| 812 | /* recursive call to get the current input byte processed |
| 813 | * as a regular IAC command. we could use a goto, but |
| 814 | * that would be gross. |
| 815 | */ |
Sean Middleditch | 3f96c5c | 2009-03-21 01:11:46 -0400 | [diff] [blame] | 816 | _process(telnet, (char *)&byte, 1); |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 817 | } |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 818 | break; |
| 819 | } |
| 820 | break; |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | /* pass through any remaining bytes */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 825 | if (telnet->state == TELNET_STATE_DATA && i != start) |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 826 | _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] | 827 | } |
| 828 | |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 829 | /* push a bytes into the state tracker */ |
Sean Middleditch | 4f0c37f | 2009-03-20 23:08:55 -0400 | [diff] [blame] | 830 | void telnet_recv(telnet_t *telnet, const char *buffer, |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 831 | size_t size) { |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 832 | #ifdef HAVE_ZLIB |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 833 | /* if we have an inflate (decompression) zlib stream, use it */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 834 | if (telnet->z != 0 && !(telnet->flags & TELNET_PFLAG_DEFLATE)) { |
Sean Middleditch | 8daf774 | 2009-03-19 02:05:24 -0400 | [diff] [blame] | 835 | char inflate_buffer[4096]; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 836 | int rs; |
| 837 | |
| 838 | /* initialize zlib state */ |
Sean Middleditch | 8daf774 | 2009-03-19 02:05:24 -0400 | [diff] [blame] | 839 | telnet->z->next_in = (unsigned char*)buffer; |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 840 | telnet->z->avail_in = size; |
Sean Middleditch | 8daf774 | 2009-03-19 02:05:24 -0400 | [diff] [blame] | 841 | telnet->z->next_out = (unsigned char *)inflate_buffer; |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 842 | telnet->z->avail_out = sizeof(inflate_buffer); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 843 | |
| 844 | /* inflate until buffer exhausted and all output is produced */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 845 | while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) { |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 846 | /* reset output buffer */ |
| 847 | |
| 848 | /* decompress */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 849 | rs = inflate(telnet->z, Z_SYNC_FLUSH); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 850 | |
| 851 | /* process the decompressed bytes on success */ |
| 852 | if (rs == Z_OK || rs == Z_STREAM_END) |
| 853 | _process(telnet, inflate_buffer, sizeof(inflate_buffer) - |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 854 | telnet->z->avail_out); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 855 | else |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 856 | _error(telnet, __LINE__, __func__, TELNET_ECOMPRESS, 1, |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 857 | "inflate() failed: %s", zError(rs)); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 858 | |
| 859 | /* prepare output buffer for next run */ |
Sean Middleditch | 8daf774 | 2009-03-19 02:05:24 -0400 | [diff] [blame] | 860 | telnet->z->next_out = (unsigned char *)inflate_buffer; |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 861 | telnet->z->avail_out = sizeof(inflate_buffer); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 862 | |
| 863 | /* on error (or on end of stream) disable further inflation */ |
| 864 | if (rs != Z_OK) { |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 865 | _event(telnet, TELNET_EV_COMPRESS, 0, 0, 0, 0, 0, 0); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 866 | |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 867 | inflateEnd(telnet->z); |
| 868 | free(telnet->z); |
| 869 | telnet->z = 0; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 870 | break; |
| 871 | } |
| 872 | } |
| 873 | |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 874 | /* COMPRESS2 is not negotiated, just process */ |
| 875 | } else |
| 876 | #endif /* HAVE_ZLIB */ |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 877 | _process(telnet, buffer, size); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 878 | } |
| 879 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 880 | /* send an iac command */ |
Sean Middleditch | 4f0c37f | 2009-03-20 23:08:55 -0400 | [diff] [blame] | 881 | void telnet_iac(telnet_t *telnet, unsigned char cmd) { |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 882 | char bytes[2] = { TELNET_IAC, cmd }; |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 883 | _send(telnet, bytes, 2); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | /* send negotiation */ |
Sean Middleditch | 4f0c37f | 2009-03-20 23:08:55 -0400 | [diff] [blame] | 887 | void telnet_negotiate(telnet_t *telnet, unsigned char cmd, |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 888 | unsigned char telopt) { |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 889 | telnet_rfc1143_t q; |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 890 | |
| 891 | /* if we're in proxy mode, just send it now */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 892 | if (telnet->flags & TELNET_FLAG_PROXY) { |
| 893 | char bytes[3] = { TELNET_IAC, cmd, telopt }; |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 894 | _send(telnet, bytes, 3); |
| 895 | return; |
| 896 | } |
| 897 | |
| 898 | /* get current option states */ |
| 899 | q = _get_rfc1143(telnet, telopt); |
| 900 | |
| 901 | switch (cmd) { |
| 902 | /* advertise willingess to support an option */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 903 | case TELNET_WILL: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 904 | switch (q.us) { |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 905 | case Q_NO: |
| 906 | _set_rfc1143(telnet, telopt, Q_WANTYES, q.him); |
Sean Middleditch | 90e79da | 2009-03-19 15:17:13 -0400 | [diff] [blame] | 907 | _send_negotiate(telnet, TELNET_WILL, telopt); |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 908 | break; |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 909 | case Q_WANTNO: |
| 910 | _set_rfc1143(telnet, telopt, Q_WANTNO_OP, q.him); |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 911 | break; |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 912 | case Q_WANTYES_OP: |
| 913 | _set_rfc1143(telnet, telopt, Q_WANTYES, q.him); |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 914 | break; |
| 915 | } |
| 916 | break; |
| 917 | |
| 918 | /* force turn-off of locally enabled option */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 919 | case TELNET_WONT: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 920 | switch (q.us) { |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 921 | case Q_YES: |
| 922 | _set_rfc1143(telnet, telopt, Q_WANTNO, q.him); |
Sean Middleditch | 90e79da | 2009-03-19 15:17:13 -0400 | [diff] [blame] | 923 | _send_negotiate(telnet, TELNET_WONT, telopt); |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 924 | break; |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 925 | case Q_WANTYES: |
| 926 | _set_rfc1143(telnet, telopt, Q_WANTYES_OP, q.him); |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 927 | break; |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 928 | case Q_WANTNO_OP: |
| 929 | _set_rfc1143(telnet, telopt, Q_WANTNO, q.him); |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 930 | break; |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 931 | } |
| 932 | break; |
| 933 | |
| 934 | /* ask remote end to enable an option */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 935 | case TELNET_DO: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 936 | switch (q.him) { |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 937 | case Q_NO: |
| 938 | _set_rfc1143(telnet, telopt, q.us, Q_WANTYES); |
Sean Middleditch | 90e79da | 2009-03-19 15:17:13 -0400 | [diff] [blame] | 939 | _send_negotiate(telnet, TELNET_DO, telopt); |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 940 | break; |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 941 | case Q_WANTNO: |
| 942 | _set_rfc1143(telnet, telopt, q.us, Q_WANTNO_OP); |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 943 | break; |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 944 | case Q_WANTYES_OP: |
| 945 | _set_rfc1143(telnet, telopt, q.us, Q_WANTYES); |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 946 | break; |
| 947 | } |
| 948 | break; |
| 949 | |
| 950 | /* demand remote end disable an option */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 951 | case TELNET_DONT: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 952 | switch (q.him) { |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 953 | case Q_YES: |
| 954 | _set_rfc1143(telnet, telopt, q.us, Q_WANTNO); |
Sean Middleditch | 90e79da | 2009-03-19 15:17:13 -0400 | [diff] [blame] | 955 | _send_negotiate(telnet, TELNET_DONT, telopt); |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 956 | break; |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 957 | case Q_WANTYES: |
| 958 | _set_rfc1143(telnet, telopt, q.us, Q_WANTYES_OP); |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 959 | break; |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 960 | case Q_WANTNO_OP: |
| 961 | _set_rfc1143(telnet, telopt, q.us, Q_WANTNO); |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 962 | break; |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 963 | } |
| 964 | break; |
| 965 | } |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | /* send non-command data (escapes IAC bytes) */ |
Sean Middleditch | 4f0c37f | 2009-03-20 23:08:55 -0400 | [diff] [blame] | 969 | void telnet_send(telnet_t *telnet, const char *buffer, |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 970 | size_t size) { |
| 971 | size_t i, l; |
Sean Middleditch | c337ba6 | 2009-03-16 16:47:27 -0400 | [diff] [blame] | 972 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 973 | for (l = i = 0; i != size; ++i) { |
| 974 | /* dump prior portion of text, send escaped bytes */ |
Sean Middleditch | 0003f05 | 2009-04-27 19:26:05 -0400 | [diff] [blame] | 975 | if (buffer[i] == (char)TELNET_IAC) { |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 976 | /* dump prior text if any */ |
| 977 | if (i != l) |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 978 | _send(telnet, buffer + l, i - l); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 979 | l = i + 1; |
| 980 | |
| 981 | /* send escape */ |
Sean Middleditch | 4f0c37f | 2009-03-20 23:08:55 -0400 | [diff] [blame] | 982 | telnet_iac(telnet, TELNET_IAC); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 983 | } |
| 984 | } |
| 985 | |
| 986 | /* send whatever portion of buffer is left */ |
| 987 | if (i != l) |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 988 | _send(telnet, buffer + l, i - l); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 989 | } |
| 990 | |
Sean Middleditch | 7894384 | 2009-03-19 15:22:06 -0400 | [diff] [blame] | 991 | /* send subnegotiation header */ |
Sean Middleditch | 1443ca4 | 2009-03-21 00:21:04 -0400 | [diff] [blame] | 992 | void telnet_begin_sb(telnet_t *telnet, unsigned char telopt) { |
Sean Middleditch | 7894384 | 2009-03-19 15:22:06 -0400 | [diff] [blame] | 993 | const char sb[3] = { TELNET_IAC, TELNET_SB, telopt }; |
| 994 | _send(telnet, sb, 3); |
| 995 | } |
| 996 | |
| 997 | |
| 998 | /* send complete subnegotiation */ |
Sean Middleditch | 4f0c37f | 2009-03-20 23:08:55 -0400 | [diff] [blame] | 999 | void telnet_subnegotiation(telnet_t *telnet, unsigned char telopt, |
Sean Middleditch | 340a51b | 2009-03-19 02:08:46 -0400 | [diff] [blame] | 1000 | const char *buffer, size_t size) { |
Sean Middleditch | 90e79da | 2009-03-19 15:17:13 -0400 | [diff] [blame] | 1001 | const char sb[3] = { TELNET_IAC, TELNET_SB, telopt }; |
| 1002 | static const char se[2] = { TELNET_IAC, TELNET_SE }; |
| 1003 | |
| 1004 | _send(telnet, sb, 3); |
Sean Middleditch | 4f0c37f | 2009-03-20 23:08:55 -0400 | [diff] [blame] | 1005 | telnet_send(telnet, buffer, size); |
Sean Middleditch | 90e79da | 2009-03-19 15:17:13 -0400 | [diff] [blame] | 1006 | _send(telnet, se, 2); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 1007 | |
| 1008 | #ifdef HAVE_ZLIB |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 1009 | /* if we're a proxy and we just sent the COMPRESS2 marker, we must |
| 1010 | * make sure all further data is compressed if not already. |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 1011 | */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 1012 | if (telnet->flags & TELNET_FLAG_PROXY && |
| 1013 | telopt == TELNET_TELOPT_COMPRESS2) { |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 1014 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 1015 | if (_init_zlib(telnet, 1, 1) != TELNET_EOK) |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 1016 | return; |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 1017 | |
| 1018 | /* notify app that compression was enabled */ |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 1019 | _event(telnet, TELNET_EV_COMPRESS, 1, 0, 0, 0, 0, 0); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 1020 | } |
| 1021 | #endif /* HAVE_ZLIB */ |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 1022 | } |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 1023 | |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 1024 | void telnet_begin_compress2(telnet_t *telnet) { |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 1025 | #ifdef HAVE_ZLIB |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 1026 | static const char compress2[] = { TELNET_IAC, TELNET_SB, |
| 1027 | TELNET_TELOPT_COMPRESS2, TELNET_IAC, TELNET_SE }; |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 1028 | |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 1029 | /* attempt to create output stream first, bail if we can't */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 1030 | if (_init_zlib(telnet, 1, 0) != TELNET_EOK) |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 1031 | return; |
| 1032 | |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 1033 | /* send compression marker. we send directly to the event handler |
| 1034 | * instead of passing through _send because _send would result in |
| 1035 | * the compress marker itself being compressed. |
| 1036 | */ |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 1037 | _event(telnet, TELNET_EV_SEND, 0, 0, compress2, sizeof(compress2), 0, 0); |
Sean Middleditch | 2b4bfc4 | 2009-03-16 01:25:52 -0400 | [diff] [blame] | 1038 | |
| 1039 | /* notify app that compression was successfully enabled */ |
Sean Middleditch | e5327da | 2009-03-21 00:54:50 -0400 | [diff] [blame] | 1040 | _event(telnet, TELNET_EV_COMPRESS, 1, 0, 0, 0, 0, 0); |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 1041 | #endif /* HAVE_ZLIB */ |
| 1042 | } |
Sean Middleditch | d58f49f | 2009-03-16 12:49:35 -0400 | [diff] [blame] | 1043 | |
Sean Middleditch | 4a15604 | 2009-03-16 17:10:58 -0400 | [diff] [blame] | 1044 | /* 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] | 1045 | int telnet_printf(telnet_t *telnet, const char *fmt, ...) { |
Sean Middleditch | 8daf774 | 2009-03-19 02:05:24 -0400 | [diff] [blame] | 1046 | static const char CRLF[] = { '\r', '\n' }; |
| 1047 | static const char CRNUL[] = { '\r', '\0' }; |
Sean Middleditch | 4a15604 | 2009-03-16 17:10:58 -0400 | [diff] [blame] | 1048 | char buffer[4096]; |
| 1049 | va_list va; |
| 1050 | int rs, i, l; |
| 1051 | |
| 1052 | /* format */ |
| 1053 | va_start(va, fmt); |
| 1054 | rs = vsnprintf(buffer, sizeof(buffer), fmt, va); |
| 1055 | va_end(va); |
| 1056 | |
| 1057 | /* send */ |
| 1058 | for (l = i = 0; i != rs; ++i) { |
| 1059 | /* special characters */ |
Sean Middleditch | 0003f05 | 2009-04-27 19:26:05 -0400 | [diff] [blame] | 1060 | if (buffer[i] == (char)TELNET_IAC || buffer[i] == '\r' || |
Sean Middleditch | 4a15604 | 2009-03-16 17:10:58 -0400 | [diff] [blame] | 1061 | buffer[i] == '\n') { |
| 1062 | /* dump prior portion of text */ |
| 1063 | if (i != l) |
Sean Middleditch | b43c10c | 2009-03-20 23:21:02 -0400 | [diff] [blame] | 1064 | _send(telnet, buffer + l, i - l); |
Sean Middleditch | 4a15604 | 2009-03-16 17:10:58 -0400 | [diff] [blame] | 1065 | l = i + 1; |
| 1066 | |
| 1067 | /* IAC -> IAC IAC */ |
Sean Middleditch | 0003f05 | 2009-04-27 19:26:05 -0400 | [diff] [blame] | 1068 | if (buffer[i] == (char)TELNET_IAC) |
Sean Middleditch | 4f0c37f | 2009-03-20 23:08:55 -0400 | [diff] [blame] | 1069 | telnet_iac(telnet, TELNET_IAC); |
Sean Middleditch | 4a15604 | 2009-03-16 17:10:58 -0400 | [diff] [blame] | 1070 | /* automatic translation of \r -> CRNUL */ |
| 1071 | else if (buffer[i] == '\r') |
| 1072 | _send(telnet, CRNUL, 2); |
| 1073 | /* automatic translation of \n -> CRLF */ |
| 1074 | else if (buffer[i] == '\n') |
| 1075 | _send(telnet, CRLF, 2); |
| 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | /* send whatever portion of buffer is left */ |
| 1080 | if (i != l) |
Sean Middleditch | b43c10c | 2009-03-20 23:21:02 -0400 | [diff] [blame] | 1081 | _send(telnet, buffer + l, i - l); |
Sean Middleditch | 4a15604 | 2009-03-16 17:10:58 -0400 | [diff] [blame] | 1082 | |
| 1083 | return rs; |
| 1084 | } |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 1085 | |
Sean Middleditch | 7491bf4 | 2009-03-20 23:52:18 -0400 | [diff] [blame] | 1086 | /* send formatted data through telnet_send */ |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 1087 | int telnet_printf2(telnet_t *telnet, const char *fmt, ...) { |
| 1088 | char buffer[4096]; |
| 1089 | va_list va; |
| 1090 | int rs; |
| 1091 | |
| 1092 | /* format */ |
| 1093 | va_start(va, fmt); |
| 1094 | rs = vsnprintf(buffer, sizeof(buffer), fmt, va); |
| 1095 | va_end(va); |
| 1096 | |
| 1097 | /* send */ |
Sean Middleditch | b43c10c | 2009-03-20 23:21:02 -0400 | [diff] [blame] | 1098 | telnet_send(telnet, buffer, rs); |
Sean Middleditch | f65f27d | 2009-03-19 02:32:04 -0400 | [diff] [blame] | 1099 | |
| 1100 | return rs; |
| 1101 | } |
Sean Middleditch | e22b477 | 2009-03-22 16:44:40 -0400 | [diff] [blame] | 1102 | |
Sean Middleditch | eb950a8 | 2009-03-22 23:04:28 -0400 | [diff] [blame] | 1103 | /* send formatted subnegotiation data for TTYPE/ENVIRON/NEW-ENVIRON/MSSP */ |
| 1104 | void telnet_format_sb(telnet_t *telnet, unsigned char telopt, |
| 1105 | size_t count, ...) { |
| 1106 | va_list va; |
| 1107 | size_t i; |
| 1108 | |
| 1109 | /* subnegotiation header */ |
| 1110 | telnet_begin_sb(telnet, telopt); |
| 1111 | |
| 1112 | /* iterate over the arguments pulling out integers and strings */ |
| 1113 | va_start(va, count); |
| 1114 | for (i = 0; i != count; ++i) { |
| 1115 | char t; |
| 1116 | const char* s; |
| 1117 | t = va_arg(va, int); |
| 1118 | s = va_arg(va, const char *); |
| 1119 | telnet_send(telnet, &t, 1); |
| 1120 | telnet_send(telnet, s, strlen(s)); |
| 1121 | } |
| 1122 | va_end(va); |
| 1123 | |
| 1124 | /* footer */ |
| 1125 | telnet_finish_sb(telnet); |
| 1126 | } |
| 1127 | |
Sean Middleditch | e22b477 | 2009-03-22 16:44:40 -0400 | [diff] [blame] | 1128 | /* send ZMP data */ |
| 1129 | void telnet_send_zmp(telnet_t *telnet, size_t argc, const char **argv) { |
| 1130 | size_t i; |
| 1131 | |
| 1132 | /* ZMP header */ |
| 1133 | telnet_begin_sb(telnet, TELNET_TELOPT_ZMP); |
| 1134 | |
| 1135 | /* send out each argument, including trailing NUL byte */ |
| 1136 | for (i = 0; i != argc; ++i) |
| 1137 | telnet_send(telnet, argv[i], strlen(argv[i] + 1)); |
| 1138 | |
| 1139 | /* ZMP footer */ |
| 1140 | telnet_finish_sb(telnet); |
| 1141 | } |
| 1142 | |
| 1143 | /* send ZMP data using varargs */ |
| 1144 | void telnet_send_zmpv(telnet_t *telnet, ...) { |
| 1145 | va_list va; |
| 1146 | const char* arg; |
| 1147 | |
| 1148 | /* ZMP header */ |
| 1149 | telnet_begin_sb(telnet, TELNET_TELOPT_ZMP); |
| 1150 | |
| 1151 | /* send out each argument, including trailing NUL byte */ |
| 1152 | va_start(va, telnet); |
| 1153 | while ((arg = va_arg(va, const char *)) != NULL) |
| 1154 | telnet_send(telnet, arg, strlen(arg) + 1); |
| 1155 | va_end(va); |
| 1156 | |
| 1157 | /* ZMP footer */ |
| 1158 | telnet_finish_sb(telnet); |
| 1159 | } |