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 | }; |
| 33 | static const unsigned int _buffer_sizes_count = |
| 34 | sizeof(_buffer_sizes) / sizeof(_buffer_sizes[0]); |
| 35 | |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 36 | /* event dispatch helper */ |
| 37 | static void _event(struct libtelnet_t *telnet, |
| 38 | enum libtelnet_event_type_t type, unsigned char command, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 39 | unsigned char telopt, unsigned char *buffer, unsigned int size) { |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 40 | struct libtelnet_event_t ev; |
| 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 | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 51 | static void _error(struct libtelnet_t *telnet, unsigned line, const char* func, |
| 52 | enum 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 | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 57 | snprintf(buffer, sizeof(buffer), "%s:%u in %s: ", |
| 58 | __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 | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 67 | } |
| 68 | |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 69 | /* initialize the zlib box for a telnet box; if deflate is non-zero, it |
| 70 | * initializes zlib for delating (compression), otherwise for inflating |
| 71 | * (decompression) |
| 72 | */ |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 73 | z_stream *_init_zlib(struct libtelnet_t *telnet, int deflate, int err_fatal) { |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 74 | z_stream *zlib; |
| 75 | int rs; |
| 76 | |
| 77 | /* allocate zstream box */ |
| 78 | if ((zlib = (z_stream *)calloc(1, sizeof(z_stream))) |
| 79 | == 0) { |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 80 | _error(telnet, __LINE__, __func__, LIBTELNET_ENOMEM, err_fatal, |
| 81 | "malloc() failed: %s", strerror(errno)); |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | /* initialize */ |
| 86 | if (deflate) { |
| 87 | if ((rs = deflateInit(zlib, Z_DEFAULT_COMPRESSION)) != Z_OK) { |
| 88 | free(zlib); |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 89 | _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, err_fatal, |
| 90 | "deflateInit() failed: %s", zError(rs)); |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 91 | return 0; |
| 92 | } |
| 93 | } else { |
| 94 | if ((rs = inflateInit(zlib)) != Z_OK) { |
| 95 | free(zlib); |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 96 | _error(telnet, __LINE__, __func__, LIBTELNET_ECOMPRESS, err_fatal, |
| 97 | "inflateInit() failed: %s", zError(rs)); |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 98 | return 0; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return zlib; |
| 103 | } |
| 104 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 105 | /* initialize a telnet state tracker */ |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 106 | void libtelnet_init(struct libtelnet_t *telnet, libtelnet_event_handler_t eh, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 107 | enum libtelnet_mode_t mode, void *user_data) { |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 108 | memset(telnet, 0, sizeof(struct libtelnet_t)); |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 109 | telnet->ud = user_data; |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 110 | telnet->eh = eh; |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 111 | telnet->mode = mode; |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /* free up any memory allocated by a state tracker */ |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 115 | void libtelnet_free(struct libtelnet_t *telnet) { |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 116 | /* free sub-request buffer */ |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 117 | if (telnet->buffer != 0) { |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 118 | free(telnet->buffer); |
| 119 | telnet->buffer = 0; |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 120 | telnet->size = 0; |
| 121 | telnet->length = 0; |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 122 | } |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 123 | |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 124 | /* free zlib box(es) */ |
| 125 | if (telnet->z_inflate != 0) { |
| 126 | inflateEnd(telnet->z_inflate); |
| 127 | free(telnet->z_inflate); |
| 128 | telnet->z_inflate = 0; |
| 129 | } |
| 130 | if (telnet->z_deflate != 0) { |
| 131 | deflateEnd(telnet->z_deflate); |
| 132 | free(telnet->z_deflate); |
| 133 | telnet->z_deflate = 0; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 134 | } |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 135 | } |
| 136 | |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 137 | /* push a byte into the telnet buffer */ |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 138 | static enum libtelnet_error_t _buffer_byte(struct libtelnet_t *telnet, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 139 | unsigned char byte) { |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 140 | unsigned char *new_buffer; |
| 141 | int i; |
| 142 | |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 143 | /* check if we're out of room */ |
| 144 | if (telnet->length == telnet->size) { |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 145 | /* find the next buffer size */ |
| 146 | for (i = 0; i != _buffer_sizes_count; ++i) { |
| 147 | if (_buffer_sizes[i] == telnet->size) |
| 148 | break; |
| 149 | } |
| 150 | |
| 151 | /* overflow -- can't grow any more */ |
| 152 | if (i >= _buffer_sizes_count - 1) { |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 153 | _error(telnet, __LINE__, __func__, LIBTELNET_EOVERFLOW, 0, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 154 | "subnegotiation buffer size limit reached"); |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 155 | libtelnet_free(telnet); |
Sean Middleditch | f66a7ee | 2009-03-15 11:54:07 -0400 | [diff] [blame] | 156 | return LIBTELNET_EOVERFLOW; |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 157 | } |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 158 | |
| 159 | /* (re)allocate buffer */ |
| 160 | new_buffer = (unsigned char *)realloc(telnet->buffer, |
| 161 | _buffer_sizes[i + 1]); |
| 162 | if (new_buffer == 0) { |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 163 | _error(telnet, __LINE__, __func__, LIBTELNET_ENOMEM, 0, |
| 164 | "realloc() failed"); |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 165 | libtelnet_free(telnet); |
Sean Middleditch | f66a7ee | 2009-03-15 11:54:07 -0400 | [diff] [blame] | 166 | return LIBTELNET_ENOMEM; |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | telnet->buffer = new_buffer; |
| 170 | telnet->size = _buffer_sizes[i + 1]; |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /* push the byte, all set */ |
| 174 | telnet->buffer[telnet->length++] = byte; |
Sean Middleditch | f66a7ee | 2009-03-15 11:54:07 -0400 | [diff] [blame] | 175 | return LIBTELNET_EOK; |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 176 | } |
| 177 | |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 178 | static void _process(struct libtelnet_t *telnet, unsigned char *buffer, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 179 | unsigned int size) { |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 180 | unsigned char byte; |
| 181 | unsigned int i, start; |
| 182 | for (i = start = 0; i != size; ++i) { |
| 183 | byte = buffer[i]; |
| 184 | switch (telnet->state) { |
| 185 | /* regular data */ |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 186 | case LIBTELNET_STATE_DATA: |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 187 | /* on an IAC byte, pass through all pending bytes and |
| 188 | * switch states */ |
| 189 | if (byte == LIBTELNET_IAC) { |
| 190 | if (i != start) |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 191 | _event(telnet, LIBTELNET_EV_DATA, 0, 0, &buffer[start], |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 192 | i - start); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 193 | telnet->state = LIBTELNET_STATE_IAC; |
| 194 | } |
| 195 | break; |
| 196 | |
| 197 | /* IAC command */ |
| 198 | case LIBTELNET_STATE_IAC: |
| 199 | switch (byte) { |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 200 | /* subnegotiation */ |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 201 | case LIBTELNET_SB: |
| 202 | telnet->state = LIBTELNET_STATE_SB; |
| 203 | break; |
| 204 | /* negotiation commands */ |
| 205 | case LIBTELNET_WILL: |
| 206 | telnet->state = LIBTELNET_STATE_WILL; |
| 207 | break; |
| 208 | case LIBTELNET_WONT: |
| 209 | telnet->state = LIBTELNET_STATE_WONT; |
| 210 | break; |
| 211 | case LIBTELNET_DO: |
| 212 | telnet->state = LIBTELNET_STATE_DO; |
| 213 | break; |
| 214 | case LIBTELNET_DONT: |
| 215 | telnet->state = LIBTELNET_STATE_DONT; |
| 216 | break; |
| 217 | /* IAC escaping */ |
| 218 | case LIBTELNET_IAC: |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 219 | _event(telnet, LIBTELNET_EV_DATA, 0, 0, &byte, 1); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 220 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 221 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 222 | break; |
| 223 | /* some other command */ |
| 224 | default: |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 225 | _event(telnet, LIBTELNET_EV_IAC, byte, 0, 0, 0); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 226 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 227 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 228 | } |
| 229 | break; |
| 230 | |
| 231 | /* negotiation commands */ |
| 232 | case LIBTELNET_STATE_DO: |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 233 | _event(telnet, LIBTELNET_EV_NEGOTIATE, LIBTELNET_DO, byte, 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 | break; |
| 237 | case LIBTELNET_STATE_DONT: |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 238 | _event(telnet, LIBTELNET_EV_NEGOTIATE, LIBTELNET_DONT, 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_WILL: |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 243 | _event(telnet, LIBTELNET_EV_NEGOTIATE, LIBTELNET_WILL, 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_WONT: |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 248 | _event(telnet, LIBTELNET_EV_NEGOTIATE, LIBTELNET_WONT, 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 | |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame] | 253 | /* subnegotiation -- determine subnegotiation telopt */ |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 254 | case LIBTELNET_STATE_SB: |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame] | 255 | telnet->sb_telopt = byte; |
| 256 | telnet->length = 0; |
| 257 | telnet->state = LIBTELNET_STATE_SB_DATA; |
| 258 | break; |
| 259 | |
| 260 | /* subnegotiation -- buffer bytes until end request */ |
| 261 | case LIBTELNET_STATE_SB_DATA: |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 262 | /* IAC command in subnegotiation -- either IAC SE or IAC IAC */ |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 263 | if (byte == LIBTELNET_IAC) { |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame] | 264 | telnet->state = LIBTELNET_STATE_SB_DATA_IAC; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 265 | /* buffer the byte, or bail if we can't */ |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 266 | } else if (_buffer_byte(telnet, byte) != LIBTELNET_EOK) { |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 267 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 268 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 269 | } |
| 270 | break; |
| 271 | |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 272 | /* IAC escaping inside a subnegotiation */ |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame] | 273 | case LIBTELNET_STATE_SB_DATA_IAC: |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 274 | switch (byte) { |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 275 | /* end subnegotiation */ |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 276 | case LIBTELNET_SE: |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 277 | /* return to default state */ |
| 278 | start = i + 1; |
| 279 | telnet->state = LIBTELNET_STATE_DATA; |
| 280 | |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 281 | /* invoke callback */ |
Sean Middleditch | 637df7f | 2009-03-15 12:57:32 -0400 | [diff] [blame] | 282 | _event(telnet, LIBTELNET_EV_SUBNEGOTIATION, 0, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 283 | telnet->sb_telopt, telnet->buffer, telnet->length); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 284 | |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 285 | #ifdef HAVE_ZLIB |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 286 | /* if we are a client or a proxy and just received the |
| 287 | * COMPRESS2 begin marker, setup our zlib box and start |
| 288 | * handling the compressed stream if it's not already. |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 289 | */ |
Sean Middleditch | da0e695 | 2009-03-15 13:28:09 -0400 | [diff] [blame] | 290 | if (telnet->sb_telopt == LIBTELNET_TELOPT_COMPRESS2 && |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 291 | telnet->z_inflate == 0 && |
| 292 | (telnet->mode == LIBTELNET_MODE_CLIENT || |
| 293 | telnet->mode == LIBTELNET_MODE_PROXY)) { |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 294 | |
Sean Middleditch | 5cf6666 | 2009-03-15 20:02:14 -0400 | [diff] [blame] | 295 | if ((telnet->z_inflate = _init_zlib(telnet, 0, 1)) == 0) |
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 */ |
| 343 | void libtelnet_push(struct 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 */ |
| 347 | if (telnet->z_inflate != 0) { |
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 | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 352 | telnet->z_inflate->next_in = buffer; |
| 353 | telnet->z_inflate->avail_in = size; |
| 354 | telnet->z_inflate->next_out = inflate_buffer; |
| 355 | telnet->z_inflate->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 | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 358 | 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] | 359 | /* reset output buffer */ |
| 360 | |
| 361 | /* decompress */ |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 362 | rs = inflate(telnet->z_inflate, 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 | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 367 | telnet->z_inflate->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 | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 373 | telnet->z_inflate->next_out = inflate_buffer; |
| 374 | telnet->z_inflate->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 | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 380 | inflateEnd(telnet->z_inflate); |
| 381 | free(telnet->z_inflate); |
| 382 | telnet->z_inflate = 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 | |
| 393 | static void _send(struct 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 */ |
| 397 | if (telnet->z_deflate != 0) { |
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 | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 401 | /* initialize z_deflate state */ |
| 402 | telnet->z_deflate->next_in = buffer; |
| 403 | telnet->z_deflate->avail_in = size; |
| 404 | telnet->z_deflate->next_out = deflate_buffer; |
| 405 | telnet->z_deflate->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 | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 408 | 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] | 409 | /* compress */ |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 410 | if ((rs = deflate(telnet->z_deflate, 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 | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 413 | deflateEnd(telnet->z_deflate); |
| 414 | free(telnet->z_deflate); |
| 415 | telnet->z_deflate = 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 | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 420 | sizeof(deflate_buffer) - telnet->z_deflate->avail_out); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 421 | |
| 422 | /* prepare output buffer for next run */ |
Sean Middleditch | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 423 | telnet->z_deflate->next_out = deflate_buffer; |
| 424 | telnet->z_deflate->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 | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 434 | void libtelnet_send_command(struct 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 */ |
| 440 | void libtelnet_send_negotiate(struct 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) */ |
| 447 | void libtelnet_send_data(struct 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 | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 469 | void libtelnet_send_subnegotiation(struct libtelnet_t *telnet, |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 470 | unsigned char opt, unsigned char *buffer, unsigned int size) { |
| 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 | 72cc964 | 2009-03-15 11:50:36 -0400 | [diff] [blame] | 480 | if (telnet->mode == LIBTELNET_MODE_PROXY && |
| 481 | telnet->z_deflate == 0 && |
| 482 | opt == LIBTELNET_TELOPT_COMPRESS2) { |
Sean Middleditch | d922c6f | 2009-03-14 22:35:01 -0400 | [diff] [blame] | 483 | |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 484 | if ((telnet->z_deflate = _init_zlib(telnet, 1, 1)) == 0) |
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 | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 493 | void libtelnet_begin_compress2(struct libtelnet_t *telnet) { |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 494 | #ifdef HAVE_ZLIB |
| 495 | z_stream *zlib; |
| 496 | |
| 497 | /* don't do this if we've already got a compression stream */ |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 498 | if (telnet->z_deflate != 0) { |
| 499 | _error(telnet, __LINE__, __func__, LIBTELNET_EBADVAL, 0, |
| 500 | "compression already enabled"); |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 501 | return; |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 502 | } |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 503 | |
| 504 | /* only supported by servers */ |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 505 | if (telnet->mode != LIBTELNET_MODE_SERVER) { |
| 506 | _error(telnet, __LINE__, __func__, LIBTELNET_EBADVAL, 0, |
| 507 | "only supported in SERVER mode"); |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 508 | return; |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 509 | } |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 510 | |
| 511 | /* attempt to create output stream first, bail if we can't */ |
Sean Middleditch | 1699227 | 2009-03-15 19:42:03 -0400 | [diff] [blame] | 512 | if ((zlib = _init_zlib(telnet, 1, 0)) == 0) |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 513 | return; |
| 514 | |
| 515 | /* send compression marker */ |
Sean Middleditch | 9f79cc5 | 2009-03-15 13:39:24 -0400 | [diff] [blame] | 516 | libtelnet_send_subnegotiation(telnet, LIBTELNET_TELOPT_COMPRESS2, 0, 0); |
Sean Middleditch | 124a1c2 | 2009-03-15 13:20:03 -0400 | [diff] [blame] | 517 | |
| 518 | /* set our deflate stream */ |
| 519 | telnet->z_deflate = zlib; |
| 520 | #endif /* HAVE_ZLIB */ |
| 521 | } |