Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libwebsockets - small server side websockets and web server implementation |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 3 | * |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 4 | * Copyright (C) 2010 Andy Green <andy@warmcat.com> |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation: |
| 9 | * version 2.1 of the License. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 19 | * MA 02110-1301 USA |
| 20 | */ |
| 21 | |
Andy Green | 70dfebd | 2010-12-20 09:35:03 +0000 | [diff] [blame] | 22 | #include <unistd.h> |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
Andy Green | 70dfebd | 2010-12-20 09:35:03 +0000 | [diff] [blame] | 26 | #include <strings.h> |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 27 | #include <ctype.h> |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 28 | #include <errno.h> |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 29 | #include <fcntl.h> |
| 30 | #include <signal.h> |
Andy Green | 2a480ea | 2012-04-12 10:54:22 +0800 | [diff] [blame] | 31 | #ifdef __MINGW64__ |
| 32 | #else |
| 33 | #ifdef __MINGW32__ |
| 34 | #else |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 35 | #include <netdb.h> |
Andy Green | 2a480ea | 2012-04-12 10:54:22 +0800 | [diff] [blame] | 36 | #endif |
| 37 | #endif |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 38 | #include <stdarg.h> |
| 39 | |
| 40 | #include <sys/stat.h> |
| 41 | |
| 42 | #ifdef WIN32 |
Andy Green | 2a480ea | 2012-04-12 10:54:22 +0800 | [diff] [blame] | 43 | #ifdef __MINGW64__ |
| 44 | #else |
| 45 | #ifdef __MINGW32__ |
| 46 | #else |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 47 | #include <time.h > |
Andy Green | 2a480ea | 2012-04-12 10:54:22 +0800 | [diff] [blame] | 48 | #endif |
| 49 | #endif |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 50 | #include <winsock2.h> |
| 51 | #include <ws2ipdef.h> |
| 52 | #include <windows.h> |
| 53 | |
| 54 | #else |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 55 | |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 56 | #include <sys/types.h> |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 57 | #include <sys/socket.h> |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 58 | #ifndef LWS_NO_FORK |
Artem Baguinski | 9153166 | 2011-12-14 22:14:03 +0100 | [diff] [blame] | 59 | #ifdef HAVE_SYS_PRCTL_H |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 60 | #include <sys/prctl.h> |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 61 | #endif |
Artem Baguinski | 9153166 | 2011-12-14 22:14:03 +0100 | [diff] [blame] | 62 | #endif |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 63 | #include <netinet/in.h> |
Andy Green | 6c93955 | 2011-03-08 08:56:57 +0000 | [diff] [blame] | 64 | #include <netinet/tcp.h> |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 65 | #include <arpa/inet.h> |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 66 | |
| 67 | #include <poll.h> |
| 68 | #include <sys/mman.h> |
Andy Green | 038d582 | 2011-02-14 20:58:26 +0000 | [diff] [blame] | 69 | #include <sys/time.h> |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 70 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 71 | #endif |
| 72 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 73 | #ifdef LWS_OPENSSL_SUPPORT |
| 74 | #include <openssl/ssl.h> |
| 75 | #include <openssl/evp.h> |
| 76 | #include <openssl/err.h> |
Andy Green | 70dfebd | 2010-12-20 09:35:03 +0000 | [diff] [blame] | 77 | #include <openssl/md5.h> |
Andy Green | e252217 | 2011-01-18 17:14:03 +0000 | [diff] [blame] | 78 | #include <openssl/sha.h> |
Darin Willits | db9ba42 | 2011-02-14 20:56:24 +0000 | [diff] [blame] | 79 | #endif |
| 80 | |
| 81 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 82 | #include "libwebsockets.h" |
| 83 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 84 | #if 0 |
| 85 | #define DEBUG |
| 86 | #endif |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 87 | |
| 88 | #ifdef DEBUG |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 89 | #ifdef WIN32 |
| 90 | static |
| 91 | #else |
| 92 | static inline |
| 93 | #endif |
| 94 | void debug(const char *format, ...) |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 95 | { |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 96 | va_list ap; |
| 97 | va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap); |
| 98 | } |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 99 | #else |
Andy Green | b1dab51 | 2011-05-14 13:12:01 +0200 | [diff] [blame] | 100 | #ifdef WIN32 |
| 101 | static |
| 102 | #else |
| 103 | static inline |
| 104 | #endif |
| 105 | void debug(const char *format, ...) |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 106 | { |
| 107 | } |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 108 | #endif |
| 109 | |
Darin Willits | c19456f | 2011-02-14 17:52:39 +0000 | [diff] [blame] | 110 | |
| 111 | /* |
| 112 | * Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag, |
| 113 | * but happily have something equivalent in the SO_NOSIGPIPE flag. |
| 114 | */ |
| 115 | #ifdef __APPLE__ |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 116 | #define MSG_NOSIGNAL SO_NOSIGPIPE |
Darin Willits | c19456f | 2011-02-14 17:52:39 +0000 | [diff] [blame] | 117 | #endif |
| 118 | |
| 119 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 120 | #define FD_HASHTABLE_MODULUS 32 |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 121 | #define MAX_CLIENTS 100 |
| 122 | #define LWS_MAX_HEADER_NAME_LENGTH 64 |
| 123 | #define LWS_MAX_HEADER_LEN 4096 |
| 124 | #define LWS_INITIAL_HDR_ALLOC 256 |
| 125 | #define LWS_ADDITIONAL_HDR_ALLOC 64 |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 126 | #define MAX_USER_RX_BUFFER 4096 |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 127 | #define MAX_BROADCAST_PAYLOAD 2048 |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 128 | #define LWS_MAX_PROTOCOLS 10 |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 129 | #define LWS_MAX_EXTENSIONS_ACTIVE 10 |
Andy Green | d85cb20 | 2011-09-25 09:32:54 +0100 | [diff] [blame] | 130 | #define SPEC_LATEST_SUPPORTED 13 |
David Galeano | c9f1ff8 | 2013-01-09 18:01:23 +0800 | [diff] [blame] | 131 | #define AWAITING_TIMEOUT 5 |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 132 | |
Andy Green | e252217 | 2011-01-18 17:14:03 +0000 | [diff] [blame] | 133 | #define MAX_WEBSOCKET_04_KEY_LEN 128 |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 134 | #define SYSTEM_RANDOM_FILEPATH "/dev/urandom" |
Andy Green | e252217 | 2011-01-18 17:14:03 +0000 | [diff] [blame] | 135 | |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 136 | enum lws_websocket_opcodes_04 { |
| 137 | LWS_WS_OPCODE_04__CONTINUATION = 0, |
| 138 | LWS_WS_OPCODE_04__CLOSE = 1, |
| 139 | LWS_WS_OPCODE_04__PING = 2, |
| 140 | LWS_WS_OPCODE_04__PONG = 3, |
| 141 | LWS_WS_OPCODE_04__TEXT_FRAME = 4, |
| 142 | LWS_WS_OPCODE_04__BINARY_FRAME = 5, |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 143 | |
| 144 | LWS_WS_OPCODE_04__RESERVED_6 = 6, |
| 145 | LWS_WS_OPCODE_04__RESERVED_7 = 7, |
| 146 | LWS_WS_OPCODE_04__RESERVED_8 = 8, |
| 147 | LWS_WS_OPCODE_04__RESERVED_9 = 9, |
| 148 | LWS_WS_OPCODE_04__RESERVED_A = 0xa, |
| 149 | LWS_WS_OPCODE_04__RESERVED_B = 0xb, |
| 150 | LWS_WS_OPCODE_04__RESERVED_C = 0xc, |
| 151 | LWS_WS_OPCODE_04__RESERVED_D = 0xd, |
| 152 | LWS_WS_OPCODE_04__RESERVED_E = 0xe, |
| 153 | LWS_WS_OPCODE_04__RESERVED_F = 0xf, |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
Andy Green | 5fd55cd | 2011-04-23 10:54:53 +0100 | [diff] [blame] | 156 | enum lws_websocket_opcodes_07 { |
| 157 | LWS_WS_OPCODE_07__CONTINUATION = 0, |
| 158 | LWS_WS_OPCODE_07__TEXT_FRAME = 1, |
| 159 | LWS_WS_OPCODE_07__BINARY_FRAME = 2, |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 160 | |
| 161 | LWS_WS_OPCODE_07__NOSPEC__MUX = 7, |
| 162 | |
| 163 | /* control extensions 8+ */ |
| 164 | |
Andy Green | 5fd55cd | 2011-04-23 10:54:53 +0100 | [diff] [blame] | 165 | LWS_WS_OPCODE_07__CLOSE = 8, |
| 166 | LWS_WS_OPCODE_07__PING = 9, |
| 167 | LWS_WS_OPCODE_07__PONG = 0xa, |
| 168 | }; |
| 169 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 170 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 171 | enum lws_connection_states { |
| 172 | WSI_STATE_HTTP, |
| 173 | WSI_STATE_HTTP_HEADERS, |
| 174 | WSI_STATE_DEAD_SOCKET, |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 175 | WSI_STATE_ESTABLISHED, |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 176 | WSI_STATE_CLIENT_UNCONNECTED, |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 177 | WSI_STATE_RETURNED_CLOSE_ALREADY, |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 178 | WSI_STATE_AWAITING_CLOSE_ACK, |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 181 | enum lws_rx_parse_state { |
| 182 | LWS_RXPS_NEW, |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 183 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 184 | LWS_RXPS_SEEN_76_FF, |
| 185 | LWS_RXPS_PULLING_76_LENGTH, |
Andy Green | 6452f1e | 2010-11-11 09:22:22 +0000 | [diff] [blame] | 186 | LWS_RXPS_EAT_UNTIL_76_FF, |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 187 | |
Andy Green | 3e5eb78 | 2011-01-18 18:14:26 +0000 | [diff] [blame] | 188 | LWS_RXPS_04_MASK_NONCE_1, |
| 189 | LWS_RXPS_04_MASK_NONCE_2, |
| 190 | LWS_RXPS_04_MASK_NONCE_3, |
| 191 | |
| 192 | LWS_RXPS_04_FRAME_HDR_1, |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 193 | LWS_RXPS_04_FRAME_HDR_LEN, |
| 194 | LWS_RXPS_04_FRAME_HDR_LEN16_2, |
| 195 | LWS_RXPS_04_FRAME_HDR_LEN16_1, |
| 196 | LWS_RXPS_04_FRAME_HDR_LEN64_8, |
| 197 | LWS_RXPS_04_FRAME_HDR_LEN64_7, |
| 198 | LWS_RXPS_04_FRAME_HDR_LEN64_6, |
| 199 | LWS_RXPS_04_FRAME_HDR_LEN64_5, |
| 200 | LWS_RXPS_04_FRAME_HDR_LEN64_4, |
| 201 | LWS_RXPS_04_FRAME_HDR_LEN64_3, |
| 202 | LWS_RXPS_04_FRAME_HDR_LEN64_2, |
| 203 | LWS_RXPS_04_FRAME_HDR_LEN64_1, |
Andy Green | 3e5eb78 | 2011-01-18 18:14:26 +0000 | [diff] [blame] | 204 | |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 205 | LWS_RXPS_07_COLLECT_FRAME_KEY_1, |
| 206 | LWS_RXPS_07_COLLECT_FRAME_KEY_2, |
| 207 | LWS_RXPS_07_COLLECT_FRAME_KEY_3, |
| 208 | LWS_RXPS_07_COLLECT_FRAME_KEY_4, |
| 209 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 210 | LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED |
| 211 | }; |
| 212 | |
| 213 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 214 | enum connection_mode { |
| 215 | LWS_CONNMODE_WS_SERVING, |
| 216 | LWS_CONNMODE_WS_CLIENT, |
| 217 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 218 | /* transient modes */ |
| 219 | LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY, |
| 220 | LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE, |
| 221 | LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY, |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 222 | LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT, |
| 223 | LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD, |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 224 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 225 | /* special internal types */ |
| 226 | LWS_CONNMODE_SERVER_LISTENER, |
| 227 | LWS_CONNMODE_BROADCAST_PROXY_LISTENER, |
| 228 | LWS_CONNMODE_BROADCAST_PROXY |
| 229 | }; |
| 230 | |
| 231 | |
| 232 | #define LWS_FD_HASH(fd) ((fd ^ (fd >> 8) ^ (fd >> 16)) % FD_HASHTABLE_MODULUS) |
| 233 | |
| 234 | struct libwebsocket_fd_hashtable { |
| 235 | struct libwebsocket *wsi[MAX_CLIENTS + 1]; |
| 236 | int length; |
| 237 | }; |
| 238 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 239 | struct libwebsocket_protocols; |
| 240 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 241 | struct libwebsocket_context { |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 242 | struct libwebsocket_fd_hashtable fd_hashtable[FD_HASHTABLE_MODULUS]; |
| 243 | struct pollfd fds[MAX_CLIENTS * FD_HASHTABLE_MODULUS + 1]; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 244 | int fds_count; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 245 | int listen_port; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 246 | char http_proxy_address[256]; |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 247 | char canonical_hostname[1024]; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 248 | unsigned int http_proxy_port; |
Andy Green | 8014b29 | 2011-01-30 20:57:25 +0000 | [diff] [blame] | 249 | unsigned int options; |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 250 | unsigned long last_timeout_check_s; |
Andy Green | 44eee68 | 2011-02-10 09:32:24 +0000 | [diff] [blame] | 251 | |
| 252 | int fd_random; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 253 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 254 | #ifdef LWS_OPENSSL_SUPPORT |
| 255 | int use_ssl; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 256 | SSL_CTX *ssl_ctx; |
| 257 | SSL_CTX *ssl_client_ctx; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 258 | #endif |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 259 | struct libwebsocket_protocols *protocols; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 260 | int count_protocols; |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 261 | struct libwebsocket_extension *extensions; |
Alon Levy | 0291eb3 | 2012-10-19 11:21:56 +0200 | [diff] [blame] | 262 | |
| 263 | void *user_space; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 264 | }; |
| 265 | |
Andy Green | f3d3b40 | 2011-02-09 07:16:34 +0000 | [diff] [blame] | 266 | |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 267 | enum pending_timeout { |
| 268 | NO_PENDING_TIMEOUT = 0, |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 269 | PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE, |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 270 | PENDING_TIMEOUT_ESTABLISH_WITH_SERVER, |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 271 | PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE, |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 272 | PENDING_TIMEOUT_AWAITING_PING, |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 273 | PENDING_TIMEOUT_CLOSE_ACK, |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 274 | PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE, |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 275 | }; |
| 276 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 277 | |
| 278 | /* |
| 279 | * This is totally opaque to code using the library. It's exported as a |
| 280 | * forward-reference pointer-only declaration; the user can use the pointer with |
| 281 | * other APIs to get information out of it. |
| 282 | */ |
| 283 | |
| 284 | struct libwebsocket { |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 285 | const struct libwebsocket_protocols *protocol; |
Andy Green | 46c2ea0 | 2011-03-22 09:04:01 +0000 | [diff] [blame] | 286 | struct libwebsocket_extension * |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 287 | active_extensions[LWS_MAX_EXTENSIONS_ACTIVE]; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 288 | void *active_extensions_user[LWS_MAX_EXTENSIONS_ACTIVE]; |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 289 | int count_active_extensions; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 290 | |
| 291 | enum lws_connection_states state; |
| 292 | |
| 293 | char name_buffer[LWS_MAX_HEADER_NAME_LENGTH]; |
| 294 | int name_buffer_pos; |
| 295 | int current_alloc_len; |
| 296 | enum lws_token_indexes parser_state; |
| 297 | struct lws_tokens utf8_token[WSI_TOKEN_COUNT]; |
| 298 | int ietf_spec_revision; |
Andy Green | 6452f1e | 2010-11-11 09:22:22 +0000 | [diff] [blame] | 299 | char rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING + MAX_USER_RX_BUFFER + |
| 300 | LWS_SEND_BUFFER_POST_PADDING]; |
| 301 | int rx_user_buffer_head; |
Andy Green | 66a36ff | 2011-04-29 17:43:06 +0800 | [diff] [blame] | 302 | enum libwebsocket_write_protocol rx_frame_type; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 303 | int protocol_index_for_broadcast_proxy; |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 304 | enum pending_timeout pending_timeout; |
| 305 | unsigned long pending_timeout_limit; |
Andy Green | 6452f1e | 2010-11-11 09:22:22 +0000 | [diff] [blame] | 306 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 307 | int sock; |
| 308 | |
| 309 | enum lws_rx_parse_state lws_rx_parse_state; |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 310 | char extension_data_pending; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 311 | struct libwebsocket *candidate_children_list; |
| 312 | struct libwebsocket *extension_handles; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 313 | |
| 314 | /* 04 protocol specific */ |
| 315 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 316 | char key_b64[150]; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 317 | unsigned char masking_key_04[20]; |
| 318 | unsigned char frame_masking_nonce_04[4]; |
| 319 | unsigned char frame_mask_04[20]; |
| 320 | unsigned char frame_mask_index; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 321 | size_t rx_packet_length; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 322 | unsigned char opcode; |
| 323 | unsigned char final; |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 324 | unsigned char rsv; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 325 | |
| 326 | int pings_vs_pongs; |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 327 | unsigned char (*xor_mask)(struct libwebsocket *, unsigned char); |
Andy Green | 92970ba | 2011-02-10 09:22:35 +0000 | [diff] [blame] | 328 | char all_zero_nonce; |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 329 | |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 330 | enum lws_close_status close_reason; |
| 331 | |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 332 | /* 07 specific */ |
| 333 | char this_frame_masked; |
| 334 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 335 | /* client support */ |
| 336 | char initial_handshake_hash_base64[30]; |
Andy Green | f3d3b40 | 2011-02-09 07:16:34 +0000 | [diff] [blame] | 337 | enum connection_mode mode; |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 338 | char *c_path; |
| 339 | char *c_host; |
| 340 | char *c_origin; |
| 341 | char *c_protocol; |
David Brooks | 6c6a3d3 | 2012-04-20 12:13:50 +0800 | [diff] [blame] | 342 | callback_function *c_callback; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 343 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 344 | char *c_address; |
| 345 | int c_port; |
| 346 | |
| 347 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 348 | #ifdef LWS_OPENSSL_SUPPORT |
| 349 | SSL *ssl; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 350 | BIO *client_bio; |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 351 | int use_ssl; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 352 | #endif |
| 353 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 354 | void *user_space; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 355 | }; |
| 356 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 357 | extern int |
| 358 | libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c); |
| 359 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 360 | extern int |
| 361 | libwebsocket_parse(struct libwebsocket *wsi, unsigned char c); |
| 362 | |
| 363 | extern int |
| 364 | libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi, |
| 365 | unsigned char *buf, size_t len); |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 366 | |
| 367 | extern int |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 368 | libwebsocket_read(struct libwebsocket_context *context, |
| 369 | struct libwebsocket *wsi, |
| 370 | unsigned char *buf, size_t len); |
Andy Green | df73616 | 2011-01-18 15:39:02 +0000 | [diff] [blame] | 371 | |
| 372 | extern int |
Andy Green | df73616 | 2011-01-18 15:39:02 +0000 | [diff] [blame] | 373 | lws_b64_selftest(void); |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 374 | |
| 375 | extern unsigned char |
| 376 | xor_no_mask(struct libwebsocket *wsi, unsigned char c); |
| 377 | |
| 378 | extern unsigned char |
| 379 | xor_mask_04(struct libwebsocket *wsi, unsigned char c); |
| 380 | |
| 381 | extern unsigned char |
| 382 | xor_mask_05(struct libwebsocket *wsi, unsigned char c); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 383 | |
| 384 | extern struct libwebsocket * |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 385 | wsi_from_fd(struct libwebsocket_context *context, int fd); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 386 | |
| 387 | extern int |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 388 | insert_wsi(struct libwebsocket_context *context, struct libwebsocket *wsi); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 389 | |
| 390 | extern int |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 391 | delete_from_fd(struct libwebsocket_context *context, int fd); |
Darin Willits | c19456f | 2011-02-14 17:52:39 +0000 | [diff] [blame] | 392 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 393 | extern void |
| 394 | libwebsocket_set_timeout(struct libwebsocket *wsi, |
| 395 | enum pending_timeout reason, int secs); |
| 396 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 397 | extern int |
Andy Green | d44bf7f | 2011-03-06 10:29:38 +0000 | [diff] [blame] | 398 | lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len); |
| 399 | |
Andy Green | 95a7b5d | 2011-03-06 10:29:39 +0000 | [diff] [blame] | 400 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 401 | extern void |
| 402 | libwebsocket_service_timeout_check(struct libwebsocket_context *context, |
| 403 | struct libwebsocket *wsi, unsigned int sec); |
| 404 | |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 405 | extern struct libwebsocket * |
| 406 | __libwebsocket_client_connect_2(struct libwebsocket_context *context, |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 407 | struct libwebsocket *wsi); |
| 408 | |
| 409 | extern struct libwebsocket * |
| 410 | libwebsocket_create_new_server_wsi(struct libwebsocket_context *context); |
| 411 | |
| 412 | extern char * |
| 413 | libwebsockets_generate_client_handshake(struct libwebsocket_context *context, |
| 414 | struct libwebsocket *wsi, char *pkt); |
| 415 | |
| 416 | extern int |
| 417 | lws_handle_POLLOUT_event(struct libwebsocket_context *context, |
| 418 | struct libwebsocket *wsi, struct pollfd *pollfd); |
| 419 | |
| 420 | extern int |
| 421 | lws_any_extension_handled(struct libwebsocket_context *context, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 422 | struct libwebsocket *wsi, |
| 423 | enum libwebsocket_extension_callback_reasons r, |
| 424 | void *v, size_t len); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 425 | |
| 426 | extern void * |
| 427 | lws_get_extension_user_matching_ext(struct libwebsocket *wsi, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 428 | struct libwebsocket_extension *ext); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 429 | |
| 430 | extern int |
| 431 | lws_client_interpret_server_handshake(struct libwebsocket_context *context, |
| 432 | struct libwebsocket *wsi); |
| 433 | |
| 434 | extern int |
| 435 | libwebsocket_rx_sm(struct libwebsocket *wsi, unsigned char c); |
| 436 | |
Andy Green | 0922650 | 2011-05-28 10:19:19 +0100 | [diff] [blame] | 437 | extern int |
| 438 | lws_issue_raw_ext_access(struct libwebsocket *wsi, |
| 439 | unsigned char *buf, size_t len); |
| 440 | |
Darin Willits | c19456f | 2011-02-14 17:52:39 +0000 | [diff] [blame] | 441 | #ifndef LWS_OPENSSL_SUPPORT |
| 442 | |
| 443 | unsigned char * |
| 444 | SHA1(const unsigned char *d, size_t n, unsigned char *md); |
| 445 | |
| 446 | void |
| 447 | MD5(const unsigned char *input, int ilen, unsigned char *output); |
| 448 | |
| 449 | #endif |