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 | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 25 | /* buffer sizes */ |
| 26 | static const unsigned int _buffer_sizes[] = { |
| 27 | 0, |
| 28 | 512, |
| 29 | 2048, |
| 30 | 8192, |
| 31 | 16384, |
| 32 | }; |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 33 | static const unsigned int _buffer_sizes_count = sizeof(_buffer_sizes) / |
| 34 | sizeof(_buffer_sizes[0]); |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 35 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 36 | /* event dispatch helper */ |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 37 | static void _event(libtelnet_t *telnet, libtelnet_event_type_t type, |
| 38 | unsigned char command, unsigned char telopt, unsigned char *buffer, |
| 39 | unsigned int size) { |
| 40 | libtelnet_event_t ev; |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 41 | ev.type = type; |
| 42 | ev.command = command; |
| 43 | ev.telopt = telopt; |
| 44 | ev.buffer = buffer; |
| 45 | ev.size = size; |
| 46 | |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 47 | telnet->eh(telnet, &ev, telnet->ud); |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 48 | } |
| 49 | |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 50 | /* error generation function */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 51 | static libtelnet_error_t _error(libtelnet_t *telnet, unsigned line, |
| 52 | const char* func, libtelnet_error_t err, int fatal, const char *fmt, |
| 53 | ...) { |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 54 | char buffer[512]; |
| 55 | va_list va; |
| 56 | |
| 57 | /* format error intro */ |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 58 | snprintf(buffer, sizeof(buffer), "%s:%u in %s: ", __FILE__, line, func); |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 59 | |
| 60 | va_start(va, fmt); |
| 61 | vsnprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), |
| 62 | fmt, va); |
| 63 | va_end(va); |
| 64 | |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 65 | _event(telnet, fatal ? LIBTELNET_EV_ERROR : LIBTELNET_EV_WARNING, err, |
Sean Middleditch | 5cf6666 | 2009-03-15 20:02:14 -0400 | [diff] [blame] | 66 | 0, (unsigned char *)buffer, strlen(buffer)); |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 67 | |
| 68 | return err; |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 69 | } |
| 70 | |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 71 | /* initialize the zlib box for a telnet box; if deflate is non-zero, it |
| 72 | * initializes zlib for delating (compression), otherwise for inflating |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 73 | * (decompression). returns LIBTELNET_EOK on success, something else on |
| 74 | * failure. |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 75 | */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 76 | libtelnet_error_t _init_zlib(libtelnet_t *telnet, int deflate, int err_fatal) { |
| 77 | z_stream *z; |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 78 | int rs; |
| 79 | |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 80 | /* if compression is already enabled, fail loudly */ |
| 81 | if (telnet->z != 0) |
| 82 | return _error(telnet, __LINE__, __func__, LIBTELNET_EBADVAL, |
| 83 | err_fatal, "cannot initialize compression twice"); |
| 84 | |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 85 | /* allocate zstream box */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 86 | if ((z= (z_stream *)calloc(1, sizeof(z_stream))) == 0) |
| 87 | return _error(telnet, __LINE__, __func__, LIBTELNET_ENOMEM, err_fatal, |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 88 | "malloc() failed: %s", strerror(errno)); |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 89 | |
| 90 | /* initialize */ |
| 91 | if (deflate) { |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 92 | if ((rs = deflateInit(z, Z_DEFAULT_COMPRESSION)) != Z_OK) { |
| 93 | free(z); |
| 94 | return _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, |
| 95 | err_fatal, "deflateInit() failed: %s", zError(rs)); |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 96 | } |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 97 | telnet->flags |= LIBTELNET_PFLAG_DEFLATE; |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 98 | } else { |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 99 | if ((rs = inflateInit(z)) != Z_OK) { |
| 100 | free(z); |
| 101 | return _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, |
| 102 | err_fatal, "inflateInit() failed: %s", zError(rs)); |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 103 | } |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 104 | telnet->flags &= ~LIBTELNET_PFLAG_DEFLATE; |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 105 | } |
| 106 | |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 107 | telnet->z = z; |
| 108 | |
| 109 | return LIBTELNET_EOK; |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 110 | } |
| 111 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 112 | /* initialize a telnet state tracker */ |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 113 | void libtelnet_init(libtelnet_t *telnet, libtelnet_event_handler_t eh, |
Sean Middleditch | 08bb05f | 2009-03-15 23:29:46 -0400 | [diff] [blame] | 114 | unsigned char flags, void *user_data) { |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 115 | memset(telnet, 0, sizeof(libtelnet_t)); |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 116 | telnet->ud = user_data; |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 117 | telnet->eh = eh; |
Sean Middleditch | 08bb05f | 2009-03-15 23:29:46 -0400 | [diff] [blame] | 118 | telnet->flags = flags; |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /* free up any memory allocated by a state tracker */ |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 122 | void libtelnet_free(libtelnet_t *telnet) { |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 123 | /* free sub-request buffer */ |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 124 | if (telnet->buffer != 0) { |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 125 | free(telnet->buffer); |
| 126 | telnet->buffer = 0; |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 127 | telnet->size = 0; |
| 128 | telnet->length = 0; |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 129 | } |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 130 | |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 131 | /* free zlib box */ |
| 132 | if (telnet->z != 0) { |
| 133 | if (telnet->flags & LIBTELNET_PFLAG_DEFLATE) |
| 134 | deflateEnd(telnet->z); |
| 135 | else |
| 136 | inflateEnd(telnet->z); |
| 137 | free(telnet->z); |
| 138 | telnet->z = 0; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 139 | } |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 140 | } |
| 141 | |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 142 | /* push a byte into the telnet buffer */ |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 143 | static libtelnet_error_t _buffer_byte(libtelnet_t *telnet, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 144 | unsigned char byte) { |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 145 | unsigned char *new_buffer; |
| 146 | int i; |
| 147 | |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 148 | /* check if we're out of room */ |
| 149 | if (telnet->length == telnet->size) { |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 150 | /* find the next buffer size */ |
| 151 | for (i = 0; i != _buffer_sizes_count; ++i) { |
| 152 | if (_buffer_sizes[i] == telnet->size) |
| 153 | break; |
| 154 | } |
| 155 | |
| 156 | /* overflow -- can't grow any more */ |
| 157 | if (i >= _buffer_sizes_count - 1) { |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 158 | _error(telnet, __LINE__, __func__, LIBTELNET_EOVERFLOW, 0, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 159 | "subnegotiation buffer size limit reached"); |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 160 | libtelnet_free(telnet); |
Sean Middleditch | f66a7ee | 2009-03-15 11:54:07 -0400 | [diff] [blame] | 161 | return LIBTELNET_EOVERFLOW; |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 162 | } |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 163 | |
| 164 | /* (re)allocate buffer */ |
| 165 | new_buffer = (unsigned char *)realloc(telnet->buffer, |
| 166 | _buffer_sizes[i + 1]); |
| 167 | if (new_buffer == 0) { |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 168 | _error(telnet, __LINE__, __func__, LIBTELNET_ENOMEM, 0, |
| 169 | "realloc() failed"); |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 170 | libtelnet_free(telnet); |
Sean Middleditch | f66a7ee | 2009-03-15 11:54:07 -0400 | [diff] [blame] | 171 | return LIBTELNET_ENOMEM; |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | telnet->buffer = new_buffer; |
| 175 | telnet->size = _buffer_sizes[i + 1]; |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /* push the byte, all set */ |
| 179 | telnet->buffer[telnet->length++] = byte; |
Sean Middleditch | f66a7ee | 2009-03-15 11:54:07 -0400 | [diff] [blame] | 180 | return LIBTELNET_EOK; |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 181 | } |
| 182 | |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 183 | static void _process(libtelnet_t *telnet, unsigned char *buffer, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 184 | unsigned int size) { |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 185 | unsigned char byte; |
| 186 | unsigned int i, start; |
| 187 | for (i = start = 0; i != size; ++i) { |
| 188 | byte = buffer[i]; |
| 189 | switch (telnet->state) { |
| 190 | /* regular data */ |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 191 | case LIBTELNET_STATE_DATA: |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 192 | /* on an IAC byte, pass through all pending bytes and |
| 193 | * switch states */ |
| 194 | if (byte == LIBTELNET_IAC) { |
| 195 | if (i != start) |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 196 | _event(telnet, LIBTELNET_EV_DATA, 0, 0, &buffer[start], |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 197 | i - start); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 198 | telnet->state = LIBTELNET_STATE_IAC; |
| 199 | } |
| 200 | break; |
| 201 | |
| 202 | /* IAC command */ |
| 203 | case LIBTELNET_STATE_IAC: |
| 204 | switch (byte) { |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 205 | /* subnegotiation */ |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 206 | case LIBTELNET_SB: |
| 207 | telnet->state = LIBTELNET_STATE_SB; |
| 208 | break; |
| 209 | /* negotiation commands */ |
| 210 | case LIBTELNET_WILL: |
| 211 | telnet->state = LIBTELNET_STATE_WILL; |
| 212 | break; |
| 213 | case LIBTELNET_WONT: |
| 214 | telnet->state = LIBTELNET_STATE_WONT; |
| 215 | break; |
| 216 | case LIBTELNET_DO: |
| 217 | telnet->state = LIBTELNET_STATE_DO; |
| 218 | break; |
| 219 | case LIBTELNET_DONT: |
| 220 | telnet->state = LIBTELNET_STATE_DONT; |
| 221 | break; |
| 222 | /* IAC escaping */ |
| 223 | case LIBTELNET_IAC: |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 224 | _event(telnet, LIBTELNET_EV_DATA, 0, 0, &byte, 1); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 225 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 226 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 227 | break; |
| 228 | /* some other command */ |
| 229 | default: |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 230 | _event(telnet, LIBTELNET_EV_IAC, byte, 0, 0, 0); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 231 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 232 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 233 | } |
| 234 | break; |
| 235 | |
| 236 | /* negotiation commands */ |
| 237 | case LIBTELNET_STATE_DO: |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 238 | _event(telnet, LIBTELNET_EV_NEGOTIATE, LIBTELNET_DO, byte, 0, 0); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 239 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 240 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 241 | break; |
| 242 | case LIBTELNET_STATE_DONT: |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 243 | _event(telnet, LIBTELNET_EV_NEGOTIATE, LIBTELNET_DONT, byte, 0, 0); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 244 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 245 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 246 | break; |
| 247 | case LIBTELNET_STATE_WILL: |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 248 | _event(telnet, LIBTELNET_EV_NEGOTIATE, LIBTELNET_WILL, byte, 0, 0); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 249 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 250 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 251 | break; |
| 252 | case LIBTELNET_STATE_WONT: |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 253 | _event(telnet, LIBTELNET_EV_NEGOTIATE, LIBTELNET_WONT, byte, 0, 0); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 254 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 255 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 256 | break; |
| 257 | |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame] | 258 | /* subnegotiation -- determine subnegotiation telopt */ |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 259 | case LIBTELNET_STATE_SB: |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame] | 260 | telnet->sb_telopt = byte; |
| 261 | telnet->length = 0; |
| 262 | telnet->state = LIBTELNET_STATE_SB_DATA; |
| 263 | break; |
| 264 | |
| 265 | /* subnegotiation -- buffer bytes until end request */ |
| 266 | case LIBTELNET_STATE_SB_DATA: |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 267 | /* IAC command in subnegotiation -- either IAC SE or IAC IAC */ |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 268 | if (byte == LIBTELNET_IAC) { |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame] | 269 | telnet->state = LIBTELNET_STATE_SB_DATA_IAC; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 270 | /* buffer the byte, or bail if we can't */ |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 271 | } else if (_buffer_byte(telnet, byte) != LIBTELNET_EOK) { |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 272 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 273 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 274 | } |
| 275 | break; |
| 276 | |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 277 | /* IAC escaping inside a subnegotiation */ |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame] | 278 | case LIBTELNET_STATE_SB_DATA_IAC: |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 279 | switch (byte) { |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 280 | /* end subnegotiation */ |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 281 | case LIBTELNET_SE: |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 282 | /* return to default state */ |
| 283 | start = i + 1; |
| 284 | telnet->state = LIBTELNET_STATE_DATA; |
| 285 | |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 286 | /* invoke callback */ |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 287 | _event(telnet, LIBTELNET_EV_SUBNEGOTIATION, 0, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 288 | telnet->sb_telopt, telnet->buffer, telnet->length); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 289 | |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 290 | #ifdef HAVE_ZLIB |
Sean Middleditch | e68511b | 2009-03-16 01:12:50 -0400 | [diff] [blame^] | 291 | /* received COMPRESS2 begin marker, setup our zlib box and |
| 292 | * start handling the compressed stream if it's not already. |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 293 | */ |
Sean Middleditch | e68511b | 2009-03-16 01:12:50 -0400 | [diff] [blame^] | 294 | if (telnet->sb_telopt == LIBTELNET_TELOPT_COMPRESS2) { |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 295 | if (_init_zlib(telnet, 0, 1) != LIBTELNET_EOK) |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 296 | break; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 297 | |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 298 | /* notify app that compression was enabled */ |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 299 | _event(telnet, LIBTELNET_EV_COMPRESS, 1, 0, 0, 0); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 300 | |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 301 | /* any remaining bytes in the buffer are compressed. |
| 302 | * we have to re-invoke libtelnet_push to get those |
| 303 | * bytes inflated and abort trying to process the |
| 304 | * remaining compressed bytes in the current _process |
| 305 | * buffer argument |
| 306 | */ |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 307 | libtelnet_push(telnet, &buffer[start], size - start); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 308 | return; |
| 309 | } |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 310 | #endif /* HAVE_ZLIB */ |
| 311 | |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 312 | break; |
| 313 | /* escaped IAC byte */ |
| 314 | case LIBTELNET_IAC: |
| 315 | /* push IAC into buffer */ |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 316 | if (_buffer_byte(telnet, LIBTELNET_IAC) != |
Sean Middleditch | f66a7ee | 2009-03-15 11:54:07 -0400 | [diff] [blame] | 317 | LIBTELNET_EOK) { |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 318 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 319 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 320 | } else { |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame] | 321 | telnet->state = LIBTELNET_STATE_SB_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 322 | } |
| 323 | break; |
| 324 | /* something else -- protocol error */ |
| 325 | default: |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 326 | _error(telnet, __LINE__, __func__, LIBTELNET_EPROTOCOL, 0, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 327 | "unexpected byte after IAC inside SB: %d", |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 328 | byte); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 329 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 330 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 331 | break; |
| 332 | } |
| 333 | break; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | /* pass through any remaining bytes */ |
Sean Middleditch | d30fd57 | 2009-03-14 12:55:17 -0400 | [diff] [blame] | 338 | if (telnet->state == LIBTELNET_STATE_DATA && i != start) |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 339 | _event(telnet, LIBTELNET_EV_DATA, 0, 0, buffer + start, i - start); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 340 | } |
| 341 | |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 342 | /* push a bytes into the state tracker */ |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 343 | void libtelnet_push(libtelnet_t *telnet, unsigned char *buffer, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 344 | unsigned int size) { |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 345 | #ifdef HAVE_ZLIB |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 346 | /* if we have an inflate (decompression) zlib stream, use it */ |
Sean Middleditch | 4611d91 | 2009-03-16 00:53:57 -0400 | [diff] [blame] | 347 | if (telnet->z != 0 && !(telnet->flags & LIBTELNET_PFLAG_DEFLATE)) { |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 348 | unsigned char inflate_buffer[4096]; |
| 349 | int rs; |
| 350 | |
| 351 | /* initialize zlib state */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 352 | telnet->z->next_in = buffer; |
| 353 | telnet->z->avail_in = size; |
| 354 | telnet->z->next_out = inflate_buffer; |
| 355 | telnet->z->avail_out = sizeof(inflate_buffer); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 356 | |
| 357 | /* inflate until buffer exhausted and all output is produced */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 358 | while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) { |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 359 | /* reset output buffer */ |
| 360 | |
| 361 | /* decompress */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 362 | rs = inflate(telnet->z, Z_SYNC_FLUSH); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 363 | |
| 364 | /* process the decompressed bytes on success */ |
| 365 | if (rs == Z_OK || rs == Z_STREAM_END) |
| 366 | _process(telnet, inflate_buffer, sizeof(inflate_buffer) - |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 367 | telnet->z->avail_out); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 368 | else |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 369 | _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, 1, |
| 370 | "inflate() failed: %s", zError(rs)); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 371 | |
| 372 | /* prepare output buffer for next run */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 373 | telnet->z->next_out = inflate_buffer; |
| 374 | telnet->z->avail_out = sizeof(inflate_buffer); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 375 | |
| 376 | /* on error (or on end of stream) disable further inflation */ |
| 377 | if (rs != Z_OK) { |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 378 | _event(telnet, LIBTELNET_EV_COMPRESS, 0, 0, 0, 0); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 379 | |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 380 | inflateEnd(telnet->z); |
| 381 | free(telnet->z); |
| 382 | telnet->z = 0; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 383 | break; |
| 384 | } |
| 385 | } |
| 386 | |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 387 | /* COMPRESS2 is not negotiated, just process */ |
| 388 | } else |
| 389 | #endif /* HAVE_ZLIB */ |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 390 | _process(telnet, buffer, size); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 391 | } |
| 392 | |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 393 | static void _send(libtelnet_t *telnet, unsigned char *buffer, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 394 | unsigned int size) { |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 395 | #ifdef HAVE_ZLIB |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 396 | /* if we have a deflate (compression) zlib box, use it */ |
Sean Middleditch | 4611d91 | 2009-03-16 00:53:57 -0400 | [diff] [blame] | 397 | if (telnet->z != 0 && telnet->flags & LIBTELNET_PFLAG_DEFLATE) { |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 398 | unsigned char deflate_buffer[1024]; |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 399 | int rs; |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 400 | |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 401 | /* initialize z state */ |
| 402 | telnet->z->next_in = buffer; |
| 403 | telnet->z->avail_in = size; |
| 404 | telnet->z->next_out = deflate_buffer; |
| 405 | telnet->z->avail_out = sizeof(deflate_buffer); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 406 | |
| 407 | /* deflate until buffer exhausted and all output is produced */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 408 | while (telnet->z->avail_in > 0 || telnet->z->avail_out == 0) { |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 409 | /* compress */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 410 | if ((rs = deflate(telnet->z, Z_SYNC_FLUSH)) != Z_OK) { |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 411 | _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, 1, |
| 412 | "deflate() failed: %s", zError(rs)); |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 413 | deflateEnd(telnet->z); |
| 414 | free(telnet->z); |
| 415 | telnet->z = 0; |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 416 | break; |
| 417 | } |
| 418 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 419 | _event(telnet, LIBTELNET_EV_SEND, 0, 0, deflate_buffer, |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 420 | sizeof(deflate_buffer) - telnet->z->avail_out); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 421 | |
| 422 | /* prepare output buffer for next run */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 423 | telnet->z->next_out = deflate_buffer; |
| 424 | telnet->z->avail_out = sizeof(deflate_buffer); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /* COMPRESS2 is not negotiated, just send */ |
| 428 | } else |
| 429 | #endif /* HAVE_ZLIB */ |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 430 | _event(telnet, LIBTELNET_EV_SEND, 0, 0, buffer, size); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 431 | } |
| 432 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 433 | /* send an iac command */ |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 434 | void libtelnet_send_command(libtelnet_t *telnet, unsigned char cmd) { |
Sean Middleditch | f9cebec | 2009-03-13 20:17:31 -0400 | [diff] [blame] | 435 | unsigned char bytes[2] = { LIBTELNET_IAC, cmd }; |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 436 | _send(telnet, bytes, 2); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /* send negotiation */ |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 440 | void libtelnet_send_negotiate(libtelnet_t *telnet, unsigned char cmd, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 441 | unsigned char opt) { |
Sean Middleditch | f9cebec | 2009-03-13 20:17:31 -0400 | [diff] [blame] | 442 | unsigned char bytes[3] = { LIBTELNET_IAC, cmd, opt }; |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 443 | _send(telnet, bytes, 3); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /* send non-command data (escapes IAC bytes) */ |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 447 | void libtelnet_send_data(libtelnet_t *telnet, unsigned char *buffer, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 448 | unsigned int size) { |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 449 | unsigned int i, l; |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 450 | for (l = i = 0; i != size; ++i) { |
| 451 | /* dump prior portion of text, send escaped bytes */ |
| 452 | if (buffer[i] == LIBTELNET_IAC) { |
| 453 | /* dump prior text if any */ |
| 454 | if (i != l) |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 455 | _send(telnet, buffer + l, i - l); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 456 | l = i + 1; |
| 457 | |
| 458 | /* send escape */ |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 459 | libtelnet_send_command(telnet, LIBTELNET_IAC); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
| 463 | /* send whatever portion of buffer is left */ |
| 464 | if (i != l) |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 465 | _send(telnet, buffer + l, i - l); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | /* send sub-request */ |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 469 | void libtelnet_send_subnegotiation(libtelnet_t *telnet, unsigned char opt, |
| 470 | unsigned char *buffer, unsigned int size) { |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 471 | libtelnet_send_command(telnet, LIBTELNET_SB); |
| 472 | libtelnet_send_data(telnet, &opt, 1); |
| 473 | libtelnet_send_data(telnet, buffer, size); |
| 474 | libtelnet_send_command(telnet, LIBTELNET_SE); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 475 | |
| 476 | #ifdef HAVE_ZLIB |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 477 | /* if we're a proxy and we just sent the COMPRESS2 marker, we must |
| 478 | * make sure all further data is compressed if not already. |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 479 | */ |
Sean Middleditch | 08bb05f | 2009-03-15 23:29:46 -0400 | [diff] [blame] | 480 | if (telnet->flags & LIBTELNET_FLAG_PROXY && |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 481 | telnet->z == 0 && |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 482 | opt == LIBTELNET_TELOPT_COMPRESS2) { |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 483 | |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 484 | if (_init_zlib(telnet, 1, 1) != LIBTELNET_EOK) |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 485 | return; |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 486 | |
| 487 | /* notify app that compression was enabled */ |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 488 | _event(telnet, LIBTELNET_EV_COMPRESS, 1, 0, 0, 0); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 489 | } |
| 490 | #endif /* HAVE_ZLIB */ |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 491 | } |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 492 | |
Sean Middleditch | 812358d | 2009-03-15 23:24:03 -0400 | [diff] [blame] | 493 | void libtelnet_begin_compress2(libtelnet_t *telnet) { |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 494 | #ifdef HAVE_ZLIB |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 495 | unsigned char compress2[] = { LIBTELNET_IAC, LIBTELNET_SB, |
| 496 | LIBTELNET_TELOPT_COMPRESS2, LIBTELNET_IAC, LIBTELNET_SE }; |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 497 | |
| 498 | /* don't do this if we've already got a compression stream */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 499 | if (telnet->z != 0) { |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 500 | _error(telnet, __LINE__, __func__, LIBTELNET_EBADVAL, 0, |
| 501 | "compression already enabled"); |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 502 | return; |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 503 | } |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 504 | |
| 505 | /* attempt to create output stream first, bail if we can't */ |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 506 | if (_init_zlib(telnet, 1, 0) != LIBTELNET_EOK) |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 507 | return; |
| 508 | |
Sean Middleditch | fbe93e3 | 2009-03-15 23:39:31 -0400 | [diff] [blame] | 509 | /* send compression marker. we send directly to the event handler |
| 510 | * instead of passing through _send because _send would result in |
| 511 | * the compress marker itself being compressed. |
| 512 | */ |
| 513 | _event(telnet, LIBTELNET_EV_SEND, 0, 0, compress2, sizeof(compress2)); |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 514 | #endif /* HAVE_ZLIB */ |
| 515 | } |