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