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