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 | e216071 | 2013-01-28 12:19:10 +0800 | [diff] [blame] | 4 | * Copyright (C) 2010 - 2013 Andy Green <andy@warmcat.com> |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 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 | */ |
Joakim Soderberg | 91de933 | 2013-02-06 15:30:33 +0900 | [diff] [blame] | 21 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 22 | /* System introspection configs */ |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 23 | #ifdef CMAKE_BUILD |
| 24 | #include "lws_config.h" |
Joakim Soderberg | 4f4a38b | 2013-02-06 15:28:07 +0900 | [diff] [blame] | 25 | #else |
Andreas Pakulat | 68bd4bd | 2013-10-28 15:18:04 +0100 | [diff] [blame] | 26 | #if defined(WIN32) || defined(_WIN32) |
Joakim Soderberg | 91de933 | 2013-02-06 15:30:33 +0900 | [diff] [blame] | 27 | #define inline __inline |
Andy Green | 158e804 | 2014-04-02 14:25:10 +0800 | [diff] [blame] | 28 | #include <tchar.h> |
Andy Green | e40aa9b | 2014-04-02 21:02:54 +0800 | [diff] [blame] | 29 | #include <mstcpip.h> |
| 30 | #ifdef _WIN32_WCE |
| 31 | #define vsnprintf _vsnprintf |
| 32 | #endif |
| 33 | |
| 34 | #else /* not WIN32 */ |
Joakim Soderberg | 4f4a38b | 2013-02-06 15:28:07 +0900 | [diff] [blame] | 35 | #include "config.h" |
Andy Green | e40aa9b | 2014-04-02 21:02:54 +0800 | [diff] [blame] | 36 | |
| 37 | #endif /* not WIN32 */ |
| 38 | #endif /* not CMAKE */ |
| 39 | |
| 40 | #ifdef HAVE_SYS_TYPES_H |
| 41 | #include <sys/types.h> |
Joakim Soderberg | 91de933 | 2013-02-06 15:30:33 +0900 | [diff] [blame] | 42 | #endif |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 43 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 44 | #include <stdio.h> |
| 45 | #include <stdlib.h> |
| 46 | #include <string.h> |
Patrick Gansterer | b13eed4 | 2014-03-30 10:19:23 +0200 | [diff] [blame] | 47 | #include <time.h> |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 48 | #include <ctype.h> |
Peter Young | 26757a7 | 2013-01-17 10:08:16 +0800 | [diff] [blame] | 49 | #include <limits.h> |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 50 | #include <stdarg.h> |
| 51 | |
Patrick Gansterer | e5720a3 | 2014-02-28 00:57:19 +0100 | [diff] [blame] | 52 | #ifdef HAVE_SYS_STAT_H |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 53 | #include <sys/stat.h> |
Patrick Gansterer | e5720a3 | 2014-02-28 00:57:19 +0100 | [diff] [blame] | 54 | #endif |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 55 | |
Andreas Pakulat | 68bd4bd | 2013-10-28 15:18:04 +0100 | [diff] [blame] | 56 | #if defined(WIN32) || defined(_WIN32) |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 57 | #define LWS_NO_DAEMONIZE |
Patrick Gansterer | 2dbd837 | 2014-02-28 12:37:52 +0100 | [diff] [blame] | 58 | #define LWS_ERRNO WSAGetLastError() |
| 59 | #define LWS_EAGAIN WSAEWOULDBLOCK |
| 60 | #define LWS_EALREADY WSAEALREADY |
| 61 | #define LWS_EINPROGRESS WSAEINPROGRESS |
| 62 | #define LWS_EINTR WSAEINTR |
| 63 | #define LWS_EISCONN WSAEISCONN |
| 64 | #define LWS_EWOULDBLOCK WSAEWOULDBLOCK |
Patrick Gansterer | b47f87b | 2014-03-30 09:18:05 +0200 | [diff] [blame] | 65 | #define LWS_POLLHUP (FD_CLOSE) |
Patrick Gansterer | 0fc37b6 | 2014-03-28 15:44:56 +0100 | [diff] [blame] | 66 | #define LWS_POLLIN (FD_READ | FD_ACCEPT) |
| 67 | #define LWS_POLLOUT (FD_WRITE) |
Patrick Gansterer | 73882e4 | 2014-03-29 08:25:58 +0100 | [diff] [blame] | 68 | #define MSG_NOSIGNAL 0 |
| 69 | #define SHUT_RDWR SD_BOTH |
| 70 | #define SOL_TCP IPPROTO_TCP |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 71 | |
Andy Green | 158e804 | 2014-04-02 14:25:10 +0800 | [diff] [blame] | 72 | #define compatible_close(fd) closesocket(fd) |
| 73 | #define compatible_file_close(fd) CloseHandle(fd) |
| 74 | #define compatible_file_seek_cur(fd, offset) SetFilePointer(fd, offset, FILE_CURRENT) |
| 75 | #define compatible_file_read(amount, fd, buf, len) {\ |
| 76 | DWORD _amount; \ |
| 77 | if (!ReadFile(fd, buf, len, &amount, NULL)) \ |
| 78 | amount = -1; \ |
| 79 | else \ |
| 80 | amount = _amount; \ |
| 81 | } |
| 82 | #define lws_set_blocking_send(wsi) wsi->sock_send_blocking = TRUE |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 83 | #include <winsock2.h> |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 84 | #include <windows.h> |
Patrick Gansterer | 81338aa | 2014-02-27 03:21:50 +0100 | [diff] [blame] | 85 | #define LWS_INVALID_FILE INVALID_HANDLE_VALUE |
Andy Green | 5266f66 | 2014-04-02 21:31:07 +0800 | [diff] [blame] | 86 | const char *inet_ntop(int af, const void *src, char *dst, int cnt); |
Andy Green | 158e804 | 2014-04-02 14:25:10 +0800 | [diff] [blame] | 87 | #else /* not windows --> */ |
Patrick Gansterer | b13eed4 | 2014-03-30 10:19:23 +0200 | [diff] [blame] | 88 | #include <errno.h> |
| 89 | #include <fcntl.h> |
| 90 | #include <netdb.h> |
| 91 | #include <signal.h> |
| 92 | #include <strings.h> |
| 93 | #include <unistd.h> |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 94 | #include <sys/types.h> |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 95 | #include <sys/socket.h> |
Andy Green | e40aa9b | 2014-04-02 21:02:54 +0800 | [diff] [blame] | 96 | #ifdef LWS_BUILTIN_GETIFADDRS |
| 97 | #include <getifaddrs.h> |
| 98 | #else |
| 99 | #include <ifaddrs.h> |
| 100 | #endif |
| 101 | #include <sys/syslog.h> |
| 102 | #include <sys/un.h> |
| 103 | #include <sys/socket.h> |
| 104 | #include <netdb.h> |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 105 | #ifndef LWS_NO_FORK |
Artem Baguinski | 9153166 | 2011-12-14 22:14:03 +0100 | [diff] [blame] | 106 | #ifdef HAVE_SYS_PRCTL_H |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 107 | #include <sys/prctl.h> |
Andy Green | ed11a02 | 2011-01-20 10:23:50 +0000 | [diff] [blame] | 108 | #endif |
Artem Baguinski | 9153166 | 2011-12-14 22:14:03 +0100 | [diff] [blame] | 109 | #endif |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 110 | #include <netinet/in.h> |
Andy Green | 6c93955 | 2011-03-08 08:56:57 +0000 | [diff] [blame] | 111 | #include <netinet/tcp.h> |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 112 | #include <arpa/inet.h> |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 113 | #include <poll.h> |
Andrew Canaday | 9769f4f | 2014-03-23 13:25:07 +0800 | [diff] [blame] | 114 | #ifdef LWS_USE_LIBEV |
| 115 | #include <ev.h> |
| 116 | #endif /* LWS_USE_LIBEV */ |
| 117 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 118 | #include <sys/mman.h> |
Andy Green | 038d582 | 2011-02-14 20:58:26 +0000 | [diff] [blame] | 119 | #include <sys/time.h> |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 120 | |
Patrick Gansterer | 2dbd837 | 2014-02-28 12:37:52 +0100 | [diff] [blame] | 121 | #define LWS_ERRNO errno |
| 122 | #define LWS_EAGAIN EAGAIN |
| 123 | #define LWS_EALREADY EALREADY |
| 124 | #define LWS_EINPROGRESS EINPROGRESS |
| 125 | #define LWS_EINTR EINTR |
| 126 | #define LWS_EISCONN EISCONN |
| 127 | #define LWS_EWOULDBLOCK EWOULDBLOCK |
Patrick Gansterer | 81338aa | 2014-02-27 03:21:50 +0100 | [diff] [blame] | 128 | #define LWS_INVALID_FILE -1 |
Patrick Gansterer | b47f87b | 2014-03-30 09:18:05 +0200 | [diff] [blame] | 129 | #define LWS_POLLHUP (POLLHUP|POLLERR) |
| 130 | #define LWS_POLLIN (POLLIN) |
| 131 | #define LWS_POLLOUT (POLLOUT) |
Andy Green | 158e804 | 2014-04-02 14:25:10 +0800 | [diff] [blame] | 132 | #define compatible_close(fd) close(fd) |
| 133 | #define compatible_file_close(fd) close(fd) |
| 134 | #define compatible_file_seek_cur(fd, offset) lseek(fd, offset, SEEK_CUR) |
| 135 | #define compatible_file_read(amount, fd, buf, len) \ |
| 136 | amount = read(fd, buf, len); |
| 137 | #define lws_set_blocking_send(wsi) |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 138 | #endif |
| 139 | |
Patrick Gansterer | 4a83727 | 2014-02-28 13:17:49 +0100 | [diff] [blame] | 140 | #ifndef HAVE_BZERO |
| 141 | #define bzero(b, len) (memset((b), '\0', (len)), (void) 0) |
| 142 | #endif |
| 143 | |
Patrick Gansterer | 9d61491 | 2014-02-28 00:59:53 +0100 | [diff] [blame] | 144 | #ifndef HAVE_STRERROR |
| 145 | #define strerror(x) "" |
| 146 | #endif |
| 147 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 148 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | 23c5f2e | 2013-02-06 15:43:00 +0900 | [diff] [blame] | 149 | #ifdef USE_CYASSL |
| 150 | #include <cyassl/openssl/ssl.h> |
| 151 | #include <cyassl/error.h> |
| 152 | unsigned char * |
| 153 | SHA1(const unsigned char *d, size_t n, unsigned char *md); |
| 154 | #else |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 155 | #include <openssl/ssl.h> |
| 156 | #include <openssl/evp.h> |
| 157 | #include <openssl/err.h> |
Andy Green | 70dfebd | 2010-12-20 09:35:03 +0000 | [diff] [blame] | 158 | #include <openssl/md5.h> |
Andy Green | e252217 | 2011-01-18 17:14:03 +0000 | [diff] [blame] | 159 | #include <openssl/sha.h> |
Andy Green | 23c5f2e | 2013-02-06 15:43:00 +0900 | [diff] [blame] | 160 | #endif /* not USE_CYASSL */ |
Darin Willits | db9ba42 | 2011-02-14 20:56:24 +0000 | [diff] [blame] | 161 | #endif |
| 162 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 163 | #include "libwebsockets.h" |
| 164 | |
Andy Green | 158e804 | 2014-04-02 14:25:10 +0800 | [diff] [blame] | 165 | #if defined(WIN32) || defined(_WIN32) |
| 166 | |
| 167 | #ifndef BIG_ENDIAN |
| 168 | #define BIG_ENDIAN 4321 /* to show byte order (taken from gcc) */ |
| 169 | #endif |
| 170 | #ifndef LITTLE_ENDIAN |
| 171 | #define LITTLE_ENDIAN 1234 |
| 172 | #endif |
| 173 | #ifndef BYTE_ORDER |
| 174 | #define BYTE_ORDER LITTLE_ENDIAN |
| 175 | #endif |
| 176 | typedef unsigned __int64 u_int64_t; |
| 177 | |
| 178 | #undef __P |
| 179 | #ifndef __P |
| 180 | #if __STDC__ |
| 181 | #define __P(protos) protos |
| 182 | #else |
| 183 | #define __P(protos) () |
| 184 | #endif |
| 185 | #endif |
| 186 | |
| 187 | #else |
| 188 | |
| 189 | #include <sys/stat.h> |
| 190 | #include <sys/cdefs.h> |
| 191 | #include <sys/time.h> |
| 192 | |
| 193 | #if defined(__APPLE__) |
| 194 | #include <machine/endian.h> |
| 195 | #elif defined(__FreeBSD__) |
| 196 | #include <sys/endian.h> |
| 197 | #elif defined(__linux__) |
| 198 | #include <endian.h> |
| 199 | #endif |
| 200 | |
| 201 | #if !defined(BYTE_ORDER) |
| 202 | # define BYTE_ORDER __BYTE_ORDER |
| 203 | #endif |
| 204 | #if !defined(LITTLE_ENDIAN) |
| 205 | # define LITTLE_ENDIAN __LITTLE_ENDIAN |
| 206 | #endif |
| 207 | #if !defined(BIG_ENDIAN) |
| 208 | # define BIG_ENDIAN __BIG_ENDIAN |
| 209 | #endif |
| 210 | |
| 211 | #endif |
| 212 | |
Darin Willits | c19456f | 2011-02-14 17:52:39 +0000 | [diff] [blame] | 213 | /* |
| 214 | * Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag, |
| 215 | * but happily have something equivalent in the SO_NOSIGPIPE flag. |
| 216 | */ |
| 217 | #ifdef __APPLE__ |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 218 | #define MSG_NOSIGNAL SO_NOSIGPIPE |
Darin Willits | c19456f | 2011-02-14 17:52:39 +0000 | [diff] [blame] | 219 | #endif |
| 220 | |
Andy Green | c0d6b63 | 2013-01-12 23:42:17 +0800 | [diff] [blame] | 221 | #ifndef LWS_MAX_HEADER_LEN |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 222 | #define LWS_MAX_HEADER_LEN 1024 |
Andy Green | c0d6b63 | 2013-01-12 23:42:17 +0800 | [diff] [blame] | 223 | #endif |
Andy Green | c0d6b63 | 2013-01-12 23:42:17 +0800 | [diff] [blame] | 224 | #ifndef LWS_MAX_PROTOCOLS |
Andy Green | d91d5e8 | 2013-02-10 16:00:47 +0800 | [diff] [blame] | 225 | #define LWS_MAX_PROTOCOLS 5 |
Andy Green | c0d6b63 | 2013-01-12 23:42:17 +0800 | [diff] [blame] | 226 | #endif |
| 227 | #ifndef LWS_MAX_EXTENSIONS_ACTIVE |
Andy Green | d91d5e8 | 2013-02-10 16:00:47 +0800 | [diff] [blame] | 228 | #define LWS_MAX_EXTENSIONS_ACTIVE 3 |
Andy Green | c0d6b63 | 2013-01-12 23:42:17 +0800 | [diff] [blame] | 229 | #endif |
| 230 | #ifndef SPEC_LATEST_SUPPORTED |
Andy Green | d85cb20 | 2011-09-25 09:32:54 +0100 | [diff] [blame] | 231 | #define SPEC_LATEST_SUPPORTED 13 |
Andy Green | c0d6b63 | 2013-01-12 23:42:17 +0800 | [diff] [blame] | 232 | #endif |
| 233 | #ifndef AWAITING_TIMEOUT |
David Galeano | c9f1ff8 | 2013-01-09 18:01:23 +0800 | [diff] [blame] | 234 | #define AWAITING_TIMEOUT 5 |
Andy Green | c0d6b63 | 2013-01-12 23:42:17 +0800 | [diff] [blame] | 235 | #endif |
| 236 | #ifndef CIPHERS_LIST_STRING |
David Galeano | f177f2a | 2013-01-10 10:15:19 +0800 | [diff] [blame] | 237 | #define CIPHERS_LIST_STRING "DEFAULT" |
Andy Green | c0d6b63 | 2013-01-12 23:42:17 +0800 | [diff] [blame] | 238 | #endif |
Andy Green | a824d18 | 2013-01-15 20:52:29 +0800 | [diff] [blame] | 239 | #ifndef LWS_SOMAXCONN |
| 240 | #define LWS_SOMAXCONN SOMAXCONN |
| 241 | #endif |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 242 | |
Andy Green | e252217 | 2011-01-18 17:14:03 +0000 | [diff] [blame] | 243 | #define MAX_WEBSOCKET_04_KEY_LEN 128 |
Andy Green | 5449511 | 2013-02-06 21:10:16 +0900 | [diff] [blame] | 244 | #define LWS_MAX_SOCKET_IO_BUF 4096 |
Andy Green | c0d6b63 | 2013-01-12 23:42:17 +0800 | [diff] [blame] | 245 | |
| 246 | #ifndef SYSTEM_RANDOM_FILEPATH |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 247 | #define SYSTEM_RANDOM_FILEPATH "/dev/urandom" |
Andy Green | c0d6b63 | 2013-01-12 23:42:17 +0800 | [diff] [blame] | 248 | #endif |
| 249 | #ifndef LWS_MAX_ZLIB_CONN_BUFFER |
| 250 | #define LWS_MAX_ZLIB_CONN_BUFFER (64 * 1024) |
| 251 | #endif |
| 252 | |
Andy Green | 65b0e91 | 2013-01-16 07:59:47 +0800 | [diff] [blame] | 253 | /* |
| 254 | * if not in a connection storm, check for incoming |
| 255 | * connections this many normal connection services |
| 256 | */ |
| 257 | #define LWS_LISTEN_SERVICE_MODULO 10 |
Andy Green | e252217 | 2011-01-18 17:14:03 +0000 | [diff] [blame] | 258 | |
Andy Green | 5fd55cd | 2011-04-23 10:54:53 +0100 | [diff] [blame] | 259 | enum lws_websocket_opcodes_07 { |
| 260 | LWS_WS_OPCODE_07__CONTINUATION = 0, |
| 261 | LWS_WS_OPCODE_07__TEXT_FRAME = 1, |
| 262 | LWS_WS_OPCODE_07__BINARY_FRAME = 2, |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 263 | |
| 264 | LWS_WS_OPCODE_07__NOSPEC__MUX = 7, |
| 265 | |
| 266 | /* control extensions 8+ */ |
| 267 | |
Andy Green | 5fd55cd | 2011-04-23 10:54:53 +0100 | [diff] [blame] | 268 | LWS_WS_OPCODE_07__CLOSE = 8, |
| 269 | LWS_WS_OPCODE_07__PING = 9, |
| 270 | LWS_WS_OPCODE_07__PONG = 0xa, |
| 271 | }; |
| 272 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 273 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 274 | enum lws_connection_states { |
| 275 | WSI_STATE_HTTP, |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 276 | WSI_STATE_HTTP_ISSUING_FILE, |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 277 | WSI_STATE_HTTP_HEADERS, |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 278 | WSI_STATE_HTTP_BODY, |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 279 | WSI_STATE_DEAD_SOCKET, |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 280 | WSI_STATE_ESTABLISHED, |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 281 | WSI_STATE_CLIENT_UNCONNECTED, |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 282 | WSI_STATE_RETURNED_CLOSE_ALREADY, |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 283 | WSI_STATE_AWAITING_CLOSE_ACK, |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 284 | }; |
| 285 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 286 | enum lws_rx_parse_state { |
| 287 | LWS_RXPS_NEW, |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 288 | |
Andy Green | 3e5eb78 | 2011-01-18 18:14:26 +0000 | [diff] [blame] | 289 | LWS_RXPS_04_MASK_NONCE_1, |
| 290 | LWS_RXPS_04_MASK_NONCE_2, |
| 291 | LWS_RXPS_04_MASK_NONCE_3, |
| 292 | |
| 293 | LWS_RXPS_04_FRAME_HDR_1, |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 294 | LWS_RXPS_04_FRAME_HDR_LEN, |
| 295 | LWS_RXPS_04_FRAME_HDR_LEN16_2, |
| 296 | LWS_RXPS_04_FRAME_HDR_LEN16_1, |
| 297 | LWS_RXPS_04_FRAME_HDR_LEN64_8, |
| 298 | LWS_RXPS_04_FRAME_HDR_LEN64_7, |
| 299 | LWS_RXPS_04_FRAME_HDR_LEN64_6, |
| 300 | LWS_RXPS_04_FRAME_HDR_LEN64_5, |
| 301 | LWS_RXPS_04_FRAME_HDR_LEN64_4, |
| 302 | LWS_RXPS_04_FRAME_HDR_LEN64_3, |
| 303 | LWS_RXPS_04_FRAME_HDR_LEN64_2, |
| 304 | LWS_RXPS_04_FRAME_HDR_LEN64_1, |
Andy Green | 3e5eb78 | 2011-01-18 18:14:26 +0000 | [diff] [blame] | 305 | |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 306 | LWS_RXPS_07_COLLECT_FRAME_KEY_1, |
| 307 | LWS_RXPS_07_COLLECT_FRAME_KEY_2, |
| 308 | LWS_RXPS_07_COLLECT_FRAME_KEY_3, |
| 309 | LWS_RXPS_07_COLLECT_FRAME_KEY_4, |
| 310 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 311 | LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED |
| 312 | }; |
| 313 | |
| 314 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 315 | enum connection_mode { |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 316 | LWS_CONNMODE_HTTP_SERVING, |
Andy Green | 7cf6cb0 | 2013-05-19 14:04:10 +0800 | [diff] [blame] | 317 | LWS_CONNMODE_HTTP_SERVING_ACCEPTED, /* actual HTTP service going on */ |
Andy Green | 38c570c | 2013-03-09 11:52:18 +0800 | [diff] [blame] | 318 | LWS_CONNMODE_PRE_WS_SERVING_ACCEPT, |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 319 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 320 | LWS_CONNMODE_WS_SERVING, |
| 321 | LWS_CONNMODE_WS_CLIENT, |
| 322 | |
Andy Green | e216071 | 2013-01-28 12:19:10 +0800 | [diff] [blame] | 323 | /* transient, ssl delay hiding */ |
| 324 | LWS_CONNMODE_SSL_ACK_PENDING, |
| 325 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 326 | /* transient modes */ |
Andy Green | 5dc62ea | 2013-09-20 20:26:12 +0800 | [diff] [blame] | 327 | LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT, |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 328 | LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY, |
| 329 | LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE, |
shys | 24f4eb6 | 2013-10-24 22:27:08 +0800 | [diff] [blame] | 330 | LWS_CONNMODE_WS_CLIENT_WAITING_SSL, |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 331 | LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY, |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 332 | LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT, |
| 333 | LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD, |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 334 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 335 | /* special internal types */ |
| 336 | LWS_CONNMODE_SERVER_LISTENER, |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 337 | }; |
| 338 | |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 339 | enum { |
| 340 | LWS_RXFLOW_ALLOW = (1 << 0), |
| 341 | LWS_RXFLOW_PENDING_CHANGE = (1 << 1), |
| 342 | }; |
| 343 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 344 | struct libwebsocket_protocols; |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 345 | struct libwebsocket; |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 346 | |
Andrew Canaday | 9769f4f | 2014-03-23 13:25:07 +0800 | [diff] [blame] | 347 | #ifdef LWS_USE_LIBEV |
| 348 | struct lws_io_watcher { |
| 349 | struct ev_io watcher; |
| 350 | struct libwebsocket_context* context; |
| 351 | }; |
| 352 | |
| 353 | struct lws_signal_watcher { |
| 354 | struct ev_signal watcher; |
| 355 | struct libwebsocket_context* context; |
| 356 | }; |
| 357 | #endif /* LWS_USE_LIBEV */ |
| 358 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 359 | struct libwebsocket_context { |
Patrick Gansterer | 0fc37b6 | 2014-03-28 15:44:56 +0100 | [diff] [blame] | 360 | #ifdef _WIN32 |
| 361 | WSAEVENT *events; |
| 362 | #endif |
Patrick Gansterer | 73882e4 | 2014-03-29 08:25:58 +0100 | [diff] [blame] | 363 | struct libwebsocket_pollfd *fds; |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 364 | struct libwebsocket **lws_lookup; /* fd to wsi */ |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 365 | int fds_count; |
Andrew Canaday | 9769f4f | 2014-03-23 13:25:07 +0800 | [diff] [blame] | 366 | #ifdef LWS_USE_LIBEV |
| 367 | struct ev_loop* io_loop; |
| 368 | struct lws_io_watcher w_accept; |
| 369 | struct lws_signal_watcher w_sigint; |
| 370 | #endif /* LWS_USE_LIBEV */ |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 371 | int max_fds; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 372 | int listen_port; |
Mattias Lundberg | 03bb8f9 | 2014-02-18 10:06:57 +0100 | [diff] [blame] | 373 | const char *iface; |
Andy Green | 8dac94d | 2013-02-18 12:02:18 +0800 | [diff] [blame] | 374 | char http_proxy_address[128]; |
| 375 | char canonical_hostname[128]; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 376 | unsigned int http_proxy_port; |
Andy Green | 8014b29 | 2011-01-30 20:57:25 +0000 | [diff] [blame] | 377 | unsigned int options; |
Patrick Gansterer | 92792b4 | 2014-02-26 21:37:31 +0100 | [diff] [blame] | 378 | time_t last_timeout_check_s; |
Andy Green | 44eee68 | 2011-02-10 09:32:24 +0000 | [diff] [blame] | 379 | |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 380 | /* |
| 381 | * usable by anything in the service code, but only if the scope |
| 382 | * does not last longer than the service action (since next service |
| 383 | * of any socket can likewise use it and overwrite) |
| 384 | */ |
Andy Green | 5449511 | 2013-02-06 21:10:16 +0900 | [diff] [blame] | 385 | unsigned char service_buffer[LWS_MAX_SOCKET_IO_BUF]; |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 386 | |
Andy Green | 24cba92 | 2013-01-19 13:56:10 +0800 | [diff] [blame] | 387 | int started_with_parent; |
| 388 | |
Andy Green | 44eee68 | 2011-02-10 09:32:24 +0000 | [diff] [blame] | 389 | int fd_random; |
Andy Green | 65b0e91 | 2013-01-16 07:59:47 +0800 | [diff] [blame] | 390 | int listen_service_modulo; |
| 391 | int listen_service_count; |
| 392 | int listen_service_fd; |
| 393 | int listen_service_extraseen; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 394 | |
Patrick Gansterer | 1ee57f6 | 2014-03-06 11:57:50 +0100 | [diff] [blame] | 395 | /* |
| 396 | * set to the Thread ID that's doing the service loop just before entry |
| 397 | * to poll indicates service thread likely idling in poll() |
| 398 | * volatile because other threads may check it as part of processing |
| 399 | * for pollfd event change. |
| 400 | */ |
| 401 | volatile int service_tid; |
| 402 | #ifndef _WIN32 |
| 403 | int dummy_pipe_fds[2]; |
| 404 | #endif |
| 405 | |
Andy Green | a690cd0 | 2013-02-09 12:25:31 +0800 | [diff] [blame] | 406 | int ka_time; |
| 407 | int ka_probes; |
| 408 | int ka_interval; |
| 409 | |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 410 | #ifdef LWS_LATENCY |
| 411 | unsigned long worst_latency; |
| 412 | char worst_latency_info[256]; |
| 413 | #endif |
| 414 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 415 | #ifdef LWS_OPENSSL_SUPPORT |
| 416 | int use_ssl; |
James Devine | 5b34c97 | 2013-12-14 11:41:29 +0800 | [diff] [blame] | 417 | int allow_non_ssl_on_ssl_port; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 418 | SSL_CTX *ssl_ctx; |
| 419 | SSL_CTX *ssl_client_ctx; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 420 | #endif |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 421 | struct libwebsocket_protocols *protocols; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 422 | int count_protocols; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 423 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 424 | struct libwebsocket_extension *extensions; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 425 | #endif |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 426 | void *user_space; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 427 | }; |
| 428 | |
Andrew Canaday | 9769f4f | 2014-03-23 13:25:07 +0800 | [diff] [blame] | 429 | #ifdef LWS_USE_LIBEV |
| 430 | #define LWS_LIBEV_ENABLED(context) (context->options & LWS_SERVER_OPTION_LIBEV) |
| 431 | #else |
| 432 | #define LWS_LIBEV_ENABLED(context) (0) |
| 433 | #endif |
| 434 | |
Andy Green | 055f297 | 2014-03-24 16:09:25 +0800 | [diff] [blame] | 435 | #ifdef LWS_USE_IPV6 |
| 436 | #define LWS_IPV6_ENABLED(context) (!(context->options & LWS_SERVER_OPTION_DISABLE_IPV6)) |
James Devine | 3f13ea2 | 2014-03-24 16:09:25 +0800 | [diff] [blame] | 437 | #else |
| 438 | #define LWS_IPV6_ENABLED(context) (0) |
| 439 | #endif |
Andrew Canaday | 9769f4f | 2014-03-23 13:25:07 +0800 | [diff] [blame] | 440 | |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 441 | enum uri_path_states { |
| 442 | URIPS_IDLE, |
| 443 | URIPS_SEEN_SLASH, |
| 444 | URIPS_SEEN_SLASH_DOT, |
| 445 | URIPS_SEEN_SLASH_DOT_DOT, |
Andy Green | 1e3f7b8 | 2013-11-13 07:45:17 +0800 | [diff] [blame] | 446 | URIPS_ARGUMENTS, |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 447 | }; |
| 448 | |
| 449 | enum uri_esc_states { |
| 450 | URIES_IDLE, |
| 451 | URIES_SEEN_PERCENT, |
| 452 | URIES_SEEN_PERCENT_H1, |
| 453 | }; |
Andy Green | f3d3b40 | 2011-02-09 07:16:34 +0000 | [diff] [blame] | 454 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 455 | /* |
| 456 | * This is totally opaque to code using the library. It's exported as a |
| 457 | * forward-reference pointer-only declaration; the user can use the pointer with |
| 458 | * other APIs to get information out of it. |
| 459 | */ |
| 460 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 461 | struct lws_fragments { |
| 462 | unsigned short offset; |
| 463 | unsigned short len; |
| 464 | unsigned char next_frag_index; |
| 465 | }; |
| 466 | |
| 467 | struct allocated_headers { |
| 468 | unsigned short next_frag_index; |
| 469 | unsigned short pos; |
| 470 | unsigned char frag_index[WSI_TOKEN_COUNT]; |
| 471 | struct lws_fragments frags[WSI_TOKEN_COUNT * 2]; |
| 472 | char data[LWS_MAX_HEADER_LEN]; |
Andy Green | a7521de | 2013-02-18 10:38:45 +0800 | [diff] [blame] | 473 | #ifndef LWS_NO_CLIENT |
| 474 | char initial_handshake_hash_base64[30]; |
| 475 | unsigned short c_port; |
| 476 | #endif |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 477 | }; |
| 478 | |
Andy Green | 84fd949 | 2013-11-09 11:40:32 +0800 | [diff] [blame] | 479 | struct _lws_http_mode_related { |
| 480 | struct allocated_headers *ah; /* mirroring _lws_header_related */ |
Patrick Gansterer | 81338aa | 2014-02-27 03:21:50 +0100 | [diff] [blame] | 481 | #if defined(WIN32) || defined(_WIN32) |
| 482 | HANDLE fd; |
| 483 | #else |
Andy Green | 84fd949 | 2013-11-09 11:40:32 +0800 | [diff] [blame] | 484 | int fd; |
Patrick Gansterer | 81338aa | 2014-02-27 03:21:50 +0100 | [diff] [blame] | 485 | #endif |
Andy Green | 84fd949 | 2013-11-09 11:40:32 +0800 | [diff] [blame] | 486 | unsigned long filepos; |
| 487 | unsigned long filelen; |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 488 | |
| 489 | int content_length; |
| 490 | int content_length_seen; |
| 491 | int body_index; |
| 492 | unsigned char *post_buffer; |
Andy Green | 84fd949 | 2013-11-09 11:40:32 +0800 | [diff] [blame] | 493 | }; |
| 494 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 495 | struct _lws_header_related { |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 496 | struct allocated_headers *ah; |
Andy Green | 6636eec | 2013-02-18 10:34:25 +0800 | [diff] [blame] | 497 | short lextable_pos; |
Andy Green | d91d5e8 | 2013-02-10 16:00:47 +0800 | [diff] [blame] | 498 | unsigned char parser_state; /* enum lws_token_indexes */ |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 499 | enum uri_path_states ups; |
| 500 | enum uri_esc_states ues; |
| 501 | char esc_stash; |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 502 | }; |
| 503 | |
| 504 | struct _lws_websocket_related { |
Andy Green | 5449511 | 2013-02-06 21:10:16 +0900 | [diff] [blame] | 505 | char *rx_user_buffer; |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 506 | int rx_user_buffer_head; |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 507 | unsigned char frame_masking_nonce_04[4]; |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 508 | unsigned char frame_mask_index; |
| 509 | size_t rx_packet_length; |
| 510 | unsigned char opcode; |
Andy Green | d91d5e8 | 2013-02-10 16:00:47 +0800 | [diff] [blame] | 511 | unsigned int final:1; |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 512 | unsigned char rsv; |
Andy Green | d91d5e8 | 2013-02-10 16:00:47 +0800 | [diff] [blame] | 513 | unsigned int frame_is_binary:1; |
Andy Green | d91d5e8 | 2013-02-10 16:00:47 +0800 | [diff] [blame] | 514 | unsigned int all_zero_nonce:1; |
Andy Green | 6636eec | 2013-02-18 10:34:25 +0800 | [diff] [blame] | 515 | short close_reason; /* enum lws_close_status */ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 516 | unsigned char *rxflow_buffer; |
| 517 | int rxflow_len; |
| 518 | int rxflow_pos; |
Andy Green | 6636eec | 2013-02-18 10:34:25 +0800 | [diff] [blame] | 519 | unsigned int rxflow_change_to:2; |
Andy Green | d91d5e8 | 2013-02-10 16:00:47 +0800 | [diff] [blame] | 520 | unsigned int this_frame_masked:1; |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 521 | unsigned int inside_frame:1; /* next write will be more of frame */ |
| 522 | unsigned int clean_buffer:1; /* buffer not rewritten by extension */ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 523 | }; |
| 524 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 525 | struct libwebsocket { |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 526 | |
| 527 | /* lifetime members */ |
| 528 | |
Andrew Canaday | 9769f4f | 2014-03-23 13:25:07 +0800 | [diff] [blame] | 529 | #ifdef LWS_USE_LIBEV |
| 530 | struct lws_io_watcher w_read; |
| 531 | struct lws_io_watcher w_write; |
| 532 | #endif /* LWS_USE_LIBEV */ |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 533 | const struct libwebsocket_protocols *protocol; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 534 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | 46c2ea0 | 2011-03-22 09:04:01 +0000 | [diff] [blame] | 535 | struct libwebsocket_extension * |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 536 | active_extensions[LWS_MAX_EXTENSIONS_ACTIVE]; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 537 | void *active_extensions_user[LWS_MAX_EXTENSIONS_ACTIVE]; |
Andy Green | d91d5e8 | 2013-02-10 16:00:47 +0800 | [diff] [blame] | 538 | unsigned char count_active_extensions; |
| 539 | unsigned int extension_data_pending:1; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 540 | #endif |
Andy Green | d91d5e8 | 2013-02-10 16:00:47 +0800 | [diff] [blame] | 541 | unsigned char ietf_spec_revision; |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 542 | |
Andy Green | d91d5e8 | 2013-02-10 16:00:47 +0800 | [diff] [blame] | 543 | char mode; /* enum connection_mode */ |
| 544 | char state; /* enum lws_connection_states */ |
| 545 | char lws_rx_parse_state; /* enum lws_rx_parse_state */ |
| 546 | char rx_frame_type; /* enum libwebsocket_write_protocol */ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 547 | |
Andy Green | 224149a | 2013-02-11 21:43:41 +0800 | [diff] [blame] | 548 | unsigned int hdr_parsing_completed:1; |
| 549 | |
Andy Green | d91d5e8 | 2013-02-10 16:00:47 +0800 | [diff] [blame] | 550 | char pending_timeout; /* enum pending_timeout */ |
Patrick Gansterer | 92792b4 | 2014-02-26 21:37:31 +0100 | [diff] [blame] | 551 | time_t pending_timeout_limit; |
Andy Green | 6452f1e | 2010-11-11 09:22:22 +0000 | [diff] [blame] | 552 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 553 | int sock; |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 554 | int position_in_fds_table; |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 555 | #ifdef LWS_LATENCY |
| 556 | unsigned long action_start; |
| 557 | unsigned long latency_start; |
| 558 | #endif |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 559 | |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 560 | /* truncated send handling */ |
| 561 | unsigned char *truncated_send_malloc; /* non-NULL means buffering in progress */ |
Andy Green | e254d95 | 2014-03-23 11:41:15 +0800 | [diff] [blame] | 562 | unsigned int truncated_send_allocation; /* size of malloc */ |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 563 | unsigned int truncated_send_offset; /* where we are in terms of spilling */ |
| 564 | unsigned int truncated_send_len; /* how much is buffered */ |
| 565 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 566 | void *user_space; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 567 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 568 | /* members with mutually exclusive lifetimes are unionized */ |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 569 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 570 | union u { |
| 571 | struct _lws_http_mode_related http; |
| 572 | struct _lws_header_related hdr; |
| 573 | struct _lws_websocket_related ws; |
| 574 | } u; |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 575 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 576 | #ifdef LWS_OPENSSL_SUPPORT |
| 577 | SSL *ssl; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 578 | BIO *client_bio; |
Andy Green | d91d5e8 | 2013-02-10 16:00:47 +0800 | [diff] [blame] | 579 | unsigned int use_ssl:2; |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 580 | #endif |
Patrick Gansterer | 0fc37b6 | 2014-03-28 15:44:56 +0100 | [diff] [blame] | 581 | |
| 582 | #ifdef _WIN32 |
| 583 | BOOL sock_send_blocking; |
| 584 | #endif |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 585 | }; |
| 586 | |
Andy Green | 3d67f51 | 2014-04-03 07:29:50 +0800 | [diff] [blame^] | 587 | LWS_EXTERN int log_level; |
| 588 | |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 589 | LWS_EXTERN void |
Andy Green | 508946c | 2013-02-12 10:19:08 +0800 | [diff] [blame] | 590 | libwebsocket_close_and_free_session(struct libwebsocket_context *context, |
| 591 | struct libwebsocket *wsi, enum lws_close_status); |
| 592 | |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 593 | #ifndef LWS_LATENCY |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 594 | static inline void lws_latency(struct libwebsocket_context *context, |
| 595 | struct libwebsocket *wsi, const char *action, |
| 596 | int ret, int completion) { while (0); } |
| 597 | static inline void lws_latency_pre(struct libwebsocket_context *context, |
| 598 | struct libwebsocket *wsi) { while (0); } |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 599 | #else |
| 600 | #define lws_latency_pre(_context, _wsi) lws_latency(_context, _wsi, NULL, 0, 0) |
| 601 | extern void |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 602 | lws_latency(struct libwebsocket_context *context, |
| 603 | struct libwebsocket *wsi, const char *action, |
| 604 | int ret, int completion); |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 605 | #endif |
| 606 | |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 607 | LWS_EXTERN int |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 608 | libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c); |
| 609 | |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 610 | LWS_EXTERN int |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 611 | libwebsocket_parse(struct libwebsocket *wsi, unsigned char c); |
| 612 | |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 613 | LWS_EXTERN int |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 614 | libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi, |
| 615 | unsigned char *buf, size_t len); |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 616 | |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 617 | LWS_EXTERN int |
Andy Green | df73616 | 2011-01-18 15:39:02 +0000 | [diff] [blame] | 618 | lws_b64_selftest(void); |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 619 | |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 620 | LWS_EXTERN struct libwebsocket * |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 621 | wsi_from_fd(struct libwebsocket_context *context, int fd); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 622 | |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 623 | LWS_EXTERN int |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 624 | insert_wsi_socket_into_fds(struct libwebsocket_context *context, |
| 625 | struct libwebsocket *wsi); |
Darin Willits | c19456f | 2011-02-14 17:52:39 +0000 | [diff] [blame] | 626 | |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 627 | LWS_EXTERN int |
Andy Green | d44bf7f | 2011-03-06 10:29:38 +0000 | [diff] [blame] | 628 | lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len); |
| 629 | |
Andy Green | 95a7b5d | 2011-03-06 10:29:39 +0000 | [diff] [blame] | 630 | |
Andy Green | 1c6e142 | 2013-02-20 19:11:31 +0800 | [diff] [blame] | 631 | LWS_EXTERN int |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 632 | libwebsocket_service_timeout_check(struct libwebsocket_context *context, |
| 633 | struct libwebsocket *wsi, unsigned int sec); |
| 634 | |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 635 | LWS_EXTERN struct libwebsocket * |
Markus Elfring | 7521233 | 2013-10-26 20:23:00 +0800 | [diff] [blame] | 636 | libwebsocket_client_connect_2(struct libwebsocket_context *context, |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 637 | struct libwebsocket *wsi); |
| 638 | |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 639 | LWS_EXTERN struct libwebsocket * |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 640 | libwebsocket_create_new_server_wsi(struct libwebsocket_context *context); |
| 641 | |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 642 | LWS_EXTERN char * |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 643 | libwebsockets_generate_client_handshake(struct libwebsocket_context *context, |
| 644 | struct libwebsocket *wsi, char *pkt); |
| 645 | |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 646 | LWS_EXTERN int |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 647 | lws_handle_POLLOUT_event(struct libwebsocket_context *context, |
Patrick Gansterer | 73882e4 | 2014-03-29 08:25:58 +0100 | [diff] [blame] | 648 | struct libwebsocket *wsi, struct libwebsocket_pollfd *pollfd); |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 649 | #ifndef LWS_NO_EXTENSIONS |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 650 | LWS_EXTERN int |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 651 | lws_any_extension_handled(struct libwebsocket_context *context, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 652 | struct libwebsocket *wsi, |
| 653 | enum libwebsocket_extension_callback_reasons r, |
| 654 | void *v, size_t len); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 655 | |
Andy Green | 2c24ec0 | 2014-04-02 19:45:42 +0800 | [diff] [blame] | 656 | LWS_EXTERN int |
| 657 | lws_ext_callback_for_each_active(struct libwebsocket *wsi, int reason, |
| 658 | void *buf, int len); |
| 659 | LWS_EXTERN int |
| 660 | lws_ext_callback_for_each_extension_type( |
| 661 | struct libwebsocket_context *context, struct libwebsocket *wsi, |
| 662 | int reason, void *arg, int len); |
| 663 | #else |
| 664 | #define lws_any_extension_handled(_a, _b, _c, _d, _e) (0) |
| 665 | #define lws_ext_callback_for_each_active(_a, _b, _c, _d) (0) |
| 666 | #define lws_ext_callback_for_each_extension_type(_a, _b, _c, _d, _e) (0) |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 667 | #endif |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 668 | |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 669 | LWS_EXTERN int |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 670 | lws_client_interpret_server_handshake(struct libwebsocket_context *context, |
| 671 | struct libwebsocket *wsi); |
| 672 | |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 673 | LWS_EXTERN int |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 674 | libwebsocket_rx_sm(struct libwebsocket *wsi, unsigned char c); |
| 675 | |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 676 | LWS_EXTERN int |
Andy Green | 0922650 | 2011-05-28 10:19:19 +0100 | [diff] [blame] | 677 | lws_issue_raw_ext_access(struct libwebsocket *wsi, |
| 678 | unsigned char *buf, size_t len); |
| 679 | |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 680 | LWS_EXTERN int |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 681 | _libwebsocket_rx_flow_control(struct libwebsocket *wsi); |
| 682 | |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 683 | LWS_EXTERN int |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 684 | user_callback_handle_rxflow(callback_function, |
| 685 | struct libwebsocket_context *context, |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 686 | struct libwebsocket *wsi, |
| 687 | enum libwebsocket_callback_reasons reason, void *user, |
| 688 | void *in, size_t len); |
| 689 | |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 690 | LWS_EXTERN int |
Andy Green | 158e804 | 2014-04-02 14:25:10 +0800 | [diff] [blame] | 691 | lws_plat_set_socket_options(struct libwebsocket_context *context, int fd); |
Andy Green | a690cd0 | 2013-02-09 12:25:31 +0800 | [diff] [blame] | 692 | |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 693 | LWS_EXTERN int |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 694 | lws_allocate_header_table(struct libwebsocket *wsi); |
| 695 | |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 696 | LWS_EXTERN char * |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 697 | lws_hdr_simple_ptr(struct libwebsocket *wsi, enum lws_token_indexes h); |
| 698 | |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 699 | LWS_EXTERN int |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 700 | lws_hdr_simple_create(struct libwebsocket *wsi, |
| 701 | enum lws_token_indexes h, const char *s); |
| 702 | |
Andy Green | 2af4d5b | 2013-02-18 16:30:10 +0800 | [diff] [blame] | 703 | LWS_EXTERN int |
| 704 | libwebsocket_ensure_user_space(struct libwebsocket *wsi); |
| 705 | |
Andy Green | 158e804 | 2014-04-02 14:25:10 +0800 | [diff] [blame] | 706 | LWS_EXTERN int |
Andy Green | 91f19d8 | 2013-12-21 11:18:34 +0800 | [diff] [blame] | 707 | lws_change_pollfd(struct libwebsocket *wsi, int _and, int _or); |
| 708 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 709 | #ifndef LWS_NO_SERVER |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 710 | LWS_EXTERN int handshake_0405(struct libwebsocket_context *context, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 711 | struct libwebsocket *wsi); |
| 712 | #endif |
| 713 | |
| 714 | #ifndef LWS_NO_DAEMONIZE |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 715 | LWS_EXTERN int get_daemonize_pid(); |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 716 | #endif |
| 717 | |
Andy Green | a654fc0 | 2014-04-03 07:16:40 +0800 | [diff] [blame] | 718 | LWS_EXTERN int interface_to_sa(struct libwebsocket_context *context, |
James Devine | 3f13ea2 | 2014-03-24 16:09:25 +0800 | [diff] [blame] | 719 | const char *ifname, struct sockaddr_in *addr, size_t addrlen); |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 720 | |
Andy Green | 158e804 | 2014-04-02 14:25:10 +0800 | [diff] [blame] | 721 | #ifdef _WIN32 |
| 722 | LWS_EXTERN HANDLE lws_plat_open_file(const char* filename, unsigned long* filelen); |
| 723 | #else |
| 724 | LWS_EXTERN int lws_plat_open_file(const char* filename, unsigned long* filelen); |
| 725 | #endif |
| 726 | |
Darin Willits | c19456f | 2011-02-14 17:52:39 +0000 | [diff] [blame] | 727 | #ifndef LWS_OPENSSL_SUPPORT |
| 728 | |
| 729 | unsigned char * |
| 730 | SHA1(const unsigned char *d, size_t n, unsigned char *md); |
| 731 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 732 | #else |
| 733 | |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 734 | LWS_EXTERN int openssl_websocket_private_data_index; |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 735 | |
Darin Willits | c19456f | 2011-02-14 17:52:39 +0000 | [diff] [blame] | 736 | #endif |
Andy Green | a654fc0 | 2014-04-03 07:16:40 +0800 | [diff] [blame] | 737 | |
| 738 | #ifndef LWS_NO_CLIENT |
| 739 | LWS_EXTERN int lws_client_socket_service( |
| 740 | struct libwebsocket_context *context, |
| 741 | struct libwebsocket *wsi, struct libwebsocket_pollfd *pollfd); |
| 742 | #endif |
| 743 | #ifndef LWS_NO_SERVER |
| 744 | LWS_EXTERN int lws_server_socket_service( |
| 745 | struct libwebsocket_context *context, |
| 746 | struct libwebsocket *wsi, struct libwebsocket_pollfd *pollfd); |
| 747 | #endif |
| 748 | |
| 749 | /* |
| 750 | * lws_plat_ |
| 751 | */ |
| 752 | LWS_EXTERN void |
| 753 | lws_plat_delete_socket_from_fds(struct libwebsocket_context *context, |
| 754 | struct libwebsocket *wsi, int m); |
| 755 | LWS_EXTERN void |
| 756 | lws_plat_insert_socket_into_fds(struct libwebsocket_context *context, |
| 757 | struct libwebsocket *wsi); |
| 758 | LWS_EXTERN void |
| 759 | lws_plat_service_periodic(struct libwebsocket_context *context); |
| 760 | |
| 761 | LWS_EXTERN int |
| 762 | lws_plat_change_pollfd(struct libwebsocket_context *context, |
| 763 | struct libwebsocket *wsi, struct libwebsocket_pollfd *pfd); |
| 764 | LWS_EXTERN int |
| 765 | lws_plat_context_early_init(void); |
| 766 | LWS_EXTERN void |
| 767 | lws_plat_context_early_destroy(struct libwebsocket_context *context); |
| 768 | LWS_EXTERN void |
| 769 | lws_plat_context_late_destroy(struct libwebsocket_context *context); |
| 770 | LWS_EXTERN int |
| 771 | lws_poll_listen_fd(struct libwebsocket_pollfd *fd); |
| 772 | LWS_EXTERN int |
| 773 | lws_plat_service(struct libwebsocket_context *context, int timeout_ms); |
| 774 | LWS_EXTERN int |
| 775 | lws_plat_init_fd_tables(struct libwebsocket_context *context); |
| 776 | LWS_EXTERN void |
| 777 | lws_plat_drop_app_privileges(struct lws_context_creation_info *info); |
| 778 | LWS_EXTERN unsigned long long |
| 779 | time_in_microseconds(void); |
| 780 | LWS_EXTERN const char * |
| 781 | lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt); |