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 | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 25 | /* RFC1143 state names */ |
| 26 | #define RFC1143_NO 0x00 |
| 27 | #define RFC1143_YES 0x01 |
| 28 | |
| 29 | #define RFC1143_WANT 0x02 |
| 30 | #define RFC1143_OP 0x04 |
| 31 | |
| 32 | #define RFC1143_WANTNO (RFC1143_WANT|RFC1143_YES) |
| 33 | #define RFC1143_WANTYES (RFC1143_WANT|RFC1143_NO) |
| 34 | #define RFC1143_WANTNO_OP (RFC1143_WANTNO|RFC1143_OP) |
| 35 | #define RFC1143_WANTYES_OP (RFC1143_WANTYES|RFC1143_OP) |
| 36 | |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 37 | /* buffer sizes */ |
| 38 | static const unsigned int _buffer_sizes[] = { |
| 39 | 0, |
| 40 | 512, |
| 41 | 2048, |
| 42 | 8192, |
| 43 | 16384, |
| 44 | }; |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 45 | static const unsigned int _buffer_sizes_count = sizeof(_buffer_sizes) / |
| 46 | sizeof(_buffer_sizes[0]); |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 47 | |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 48 | /* event dispatch helper; return value is value of the accept field of the |
| 49 | * event struct after dispatch; used for the funky REQUEST event */ |
Sean Middleditch | 35b95be | 2009-03-15 23:46:31 -0400 | [diff] [blame] | 50 | static int _event(libtelnet_t *telnet, libtelnet_event_type_t type, |
Sean Middleditch | 97a8cb2 | 2009-03-16 16:51:41 -0400 | [diff] [blame^] | 51 | unsigned char command, unsigned char telopt, |
| 52 | const unsigned char *buffer, unsigned int size) { |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 53 | libtelnet_event_t ev; |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 54 | ev.buffer = buffer; |
| 55 | ev.size = size; |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 56 | ev.type = type; |
| 57 | ev.command = command; |
| 58 | ev.telopt = telopt; |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 59 | ev.accept = 0; |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 60 | |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 61 | telnet->eh(telnet, &ev, telnet->ud); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 62 | |
| 63 | return ev.accept; |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 64 | } |
| 65 | |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 66 | /* error generation function */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 67 | static libtelnet_error_t _error(libtelnet_t *telnet, unsigned line, |
| 68 | const char* func, libtelnet_error_t err, int fatal, const char *fmt, |
| 69 | ...) { |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 70 | char buffer[512]; |
| 71 | va_list va; |
| 72 | |
| 73 | /* format error intro */ |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 74 | snprintf(buffer, sizeof(buffer), "%s:%u in %s: ", __FILE__, line, func); |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 75 | |
| 76 | va_start(va, fmt); |
| 77 | vsnprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), |
| 78 | fmt, va); |
| 79 | va_end(va); |
| 80 | |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 81 | _event(telnet, fatal ? LIBTELNET_EV_ERROR : LIBTELNET_EV_WARNING, err, |
Sean Middleditch | 5cf6666 | 2009-03-15 20:02:14 -0400 | [diff] [blame] | 82 | 0, (unsigned char *)buffer, strlen(buffer)); |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 83 | |
| 84 | return err; |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 85 | } |
| 86 | |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 87 | /* initialize the zlib box for a telnet box; if deflate is non-zero, it |
| 88 | * initializes zlib for delating (compression), otherwise for inflating |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 89 | * (decompression). returns LIBTELNET_EOK on success, something else on |
| 90 | * failure. |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 91 | */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 92 | libtelnet_error_t _init_zlib(libtelnet_t *telnet, int deflate, int err_fatal) { |
| 93 | z_stream *z; |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 94 | int rs; |
| 95 | |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 96 | /* if compression is already enabled, fail loudly */ |
| 97 | if (telnet->z != 0) |
| 98 | return _error(telnet, __LINE__, __func__, LIBTELNET_EBADVAL, |
| 99 | err_fatal, "cannot initialize compression twice"); |
| 100 | |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 101 | /* allocate zstream box */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 102 | if ((z= (z_stream *)calloc(1, sizeof(z_stream))) == 0) |
| 103 | return _error(telnet, __LINE__, __func__, LIBTELNET_ENOMEM, err_fatal, |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 104 | "malloc() failed: %s", strerror(errno)); |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 105 | |
| 106 | /* initialize */ |
| 107 | if (deflate) { |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 108 | if ((rs = deflateInit(z, Z_DEFAULT_COMPRESSION)) != Z_OK) { |
| 109 | free(z); |
| 110 | return _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, |
| 111 | err_fatal, "deflateInit() failed: %s", zError(rs)); |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 112 | } |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 113 | telnet->flags |= LIBTELNET_PFLAG_DEFLATE; |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 114 | } else { |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 115 | if ((rs = inflateInit(z)) != Z_OK) { |
| 116 | free(z); |
| 117 | return _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, |
| 118 | err_fatal, "inflateInit() failed: %s", zError(rs)); |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 119 | } |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 120 | telnet->flags &= ~LIBTELNET_PFLAG_DEFLATE; |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 121 | } |
| 122 | |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 123 | telnet->z = z; |
| 124 | |
| 125 | return LIBTELNET_EOK; |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 126 | } |
| 127 | |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 128 | /* push bytes out, compressing them first if need be */ |
Sean Middleditch | 97a8cb2 | 2009-03-16 16:51:41 -0400 | [diff] [blame^] | 129 | static void _send(libtelnet_t *telnet, const unsigned char *buffer, |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 130 | unsigned int size) { |
| 131 | #ifdef HAVE_ZLIB |
| 132 | /* if we have a deflate (compression) zlib box, use it */ |
Sean Middleditch | 823bc00 | 2009-03-16 01:09:26 -0400 | [diff] [blame] | 133 | if (telnet->z != 0 && telnet->flags & LIBTELNET_PFLAG_DEFLATE) { |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 134 | unsigned char deflate_buffer[1024]; |
| 135 | int rs; |
| 136 | |
| 137 | /* initialize z state */ |
Sean Middleditch | 97a8cb2 | 2009-03-16 16:51:41 -0400 | [diff] [blame^] | 138 | telnet->z->next_in = (unsigned char *)buffer; |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 139 | telnet->z->avail_in = size; |
| 140 | telnet->z->next_out = deflate_buffer; |
| 141 | telnet->z->avail_out = sizeof(deflate_buffer); |
| 142 | |
| 143 | /* deflate until buffer exhausted and all output is produced */ |
| 144 | while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) { |
| 145 | /* compress */ |
| 146 | if ((rs = deflate(telnet->z, Z_SYNC_FLUSH)) != Z_OK) { |
| 147 | _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, 1, |
| 148 | "deflate() failed: %s", zError(rs)); |
| 149 | deflateEnd(telnet->z); |
| 150 | free(telnet->z); |
| 151 | telnet->z = 0; |
| 152 | break; |
| 153 | } |
| 154 | |
| 155 | _event(telnet, LIBTELNET_EV_SEND, 0, 0, deflate_buffer, |
| 156 | sizeof(deflate_buffer) - telnet->z->avail_out); |
| 157 | |
| 158 | /* prepare output buffer for next run */ |
| 159 | telnet->z->next_out = deflate_buffer; |
| 160 | telnet->z->avail_out = sizeof(deflate_buffer); |
| 161 | } |
| 162 | |
| 163 | /* COMPRESS2 is not negotiated, just send */ |
| 164 | } else |
| 165 | #endif /* HAVE_ZLIB */ |
| 166 | _event(telnet, LIBTELNET_EV_SEND, 0, 0, buffer, size); |
| 167 | } |
| 168 | |
| 169 | /* retrieve RFC1143 option state */ |
| 170 | libtelnet_rfc1143_t _get_rfc1143(libtelnet_t *telnet, unsigned char telopt) { |
| 171 | static const libtelnet_rfc1143_t empty = { 0, 0, 0}; |
| 172 | int i; |
| 173 | |
| 174 | /* search for entry */ |
| 175 | for (i = 0; i != telnet->q_size; ++i) |
| 176 | if (telnet->q[i].telopt == telopt) |
| 177 | return telnet->q[i]; |
| 178 | |
| 179 | /* not found, return empty value */ |
| 180 | return empty; |
| 181 | } |
| 182 | |
| 183 | /* save RFC1143 option state */ |
| 184 | void _set_rfc1143(libtelnet_t *telnet, libtelnet_rfc1143_t q) { |
| 185 | libtelnet_rfc1143_t *qtmp; |
| 186 | int i; |
| 187 | |
| 188 | /* search for entry */ |
| 189 | for (i = 0; i != telnet->q_size; ++i) { |
| 190 | if (telnet->q[i].telopt == q.telopt) { |
| 191 | telnet->q[i] = q; |
| 192 | return; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /* we're going to need to track state for it, so grow the queue |
| 197 | * and put the telopt into it; bail on allocation error |
| 198 | */ |
| 199 | if ((qtmp = (libtelnet_rfc1143_t *)malloc(sizeof( |
| 200 | libtelnet_rfc1143_t) * (telnet->q_size + 1))) == 0) { |
| 201 | _error(telnet, __LINE__, __func__, LIBTELNET_ENOMEM, 0, |
| 202 | "malloc() failed: %s", strerror(errno)); |
| 203 | return; |
| 204 | } |
| 205 | telnet->q = qtmp; |
| 206 | telnet->q[telnet->q_size++] = q; |
| 207 | } |
| 208 | |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 209 | /* negotiation handling magic for RFC1143 */ |
| 210 | static void _negotiate(libtelnet_t *telnet, unsigned char cmd, |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 211 | unsigned char telopt) { |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 212 | libtelnet_rfc1143_t q; |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 213 | |
| 214 | /* in PROXY mode, just pass it thru and do nothing */ |
Sean Middleditch | 562257e | 2009-03-15 23:56:25 -0400 | [diff] [blame] | 215 | if (telnet->flags & LIBTELNET_FLAG_PROXY) { |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 216 | switch (cmd) { |
| 217 | case LIBTELNET_WILL: |
Sean Middleditch | 562257e | 2009-03-15 23:56:25 -0400 | [diff] [blame] | 218 | _event(telnet, LIBTELNET_EV_WILL, 0, telopt, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 219 | break; |
| 220 | case LIBTELNET_WONT: |
Sean Middleditch | 562257e | 2009-03-15 23:56:25 -0400 | [diff] [blame] | 221 | _event(telnet, LIBTELNET_EV_WONT, 0, telopt, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 222 | break; |
| 223 | case LIBTELNET_DO: |
Sean Middleditch | 562257e | 2009-03-15 23:56:25 -0400 | [diff] [blame] | 224 | _event(telnet, LIBTELNET_EV_DO, 0, telopt, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 225 | break; |
| 226 | case LIBTELNET_DONT: |
Sean Middleditch | 562257e | 2009-03-15 23:56:25 -0400 | [diff] [blame] | 227 | _event(telnet, LIBTELNET_EV_DONT, 0, telopt, 0, 0); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 228 | break; |
| 229 | } |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | /* lookup the current state of the option */ |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 234 | q = _get_rfc1143(telnet, telopt); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 235 | |
| 236 | /* start processing... */ |
| 237 | switch (cmd) { |
| 238 | /* request to enable option on remote end or confirm DO */ |
| 239 | case LIBTELNET_WILL: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 240 | switch (q.him) { |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 241 | case RFC1143_NO: |
| 242 | if (_event(telnet, LIBTELNET_EV_WILL, cmd, telopt, 0, 0) == 1) { |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 243 | q.him = RFC1143_YES; |
| 244 | _set_rfc1143(telnet, q); |
Sean Middleditch | 2b4bfc4 | 2009-03-16 01:25:52 -0400 | [diff] [blame] | 245 | libtelnet_send_telopt(telnet, LIBTELNET_DO, telopt); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 246 | } else |
Sean Middleditch | 2b4bfc4 | 2009-03-16 01:25:52 -0400 | [diff] [blame] | 247 | libtelnet_send_telopt(telnet, LIBTELNET_DONT, telopt); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 248 | break; |
| 249 | case RFC1143_YES: |
| 250 | break; |
| 251 | case RFC1143_WANTNO: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 252 | q.him = RFC1143_NO; |
| 253 | _set_rfc1143(telnet, q); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 254 | _error(telnet, __LINE__, __func__, LIBTELNET_EPROTOCOL, 0, |
| 255 | "DONT answered by WILL"); |
| 256 | break; |
| 257 | case RFC1143_WANTNO_OP: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 258 | q.him = RFC1143_YES; |
| 259 | _set_rfc1143(telnet, q); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 260 | _error(telnet, __LINE__, __func__, LIBTELNET_EPROTOCOL, 0, |
| 261 | "DONT answered by WILL"); |
| 262 | break; |
| 263 | case RFC1143_WANTYES: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 264 | q.him = RFC1143_YES; |
| 265 | _set_rfc1143(telnet, q); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 266 | break; |
| 267 | case RFC1143_WANTYES_OP: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 268 | q.him = RFC1143_WANTNO; |
| 269 | _set_rfc1143(telnet, q); |
Sean Middleditch | 2b4bfc4 | 2009-03-16 01:25:52 -0400 | [diff] [blame] | 270 | libtelnet_send_telopt(telnet, LIBTELNET_DONT, telopt); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 271 | break; |
| 272 | } |
| 273 | break; |
| 274 | |
| 275 | /* request to disable option on remote end, confirm DONT, reject DO */ |
| 276 | case LIBTELNET_WONT: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 277 | switch (q.him) { |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 278 | case RFC1143_NO: |
| 279 | break; |
| 280 | case RFC1143_YES: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 281 | q.him = RFC1143_NO; |
| 282 | _set_rfc1143(telnet, q); |
Sean Middleditch | 2b4bfc4 | 2009-03-16 01:25:52 -0400 | [diff] [blame] | 283 | libtelnet_send_telopt(telnet, LIBTELNET_DONT, telopt); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 284 | _event(telnet, LIBTELNET_EV_WONT, 0, telopt, |
| 285 | 0, 0); |
| 286 | break; |
| 287 | case RFC1143_WANTNO: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 288 | q.him = RFC1143_NO; |
| 289 | _set_rfc1143(telnet, q); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 290 | _event(telnet, LIBTELNET_EV_WONT, 0, telopt, |
| 291 | 0, 0); |
| 292 | break; |
| 293 | case RFC1143_WANTNO_OP: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 294 | q.him = RFC1143_WANTYES; |
| 295 | _set_rfc1143(telnet, q); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 296 | _event(telnet, LIBTELNET_EV_DO, 0, telopt, |
| 297 | 0, 0); |
| 298 | break; |
| 299 | case RFC1143_WANTYES: |
| 300 | case RFC1143_WANTYES_OP: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 301 | q.him = RFC1143_NO; |
| 302 | _set_rfc1143(telnet, q); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 303 | break; |
| 304 | } |
| 305 | break; |
| 306 | |
| 307 | /* request to enable option on local end or confirm WILL */ |
| 308 | case LIBTELNET_DO: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 309 | switch (q.us) { |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 310 | case RFC1143_NO: |
| 311 | if (_event(telnet, LIBTELNET_EV_DO, cmd, telopt, 0, 0) == 1) { |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 312 | q.us = RFC1143_YES; |
| 313 | _set_rfc1143(telnet, q); |
Sean Middleditch | 2b4bfc4 | 2009-03-16 01:25:52 -0400 | [diff] [blame] | 314 | libtelnet_send_telopt(telnet, LIBTELNET_WILL, telopt); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 315 | } else |
Sean Middleditch | 2b4bfc4 | 2009-03-16 01:25:52 -0400 | [diff] [blame] | 316 | libtelnet_send_telopt(telnet, LIBTELNET_WONT, telopt); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 317 | break; |
| 318 | case RFC1143_YES: |
| 319 | break; |
| 320 | case RFC1143_WANTNO: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 321 | q.us = RFC1143_NO; |
| 322 | _set_rfc1143(telnet, q); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 323 | _error(telnet, __LINE__, __func__, LIBTELNET_EPROTOCOL, 0, |
| 324 | "WONT answered by DO"); |
| 325 | break; |
| 326 | case RFC1143_WANTNO_OP: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 327 | q.us = RFC1143_YES; |
| 328 | _set_rfc1143(telnet, q); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 329 | _error(telnet, __LINE__, __func__, LIBTELNET_EPROTOCOL, 0, |
| 330 | "WONT answered by DO"); |
| 331 | break; |
| 332 | case RFC1143_WANTYES: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 333 | q.us = RFC1143_YES; |
| 334 | _set_rfc1143(telnet, q); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 335 | break; |
| 336 | case RFC1143_WANTYES_OP: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 337 | q.us = RFC1143_WANTNO; |
| 338 | _set_rfc1143(telnet, q); |
Sean Middleditch | 2b4bfc4 | 2009-03-16 01:25:52 -0400 | [diff] [blame] | 339 | libtelnet_send_telopt(telnet, LIBTELNET_WONT, telopt); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 340 | break; |
| 341 | } |
| 342 | break; |
| 343 | |
| 344 | /* request to disable option on local end, confirm WONT, reject WILL */ |
| 345 | case LIBTELNET_DONT: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 346 | switch (q.us) { |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 347 | case RFC1143_NO: |
| 348 | break; |
| 349 | case RFC1143_YES: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 350 | q.us = RFC1143_NO; |
| 351 | _set_rfc1143(telnet, q); |
Sean Middleditch | 2b4bfc4 | 2009-03-16 01:25:52 -0400 | [diff] [blame] | 352 | libtelnet_send_telopt(telnet, LIBTELNET_WONT, telopt); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 353 | _event(telnet, LIBTELNET_EV_DONT, 0, telopt, 0, 0); |
| 354 | break; |
| 355 | case RFC1143_WANTNO: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 356 | q.us = RFC1143_NO; |
| 357 | _set_rfc1143(telnet, q); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 358 | _event(telnet, LIBTELNET_EV_WONT, 0, telopt, 0, 0); |
| 359 | break; |
| 360 | case RFC1143_WANTNO_OP: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 361 | q.us = RFC1143_WANTYES; |
| 362 | _set_rfc1143(telnet, q); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 363 | _event(telnet, LIBTELNET_EV_WILL, 0, telopt, 0, 0); |
| 364 | break; |
| 365 | case RFC1143_WANTYES: |
| 366 | case RFC1143_WANTYES_OP: |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 367 | q.us = RFC1143_NO; |
| 368 | _set_rfc1143(telnet, q); |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 369 | break; |
| 370 | } |
| 371 | break; |
| 372 | } |
| 373 | } |
| 374 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 375 | /* initialize a telnet state tracker */ |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 376 | void libtelnet_init(libtelnet_t *telnet, libtelnet_event_handler_t eh, |
Sean Middleditch | 08bb05f | 2009-03-15 23:29:46 -0400 | [diff] [blame] | 377 | unsigned char flags, void *user_data) { |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 378 | memset(telnet, 0, sizeof(libtelnet_t)); |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 379 | telnet->ud = user_data; |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 380 | telnet->eh = eh; |
Sean Middleditch | 08bb05f | 2009-03-15 23:29:46 -0400 | [diff] [blame] | 381 | telnet->flags = flags; |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /* free up any memory allocated by a state tracker */ |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 385 | void libtelnet_free(libtelnet_t *telnet) { |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 386 | /* free sub-request buffer */ |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 387 | if (telnet->buffer != 0) { |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 388 | free(telnet->buffer); |
| 389 | telnet->buffer = 0; |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 390 | telnet->buffer_size = 0; |
| 391 | telnet->buffer_pos = 0; |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 392 | } |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 393 | |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 394 | /* free zlib box */ |
| 395 | if (telnet->z != 0) { |
| 396 | if (telnet->flags & LIBTELNET_PFLAG_DEFLATE) |
| 397 | deflateEnd(telnet->z); |
| 398 | else |
| 399 | inflateEnd(telnet->z); |
| 400 | free(telnet->z); |
| 401 | telnet->z = 0; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 402 | } |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 403 | |
| 404 | /* free RFC1143 queue */ |
| 405 | if (telnet->q) { |
| 406 | free(telnet->q); |
| 407 | telnet->q = 0; |
| 408 | telnet->q_size = 0; |
| 409 | } |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 410 | } |
| 411 | |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 412 | /* push a byte into the telnet buffer */ |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 413 | static libtelnet_error_t _buffer_byte(libtelnet_t *telnet, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 414 | unsigned char byte) { |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 415 | unsigned char *new_buffer; |
| 416 | int i; |
| 417 | |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 418 | /* check if we're out of room */ |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 419 | if (telnet->buffer_pos == telnet->buffer_size) { |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 420 | /* find the next buffer size */ |
| 421 | for (i = 0; i != _buffer_sizes_count; ++i) { |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 422 | if (_buffer_sizes[i] == telnet->buffer_size) |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 423 | break; |
| 424 | } |
| 425 | |
| 426 | /* overflow -- can't grow any more */ |
| 427 | if (i >= _buffer_sizes_count - 1) { |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 428 | _error(telnet, __LINE__, __func__, LIBTELNET_EOVERFLOW, 0, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 429 | "subnegotiation buffer size limit reached"); |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 430 | libtelnet_free(telnet); |
Sean Middleditch | f66a7ee | 2009-03-15 11:54:07 -0400 | [diff] [blame] | 431 | return LIBTELNET_EOVERFLOW; |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 432 | } |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 433 | |
| 434 | /* (re)allocate buffer */ |
| 435 | new_buffer = (unsigned char *)realloc(telnet->buffer, |
| 436 | _buffer_sizes[i + 1]); |
| 437 | if (new_buffer == 0) { |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 438 | _error(telnet, __LINE__, __func__, LIBTELNET_ENOMEM, 0, |
| 439 | "realloc() failed"); |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 440 | libtelnet_free(telnet); |
Sean Middleditch | f66a7ee | 2009-03-15 11:54:07 -0400 | [diff] [blame] | 441 | return LIBTELNET_ENOMEM; |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | telnet->buffer = new_buffer; |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 445 | telnet->buffer_size = _buffer_sizes[i + 1]; |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /* push the byte, all set */ |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 449 | telnet->buffer[telnet->buffer_pos++] = byte; |
Sean Middleditch | f66a7ee | 2009-03-15 11:54:07 -0400 | [diff] [blame] | 450 | return LIBTELNET_EOK; |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 451 | } |
| 452 | |
Sean Middleditch | 97a8cb2 | 2009-03-16 16:51:41 -0400 | [diff] [blame^] | 453 | static void _process(libtelnet_t *telnet, const unsigned char *buffer, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 454 | unsigned int size) { |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 455 | unsigned char byte; |
| 456 | unsigned int i, start; |
| 457 | for (i = start = 0; i != size; ++i) { |
| 458 | byte = buffer[i]; |
| 459 | switch (telnet->state) { |
| 460 | /* regular data */ |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 461 | case LIBTELNET_STATE_DATA: |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 462 | /* on an IAC byte, pass through all pending bytes and |
| 463 | * switch states */ |
| 464 | if (byte == LIBTELNET_IAC) { |
| 465 | if (i != start) |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 466 | _event(telnet, LIBTELNET_EV_DATA, 0, 0, &buffer[start], |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 467 | i - start); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 468 | telnet->state = LIBTELNET_STATE_IAC; |
| 469 | } |
| 470 | break; |
| 471 | |
| 472 | /* IAC command */ |
| 473 | case LIBTELNET_STATE_IAC: |
| 474 | switch (byte) { |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 475 | /* subnegotiation */ |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 476 | case LIBTELNET_SB: |
| 477 | telnet->state = LIBTELNET_STATE_SB; |
| 478 | break; |
| 479 | /* negotiation commands */ |
| 480 | case LIBTELNET_WILL: |
| 481 | telnet->state = LIBTELNET_STATE_WILL; |
| 482 | break; |
| 483 | case LIBTELNET_WONT: |
| 484 | telnet->state = LIBTELNET_STATE_WONT; |
| 485 | break; |
| 486 | case LIBTELNET_DO: |
| 487 | telnet->state = LIBTELNET_STATE_DO; |
| 488 | break; |
| 489 | case LIBTELNET_DONT: |
| 490 | telnet->state = LIBTELNET_STATE_DONT; |
| 491 | break; |
| 492 | /* IAC escaping */ |
| 493 | case LIBTELNET_IAC: |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 494 | _event(telnet, LIBTELNET_EV_DATA, 0, 0, &byte, 1); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 495 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 496 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 497 | break; |
| 498 | /* some other command */ |
| 499 | default: |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 500 | _event(telnet, LIBTELNET_EV_IAC, byte, 0, 0, 0); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 501 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 502 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 503 | } |
| 504 | break; |
| 505 | |
| 506 | /* negotiation commands */ |
| 507 | case LIBTELNET_STATE_DO: |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 508 | _negotiate(telnet, LIBTELNET_DO, byte); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 509 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 510 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 511 | break; |
| 512 | case LIBTELNET_STATE_DONT: |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 513 | _negotiate(telnet, LIBTELNET_DONT, byte); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 514 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 515 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 516 | break; |
| 517 | case LIBTELNET_STATE_WILL: |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 518 | _negotiate(telnet, LIBTELNET_WILL, byte); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 519 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 520 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 521 | break; |
| 522 | case LIBTELNET_STATE_WONT: |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 523 | _negotiate(telnet, LIBTELNET_WONT, byte); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 524 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 525 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 526 | break; |
| 527 | |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame] | 528 | /* subnegotiation -- determine subnegotiation telopt */ |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 529 | case LIBTELNET_STATE_SB: |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame] | 530 | telnet->sb_telopt = byte; |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 531 | telnet->buffer_pos = 0; |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame] | 532 | telnet->state = LIBTELNET_STATE_SB_DATA; |
| 533 | break; |
| 534 | |
| 535 | /* subnegotiation -- buffer bytes until end request */ |
| 536 | case LIBTELNET_STATE_SB_DATA: |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 537 | /* IAC command in subnegotiation -- either IAC SE or IAC IAC */ |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 538 | if (byte == LIBTELNET_IAC) { |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame] | 539 | telnet->state = LIBTELNET_STATE_SB_DATA_IAC; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 540 | /* buffer the byte, or bail if we can't */ |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 541 | } else if (_buffer_byte(telnet, byte) != LIBTELNET_EOK) { |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 542 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 543 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 544 | } |
| 545 | break; |
| 546 | |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 547 | /* IAC escaping inside a subnegotiation */ |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame] | 548 | case LIBTELNET_STATE_SB_DATA_IAC: |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 549 | switch (byte) { |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 550 | /* end subnegotiation */ |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 551 | case LIBTELNET_SE: |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 552 | /* return to default state */ |
| 553 | start = i + 1; |
| 554 | telnet->state = LIBTELNET_STATE_DATA; |
| 555 | |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 556 | /* invoke callback */ |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 557 | _event(telnet, LIBTELNET_EV_SUBNEGOTIATION, 0, |
Sean Middleditch | 5b5bc92 | 2009-03-15 23:02:10 -0400 | [diff] [blame] | 558 | telnet->sb_telopt, telnet->buffer, telnet->buffer_pos); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 559 | |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 560 | #ifdef HAVE_ZLIB |
Sean Middleditch | 3df17f9 | 2009-03-16 01:12:16 -0400 | [diff] [blame] | 561 | /* received COMPRESS2 begin marker, setup our zlib box and |
| 562 | * start handling the compressed stream if it's not already. |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 563 | */ |
Sean Middleditch | 3df17f9 | 2009-03-16 01:12:16 -0400 | [diff] [blame] | 564 | if (telnet->sb_telopt == LIBTELNET_TELOPT_COMPRESS2) { |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 565 | if (_init_zlib(telnet, 0, 1) != LIBTELNET_EOK) |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 566 | break; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 567 | |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 568 | /* notify app that compression was enabled */ |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 569 | _event(telnet, LIBTELNET_EV_COMPRESS, 1, 0, 0, 0); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 570 | |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 571 | /* any remaining bytes in the buffer are compressed. |
| 572 | * we have to re-invoke libtelnet_push to get those |
| 573 | * bytes inflated and abort trying to process the |
| 574 | * remaining compressed bytes in the current _process |
| 575 | * buffer argument |
| 576 | */ |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 577 | libtelnet_push(telnet, &buffer[start], size - start); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 578 | return; |
| 579 | } |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 580 | #endif /* HAVE_ZLIB */ |
| 581 | |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 582 | break; |
| 583 | /* escaped IAC byte */ |
| 584 | case LIBTELNET_IAC: |
| 585 | /* push IAC into buffer */ |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 586 | if (_buffer_byte(telnet, LIBTELNET_IAC) != |
Sean Middleditch | f66a7ee | 2009-03-15 11:54:07 -0400 | [diff] [blame] | 587 | LIBTELNET_EOK) { |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 588 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 589 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 590 | } else { |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame] | 591 | telnet->state = LIBTELNET_STATE_SB_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 592 | } |
| 593 | break; |
| 594 | /* something else -- protocol error */ |
| 595 | default: |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 596 | _error(telnet, __LINE__, __func__, LIBTELNET_EPROTOCOL, 0, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 597 | "unexpected byte after IAC inside SB: %d", |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 598 | byte); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 599 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 600 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 601 | break; |
| 602 | } |
| 603 | break; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | /* pass through any remaining bytes */ |
Sean Middleditch | d30fd57 | 2009-03-14 12:55:17 -0400 | [diff] [blame] | 608 | if (telnet->state == LIBTELNET_STATE_DATA && i != start) |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 609 | _event(telnet, LIBTELNET_EV_DATA, 0, 0, buffer + start, i - start); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 610 | } |
| 611 | |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 612 | /* push a bytes into the state tracker */ |
Sean Middleditch | 97a8cb2 | 2009-03-16 16:51:41 -0400 | [diff] [blame^] | 613 | void libtelnet_push(libtelnet_t *telnet, const unsigned char *buffer, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 614 | unsigned int size) { |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 615 | #ifdef HAVE_ZLIB |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 616 | /* if we have an inflate (decompression) zlib stream, use it */ |
Sean Middleditch | 4611d91 | 2009-03-16 00:53:57 -0400 | [diff] [blame] | 617 | if (telnet->z != 0 && !(telnet->flags & LIBTELNET_PFLAG_DEFLATE)) { |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 618 | unsigned char inflate_buffer[4096]; |
| 619 | int rs; |
| 620 | |
| 621 | /* initialize zlib state */ |
Sean Middleditch | 97a8cb2 | 2009-03-16 16:51:41 -0400 | [diff] [blame^] | 622 | telnet->z->next_in = (unsigned char *)buffer; |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 623 | telnet->z->avail_in = size; |
| 624 | telnet->z->next_out = inflate_buffer; |
| 625 | telnet->z->avail_out = sizeof(inflate_buffer); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 626 | |
| 627 | /* inflate until buffer exhausted and all output is produced */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 628 | while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) { |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 629 | /* reset output buffer */ |
| 630 | |
| 631 | /* decompress */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 632 | rs = inflate(telnet->z, Z_SYNC_FLUSH); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 633 | |
| 634 | /* process the decompressed bytes on success */ |
| 635 | if (rs == Z_OK || rs == Z_STREAM_END) |
| 636 | _process(telnet, inflate_buffer, sizeof(inflate_buffer) - |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 637 | telnet->z->avail_out); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 638 | else |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 639 | _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, 1, |
| 640 | "inflate() failed: %s", zError(rs)); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 641 | |
| 642 | /* prepare output buffer for next run */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 643 | telnet->z->next_out = inflate_buffer; |
| 644 | telnet->z->avail_out = sizeof(inflate_buffer); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 645 | |
| 646 | /* on error (or on end of stream) disable further inflation */ |
| 647 | if (rs != Z_OK) { |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 648 | _event(telnet, LIBTELNET_EV_COMPRESS, 0, 0, 0, 0); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 649 | |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 650 | inflateEnd(telnet->z); |
| 651 | free(telnet->z); |
| 652 | telnet->z = 0; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 653 | break; |
| 654 | } |
| 655 | } |
| 656 | |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 657 | /* COMPRESS2 is not negotiated, just process */ |
| 658 | } else |
| 659 | #endif /* HAVE_ZLIB */ |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 660 | _process(telnet, buffer, size); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 661 | } |
| 662 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 663 | /* send an iac command */ |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 664 | void libtelnet_send_command(libtelnet_t *telnet, unsigned char cmd) { |
Sean Middleditch | f9cebec | 2009-03-13 20:17:31 -0400 | [diff] [blame] | 665 | unsigned char bytes[2] = { LIBTELNET_IAC, cmd }; |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 666 | _send(telnet, bytes, 2); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 667 | } |
| 668 | |
Sean Middleditch | 2b4bfc4 | 2009-03-16 01:25:52 -0400 | [diff] [blame] | 669 | /* send an iac command with telopt */ |
| 670 | void libtelnet_send_telopt(libtelnet_t *telnet, unsigned char cmd, |
| 671 | unsigned char telopt) { |
| 672 | unsigned char bytes[3] = { LIBTELNET_IAC, cmd, telopt }; |
| 673 | _send(telnet, bytes, 3); |
| 674 | } |
| 675 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 676 | /* send negotiation */ |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 677 | void libtelnet_send_negotiate(libtelnet_t *telnet, unsigned char cmd, |
Sean Middleditch | 8b78896 | 2009-03-16 01:06:27 -0400 | [diff] [blame] | 678 | unsigned char telopt) { |
| 679 | libtelnet_rfc1143_t q; |
| 680 | |
| 681 | /* if we're in proxy mode, just send it now */ |
| 682 | if (telnet->flags & LIBTELNET_FLAG_PROXY) { |
| 683 | unsigned char bytes[3] = { LIBTELNET_IAC, cmd, telopt }; |
| 684 | _send(telnet, bytes, 3); |
| 685 | return; |
| 686 | } |
| 687 | |
| 688 | /* get current option states */ |
| 689 | q = _get_rfc1143(telnet, telopt); |
| 690 | |
| 691 | switch (cmd) { |
| 692 | /* advertise willingess to support an option */ |
| 693 | case LIBTELNET_WILL: |
| 694 | switch (q.us) { |
| 695 | case RFC1143_NO: |
| 696 | q.us = RFC1143_WANTYES; |
| 697 | _set_rfc1143(telnet, q); |
| 698 | _negotiate(telnet, LIBTELNET_WILL, telopt); |
| 699 | break; |
| 700 | case RFC1143_YES: |
| 701 | break; |
| 702 | case RFC1143_WANTNO: |
| 703 | q.us = RFC1143_WANTNO_OP; |
| 704 | _set_rfc1143(telnet, q); |
| 705 | break; |
| 706 | case RFC1143_WANTYES: |
| 707 | break; |
| 708 | case RFC1143_WANTNO_OP: |
| 709 | break; |
| 710 | case RFC1143_WANTYES_OP: |
| 711 | q.us = RFC1143_WANTYES; |
| 712 | _set_rfc1143(telnet, q); |
| 713 | break; |
| 714 | } |
| 715 | break; |
| 716 | |
| 717 | /* force turn-off of locally enabled option */ |
| 718 | case LIBTELNET_WONT: |
| 719 | switch (q.us) { |
| 720 | case RFC1143_NO: |
| 721 | break; |
| 722 | case RFC1143_YES: |
| 723 | q.us = RFC1143_WANTNO; |
| 724 | _set_rfc1143(telnet, q); |
| 725 | _negotiate(telnet, LIBTELNET_WONT, telopt); |
| 726 | break; |
| 727 | case RFC1143_WANTNO: |
| 728 | break; |
| 729 | case RFC1143_WANTYES: |
| 730 | q.us = RFC1143_WANTYES_OP; |
| 731 | _set_rfc1143(telnet, q); |
| 732 | break; |
| 733 | case RFC1143_WANTNO_OP: |
| 734 | q.us = RFC1143_WANTNO; |
| 735 | _set_rfc1143(telnet, q); |
| 736 | break; |
| 737 | case RFC1143_WANTYES_OP: |
| 738 | break; |
| 739 | } |
| 740 | break; |
| 741 | |
| 742 | /* ask remote end to enable an option */ |
| 743 | case LIBTELNET_DO: |
| 744 | switch (q.him) { |
| 745 | case RFC1143_NO: |
| 746 | q.him = RFC1143_WANTYES; |
| 747 | _set_rfc1143(telnet, q); |
| 748 | _negotiate(telnet, LIBTELNET_DO, telopt); |
| 749 | break; |
| 750 | case RFC1143_YES: |
| 751 | break; |
| 752 | case RFC1143_WANTNO: |
| 753 | q.him = RFC1143_WANTNO_OP; |
| 754 | _set_rfc1143(telnet, q); |
| 755 | break; |
| 756 | case RFC1143_WANTYES: |
| 757 | break; |
| 758 | case RFC1143_WANTNO_OP: |
| 759 | break; |
| 760 | case RFC1143_WANTYES_OP: |
| 761 | q.him = RFC1143_WANTYES; |
| 762 | _set_rfc1143(telnet, q); |
| 763 | break; |
| 764 | } |
| 765 | break; |
| 766 | |
| 767 | /* demand remote end disable an option */ |
| 768 | case LIBTELNET_DONT: |
| 769 | switch (q.him) { |
| 770 | case RFC1143_NO: |
| 771 | break; |
| 772 | case RFC1143_YES: |
| 773 | q.him = RFC1143_WANTNO; |
| 774 | _set_rfc1143(telnet, q); |
| 775 | _negotiate(telnet, LIBTELNET_DONT, telopt); |
| 776 | break; |
| 777 | case RFC1143_WANTNO: |
| 778 | break; |
| 779 | case RFC1143_WANTYES: |
| 780 | q.him = RFC1143_WANTYES_OP; |
| 781 | _set_rfc1143(telnet, q); |
| 782 | break; |
| 783 | case RFC1143_WANTNO_OP: |
| 784 | q.him = RFC1143_WANTNO; |
| 785 | _set_rfc1143(telnet, q); |
| 786 | break; |
| 787 | case RFC1143_WANTYES_OP: |
| 788 | break; |
| 789 | } |
| 790 | break; |
| 791 | } |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | /* send non-command data (escapes IAC bytes) */ |
Sean Middleditch | 97a8cb2 | 2009-03-16 16:51:41 -0400 | [diff] [blame^] | 795 | void libtelnet_send_data(libtelnet_t *telnet, const unsigned char *buffer, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 796 | unsigned int size) { |
Sean Middleditch | 97a8cb2 | 2009-03-16 16:51:41 -0400 | [diff] [blame^] | 797 | static const unsigned char CRLF[] = { '\r', '\n' }; |
| 798 | static const unsigned char CRNUL[] = { '\r', '\0' }; |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 799 | unsigned int i, l; |
Sean Middleditch | c337ba6 | 2009-03-16 16:47:27 -0400 | [diff] [blame] | 800 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 801 | for (l = i = 0; i != size; ++i) { |
| 802 | /* dump prior portion of text, send escaped bytes */ |
| 803 | if (buffer[i] == LIBTELNET_IAC) { |
| 804 | /* dump prior text if any */ |
| 805 | if (i != l) |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 806 | _send(telnet, buffer + l, i - l); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 807 | l = i + 1; |
| 808 | |
| 809 | /* send escape */ |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 810 | libtelnet_send_command(telnet, LIBTELNET_IAC); |
Sean Middleditch | c337ba6 | 2009-03-16 16:47:27 -0400 | [diff] [blame] | 811 | |
| 812 | /* automatic translation of \n -> CRLF and \r -> CRNUL */ |
| 813 | } else if (telnet->flags & LIBTELNET_FLAG_AUTO_CRLF && |
| 814 | (buffer[i] == '\r' || buffer[i] == '\n')) { |
| 815 | /* dump prior text if any */ |
| 816 | if (i != l) |
| 817 | _send(telnet, buffer + l, i - l); |
| 818 | l = i + 1; |
| 819 | |
| 820 | /* translate */ |
| 821 | if (buffer[i] == '\r') |
| 822 | _send(telnet, CRNUL, 2); |
| 823 | else |
| 824 | _send(telnet, CRLF, 2); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 825 | } |
| 826 | } |
| 827 | |
| 828 | /* send whatever portion of buffer is left */ |
| 829 | if (i != l) |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 830 | _send(telnet, buffer + l, i - l); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | /* send sub-request */ |
Sean Middleditch | 2b4bfc4 | 2009-03-16 01:25:52 -0400 | [diff] [blame] | 834 | void libtelnet_send_subnegotiation(libtelnet_t *telnet, unsigned char telopt, |
Sean Middleditch | 97a8cb2 | 2009-03-16 16:51:41 -0400 | [diff] [blame^] | 835 | const unsigned char *buffer, unsigned int size) { |
Sean Middleditch | 2b4bfc4 | 2009-03-16 01:25:52 -0400 | [diff] [blame] | 836 | libtelnet_send_telopt(telnet, LIBTELNET_SB, telopt); |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 837 | libtelnet_send_data(telnet, buffer, size); |
| 838 | libtelnet_send_command(telnet, LIBTELNET_SE); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 839 | |
| 840 | #ifdef HAVE_ZLIB |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 841 | /* if we're a proxy and we just sent the COMPRESS2 marker, we must |
| 842 | * make sure all further data is compressed if not already. |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 843 | */ |
Sean Middleditch | 08bb05f | 2009-03-15 23:29:46 -0400 | [diff] [blame] | 844 | if (telnet->flags & LIBTELNET_FLAG_PROXY && |
Sean Middleditch | 2b4bfc4 | 2009-03-16 01:25:52 -0400 | [diff] [blame] | 845 | telopt == LIBTELNET_TELOPT_COMPRESS2) { |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 846 | |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 847 | if (_init_zlib(telnet, 1, 1) != LIBTELNET_EOK) |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 848 | return; |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 849 | |
| 850 | /* notify app that compression was enabled */ |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 851 | _event(telnet, LIBTELNET_EV_COMPRESS, 1, 0, 0, 0); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 852 | } |
| 853 | #endif /* HAVE_ZLIB */ |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 854 | } |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 855 | |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 856 | void libtelnet_begin_compress2(libtelnet_t *telnet) { |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 857 | #ifdef HAVE_ZLIB |
Sean Middleditch | 97a8cb2 | 2009-03-16 16:51:41 -0400 | [diff] [blame^] | 858 | static const unsigned char compress2[] = { LIBTELNET_IAC, LIBTELNET_SB, |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 859 | LIBTELNET_TELOPT_COMPRESS2, LIBTELNET_IAC, LIBTELNET_SE }; |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 860 | |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 861 | /* attempt to create output stream first, bail if we can't */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 862 | if (_init_zlib(telnet, 1, 0) != LIBTELNET_EOK) |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 863 | return; |
| 864 | |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 865 | /* send compression marker. we send directly to the event handler |
| 866 | * instead of passing through _send because _send would result in |
| 867 | * the compress marker itself being compressed. |
| 868 | */ |
| 869 | _event(telnet, LIBTELNET_EV_SEND, 0, 0, compress2, sizeof(compress2)); |
Sean Middleditch | 2b4bfc4 | 2009-03-16 01:25:52 -0400 | [diff] [blame] | 870 | |
| 871 | /* notify app that compression was successfully enabled */ |
| 872 | _event(telnet, LIBTELNET_EV_COMPRESS, 1, 0, 0, 0); |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 873 | #endif /* HAVE_ZLIB */ |
| 874 | } |
Sean Middleditch | d58f49f | 2009-03-16 12:49:35 -0400 | [diff] [blame] | 875 | |
| 876 | /* send formatted data through libtelnet_send_data */ |
| 877 | int libtelnet_send_printf(libtelnet_t *telnet, const char *fmt, ...) { |
| 878 | char buffer[4096]; |
| 879 | va_list va; |
| 880 | int rs; |
| 881 | |
| 882 | /* format */ |
| 883 | va_start(va, fmt); |
| 884 | rs = vsnprintf(buffer, sizeof(buffer), fmt, va); |
| 885 | va_end(va); |
| 886 | |
| 887 | /* send */ |
| 888 | libtelnet_send_data(telnet, (unsigned char *)buffer, rs); |
| 889 | |
| 890 | return rs; |
| 891 | } |