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