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