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