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