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> |
| 14 | |
| 15 | #ifdef HAVE_ZLIB |
| 16 | #include "zlib.h" |
| 17 | #endif |
| 18 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 19 | #include "libtelnet.h" |
| 20 | |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 21 | /* buffer sizes */ |
| 22 | static const unsigned int _buffer_sizes[] = { |
| 23 | 0, |
| 24 | 512, |
| 25 | 2048, |
| 26 | 8192, |
| 27 | 16384, |
| 28 | }; |
| 29 | static const unsigned int _buffer_sizes_count = |
| 30 | sizeof(_buffer_sizes) / sizeof(_buffer_sizes[0]); |
| 31 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 32 | /* initialize a telnet state tracker */ |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 33 | void libtelnet_init(struct libtelnet_t *telnet, enum libtelnet_mode_t mode) { |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 34 | memset(telnet, 0, sizeof(struct libtelnet_t)); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 35 | telnet->mode = mode; |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | /* free up any memory allocated by a state tracker */ |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 39 | void libtelnet_free(struct libtelnet_t *telnet) { |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 40 | /* free sub-request buffer */ |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 41 | if (telnet->buffer != 0) { |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 42 | free(telnet->buffer); |
| 43 | telnet->buffer = 0; |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 44 | telnet->size = 0; |
| 45 | telnet->length = 0; |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 46 | } |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 47 | |
| 48 | /* free zlib box */ |
| 49 | if (telnet->zlib != 0) { |
Sean Middleditch | 2d432c5 | 2009-03-14 11:32:58 -0400 | [diff] [blame] | 50 | if (telnet->mode == LIBTELNET_MODE_SERVER) |
| 51 | deflateEnd(telnet->zlib); |
| 52 | else |
| 53 | inflateEnd(telnet->zlib); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 54 | free(telnet->zlib); |
| 55 | telnet->zlib = 0; |
| 56 | } |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 57 | } |
| 58 | |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 59 | /* push a byte into the telnet buffer */ |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 60 | static enum libtelnet_error_t _buffer_byte( |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 61 | struct libtelnet_t *telnet, unsigned char byte, void *user_data) { |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 62 | unsigned char *new_buffer; |
| 63 | int i; |
| 64 | |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 65 | /* check if we're out of room */ |
| 66 | if (telnet->length == telnet->size) { |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 67 | /* find the next buffer size */ |
| 68 | for (i = 0; i != _buffer_sizes_count; ++i) { |
| 69 | if (_buffer_sizes[i] == telnet->size) |
| 70 | break; |
| 71 | } |
| 72 | |
| 73 | /* overflow -- can't grow any more */ |
| 74 | if (i >= _buffer_sizes_count - 1) { |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 75 | libtelnet_error_cb(telnet, LIBTELNET_ERROR_OVERFLOW, user_data); |
| 76 | libtelnet_free(telnet); |
| 77 | return LIBTELNET_ERROR_OVERFLOW; |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 78 | } |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 79 | |
| 80 | /* (re)allocate buffer */ |
| 81 | new_buffer = (unsigned char *)realloc(telnet->buffer, |
| 82 | _buffer_sizes[i + 1]); |
| 83 | if (new_buffer == 0) { |
| 84 | libtelnet_error_cb(telnet, LIBTELNET_ERROR_NOMEM, |
| 85 | user_data); |
| 86 | libtelnet_free(telnet); |
| 87 | return LIBTELNET_ERROR_NOMEM; |
| 88 | } |
| 89 | |
| 90 | telnet->buffer = new_buffer; |
| 91 | telnet->size = _buffer_sizes[i + 1]; |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /* push the byte, all set */ |
| 95 | telnet->buffer[telnet->length++] = byte; |
| 96 | return LIBTELNET_ERROR_OK; |
| 97 | } |
| 98 | |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 99 | static void _process(struct libtelnet_t *telnet, unsigned char *buffer, |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 100 | unsigned int size, void *user_data) { |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 101 | unsigned char byte; |
| 102 | unsigned int i, start; |
| 103 | for (i = start = 0; i != size; ++i) { |
| 104 | byte = buffer[i]; |
| 105 | switch (telnet->state) { |
| 106 | /* regular data */ |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 107 | case LIBTELNET_STATE_DATA: |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 108 | /* on an IAC byte, pass through all pending bytes and |
| 109 | * switch states */ |
| 110 | if (byte == LIBTELNET_IAC) { |
| 111 | if (i != start) |
Sean Middleditch | 78f400f | 2009-03-14 01:26:43 -0400 | [diff] [blame] | 112 | libtelnet_data_cb(telnet, &buffer[start], i - start, |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 113 | user_data); |
| 114 | telnet->state = LIBTELNET_STATE_IAC; |
| 115 | } |
| 116 | break; |
| 117 | |
| 118 | /* IAC command */ |
| 119 | case LIBTELNET_STATE_IAC: |
| 120 | switch (byte) { |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 121 | /* subnegotiation */ |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 122 | case LIBTELNET_SB: |
| 123 | telnet->state = LIBTELNET_STATE_SB; |
| 124 | break; |
| 125 | /* negotiation commands */ |
| 126 | case LIBTELNET_WILL: |
| 127 | telnet->state = LIBTELNET_STATE_WILL; |
| 128 | break; |
| 129 | case LIBTELNET_WONT: |
| 130 | telnet->state = LIBTELNET_STATE_WONT; |
| 131 | break; |
| 132 | case LIBTELNET_DO: |
| 133 | telnet->state = LIBTELNET_STATE_DO; |
| 134 | break; |
| 135 | case LIBTELNET_DONT: |
| 136 | telnet->state = LIBTELNET_STATE_DONT; |
| 137 | break; |
| 138 | /* IAC escaping */ |
| 139 | case LIBTELNET_IAC: |
Sean Middleditch | 78f400f | 2009-03-14 01:26:43 -0400 | [diff] [blame] | 140 | libtelnet_data_cb(telnet, &byte, 1, user_data); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 141 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 142 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 143 | break; |
| 144 | /* some other command */ |
| 145 | default: |
| 146 | libtelnet_command_cb(telnet, byte, user_data); |
| 147 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 148 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 149 | } |
| 150 | break; |
| 151 | |
| 152 | /* negotiation commands */ |
| 153 | case LIBTELNET_STATE_DO: |
| 154 | libtelnet_negotiate_cb(telnet, LIBTELNET_DO, byte, user_data); |
| 155 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 156 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 157 | break; |
| 158 | case LIBTELNET_STATE_DONT: |
| 159 | libtelnet_negotiate_cb(telnet, LIBTELNET_DONT, byte, user_data); |
| 160 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 161 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 162 | break; |
| 163 | case LIBTELNET_STATE_WILL: |
| 164 | libtelnet_negotiate_cb(telnet, LIBTELNET_WILL, byte, user_data); |
| 165 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 166 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 167 | break; |
| 168 | case LIBTELNET_STATE_WONT: |
| 169 | libtelnet_negotiate_cb(telnet, LIBTELNET_WONT, byte, user_data); |
| 170 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 171 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 172 | break; |
| 173 | |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 174 | /* subnegotiation -- buffer bytes until end request */ |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 175 | case LIBTELNET_STATE_SB: |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 176 | /* IAC command in subnegotiation -- either IAC SE or IAC IAC */ |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 177 | if (byte == LIBTELNET_IAC) { |
| 178 | telnet->state = LIBTELNET_STATE_SB_IAC; |
| 179 | /* buffer the byte, or bail if we can't */ |
Sean Middleditch | 7e517ec | 2009-03-14 00:18:30 -0400 | [diff] [blame] | 180 | } else if (_buffer_byte(telnet, byte, user_data) != |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 181 | LIBTELNET_ERROR_OK) { |
| 182 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 183 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 184 | } |
| 185 | break; |
| 186 | |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 187 | /* IAC escaping inside a subnegotiation */ |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 188 | case LIBTELNET_STATE_SB_IAC: |
| 189 | switch (byte) { |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 190 | /* end subnegotiation */ |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 191 | case LIBTELNET_SE: |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 192 | /* return to default state */ |
| 193 | start = i + 1; |
| 194 | telnet->state = LIBTELNET_STATE_DATA; |
| 195 | |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 196 | /* zero-size buffer is a protocol error */ |
| 197 | if (telnet->length == 0) { |
| 198 | libtelnet_error_cb(telnet, LIBTELNET_ERROR_PROTOCOL, |
| 199 | user_data); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 200 | break; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 201 | } |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 202 | |
| 203 | /* invoke callback */ |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 204 | libtelnet_subnegotiation_cb(telnet, telnet->buffer[0], |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 205 | telnet->buffer + 1, telnet->length - 1, user_data); |
| 206 | telnet->length = 0; |
| 207 | |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 208 | #ifdef HAVE_ZLIB |
| 209 | /* if we are a client and just received the COMPRESS2 |
| 210 | * begin marker, setup our zlib box and start handling |
| 211 | * the compressed stream |
| 212 | */ |
| 213 | if (telnet->mode == LIBTELNET_MODE_CLIENT && |
| 214 | telnet->buffer[0] == LIBTELNET_OPTION_COMPRESS2) { |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 215 | /* allocate zstream box */ |
| 216 | if ((telnet->zlib = (z_stream *)malloc(sizeof(z_stream))) |
| 217 | == 0) { |
| 218 | libtelnet_error_cb(telnet, |
| 219 | LIBTELNET_ERROR_NOMEM, user_data); |
| 220 | } |
| 221 | |
| 222 | /* initialize */ |
| 223 | memset(telnet->zlib, 0, sizeof(z_stream)); |
| 224 | if (inflateInit(telnet->zlib) != Z_OK) { |
| 225 | free(telnet->zlib); |
| 226 | telnet->zlib = 0; |
| 227 | libtelnet_error_cb(telnet, |
| 228 | LIBTELNET_ERROR_UNKNOWN, user_data); |
| 229 | break; |
| 230 | } |
| 231 | |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 232 | /* notify app that compression was enabled */ |
| 233 | libtelnet_compress_cb(telnet, 1, user_data); |
| 234 | |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 235 | /* any remaining bytes in the buffer are compressed. |
| 236 | * we have to re-invoke libtelnet_push to get those |
| 237 | * bytes inflated and abort trying to process the |
| 238 | * remaining compressed bytes in the current _process |
| 239 | * buffer argument |
| 240 | */ |
Sean Middleditch | 2d432c5 | 2009-03-14 11:32:58 -0400 | [diff] [blame] | 241 | libtelnet_push(telnet, &buffer[start], size - start, |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 242 | user_data); |
| 243 | return; |
| 244 | } |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 245 | #endif /* HAVE_ZLIB */ |
| 246 | |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 247 | break; |
| 248 | /* escaped IAC byte */ |
| 249 | case LIBTELNET_IAC: |
| 250 | /* push IAC into buffer */ |
| 251 | if (_buffer_byte(telnet, LIBTELNET_IAC, user_data) != |
| 252 | LIBTELNET_ERROR_OK) { |
| 253 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 254 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 255 | } else { |
| 256 | telnet->state = LIBTELNET_STATE_SB; |
| 257 | } |
| 258 | break; |
| 259 | /* something else -- protocol error */ |
| 260 | default: |
| 261 | libtelnet_error_cb(telnet, LIBTELNET_ERROR_PROTOCOL, |
| 262 | user_data); |
| 263 | start = i + 1; |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 264 | telnet->state = LIBTELNET_STATE_DATA; |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 265 | break; |
| 266 | } |
| 267 | break; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /* pass through any remaining bytes */ |
Sean Middleditch | d30fd57 | 2009-03-14 12:55:17 -0400 | [diff] [blame] | 272 | if (telnet->state == LIBTELNET_STATE_DATA && i != start) |
Sean Middleditch | 78f400f | 2009-03-14 01:26:43 -0400 | [diff] [blame] | 273 | libtelnet_data_cb(telnet, &buffer[start], i - start, user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 274 | } |
| 275 | |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 276 | /* push a bytes into the state tracker */ |
| 277 | void libtelnet_push(struct libtelnet_t *telnet, unsigned char *buffer, |
| 278 | unsigned int size, void *user_data) { |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 279 | #ifdef HAVE_ZLIB |
| 280 | /* if we are a client and we have a zlib box, then COMPRESS2 has been |
| 281 | * negotiated and we need to inflate the buffer before processing |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 282 | */ |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 283 | if (telnet->mode == LIBTELNET_MODE_CLIENT && telnet->zlib != 0) { |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 284 | unsigned char inflate_buffer[4096]; |
| 285 | int rs; |
| 286 | |
| 287 | /* initialize zlib state */ |
| 288 | telnet->zlib->next_in = buffer; |
| 289 | telnet->zlib->avail_in = size; |
| 290 | telnet->zlib->next_out = inflate_buffer; |
| 291 | telnet->zlib->avail_out = sizeof(inflate_buffer); |
| 292 | |
| 293 | /* inflate until buffer exhausted and all output is produced */ |
| 294 | while (telnet->zlib->avail_in > 0 || telnet->zlib->avail_out == 0) { |
| 295 | /* reset output buffer */ |
| 296 | |
| 297 | /* decompress */ |
| 298 | rs = inflate(telnet->zlib, Z_SYNC_FLUSH); |
| 299 | |
| 300 | /* process the decompressed bytes on success */ |
| 301 | if (rs == Z_OK || rs == Z_STREAM_END) |
| 302 | _process(telnet, inflate_buffer, sizeof(inflate_buffer) - |
| 303 | telnet->zlib->avail_out, user_data); |
| 304 | else |
| 305 | libtelnet_error_cb(telnet, LIBTELNET_ERROR_UNKNOWN, |
| 306 | user_data); |
| 307 | |
| 308 | /* prepare output buffer for next run */ |
| 309 | telnet->zlib->next_out = inflate_buffer; |
| 310 | telnet->zlib->avail_out = sizeof(inflate_buffer); |
| 311 | |
| 312 | /* on error (or on end of stream) disable further inflation */ |
| 313 | if (rs != Z_OK) { |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 314 | libtelnet_compress_cb(telnet, 0, user_data); |
| 315 | |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 316 | inflateEnd(telnet->zlib); |
| 317 | free(telnet->zlib); |
| 318 | telnet->zlib = 0; |
| 319 | break; |
| 320 | } |
| 321 | } |
| 322 | |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 323 | /* COMPRESS2 is not negotiated, just process */ |
| 324 | } else |
| 325 | #endif /* HAVE_ZLIB */ |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 326 | _process(telnet, buffer, size, user_data); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | static void _send(struct libtelnet_t *telnet, unsigned char *buffer, |
| 330 | unsigned int size, void *user_data) { |
| 331 | #ifdef HAVE_ZLIB |
| 332 | /* if we are a server and we have a zlib box, then COMPRESS2 has been |
| 333 | * negotiated and we need to deflate the buffer before sending it out |
| 334 | */ |
| 335 | if (telnet->mode == LIBTELNET_MODE_SERVER && telnet->zlib != 0) { |
| 336 | unsigned char deflate_buffer[1024]; |
| 337 | |
| 338 | /* initialize zlib state */ |
| 339 | telnet->zlib->next_in = buffer; |
| 340 | telnet->zlib->avail_in = size; |
| 341 | telnet->zlib->next_out = deflate_buffer; |
| 342 | telnet->zlib->avail_out = sizeof(deflate_buffer); |
| 343 | |
| 344 | /* deflate until buffer exhausted and all output is produced */ |
| 345 | while (telnet->zlib->avail_in > 0 || telnet->zlib->avail_out == 0) { |
| 346 | /* reset output buffer */ |
| 347 | |
| 348 | /* compress */ |
| 349 | if (deflate(telnet->zlib, Z_SYNC_FLUSH) != Z_OK) { |
| 350 | libtelnet_error_cb(telnet, LIBTELNET_ERROR_UNKNOWN, |
| 351 | user_data); |
| 352 | deflateEnd(telnet->zlib); |
| 353 | free(telnet->zlib); |
| 354 | telnet->zlib = 0; |
| 355 | break; |
| 356 | } |
| 357 | |
| 358 | libtelnet_send_cb(telnet, deflate_buffer, sizeof(deflate_buffer) - |
| 359 | telnet->zlib->avail_out, user_data); |
| 360 | |
| 361 | /* prepare output buffer for next run */ |
| 362 | telnet->zlib->next_out = deflate_buffer; |
| 363 | telnet->zlib->avail_out = sizeof(deflate_buffer); |
| 364 | } |
| 365 | |
| 366 | /* COMPRESS2 is not negotiated, just send */ |
| 367 | } else |
| 368 | #endif /* HAVE_ZLIB */ |
| 369 | libtelnet_send_cb(telnet, buffer, size, user_data); |
Sean Middleditch | 9de1598 | 2009-03-14 03:35:49 -0400 | [diff] [blame] | 370 | } |
| 371 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 372 | /* send an iac command */ |
| 373 | void libtelnet_send_command(struct libtelnet_t *telnet, unsigned char cmd, |
| 374 | void *user_data) { |
Sean Middleditch | f9cebec | 2009-03-13 20:17:31 -0400 | [diff] [blame] | 375 | unsigned char bytes[2] = { LIBTELNET_IAC, cmd }; |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 376 | _send(telnet, bytes, 2, user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /* send negotiation */ |
| 380 | void libtelnet_send_negotiate(struct libtelnet_t *telnet, unsigned char cmd, |
| 381 | unsigned char opt, void *user_data) { |
Sean Middleditch | f9cebec | 2009-03-13 20:17:31 -0400 | [diff] [blame] | 382 | unsigned char bytes[3] = { LIBTELNET_IAC, cmd, opt }; |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 383 | _send(telnet, bytes, 3, user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /* send non-command data (escapes IAC bytes) */ |
| 387 | void libtelnet_send_data(struct libtelnet_t *telnet, unsigned char *buffer, |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 388 | unsigned int size, void *user_data) { |
| 389 | unsigned int i, l; |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 390 | for (l = i = 0; i != size; ++i) { |
| 391 | /* dump prior portion of text, send escaped bytes */ |
| 392 | if (buffer[i] == LIBTELNET_IAC) { |
| 393 | /* dump prior text if any */ |
| 394 | if (i != l) |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 395 | _send(telnet, buffer + l, i - l, user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 396 | l = i + 1; |
| 397 | |
| 398 | /* send escape */ |
| 399 | libtelnet_send_command(telnet, LIBTELNET_IAC, user_data); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | /* send whatever portion of buffer is left */ |
| 404 | if (i != l) |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 405 | _send(telnet, buffer + l, i - l, user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | /* send sub-request */ |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 409 | void libtelnet_send_subnegotiation(struct libtelnet_t *telnet, |
Sean Middleditch | c974b97 | 2009-03-14 13:23:33 -0400 | [diff] [blame^] | 410 | unsigned char opt, unsigned char *buffer, unsigned int size, |
Sean Middleditch | 6b37288 | 2009-03-14 13:06:47 -0400 | [diff] [blame] | 411 | void *user_data) { |
Sean Middleditch | f9cebec | 2009-03-13 20:17:31 -0400 | [diff] [blame] | 412 | libtelnet_send_command(telnet, LIBTELNET_SB, user_data); |
Sean Middleditch | c974b97 | 2009-03-14 13:23:33 -0400 | [diff] [blame^] | 413 | libtelnet_send_data(telnet, &opt, 1, user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 414 | libtelnet_send_data(telnet, buffer, size, user_data); |
Sean Middleditch | f9cebec | 2009-03-13 20:17:31 -0400 | [diff] [blame] | 415 | libtelnet_send_command(telnet, LIBTELNET_SE, user_data); |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 416 | |
| 417 | #ifdef HAVE_ZLIB |
| 418 | /* if we're a server and we just sent the COMPRESS2 marker, we must |
| 419 | * make sure all further data is compressed |
| 420 | */ |
Sean Middleditch | c974b97 | 2009-03-14 13:23:33 -0400 | [diff] [blame^] | 421 | if (telnet->mode == LIBTELNET_MODE_SERVER && opt == |
Sean Middleditch | 61f8eb6 | 2009-03-14 04:57:27 -0400 | [diff] [blame] | 422 | LIBTELNET_OPTION_COMPRESS2) { |
| 423 | /* allocate zstream box */ |
| 424 | if ((telnet->zlib = (z_stream *)malloc(sizeof(z_stream))) |
| 425 | == 0) { |
| 426 | libtelnet_error_cb(telnet, |
| 427 | LIBTELNET_ERROR_NOMEM, user_data); |
| 428 | } |
| 429 | |
| 430 | /* initialize */ |
| 431 | memset(telnet->zlib, 0, sizeof(z_stream)); |
| 432 | if (deflateInit(telnet->zlib, Z_DEFAULT_COMPRESSION) != Z_OK) { |
| 433 | free(telnet->zlib); |
| 434 | telnet->zlib = 0; |
| 435 | libtelnet_error_cb(telnet, |
| 436 | LIBTELNET_ERROR_UNKNOWN, user_data); |
| 437 | } |
| 438 | |
| 439 | /* notify app that compression was enabled */ |
| 440 | libtelnet_compress_cb(telnet, 1, user_data); |
| 441 | } |
| 442 | #endif /* HAVE_ZLIB */ |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 443 | } |