Sean Middleditch | 6aef073 | 2009-03-12 23:27:35 -0400 | [diff] [blame] | 1 | /* |
| 2 | * The author or authors of this code dedicate any and all copyright interest |
| 3 | * in this code to the public domain. We make this dedication for the benefit |
| 4 | * of the public at large and to the detriment of our heirs and successors. We |
| 5 | * intend this dedication to be an overt act of relinquishment in perpetuity of |
| 6 | * all present and future rights to this code under copyright law. |
| 7 | */ |
| 8 | |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 9 | #include <malloc.h> |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 10 | #include "libtelnet.h" |
| 11 | |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 12 | /* buffer sizes */ |
| 13 | static const unsigned int _buffer_sizes[] = { |
| 14 | 0, |
| 15 | 512, |
| 16 | 2048, |
| 17 | 8192, |
| 18 | 16384, |
| 19 | }; |
| 20 | static const unsigned int _buffer_sizes_count = |
| 21 | sizeof(_buffer_sizes) / sizeof(_buffer_sizes[0]); |
| 22 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 23 | /* initialize a telnet state tracker */ |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 24 | void libtelnet_init(struct libtelnet_t *telnet) { |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 25 | telnet->state = LIBTELNET_STATE_TEXT; |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 26 | telnet->buffer = 0; |
| 27 | telnet->size = 0; |
| 28 | telnet->length = 0; |
| 29 | } |
| 30 | |
| 31 | /* free up any memory allocated by a state tracker */ |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 32 | void libtelnet_free(struct libtelnet_t *telnet) { |
| 33 | if (telnet->buffer != 0) { |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 34 | free(telnet->buffer); |
| 35 | telnet->buffer = 0; |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 36 | telnet->size = 0; |
| 37 | telnet->length = 0; |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 41 | /* push a byte into the telnet buffer */ |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 42 | static enum libtelnet_error_t _buffer_byte( |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 43 | struct libtelnet_t *telnet, unsigned char byte, void *user_data) { |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 44 | unsigned char *new_buffer; |
| 45 | int i; |
| 46 | |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 47 | /* check if we're out of room */ |
| 48 | if (telnet->length == telnet->size) { |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 49 | /* find the next buffer size */ |
| 50 | for (i = 0; i != _buffer_sizes_count; ++i) { |
| 51 | if (_buffer_sizes[i] == telnet->size) |
| 52 | break; |
| 53 | } |
| 54 | |
| 55 | /* overflow -- can't grow any more */ |
| 56 | if (i >= _buffer_sizes_count - 1) { |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 57 | libtelnet_error_cb(telnet, LIBTELNET_ERROR_OVERFLOW, user_data); |
| 58 | libtelnet_free(telnet); |
| 59 | return LIBTELNET_ERROR_OVERFLOW; |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 60 | } |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 61 | |
| 62 | /* (re)allocate buffer */ |
| 63 | new_buffer = (unsigned char *)realloc(telnet->buffer, |
| 64 | _buffer_sizes[i + 1]); |
| 65 | if (new_buffer == 0) { |
| 66 | libtelnet_error_cb(telnet, LIBTELNET_ERROR_NOMEM, |
| 67 | user_data); |
| 68 | libtelnet_free(telnet); |
| 69 | return LIBTELNET_ERROR_NOMEM; |
| 70 | } |
| 71 | |
| 72 | telnet->buffer = new_buffer; |
| 73 | telnet->size = _buffer_sizes[i + 1]; |
Sean Middleditch | 51ad679 | 2009-03-13 20:15:59 -0400 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /* push the byte, all set */ |
| 77 | telnet->buffer[telnet->length++] = byte; |
| 78 | return LIBTELNET_ERROR_OK; |
| 79 | } |
| 80 | |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 81 | /* push a single byte into the state tracker */ |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 82 | void libtelnet_push(struct libtelnet_t *telnet, unsigned char *buffer, |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 83 | unsigned int size, void *user_data) { |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 84 | unsigned char byte; |
| 85 | unsigned int i, start; |
| 86 | for (i = start = 0; i != size; ++i) { |
| 87 | byte = buffer[i]; |
| 88 | switch (telnet->state) { |
| 89 | /* regular data */ |
| 90 | case LIBTELNET_STATE_TEXT: |
| 91 | /* on an IAC byte, pass through all pending bytes and |
| 92 | * switch states */ |
| 93 | if (byte == LIBTELNET_IAC) { |
| 94 | if (i != start) |
Sean Middleditch | 78f400f | 2009-03-14 01:26:43 -0400 | [diff] [blame] | 95 | libtelnet_data_cb(telnet, &buffer[start], i - start, |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 96 | user_data); |
| 97 | telnet->state = LIBTELNET_STATE_IAC; |
| 98 | } |
| 99 | break; |
| 100 | |
| 101 | /* IAC command */ |
| 102 | case LIBTELNET_STATE_IAC: |
| 103 | switch (byte) { |
| 104 | /* subrequest */ |
| 105 | case LIBTELNET_SB: |
| 106 | telnet->state = LIBTELNET_STATE_SB; |
| 107 | break; |
| 108 | /* negotiation commands */ |
| 109 | case LIBTELNET_WILL: |
| 110 | telnet->state = LIBTELNET_STATE_WILL; |
| 111 | break; |
| 112 | case LIBTELNET_WONT: |
| 113 | telnet->state = LIBTELNET_STATE_WONT; |
| 114 | break; |
| 115 | case LIBTELNET_DO: |
| 116 | telnet->state = LIBTELNET_STATE_DO; |
| 117 | break; |
| 118 | case LIBTELNET_DONT: |
| 119 | telnet->state = LIBTELNET_STATE_DONT; |
| 120 | break; |
| 121 | /* IAC escaping */ |
| 122 | case LIBTELNET_IAC: |
Sean Middleditch | 78f400f | 2009-03-14 01:26:43 -0400 | [diff] [blame] | 123 | libtelnet_data_cb(telnet, &byte, 1, user_data); |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 124 | start = i + 1; |
| 125 | telnet->state = LIBTELNET_STATE_TEXT; |
| 126 | break; |
| 127 | /* some other command */ |
| 128 | default: |
| 129 | libtelnet_command_cb(telnet, byte, user_data); |
| 130 | start = i + 1; |
| 131 | telnet->state = LIBTELNET_STATE_TEXT; |
| 132 | } |
| 133 | break; |
| 134 | |
| 135 | /* negotiation commands */ |
| 136 | case LIBTELNET_STATE_DO: |
| 137 | libtelnet_negotiate_cb(telnet, LIBTELNET_DO, byte, user_data); |
| 138 | start = i + 1; |
| 139 | telnet->state = LIBTELNET_STATE_TEXT; |
| 140 | break; |
| 141 | case LIBTELNET_STATE_DONT: |
| 142 | libtelnet_negotiate_cb(telnet, LIBTELNET_DONT, byte, user_data); |
| 143 | start = i + 1; |
| 144 | telnet->state = LIBTELNET_STATE_TEXT; |
| 145 | break; |
| 146 | case LIBTELNET_STATE_WILL: |
| 147 | libtelnet_negotiate_cb(telnet, LIBTELNET_WILL, byte, user_data); |
| 148 | start = i + 1; |
| 149 | telnet->state = LIBTELNET_STATE_TEXT; |
| 150 | break; |
| 151 | case LIBTELNET_STATE_WONT: |
| 152 | libtelnet_negotiate_cb(telnet, LIBTELNET_WONT, byte, user_data); |
| 153 | start = i + 1; |
| 154 | telnet->state = LIBTELNET_STATE_TEXT; |
| 155 | break; |
| 156 | |
| 157 | /* subrequest -- buffer bytes until end request */ |
| 158 | case LIBTELNET_STATE_SB: |
| 159 | /* IAC command in subrequest -- either IAC SE or IAC IAC */ |
| 160 | if (byte == LIBTELNET_IAC) { |
| 161 | telnet->state = LIBTELNET_STATE_SB_IAC; |
| 162 | /* buffer the byte, or bail if we can't */ |
Sean Middleditch | 7e517ec | 2009-03-14 00:18:30 -0400 | [diff] [blame] | 163 | } else if (_buffer_byte(telnet, byte, user_data) != |
Sean Middleditch | 8b5e2b1 | 2009-03-13 23:39:18 -0400 | [diff] [blame] | 164 | LIBTELNET_ERROR_OK) { |
| 165 | start = i + 1; |
| 166 | telnet->state = LIBTELNET_STATE_TEXT; |
| 167 | } else { |
| 168 | telnet->state = LIBTELNET_STATE_SB; |
| 169 | } |
| 170 | break; |
| 171 | |
| 172 | /* IAC escaping inside a subrequest */ |
| 173 | case LIBTELNET_STATE_SB_IAC: |
| 174 | switch (byte) { |
| 175 | /* end subrequest */ |
| 176 | case LIBTELNET_SE: |
| 177 | /* zero-size buffer is a protocol error */ |
| 178 | if (telnet->length == 0) { |
| 179 | libtelnet_error_cb(telnet, LIBTELNET_ERROR_PROTOCOL, |
| 180 | user_data); |
| 181 | /* process */ |
| 182 | } else { |
| 183 | libtelnet_subrequest_cb(telnet, telnet->buffer[0], |
| 184 | telnet->buffer + 1, telnet->length - 1, user_data); |
| 185 | telnet->length = 0; |
| 186 | } |
| 187 | |
| 188 | /* return to default state */ |
| 189 | start = i + 1; |
| 190 | telnet->state = LIBTELNET_STATE_TEXT; |
| 191 | break; |
| 192 | /* escaped IAC byte */ |
| 193 | case LIBTELNET_IAC: |
| 194 | /* push IAC into buffer */ |
| 195 | if (_buffer_byte(telnet, LIBTELNET_IAC, user_data) != |
| 196 | LIBTELNET_ERROR_OK) { |
| 197 | start = i + 1; |
| 198 | telnet->state = LIBTELNET_STATE_TEXT; |
| 199 | } else { |
| 200 | telnet->state = LIBTELNET_STATE_SB; |
| 201 | } |
| 202 | break; |
| 203 | /* something else -- protocol error */ |
| 204 | default: |
| 205 | libtelnet_error_cb(telnet, LIBTELNET_ERROR_PROTOCOL, |
| 206 | user_data); |
| 207 | start = i + 1; |
| 208 | telnet->state = LIBTELNET_STATE_TEXT; |
| 209 | break; |
| 210 | } |
| 211 | break; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /* pass through any remaining bytes */ |
| 216 | if (i != start) |
Sean Middleditch | 78f400f | 2009-03-14 01:26:43 -0400 | [diff] [blame] | 217 | libtelnet_data_cb(telnet, &buffer[start], i - start, user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /* send an iac command */ |
| 221 | void libtelnet_send_command(struct libtelnet_t *telnet, unsigned char cmd, |
| 222 | void *user_data) { |
Sean Middleditch | f9cebec | 2009-03-13 20:17:31 -0400 | [diff] [blame] | 223 | unsigned char bytes[2] = { LIBTELNET_IAC, cmd }; |
Sean Middleditch | 78f400f | 2009-03-14 01:26:43 -0400 | [diff] [blame] | 224 | libtelnet_send_cb(telnet, bytes, 2, user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /* send negotiation */ |
| 228 | void libtelnet_send_negotiate(struct libtelnet_t *telnet, unsigned char cmd, |
| 229 | unsigned char opt, void *user_data) { |
Sean Middleditch | f9cebec | 2009-03-13 20:17:31 -0400 | [diff] [blame] | 230 | unsigned char bytes[3] = { LIBTELNET_IAC, cmd, opt }; |
Sean Middleditch | 78f400f | 2009-03-14 01:26:43 -0400 | [diff] [blame] | 231 | libtelnet_send_cb(telnet, bytes, 3, user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /* send non-command data (escapes IAC bytes) */ |
| 235 | void libtelnet_send_data(struct libtelnet_t *telnet, unsigned char *buffer, |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 236 | unsigned int size, void *user_data) { |
| 237 | unsigned int i, l; |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 238 | for (l = i = 0; i != size; ++i) { |
| 239 | /* dump prior portion of text, send escaped bytes */ |
| 240 | if (buffer[i] == LIBTELNET_IAC) { |
| 241 | /* dump prior text if any */ |
| 242 | if (i != l) |
Sean Middleditch | 78f400f | 2009-03-14 01:26:43 -0400 | [diff] [blame] | 243 | libtelnet_send_cb(telnet, buffer + l, i - l, user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 244 | l = i + 1; |
| 245 | |
| 246 | /* send escape */ |
| 247 | libtelnet_send_command(telnet, LIBTELNET_IAC, user_data); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /* send whatever portion of buffer is left */ |
| 252 | if (i != l) |
Sean Middleditch | 78f400f | 2009-03-14 01:26:43 -0400 | [diff] [blame] | 253 | libtelnet_send_cb(telnet, buffer + l, i - l, user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /* send sub-request */ |
| 257 | void libtelnet_send_subrequest(struct libtelnet_t *telnet, unsigned char type, |
Sean Middleditch | 4d9444d | 2009-03-13 22:48:05 -0400 | [diff] [blame] | 258 | unsigned char *buffer, unsigned int size, void *user_data) { |
Sean Middleditch | f9cebec | 2009-03-13 20:17:31 -0400 | [diff] [blame] | 259 | libtelnet_send_command(telnet, LIBTELNET_SB, user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 260 | libtelnet_send_data(telnet, &type, 1, user_data); |
| 261 | libtelnet_send_data(telnet, buffer, size, user_data); |
Sean Middleditch | f9cebec | 2009-03-13 20:17:31 -0400 | [diff] [blame] | 262 | libtelnet_send_command(telnet, LIBTELNET_SE, user_data); |
Sean Middleditch | 2914485 | 2009-03-12 23:14:47 -0400 | [diff] [blame] | 263 | } |