Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <ctype.h> |
| 5 | #include <unistd.h> |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 6 | #include <errno.h> |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 7 | |
| 8 | #include <sys/types.h> |
| 9 | #include <sys/socket.h> |
| 10 | #include <netinet/in.h> |
| 11 | |
| 12 | #include <poll.h> |
| 13 | #include <sys/mman.h> |
| 14 | |
| 15 | #include "libwebsockets.h" |
| 16 | |
| 17 | void md5(const unsigned char *input, int ilen, unsigned char output[16]); |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 18 | static void libwebsocket_service(struct libwebsocket *wsi, int sock); |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 19 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 20 | #define LWS_MAX_HEADER_NAME_LENGTH 64 |
| 21 | #define LWS_MAX_HEADER_LEN 4096 |
| 22 | #define LWS_INITIAL_HDR_ALLOC 256 |
| 23 | #define LWS_ADDITIONAL_HDR_ALLOC 64 |
| 24 | |
| 25 | |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 26 | /* |
| 27 | * Chrome (v0) |
| 28 | * |
| 29 | * GET / HTTP/1.1 |
| 30 | * Upgrade: WebSocket |
| 31 | * Connection: Upgrade |
| 32 | * Host: 127.0.0.1:7681 |
| 33 | * Origin: null |
| 34 | * Sec-WebSocket-Key1: +46 3 1 75 7Y 60 |
| 35 | * Sec-WebSocket-Key2: m^+J358s0 6N 6e0 Q1 0 ~4~ |
| 36 | * |
| 37 | * Firefox (v76) |
| 38 | * |
| 39 | * GET / HTTP/1.1 |
| 40 | * Upgrade: WebSocket |
| 41 | * Host: 127.0.0.1:7681 |
| 42 | * Connection: Upgrade |
| 43 | * Sec-WebSocket-Key1: EC."/$14 7 687YG+gZ 44d 16 |
| 44 | * Origin: file:// |
| 45 | * Sec-WebSocket-Key2: 2 / 9 0. 4 B8 77|ov968 |
| 46 | * |
| 47 | */ |
| 48 | |
| 49 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 50 | enum lws_connection_states { |
| 51 | WSI_STATE_CLOSED, |
| 52 | WSI_STATE_HANDSHAKE_RX, |
| 53 | WSI_STATE_DEAD_SOCKET, |
| 54 | WSI_STATE_ESTABLISHED |
| 55 | }; |
| 56 | |
| 57 | enum lws_token_indexes { |
| 58 | WSI_TOKEN_GET_URI, |
| 59 | WSI_TOKEN_HOST, |
| 60 | WSI_TOKEN_CONNECTION, |
| 61 | WSI_TOKEN_KEY1, |
| 62 | WSI_TOKEN_KEY2, |
| 63 | WSI_TOKEN_PROTOCOL, |
| 64 | WSI_TOKEN_UPGRADE, |
| 65 | WSI_TOKEN_ORIGIN, |
| 66 | WSI_TOKEN_CHALLENGE, |
| 67 | |
| 68 | /* always last real token index*/ |
| 69 | WSI_TOKEN_COUNT, |
| 70 | /* parser state additions */ |
| 71 | WSI_TOKEN_NAME_PART, |
| 72 | WSI_TOKEN_SKIPPING, |
| 73 | WSI_TOKEN_SKIPPING_SAW_CR, |
| 74 | WSI_PARSING_COMPLETE |
| 75 | }; |
| 76 | |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 77 | enum lws_rx_parse_state { |
| 78 | LWS_RXPS_NEW, |
| 79 | |
| 80 | LWS_RXPS_SEEN_76_FF, |
| 81 | LWS_RXPS_PULLING_76_LENGTH, |
| 82 | |
| 83 | LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED |
| 84 | }; |
| 85 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 86 | |
| 87 | struct lws_tokens { |
| 88 | char * token; |
| 89 | int token_len; |
| 90 | }; |
| 91 | |
| 92 | |
| 93 | /* |
| 94 | * This is totally opaque to code using the library. It's exported as a |
| 95 | * forward-reference pointer-only declaration. |
| 96 | */ |
| 97 | |
| 98 | struct libwebsocket { |
| 99 | int (*callback)(struct libwebsocket *, |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 100 | enum libwebsocket_callback_reasons reason, void *, size_t); |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 101 | |
| 102 | enum lws_connection_states state; |
| 103 | |
| 104 | char name_buffer[LWS_MAX_HEADER_NAME_LENGTH]; |
| 105 | int name_buffer_pos; |
| 106 | int current_alloc_len; |
| 107 | enum lws_token_indexes parser_state; |
| 108 | struct lws_tokens utf8_token[WSI_TOKEN_COUNT]; |
| 109 | int ietf_spec_revision; |
| 110 | |
| 111 | int sock; |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 112 | |
| 113 | enum lws_rx_parse_state lws_rx_parse_state; |
| 114 | size_t rx_packet_length; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 115 | }; |
| 116 | |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 117 | |
| 118 | const struct lws_tokens lws_tokens[WSI_TOKEN_COUNT] = { |
| 119 | { "GET ", 4 }, |
| 120 | { "Host:", 5 }, |
| 121 | { "Connection:", 11 }, |
| 122 | { "Sec-WebSocket-Key1:", 19 }, |
| 123 | { "Sec-WebSocket-Key2:", 19 }, |
| 124 | { "Sec-WebSocket-Protocol:", 23 }, |
| 125 | { "Upgrade:", 8 }, |
| 126 | { "Origin:", 7 }, |
| 127 | { "\x0d\x0a", 2 }, |
| 128 | }; |
| 129 | |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 130 | |
| 131 | int libwebsocket_create_server(int port, int (*callback)(struct libwebsocket *, enum libwebsocket_callback_reasons, void *, size_t)) |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 132 | { |
| 133 | int n; |
| 134 | int sockfd, newsockfd; |
| 135 | unsigned int clilen; |
| 136 | struct sockaddr_in serv_addr, cli_addr; |
| 137 | int pid; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 138 | struct libwebsocket *wsi = malloc(sizeof(struct libwebsocket)); |
| 139 | |
| 140 | if (!wsi) |
| 141 | return -1; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 142 | |
| 143 | wsi->state = WSI_STATE_CLOSED; |
| 144 | wsi->name_buffer_pos = 0; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 145 | |
| 146 | for (n = 0; n < WSI_TOKEN_COUNT; n++) { |
| 147 | wsi->utf8_token[n].token = NULL; |
| 148 | wsi->utf8_token[n].token_len = 0; |
| 149 | } |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 150 | |
| 151 | wsi->callback = callback; |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 152 | // wsi->ietf_spec_revision = 0; |
| 153 | wsi->ietf_spec_revision = 76; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 154 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 155 | /* sit there listening for connects, accept and spawn session servers */ |
| 156 | |
| 157 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
| 158 | if (sockfd < 0) { |
| 159 | fprintf(stderr, "ERROR opening socket"); |
| 160 | } |
| 161 | bzero((char *) &serv_addr, sizeof(serv_addr)); |
| 162 | |
| 163 | serv_addr.sin_family = AF_INET; |
| 164 | serv_addr.sin_addr.s_addr = INADDR_ANY; |
| 165 | serv_addr.sin_port = htons(port); |
| 166 | n = bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); |
| 167 | if (n < 0) { |
| 168 | fprintf(stderr, "ERROR on binding %d %d\n", n, errno); |
| 169 | return -1; |
| 170 | } |
| 171 | |
| 172 | /* fork off a master server for this websocket server */ |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 173 | |
| 174 | n = fork(); |
| 175 | if (n < 0) { |
| 176 | fprintf(stderr, "Failed on forking server thread: %d\n", n); |
| 177 | exit(1); |
| 178 | } |
| 179 | |
| 180 | /* we are done as far as the caller is concerned */ |
| 181 | |
| 182 | if (n) |
| 183 | return 0; |
| 184 | |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 185 | |
| 186 | listen(sockfd, 5); |
| 187 | |
| 188 | while (1) { |
| 189 | clilen = sizeof(cli_addr); |
| 190 | |
| 191 | newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 192 | if (newsockfd < 0) { |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 193 | fprintf(stderr, "ERROR on accept"); |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 194 | continue; |
| 195 | } |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 196 | |
| 197 | /* fork off a new server instance */ |
| 198 | |
| 199 | pid = fork(); |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 200 | if (pid < 0) { |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 201 | fprintf(stderr, "ERROR on fork"); |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 202 | continue; |
| 203 | } |
| 204 | |
| 205 | if (pid) { |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 206 | close(newsockfd); |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 207 | continue; |
| 208 | } |
| 209 | |
| 210 | /* we are the session process */ |
| 211 | |
| 212 | close(sockfd); |
| 213 | |
| 214 | /* sit in libwebsocket_service() until session socket closed */ |
| 215 | |
| 216 | libwebsocket_service(wsi, newsockfd); |
| 217 | exit(0); |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
| 221 | void libwebsocket_close(struct libwebsocket *wsi) |
| 222 | { |
| 223 | int n; |
| 224 | |
| 225 | wsi->state = WSI_STATE_DEAD_SOCKET; |
| 226 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 227 | if (wsi->callback) |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 228 | wsi->callback(wsi, LWS_CALLBACK_CLOSED, NULL, 0); |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 229 | |
| 230 | for (n = 0; n < WSI_TOKEN_COUNT; n++) |
| 231 | if (wsi->utf8_token[n].token) |
| 232 | free(wsi->utf8_token[n].token); |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | |
| 236 | static int libwebsocket_parse(struct libwebsocket *wsi, unsigned char c) |
| 237 | { |
| 238 | int n; |
| 239 | |
| 240 | switch (wsi->parser_state) { |
| 241 | case WSI_TOKEN_GET_URI: |
| 242 | case WSI_TOKEN_HOST: |
| 243 | case WSI_TOKEN_CONNECTION: |
| 244 | case WSI_TOKEN_KEY1: |
| 245 | case WSI_TOKEN_KEY2: |
| 246 | case WSI_TOKEN_PROTOCOL: |
| 247 | case WSI_TOKEN_UPGRADE: |
| 248 | case WSI_TOKEN_ORIGIN: |
| 249 | case WSI_TOKEN_CHALLENGE: |
| 250 | |
| 251 | // fprintf(stderr, "WSI_TOKEN_(body %d) '%c'\n", wsi->parser_state, c); |
| 252 | |
| 253 | /* collect into malloc'd buffers */ |
| 254 | /* optional space swallow */ |
| 255 | if (!wsi->utf8_token[wsi->parser_state].token_len && c == ' ') |
| 256 | break; |
| 257 | |
| 258 | /* special case space terminator for get-uri */ |
| 259 | if (wsi->parser_state == WSI_TOKEN_GET_URI && c == ' ') { |
| 260 | wsi->utf8_token[wsi->parser_state].token[ |
| 261 | wsi->utf8_token[wsi->parser_state].token_len] = '\0'; |
| 262 | wsi->parser_state = WSI_TOKEN_SKIPPING; |
| 263 | break; |
| 264 | } |
| 265 | |
| 266 | /* allocate appropriate memory */ |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 267 | if (wsi->utf8_token[wsi->parser_state].token_len == |
| 268 | wsi->current_alloc_len - 1) { |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 269 | /* need to extend */ |
| 270 | wsi->current_alloc_len += LWS_ADDITIONAL_HDR_ALLOC; |
| 271 | if (wsi->current_alloc_len >= LWS_MAX_HEADER_LEN) { |
| 272 | /* it's waaay to much payload, fail it */ |
| 273 | strcpy(wsi->utf8_token[wsi->parser_state].token, |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 274 | "!!! Length exceeded maximum supported !!!"); |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 275 | wsi->parser_state = WSI_TOKEN_SKIPPING; |
| 276 | break; |
| 277 | } |
| 278 | wsi->utf8_token[wsi->parser_state].token = |
| 279 | realloc(wsi->utf8_token[wsi->parser_state].token, |
| 280 | wsi->current_alloc_len); |
| 281 | } |
| 282 | |
| 283 | /* bail at EOL */ |
| 284 | if (wsi->parser_state != WSI_TOKEN_CHALLENGE && c == '\x0d') { |
| 285 | wsi->utf8_token[wsi->parser_state].token[ |
| 286 | wsi->utf8_token[wsi->parser_state].token_len] = '\0'; |
| 287 | wsi->parser_state = WSI_TOKEN_SKIPPING_SAW_CR; |
| 288 | break; |
| 289 | } |
| 290 | |
| 291 | wsi->utf8_token[wsi->parser_state].token[ |
| 292 | wsi->utf8_token[wsi->parser_state].token_len++] = c; |
| 293 | |
| 294 | /* special payload limiting */ |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 295 | if (wsi->parser_state == WSI_TOKEN_CHALLENGE && |
| 296 | wsi->utf8_token[wsi->parser_state].token_len == 8) { |
| 297 | // fprintf(stderr, "Setting WSI_PARSING_COMPLETE\n"); |
| 298 | wsi->parser_state = WSI_PARSING_COMPLETE; |
| 299 | break; |
| 300 | } |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 301 | |
| 302 | break; |
| 303 | |
| 304 | /* collecting and checking a name part */ |
| 305 | case WSI_TOKEN_NAME_PART: |
| 306 | // fprintf(stderr, "WSI_TOKEN_NAME_PART '%c'\n", c); |
| 307 | |
| 308 | if (wsi->name_buffer_pos == sizeof(wsi->name_buffer) - 1) { |
| 309 | /* name bigger than we can handle, skip until next */ |
| 310 | wsi->parser_state = WSI_TOKEN_SKIPPING; |
| 311 | break; |
| 312 | } |
| 313 | wsi->name_buffer[wsi->name_buffer_pos++] = c; |
| 314 | wsi->name_buffer[wsi->name_buffer_pos] = '\0'; |
| 315 | |
| 316 | for (n = 0; n < WSI_TOKEN_COUNT; n++) { |
| 317 | if (wsi->name_buffer_pos != lws_tokens[n].token_len) |
| 318 | continue; |
| 319 | if (strcmp(lws_tokens[n].token, wsi->name_buffer)) |
| 320 | continue; |
| 321 | wsi->parser_state = WSI_TOKEN_GET_URI + n; |
| 322 | wsi->current_alloc_len = LWS_INITIAL_HDR_ALLOC; |
| 323 | wsi->utf8_token[wsi->parser_state].token = |
| 324 | malloc(wsi->current_alloc_len); |
| 325 | wsi->utf8_token[wsi->parser_state].token_len = 0; |
| 326 | n = WSI_TOKEN_COUNT; |
| 327 | } |
| 328 | if (wsi->parser_state != WSI_TOKEN_NAME_PART) |
| 329 | break; |
| 330 | break; |
| 331 | |
| 332 | /* skipping arg part of a name we didn't recognize */ |
| 333 | case WSI_TOKEN_SKIPPING: |
| 334 | // fprintf(stderr, "WSI_TOKEN_SKIPPING '%c'\n", c); |
| 335 | if (c == '\x0d') |
| 336 | wsi->parser_state = WSI_TOKEN_SKIPPING_SAW_CR; |
| 337 | break; |
| 338 | case WSI_TOKEN_SKIPPING_SAW_CR: |
| 339 | // fprintf(stderr, "WSI_TOKEN_SKIPPING_SAW_CR '%c'\n", c); |
| 340 | if (c == '\x0a') |
| 341 | wsi->parser_state = WSI_TOKEN_NAME_PART; |
| 342 | else |
| 343 | wsi->parser_state = WSI_TOKEN_SKIPPING; |
| 344 | wsi->name_buffer_pos = 0; |
| 345 | break; |
| 346 | /* we're done, ignore anything else */ |
| 347 | case WSI_PARSING_COMPLETE: |
| 348 | // fprintf(stderr, "WSI_PARSING_COMPLETE '%c'\n", c); |
| 349 | break; |
| 350 | |
| 351 | default: /* keep gcc happy */ |
| 352 | break; |
| 353 | } |
| 354 | |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | static int interpret_key(const char *key, unsigned int *result) |
| 359 | { |
| 360 | char digits[20]; |
| 361 | int digit_pos = 0; |
| 362 | const char *p = key; |
| 363 | int spaces = 0; |
| 364 | |
| 365 | while (*p) { |
| 366 | if (isdigit(*p)) { |
| 367 | if (digit_pos == sizeof(digits) - 1) |
| 368 | return -1; |
| 369 | digits[digit_pos++] = *p; |
| 370 | } |
| 371 | p++; |
| 372 | } |
| 373 | digits[digit_pos] = '\0'; |
| 374 | if (!digit_pos) |
| 375 | return -2; |
| 376 | |
| 377 | while (*key) { |
| 378 | if (*key == ' ') |
| 379 | spaces++; |
| 380 | key++; |
| 381 | } |
| 382 | |
| 383 | if (!spaces) |
| 384 | return -3; |
| 385 | |
| 386 | *result = atol(digits) / spaces; |
| 387 | |
| 388 | return 0; |
| 389 | } |
| 390 | |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 391 | static int libwebsocket_rx_sm(struct libwebsocket *wsi, unsigned char c) |
| 392 | { |
| 393 | int n; |
| 394 | unsigned char buf[2]; |
| 395 | |
| 396 | switch (wsi->lws_rx_parse_state) { |
| 397 | case LWS_RXPS_NEW: |
| 398 | |
| 399 | switch (wsi->ietf_spec_revision) { |
| 400 | /* Firefox 4.0b6 likes this as of 30 Oct */ |
| 401 | case 76: |
| 402 | if (c == 0xff) |
| 403 | wsi->lws_rx_parse_state = LWS_RXPS_SEEN_76_FF; |
| 404 | break; |
| 405 | case 0: |
| 406 | break; |
| 407 | } |
| 408 | break; |
| 409 | case LWS_RXPS_SEEN_76_FF: |
| 410 | if (c != 0) { |
| 411 | break; |
| 412 | } |
| 413 | |
| 414 | fprintf(stderr, "Seen that client is requesting a v76 close, sending ack\n"); |
| 415 | buf[0] = 0xff; |
| 416 | buf[1] = 0; |
| 417 | n = write(wsi->sock, buf, 2); |
| 418 | if (n < 0) { |
| 419 | fprintf(stderr, "ERROR writing to socket"); |
| 420 | return -1; |
| 421 | } |
| 422 | fprintf(stderr, " v76 close ack sent, server closing socket\n"); |
| 423 | /* returning < 0 will get it closed in parent */ |
| 424 | return -1; |
| 425 | |
| 426 | case LWS_RXPS_PULLING_76_LENGTH: |
| 427 | break; |
| 428 | case LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED: |
| 429 | break; |
| 430 | } |
| 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | static int libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi, |
| 436 | unsigned char *buf, size_t len) |
| 437 | { |
| 438 | int n; |
| 439 | |
| 440 | fprintf(stderr, "received %d byte packet\n", (int)len); |
| 441 | for (n = 0; n < len; n++) |
| 442 | fprintf(stderr, "%02X ", buf[n]); |
| 443 | fprintf(stderr, "\n"); |
| 444 | |
| 445 | /* let the rx protocol state machine have as much as it needs */ |
| 446 | |
| 447 | n = 0; |
| 448 | while (wsi->lws_rx_parse_state != |
| 449 | LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED && n < len) |
| 450 | if (libwebsocket_rx_sm(wsi, buf[n++]) < 0) |
| 451 | return -1; |
| 452 | |
| 453 | if (n != len) { |
| 454 | if (wsi->callback) |
| 455 | wsi->callback(wsi, LWS_CALLBACK_RECEIVE, &buf[n], len - n); |
| 456 | } |
| 457 | |
| 458 | return -0; |
| 459 | } |
| 460 | |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 461 | |
| 462 | /* |
| 463 | * We have to take care about parsing because the headers may be split |
| 464 | * into multiple fragments. They may contain unknown headers with arbitrary |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 465 | * argument lengths. So, we parse using a single-character at a time state |
| 466 | * machine that is completely independent of packet size. |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 467 | */ |
| 468 | |
| 469 | int libwebsocket_read(struct libwebsocket *wsi, unsigned char * buf, size_t len) |
| 470 | { |
| 471 | size_t n; |
| 472 | char *p; |
| 473 | unsigned int key1, key2; |
| 474 | unsigned char sum[16]; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 475 | char *response; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 476 | |
| 477 | switch (wsi->state) { |
| 478 | case WSI_STATE_CLOSED: |
| 479 | wsi->state = WSI_STATE_HANDSHAKE_RX; |
| 480 | wsi->parser_state = WSI_TOKEN_NAME_PART; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 481 | /* fallthru */ |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 482 | case WSI_STATE_HANDSHAKE_RX: |
| 483 | |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 484 | fprintf(stderr, "issuing %d bytes to parser\n", (int)len); |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 485 | |
| 486 | fwrite(buf, 1, len, stderr); |
| 487 | for (n = 0; n< len; n++) |
| 488 | libwebsocket_parse(wsi, *buf++); |
| 489 | |
| 490 | if (wsi->parser_state != WSI_PARSING_COMPLETE) |
| 491 | break; |
| 492 | |
| 493 | fprintf(stderr, "Preparing return packet\n"); |
| 494 | |
| 495 | |
| 496 | /* Confirm we have all the necessary pieces */ |
| 497 | |
| 498 | if ( |
| 499 | !wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len || |
| 500 | !wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len || |
| 501 | !wsi->utf8_token[WSI_TOKEN_ORIGIN].token_len || |
| 502 | !wsi->utf8_token[WSI_TOKEN_HOST].token_len || |
| 503 | !wsi->utf8_token[WSI_TOKEN_CHALLENGE].token_len || |
| 504 | !wsi->utf8_token[WSI_TOKEN_KEY1].token_len || |
| 505 | !wsi->utf8_token[WSI_TOKEN_KEY2].token_len) { |
| 506 | |
| 507 | /* completed header processing, but missing some bits */ |
| 508 | goto bail; |
| 509 | } |
| 510 | |
| 511 | /* create the response packet */ |
| 512 | |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 513 | /* make a buffer big enough for everything */ |
| 514 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 515 | response = malloc(256 + |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 516 | wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len + |
| 517 | wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len + |
| 518 | wsi->utf8_token[WSI_TOKEN_HOST].token_len + |
| 519 | wsi->utf8_token[WSI_TOKEN_ORIGIN].token_len + |
| 520 | wsi->utf8_token[WSI_TOKEN_GET_URI].token_len + |
| 521 | wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len); |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 522 | if (!response) { |
| 523 | fprintf(stderr, "Out of memory for response buffer\n"); |
| 524 | goto bail; |
| 525 | } |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 526 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 527 | p = response; |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 528 | strcpy(p, "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a" |
| 529 | "Upgrade: WebSocket\x0d\x0a"); |
| 530 | p += strlen("HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a" |
| 531 | "Upgrade: WebSocket\x0d\x0a"); |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 532 | strcpy(p, "Connection: Upgrade\x0d\x0aSec-WebSocket-Origin: "); |
| 533 | p += strlen("Connection: Upgrade\x0d\x0aSec-WebSocket-Origin: "); |
| 534 | strcpy(p, wsi->utf8_token[WSI_TOKEN_ORIGIN].token); |
| 535 | p += wsi->utf8_token[WSI_TOKEN_ORIGIN].token_len; |
| 536 | strcpy(p, "\x0d\x0aSec-WebSocket-Location: ws://"); |
| 537 | p += strlen("\x0d\x0aSec-WebSocket-Location: ws://"); |
| 538 | strcpy(p, wsi->utf8_token[WSI_TOKEN_HOST].token); |
| 539 | p += wsi->utf8_token[WSI_TOKEN_HOST].token_len; |
| 540 | strcpy(p, wsi->utf8_token[WSI_TOKEN_GET_URI].token); |
| 541 | p += wsi->utf8_token[WSI_TOKEN_GET_URI].token_len; |
| 542 | strcpy(p, "\x0d\x0aSec-WebSocket-Protocol: "); |
| 543 | p += strlen("\x0d\x0aSec-WebSocket-Protocol: "); |
| 544 | if (wsi->utf8_token[WSI_TOKEN_PROTOCOL].token) { |
| 545 | strcpy(p, wsi->utf8_token[WSI_TOKEN_PROTOCOL].token); |
| 546 | p += wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 547 | } else { |
| 548 | strcpy(p, "none"); |
| 549 | p += strlen("none"); |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 550 | } |
| 551 | strcpy(p, "\x0d\x0a\x0d\x0a"); |
| 552 | p += strlen("\x0d\x0a\x0d\x0a"); |
| 553 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 554 | /* convert the two keys into 32-bit integers */ |
| 555 | |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 556 | if (interpret_key(wsi->utf8_token[WSI_TOKEN_KEY1].token, &key1)) |
| 557 | goto bail; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 558 | if (interpret_key(wsi->utf8_token[WSI_TOKEN_KEY2].token, &key2)) |
| 559 | goto bail; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 560 | |
| 561 | /* lay them out in network byte order (MSB first */ |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 562 | |
| 563 | sum[0] = key1 >> 24; |
| 564 | sum[1] = key1 >> 16; |
| 565 | sum[2] = key1 >> 8; |
| 566 | sum[3] = key1; |
| 567 | sum[4] = key2 >> 24; |
| 568 | sum[5] = key2 >> 16; |
| 569 | sum[6] = key2 >> 8; |
| 570 | sum[7] = key2; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 571 | |
| 572 | /* follow them with the challenge token we were sent */ |
| 573 | |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 574 | memcpy(&sum[8], wsi->utf8_token[WSI_TOKEN_CHALLENGE].token, 8); |
| 575 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 576 | /* |
| 577 | * compute the md5sum of that 16-byte series and use as our |
| 578 | * payload after our headers |
| 579 | */ |
| 580 | |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 581 | md5(sum, 16, (unsigned char *)p); |
| 582 | p += 16; |
| 583 | |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 584 | /* it's complete: go ahead and send it */ |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 585 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 586 | fprintf(stderr, "issuing response packet %d len\n", |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 587 | (int)(p - response)); |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 588 | fwrite(response, 1, p - response, stderr); |
| 589 | |
| 590 | n = write(wsi->sock, response, p - response); |
| 591 | if (n < 0) { |
| 592 | fprintf(stderr, "ERROR writing to socket"); |
| 593 | goto bail; |
| 594 | } |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 595 | |
| 596 | /* alright clean up and set ourselves into established state */ |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 597 | |
| 598 | free(response); |
| 599 | wsi->state = WSI_STATE_ESTABLISHED; |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 600 | wsi->lws_rx_parse_state = LWS_RXPS_NEW; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 601 | |
| 602 | /* notify user code that we're ready to roll */ |
| 603 | |
| 604 | if (wsi->callback) |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 605 | wsi->callback(wsi, LWS_CALLBACK_ESTABLISHED, NULL, 0); |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 606 | break; |
| 607 | |
| 608 | case WSI_STATE_ESTABLISHED: |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 609 | if (libwebsocket_interpret_incoming_packet(wsi, buf, len) < 0) |
| 610 | goto bail; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 611 | break; |
| 612 | default: |
| 613 | break; |
| 614 | } |
| 615 | |
| 616 | return 0; |
| 617 | |
| 618 | bail: |
| 619 | libwebsocket_close(wsi); |
| 620 | return -1; |
| 621 | } |
| 622 | |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 623 | |
| 624 | /* |
| 625 | * notice, we will use up to LWS_SEND_BUFFER_PRE_PADDING bytes BEFORE the |
| 626 | * buffer pointer given and LWS_SEND_BUFFER_POST_PADDING bytes AFTER |
| 627 | * buf + len !!! Caller must allocate and offset pointer accordingly! |
| 628 | * |
| 629 | * This lets us send packets in one write() action including the protocol |
| 630 | * pre- and post- data without copying the payload around. |
| 631 | */ |
| 632 | |
| 633 | int libwebsocket_write(struct libwebsocket * wsi, unsigned char *buf, |
| 634 | size_t len, int is_binary) |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 635 | { |
| 636 | int n; |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 637 | int pre = 0; |
| 638 | int post = 0; |
| 639 | unsigned int shift = 7; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 640 | |
| 641 | if (wsi->state != WSI_STATE_ESTABLISHED) |
| 642 | return -1; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 643 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 644 | switch (wsi->ietf_spec_revision) { |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 645 | /* Firefox 4.0b6 likes this as of 30 Oct */ |
| 646 | case 76: |
| 647 | if (is_binary) { |
| 648 | /* in binary mode we send 7-bit used length blocks */ |
| 649 | pre = 1; |
| 650 | while (len & (127 << shift)) { |
| 651 | pre++; |
| 652 | shift += 7; |
| 653 | } |
| 654 | n = 0; |
| 655 | shift -= 7; |
| 656 | while (shift >= 0) { |
| 657 | if (shift) |
| 658 | buf[0 - pre + n] = |
| 659 | ((len >> shift) & 127) | 0x80; |
| 660 | else |
| 661 | buf[0 - pre + n] = |
| 662 | ((len >> shift) & 127); |
| 663 | n++; |
| 664 | shift -= 7; |
| 665 | } |
| 666 | break; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 667 | } |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 668 | |
| 669 | /* frame type = text, length-free spam mode */ |
| 670 | |
| 671 | buf[-1] = 0; |
| 672 | buf[len] = 0xff; /* EOT marker */ |
| 673 | pre = 1; |
| 674 | post = 1; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 675 | break; |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 676 | |
| 677 | /* chrome likes this as of 30 Oct */ |
| 678 | case 0: |
| 679 | buf[-9] = 0xff; |
| 680 | #if defined __LP64__ |
| 681 | buf[-8] = len >> 56; |
| 682 | buf[-7] = len >> 48; |
| 683 | buf[-6] = len >> 40; |
| 684 | buf[-5] = len >> 32; |
| 685 | #else |
| 686 | buf[-8] = 0; |
| 687 | buf[-7] = 0; |
| 688 | buf[-6] = 0; |
| 689 | buf[-5] = 0; |
| 690 | #endif |
| 691 | buf[-4] = len >> 24; |
| 692 | buf[-3] = len >> 16; |
| 693 | buf[-2] = len >> 8; |
| 694 | buf[-1] = len; |
| 695 | pre = 9; |
| 696 | break; |
| 697 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 698 | /* just an unimplemented spec right now apparently */ |
| 699 | case 2: |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 700 | n = 4; /* text */ |
| 701 | if (is_binary) |
| 702 | n = 5; /* binary */ |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 703 | if (len < 126) { |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 704 | buf[-2] = n; |
| 705 | buf[-1] = len; |
| 706 | pre = 2; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 707 | } else { |
| 708 | if (len < 65536) { |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 709 | buf[-4] = n; |
| 710 | buf[-3] = 126; |
| 711 | buf[-2] = len >> 8; |
| 712 | buf[-1] = len; |
| 713 | pre = 4; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 714 | } else { |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 715 | buf[-10] = n; |
| 716 | buf[-9] = 127; |
| 717 | #if defined __LP64__ |
| 718 | buf[-8] = (len >> 56) & 0x7f; |
| 719 | buf[-7] = len >> 48; |
| 720 | buf[-6] = len >> 40; |
| 721 | buf[-5] = len >> 32; |
| 722 | #else |
| 723 | buf[-8] = 0; |
| 724 | buf[-7] = 0; |
| 725 | buf[-6] = 0; |
| 726 | buf[-5] = 0; |
| 727 | #endif |
| 728 | buf[-4] = len >> 24; |
| 729 | buf[-3] = len >> 16; |
| 730 | buf[-2] = len >> 8; |
| 731 | buf[-1] = len; |
| 732 | pre = 10; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 733 | } |
| 734 | } |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 735 | break; |
| 736 | } |
| 737 | |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 738 | n = write(wsi->sock, buf - pre, len + pre + post); |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 739 | if (n < 0) { |
| 740 | fprintf(stderr, "ERROR writing to socket"); |
| 741 | return -1; |
| 742 | } |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 743 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 744 | fprintf(stderr, "written %d bytes to websocket\n", (int)len); |
| 745 | |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | static void libwebsocket_service(struct libwebsocket *wsi, int sock) |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 750 | { |
| 751 | int n; |
| 752 | unsigned char buf[256]; |
| 753 | struct pollfd fds; |
| 754 | |
| 755 | wsi->sock = sock; |
| 756 | |
| 757 | fds.fd = sock; |
| 758 | fds.events = POLLIN | POLLOUT; |
| 759 | |
| 760 | while (1) { |
| 761 | |
| 762 | n = poll(&fds, 1, 10); |
| 763 | if (n < 0) { |
| 764 | fprintf(stderr, "Socket dead (poll = %d)\n", n); |
| 765 | return; |
| 766 | } |
| 767 | |
| 768 | if (fds.revents & (POLLERR | POLLHUP)) { |
| 769 | fprintf(stderr, "Socket dead\n"); |
| 770 | return; |
| 771 | } |
| 772 | |
| 773 | if (wsi->state == WSI_STATE_DEAD_SOCKET) |
| 774 | return; |
| 775 | |
| 776 | |
| 777 | if (fds.revents & POLLIN) { |
| 778 | |
| 779 | // fprintf(stderr, "POLLIN\n"); |
| 780 | |
| 781 | n = read(sock, buf, sizeof(buf)); |
| 782 | if (n < 0) { |
| 783 | fprintf(stderr, "Socket read returned %d\n", n); |
| 784 | continue; |
| 785 | } |
| 786 | if (n) |
| 787 | libwebsocket_read(wsi, buf, n); |
| 788 | } |
| 789 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 790 | if (wsi->state != WSI_STATE_ESTABLISHED) |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 791 | continue; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 792 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 793 | if (wsi->callback) |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame^] | 794 | wsi->callback(wsi, LWS_CALLBACK_SEND, NULL, 0); |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 795 | } |
| 796 | } |
| 797 | |