2010-11-08 20:20:42 +00:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
2010-11-13 10:03:47 +00:00
|
|
|
*
|
2013-01-28 12:19:10 +08:00
|
|
|
* Copyright (C) 2010 - 2013 Andy Green <andy@warmcat.com>
|
2010-11-08 20:20:42 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation:
|
|
|
|
* version 2.1 of the License.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301 USA
|
|
|
|
*/
|
2013-02-06 15:30:33 +09:00
|
|
|
|
2013-02-11 17:13:32 +08:00
|
|
|
/* System introspection configs */
|
2013-02-06 15:26:58 +09:00
|
|
|
#ifdef CMAKE_BUILD
|
|
|
|
#include "lws_config.h"
|
2013-02-06 15:28:07 +09:00
|
|
|
#else
|
2013-02-06 15:30:33 +09:00
|
|
|
#ifdef WIN32
|
|
|
|
#define inline __inline
|
|
|
|
#else
|
2013-02-06 15:28:07 +09:00
|
|
|
#include "config.h"
|
2013-02-06 15:26:58 +09:00
|
|
|
#endif
|
2013-02-06 15:30:33 +09:00
|
|
|
#endif
|
2013-02-06 15:26:58 +09:00
|
|
|
|
2013-01-19 13:58:06 +08:00
|
|
|
#if _MSC_VER > 1000 || defined(_WIN32)
|
|
|
|
#else
|
2010-12-20 09:35:03 +00:00
|
|
|
#include <unistd.h>
|
2013-01-19 13:58:06 +08:00
|
|
|
#include <strings.h>
|
|
|
|
#endif
|
2010-11-08 20:20:42 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
2013-01-17 10:08:16 +08:00
|
|
|
#include <limits.h>
|
2013-02-11 17:13:32 +08:00
|
|
|
#ifdef __MINGW64__
|
2012-04-12 10:54:22 +08:00
|
|
|
#else
|
2013-02-11 17:13:32 +08:00
|
|
|
#ifdef __MINGW32__
|
2013-01-19 13:58:06 +08:00
|
|
|
#elif _MSC_VER > 1000 || defined(_WIN32)
|
2012-04-12 10:54:22 +08:00
|
|
|
#else
|
2011-01-28 10:00:18 +00:00
|
|
|
#include <netdb.h>
|
2012-04-12 10:54:22 +08:00
|
|
|
#endif
|
|
|
|
#endif
|
2011-03-02 22:03:47 +00:00
|
|
|
#include <stdarg.h>
|
2010-11-08 20:20:42 +00:00
|
|
|
|
2010-12-20 09:35:03 +00:00
|
|
|
#include <sys/stat.h>
|
2011-03-02 22:03:47 +00:00
|
|
|
|
|
|
|
#ifdef WIN32
|
2013-02-06 15:26:58 +09:00
|
|
|
#define LWS_NO_DAEMONIZE
|
2013-04-27 08:33:26 +08:00
|
|
|
#ifndef EWOULDBLOCK
|
|
|
|
#define EWOULDBLOCK EAGAIN
|
|
|
|
#endif
|
2013-02-06 15:26:58 +09:00
|
|
|
|
2013-01-14 15:35:02 +08:00
|
|
|
#define compatible_close(fd) closesocket(fd);
|
2013-02-11 17:13:32 +08:00
|
|
|
#ifdef __MINGW64__
|
|
|
|
#else
|
|
|
|
#ifdef __MINGW32__
|
2012-04-12 10:54:22 +08:00
|
|
|
#else
|
2011-03-02 22:03:47 +00:00
|
|
|
#include <time.h >
|
2012-04-12 10:54:22 +08:00
|
|
|
#endif
|
|
|
|
#endif
|
2011-03-02 22:03:47 +00:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2ipdef.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
|
|
|
#include <sys/types.h>
|
2010-11-08 20:20:42 +00:00
|
|
|
#include <sys/socket.h>
|
2011-01-20 10:23:50 +00:00
|
|
|
#ifndef LWS_NO_FORK
|
2011-12-14 22:14:03 +01:00
|
|
|
#ifdef HAVE_SYS_PRCTL_H
|
2010-12-18 15:13:50 +00:00
|
|
|
#include <sys/prctl.h>
|
2011-01-20 10:23:50 +00:00
|
|
|
#endif
|
2011-12-14 22:14:03 +01:00
|
|
|
#endif
|
2010-11-08 20:20:42 +00:00
|
|
|
#include <netinet/in.h>
|
2011-03-08 08:56:57 +00:00
|
|
|
#include <netinet/tcp.h>
|
2010-12-18 15:13:50 +00:00
|
|
|
#include <arpa/inet.h>
|
2010-11-08 20:20:42 +00:00
|
|
|
|
|
|
|
#include <poll.h>
|
|
|
|
#include <sys/mman.h>
|
2011-02-14 20:58:26 +00:00
|
|
|
#include <sys/time.h>
|
2010-11-08 20:20:42 +00:00
|
|
|
|
2013-01-14 15:35:02 +08:00
|
|
|
#define compatible_close(fd) close(fd);
|
2011-03-02 22:03:47 +00:00
|
|
|
#endif
|
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
#ifdef LWS_OPENSSL_SUPPORT
|
2013-02-06 15:43:00 +09:00
|
|
|
#ifdef USE_CYASSL
|
|
|
|
#include <cyassl/openssl/ssl.h>
|
|
|
|
#include <cyassl/error.h>
|
|
|
|
unsigned char *
|
|
|
|
SHA1(const unsigned char *d, size_t n, unsigned char *md);
|
|
|
|
#else
|
2010-11-08 20:20:42 +00:00
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/err.h>
|
2010-12-20 09:35:03 +00:00
|
|
|
#include <openssl/md5.h>
|
2011-01-18 17:14:03 +00:00
|
|
|
#include <openssl/sha.h>
|
2013-02-06 15:43:00 +09:00
|
|
|
#endif /* not USE_CYASSL */
|
2011-02-14 20:56:24 +00:00
|
|
|
#endif
|
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
#include "libwebsockets.h"
|
|
|
|
|
2011-02-14 17:52:39 +00:00
|
|
|
/*
|
|
|
|
* Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag,
|
|
|
|
* but happily have something equivalent in the SO_NOSIGPIPE flag.
|
|
|
|
*/
|
|
|
|
#ifdef __APPLE__
|
2012-04-09 15:09:01 +08:00
|
|
|
#define MSG_NOSIGNAL SO_NOSIGPIPE
|
2011-02-14 17:52:39 +00:00
|
|
|
#endif
|
|
|
|
|
2013-01-12 23:42:17 +08:00
|
|
|
#ifndef LWS_MAX_HEADER_LEN
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
#define LWS_MAX_HEADER_LEN 1024
|
2013-01-12 23:42:17 +08:00
|
|
|
#endif
|
|
|
|
#ifndef LWS_MAX_PROTOCOLS
|
2013-02-10 16:00:47 +08:00
|
|
|
#define LWS_MAX_PROTOCOLS 5
|
2013-01-12 23:42:17 +08:00
|
|
|
#endif
|
|
|
|
#ifndef LWS_MAX_EXTENSIONS_ACTIVE
|
2013-02-10 16:00:47 +08:00
|
|
|
#define LWS_MAX_EXTENSIONS_ACTIVE 3
|
2013-01-12 23:42:17 +08:00
|
|
|
#endif
|
|
|
|
#ifndef SPEC_LATEST_SUPPORTED
|
2011-09-25 09:32:54 +01:00
|
|
|
#define SPEC_LATEST_SUPPORTED 13
|
2013-01-12 23:42:17 +08:00
|
|
|
#endif
|
|
|
|
#ifndef AWAITING_TIMEOUT
|
2013-01-09 18:01:23 +08:00
|
|
|
#define AWAITING_TIMEOUT 5
|
2013-01-12 23:42:17 +08:00
|
|
|
#endif
|
|
|
|
#ifndef CIPHERS_LIST_STRING
|
2013-01-10 10:15:19 +08:00
|
|
|
#define CIPHERS_LIST_STRING "DEFAULT"
|
2013-01-12 23:42:17 +08:00
|
|
|
#endif
|
2013-01-15 20:52:29 +08:00
|
|
|
#ifndef LWS_SOMAXCONN
|
|
|
|
#define LWS_SOMAXCONN SOMAXCONN
|
|
|
|
#endif
|
2010-11-08 20:20:42 +00:00
|
|
|
|
2011-01-18 17:14:03 +00:00
|
|
|
#define MAX_WEBSOCKET_04_KEY_LEN 128
|
2013-02-06 21:10:16 +09:00
|
|
|
#define LWS_MAX_SOCKET_IO_BUF 4096
|
2013-01-12 23:42:17 +08:00
|
|
|
|
|
|
|
#ifndef SYSTEM_RANDOM_FILEPATH
|
2011-01-22 12:51:57 +00:00
|
|
|
#define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
|
2013-01-12 23:42:17 +08:00
|
|
|
#endif
|
|
|
|
#ifndef LWS_MAX_ZLIB_CONN_BUFFER
|
|
|
|
#define LWS_MAX_ZLIB_CONN_BUFFER (64 * 1024)
|
|
|
|
#endif
|
|
|
|
|
listen socket more frequent service
From an idea by Edwin van den Oetelaar <oetelaar.automatisering@gmail.com>
When testing libwebsockets with ab, Edwin found an unexpected bump in
the distribution of latencies, some connections were held back almost
the whole test duration.
http://ml.libwebsockets.org/pipermail/libwebsockets/2013-January/000006.html
Studying the problem revealed that when there are mass pending connections
amongst many active connections, we do not service the listen socket often
enough to clear the backlog, some seem to get stale violating FIFO ordering.
This patch introduces listen socket service "piggybacking", where every n
normal socket service actions we also check the listen socket and deal with
pending connections there.
Normally, it checks the listen socket gratuitously every 10 normal socket
services. However, if it finds something waiting, it forces a check on the
next normal socket service too by keeping stats on how often something was
waiting. If the probability of something waiting each time becomes high,
it will allow up to two waiting connections to be serviced for each normal
socket service.
In that way it has low burden in the normal case, but rapidly adapts by
detecting mass connection loads as found in ab.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-16 07:59:47 +08:00
|
|
|
/*
|
|
|
|
* if not in a connection storm, check for incoming
|
|
|
|
* connections this many normal connection services
|
|
|
|
*/
|
|
|
|
#define LWS_LISTEN_SERVICE_MODULO 10
|
2011-01-18 17:14:03 +00:00
|
|
|
|
2011-04-23 10:54:53 +01:00
|
|
|
enum lws_websocket_opcodes_07 {
|
|
|
|
LWS_WS_OPCODE_07__CONTINUATION = 0,
|
|
|
|
LWS_WS_OPCODE_07__TEXT_FRAME = 1,
|
|
|
|
LWS_WS_OPCODE_07__BINARY_FRAME = 2,
|
2011-05-23 10:00:03 +01:00
|
|
|
|
|
|
|
LWS_WS_OPCODE_07__NOSPEC__MUX = 7,
|
|
|
|
|
|
|
|
/* control extensions 8+ */
|
|
|
|
|
2011-04-23 10:54:53 +01:00
|
|
|
LWS_WS_OPCODE_07__CLOSE = 8,
|
|
|
|
LWS_WS_OPCODE_07__PING = 9,
|
|
|
|
LWS_WS_OPCODE_07__PONG = 0xa,
|
|
|
|
};
|
|
|
|
|
2011-05-23 10:00:03 +01:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
enum lws_connection_states {
|
|
|
|
WSI_STATE_HTTP,
|
2013-01-15 13:40:23 +08:00
|
|
|
WSI_STATE_HTTP_ISSUING_FILE,
|
2010-11-08 20:20:42 +00:00
|
|
|
WSI_STATE_HTTP_HEADERS,
|
|
|
|
WSI_STATE_DEAD_SOCKET,
|
2011-01-22 12:51:57 +00:00
|
|
|
WSI_STATE_ESTABLISHED,
|
2011-02-10 09:07:05 +00:00
|
|
|
WSI_STATE_CLIENT_UNCONNECTED,
|
2011-02-14 20:25:43 +00:00
|
|
|
WSI_STATE_RETURNED_CLOSE_ALREADY,
|
2011-05-23 10:00:03 +01:00
|
|
|
WSI_STATE_AWAITING_CLOSE_ACK,
|
2010-11-08 20:20:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum lws_rx_parse_state {
|
|
|
|
LWS_RXPS_NEW,
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2011-01-18 18:14:26 +00:00
|
|
|
LWS_RXPS_04_MASK_NONCE_1,
|
|
|
|
LWS_RXPS_04_MASK_NONCE_2,
|
|
|
|
LWS_RXPS_04_MASK_NONCE_3,
|
|
|
|
|
|
|
|
LWS_RXPS_04_FRAME_HDR_1,
|
2011-01-19 12:20:27 +00:00
|
|
|
LWS_RXPS_04_FRAME_HDR_LEN,
|
|
|
|
LWS_RXPS_04_FRAME_HDR_LEN16_2,
|
|
|
|
LWS_RXPS_04_FRAME_HDR_LEN16_1,
|
|
|
|
LWS_RXPS_04_FRAME_HDR_LEN64_8,
|
|
|
|
LWS_RXPS_04_FRAME_HDR_LEN64_7,
|
|
|
|
LWS_RXPS_04_FRAME_HDR_LEN64_6,
|
|
|
|
LWS_RXPS_04_FRAME_HDR_LEN64_5,
|
|
|
|
LWS_RXPS_04_FRAME_HDR_LEN64_4,
|
|
|
|
LWS_RXPS_04_FRAME_HDR_LEN64_3,
|
|
|
|
LWS_RXPS_04_FRAME_HDR_LEN64_2,
|
|
|
|
LWS_RXPS_04_FRAME_HDR_LEN64_1,
|
2011-01-18 18:14:26 +00:00
|
|
|
|
2011-04-24 05:46:23 +01:00
|
|
|
LWS_RXPS_07_COLLECT_FRAME_KEY_1,
|
|
|
|
LWS_RXPS_07_COLLECT_FRAME_KEY_2,
|
|
|
|
LWS_RXPS_07_COLLECT_FRAME_KEY_3,
|
|
|
|
LWS_RXPS_07_COLLECT_FRAME_KEY_4,
|
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-02-12 11:57:43 +00:00
|
|
|
enum connection_mode {
|
2013-01-15 13:40:23 +08:00
|
|
|
LWS_CONNMODE_HTTP_SERVING,
|
2013-05-19 14:04:10 +08:00
|
|
|
LWS_CONNMODE_HTTP_SERVING_ACCEPTED, /* actual HTTP service going on */
|
2013-03-09 11:52:18 +08:00
|
|
|
LWS_CONNMODE_PRE_WS_SERVING_ACCEPT,
|
2013-01-15 13:40:23 +08:00
|
|
|
|
2011-02-12 11:57:43 +00:00
|
|
|
LWS_CONNMODE_WS_SERVING,
|
|
|
|
LWS_CONNMODE_WS_CLIENT,
|
|
|
|
|
2013-01-28 12:19:10 +08:00
|
|
|
/* transient, ssl delay hiding */
|
|
|
|
LWS_CONNMODE_SSL_ACK_PENDING,
|
|
|
|
|
2011-02-14 20:25:43 +00:00
|
|
|
/* transient modes */
|
2013-09-20 20:26:12 +08:00
|
|
|
LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT,
|
2011-02-14 20:25:43 +00:00
|
|
|
LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY,
|
|
|
|
LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE,
|
|
|
|
LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY,
|
2011-05-23 10:00:03 +01:00
|
|
|
LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT,
|
|
|
|
LWS_CONNMODE_WS_CLIENT_PENDING_CANDIDATE_CHILD,
|
2011-02-14 20:25:43 +00:00
|
|
|
|
2011-02-12 11:57:43 +00:00
|
|
|
/* special internal types */
|
|
|
|
LWS_CONNMODE_SERVER_LISTENER,
|
|
|
|
};
|
|
|
|
|
2013-03-16 11:24:23 +08:00
|
|
|
enum {
|
|
|
|
LWS_RXFLOW_ALLOW = (1 << 0),
|
|
|
|
LWS_RXFLOW_PENDING_CHANGE = (1 << 1),
|
|
|
|
};
|
|
|
|
|
2011-01-19 13:11:55 +00:00
|
|
|
struct libwebsocket_protocols;
|
2013-01-17 12:26:48 +08:00
|
|
|
struct libwebsocket;
|
2011-01-19 13:11:55 +00:00
|
|
|
|
2010-12-18 15:13:50 +00:00
|
|
|
struct libwebsocket_context {
|
2013-01-17 12:26:48 +08:00
|
|
|
struct pollfd *fds;
|
|
|
|
struct libwebsocket **lws_lookup; /* fd to wsi */
|
2010-12-18 15:13:50 +00:00
|
|
|
int fds_count;
|
2013-01-17 12:26:48 +08:00
|
|
|
int max_fds;
|
2011-01-22 12:51:57 +00:00
|
|
|
int listen_port;
|
2013-02-18 12:02:18 +08:00
|
|
|
char http_proxy_address[128];
|
|
|
|
char canonical_hostname[128];
|
2011-01-27 22:01:43 +00:00
|
|
|
unsigned int http_proxy_port;
|
2011-01-30 20:57:25 +00:00
|
|
|
unsigned int options;
|
2011-02-14 17:59:43 +00:00
|
|
|
unsigned long last_timeout_check_s;
|
2011-02-10 09:32:24 +00:00
|
|
|
|
2013-01-22 07:20:08 +08:00
|
|
|
/*
|
|
|
|
* usable by anything in the service code, but only if the scope
|
|
|
|
* does not last longer than the service action (since next service
|
|
|
|
* of any socket can likewise use it and overwrite)
|
|
|
|
*/
|
2013-02-06 21:10:16 +09:00
|
|
|
unsigned char service_buffer[LWS_MAX_SOCKET_IO_BUF];
|
2013-01-22 07:20:08 +08:00
|
|
|
|
2013-01-19 13:56:10 +08:00
|
|
|
int started_with_parent;
|
|
|
|
|
2011-02-10 09:32:24 +00:00
|
|
|
int fd_random;
|
listen socket more frequent service
From an idea by Edwin van den Oetelaar <oetelaar.automatisering@gmail.com>
When testing libwebsockets with ab, Edwin found an unexpected bump in
the distribution of latencies, some connections were held back almost
the whole test duration.
http://ml.libwebsockets.org/pipermail/libwebsockets/2013-January/000006.html
Studying the problem revealed that when there are mass pending connections
amongst many active connections, we do not service the listen socket often
enough to clear the backlog, some seem to get stale violating FIFO ordering.
This patch introduces listen socket service "piggybacking", where every n
normal socket service actions we also check the listen socket and deal with
pending connections there.
Normally, it checks the listen socket gratuitously every 10 normal socket
services. However, if it finds something waiting, it forces a check on the
next normal socket service too by keeping stats on how often something was
waiting. If the probability of something waiting each time becomes high,
it will allow up to two waiting connections to be serviced for each normal
socket service.
In that way it has low burden in the normal case, but rapidly adapts by
detecting mass connection loads as found in ab.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-16 07:59:47 +08:00
|
|
|
int listen_service_modulo;
|
|
|
|
int listen_service_count;
|
|
|
|
int listen_service_fd;
|
|
|
|
int listen_service_extraseen;
|
2012-04-09 15:09:01 +08:00
|
|
|
|
2013-02-09 12:25:31 +08:00
|
|
|
int ka_time;
|
|
|
|
int ka_probes;
|
|
|
|
int ka_interval;
|
|
|
|
|
2013-01-29 12:36:17 +08:00
|
|
|
#ifdef LWS_LATENCY
|
|
|
|
unsigned long worst_latency;
|
|
|
|
char worst_latency_info[256];
|
|
|
|
#endif
|
|
|
|
|
2010-12-18 15:13:50 +00:00
|
|
|
#ifdef LWS_OPENSSL_SUPPORT
|
|
|
|
int use_ssl;
|
2011-01-27 06:26:52 +00:00
|
|
|
SSL_CTX *ssl_ctx;
|
|
|
|
SSL_CTX *ssl_client_ctx;
|
2010-12-18 15:13:50 +00:00
|
|
|
#endif
|
2011-01-19 13:11:55 +00:00
|
|
|
struct libwebsocket_protocols *protocols;
|
2010-12-18 15:13:50 +00:00
|
|
|
int count_protocols;
|
2013-01-20 17:08:31 +08:00
|
|
|
#ifndef LWS_NO_EXTENSIONS
|
2011-03-05 16:12:15 +00:00
|
|
|
struct libwebsocket_extension *extensions;
|
2013-01-20 17:08:31 +08:00
|
|
|
#endif
|
2013-02-11 17:13:32 +08:00
|
|
|
void *user_space;
|
2010-12-18 15:13:50 +00:00
|
|
|
};
|
|
|
|
|
2011-02-09 07:16:34 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
/*
|
|
|
|
* This is totally opaque to code using the library. It's exported as a
|
|
|
|
* forward-reference pointer-only declaration; the user can use the pointer with
|
|
|
|
* other APIs to get information out of it.
|
|
|
|
*/
|
|
|
|
|
2013-01-21 11:04:23 +08:00
|
|
|
struct _lws_http_mode_related {
|
2013-01-22 07:20:08 +08:00
|
|
|
int fd;
|
2013-01-21 11:04:23 +08:00
|
|
|
unsigned long filepos;
|
|
|
|
unsigned long filelen;
|
|
|
|
};
|
2010-11-08 20:20:42 +00:00
|
|
|
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
struct lws_fragments {
|
|
|
|
unsigned short offset;
|
|
|
|
unsigned short len;
|
|
|
|
unsigned char next_frag_index;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct allocated_headers {
|
|
|
|
unsigned short next_frag_index;
|
|
|
|
unsigned short pos;
|
|
|
|
unsigned char frag_index[WSI_TOKEN_COUNT];
|
|
|
|
struct lws_fragments frags[WSI_TOKEN_COUNT * 2];
|
|
|
|
char data[LWS_MAX_HEADER_LEN];
|
2013-02-18 10:38:45 +08:00
|
|
|
#ifndef LWS_NO_CLIENT
|
|
|
|
char initial_handshake_hash_base64[30];
|
|
|
|
unsigned short c_port;
|
|
|
|
#endif
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
};
|
|
|
|
|
2013-01-21 11:04:23 +08:00
|
|
|
struct _lws_header_related {
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
struct allocated_headers *ah;
|
2013-02-18 10:34:25 +08:00
|
|
|
short lextable_pos;
|
2013-02-10 16:00:47 +08:00
|
|
|
unsigned char parser_state; /* enum lws_token_indexes */
|
2013-01-21 11:04:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _lws_websocket_related {
|
2013-02-06 21:10:16 +09:00
|
|
|
char *rx_user_buffer;
|
2010-11-11 09:22:22 +00:00
|
|
|
int rx_user_buffer_head;
|
2011-01-19 12:20:27 +00:00
|
|
|
unsigned char frame_masking_nonce_04[4];
|
|
|
|
unsigned char frame_mask_index;
|
2010-11-08 20:20:42 +00:00
|
|
|
size_t rx_packet_length;
|
2011-01-19 12:20:27 +00:00
|
|
|
unsigned char opcode;
|
2013-02-10 16:00:47 +08:00
|
|
|
unsigned int final:1;
|
2013-01-09 18:06:55 +08:00
|
|
|
unsigned char rsv;
|
2013-02-10 16:00:47 +08:00
|
|
|
unsigned int frame_is_binary:1;
|
|
|
|
unsigned int all_zero_nonce:1;
|
2013-02-18 10:34:25 +08:00
|
|
|
short close_reason; /* enum lws_close_status */
|
2013-01-21 11:04:23 +08:00
|
|
|
unsigned char *rxflow_buffer;
|
|
|
|
int rxflow_len;
|
|
|
|
int rxflow_pos;
|
2013-02-18 10:34:25 +08:00
|
|
|
unsigned int rxflow_change_to:2;
|
2013-02-10 16:00:47 +08:00
|
|
|
unsigned int this_frame_masked:1;
|
2013-01-21 11:04:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct libwebsocket {
|
|
|
|
|
|
|
|
/* lifetime members */
|
|
|
|
|
|
|
|
const struct libwebsocket_protocols *protocol;
|
|
|
|
#ifndef LWS_NO_EXTENSIONS
|
|
|
|
struct libwebsocket_extension *
|
|
|
|
active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
|
|
|
|
void *active_extensions_user[LWS_MAX_EXTENSIONS_ACTIVE];
|
2013-02-10 16:00:47 +08:00
|
|
|
unsigned char count_active_extensions;
|
|
|
|
unsigned int extension_data_pending:1;
|
2013-01-21 11:04:23 +08:00
|
|
|
#endif
|
2013-02-10 16:00:47 +08:00
|
|
|
unsigned char ietf_spec_revision;
|
2011-04-24 05:46:23 +01:00
|
|
|
|
2013-02-10 16:00:47 +08:00
|
|
|
char mode; /* enum connection_mode */
|
|
|
|
char state; /* enum lws_connection_states */
|
|
|
|
char lws_rx_parse_state; /* enum lws_rx_parse_state */
|
|
|
|
char rx_frame_type; /* enum libwebsocket_write_protocol */
|
2013-01-21 11:04:23 +08:00
|
|
|
|
2013-02-11 21:43:41 +08:00
|
|
|
unsigned int hdr_parsing_completed:1;
|
|
|
|
|
2013-02-10 16:00:47 +08:00
|
|
|
char pending_timeout; /* enum pending_timeout */
|
2013-01-21 11:04:23 +08:00
|
|
|
unsigned long pending_timeout_limit;
|
|
|
|
|
|
|
|
int sock;
|
|
|
|
int position_in_fds_table;
|
2013-01-29 12:36:17 +08:00
|
|
|
#ifdef LWS_LATENCY
|
|
|
|
unsigned long action_start;
|
|
|
|
unsigned long latency_start;
|
|
|
|
#endif
|
2013-01-21 11:04:23 +08:00
|
|
|
|
|
|
|
void *user_space;
|
|
|
|
|
|
|
|
/* members with mutually exclusive lifetimes are unionized */
|
|
|
|
|
|
|
|
union u {
|
|
|
|
struct _lws_http_mode_related http;
|
|
|
|
struct _lws_header_related hdr;
|
|
|
|
struct _lws_websocket_related ws;
|
|
|
|
} u;
|
2013-01-18 11:43:21 +08:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
#ifdef LWS_OPENSSL_SUPPORT
|
|
|
|
SSL *ssl;
|
2011-01-27 06:26:52 +00:00
|
|
|
BIO *client_bio;
|
2013-02-10 16:00:47 +08:00
|
|
|
unsigned int use_ssl:2;
|
2013-02-11 17:13:32 +08:00
|
|
|
#endif
|
2010-11-08 20:20:42 +00:00
|
|
|
};
|
|
|
|
|
2013-02-13 09:29:26 +08:00
|
|
|
LWS_EXTERN void
|
2013-02-12 10:19:08 +08:00
|
|
|
libwebsocket_close_and_free_session(struct libwebsocket_context *context,
|
|
|
|
struct libwebsocket *wsi, enum lws_close_status);
|
|
|
|
|
2013-01-29 12:36:17 +08:00
|
|
|
#ifndef LWS_LATENCY
|
2013-02-11 17:13:32 +08:00
|
|
|
static inline void lws_latency(struct libwebsocket_context *context,
|
|
|
|
struct libwebsocket *wsi, const char *action,
|
|
|
|
int ret, int completion) { while (0); }
|
|
|
|
static inline void lws_latency_pre(struct libwebsocket_context *context,
|
|
|
|
struct libwebsocket *wsi) { while (0); }
|
2013-01-29 12:36:17 +08:00
|
|
|
#else
|
|
|
|
#define lws_latency_pre(_context, _wsi) lws_latency(_context, _wsi, NULL, 0, 0)
|
|
|
|
extern void
|
2013-02-11 17:13:32 +08:00
|
|
|
lws_latency(struct libwebsocket_context *context,
|
|
|
|
struct libwebsocket *wsi, const char *action,
|
|
|
|
int ret, int completion);
|
2013-01-29 12:36:17 +08:00
|
|
|
#endif
|
|
|
|
|
2013-02-13 09:29:26 +08:00
|
|
|
LWS_EXTERN int
|
2011-01-22 12:51:57 +00:00
|
|
|
libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c);
|
|
|
|
|
2013-02-13 09:29:26 +08:00
|
|
|
LWS_EXTERN int
|
2010-12-18 15:13:50 +00:00
|
|
|
libwebsocket_parse(struct libwebsocket *wsi, unsigned char c);
|
|
|
|
|
2013-02-13 09:29:26 +08:00
|
|
|
LWS_EXTERN int
|
2010-12-18 15:13:50 +00:00
|
|
|
libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi,
|
|
|
|
unsigned char *buf, size_t len);
|
2010-12-19 22:13:26 +00:00
|
|
|
|
2013-02-13 09:29:26 +08:00
|
|
|
LWS_EXTERN int
|
2011-01-18 15:39:02 +00:00
|
|
|
lws_b64_selftest(void);
|
2011-02-09 08:49:14 +00:00
|
|
|
|
2013-02-13 09:29:26 +08:00
|
|
|
LWS_EXTERN struct libwebsocket *
|
2011-03-02 22:03:47 +00:00
|
|
|
wsi_from_fd(struct libwebsocket_context *context, int fd);
|
2011-02-12 11:57:43 +00:00
|
|
|
|
2013-02-13 09:29:26 +08:00
|
|
|
LWS_EXTERN int
|
2013-02-11 17:13:32 +08:00
|
|
|
insert_wsi_socket_into_fds(struct libwebsocket_context *context,
|
|
|
|
struct libwebsocket *wsi);
|
2011-02-14 17:52:39 +00:00
|
|
|
|
2013-02-13 09:29:26 +08:00
|
|
|
LWS_EXTERN int
|
2011-03-06 10:29:38 +00:00
|
|
|
lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len);
|
|
|
|
|
2011-03-06 10:29:39 +00:00
|
|
|
|
2013-02-20 19:11:31 +08:00
|
|
|
LWS_EXTERN int
|
2011-05-23 10:00:03 +01:00
|
|
|
libwebsocket_service_timeout_check(struct libwebsocket_context *context,
|
|
|
|
struct libwebsocket *wsi, unsigned int sec);
|
|
|
|
|
2013-02-13 09:29:26 +08:00
|
|
|
LWS_EXTERN struct libwebsocket *
|
2012-04-09 15:09:01 +08:00
|
|
|
__libwebsocket_client_connect_2(struct libwebsocket_context *context,
|
2011-05-23 10:00:03 +01:00
|
|
|
struct libwebsocket *wsi);
|
|
|
|
|
2013-02-13 09:29:26 +08:00
|
|
|
LWS_EXTERN struct libwebsocket *
|
2011-05-23 10:00:03 +01:00
|
|
|
libwebsocket_create_new_server_wsi(struct libwebsocket_context *context);
|
|
|
|
|
2013-02-13 09:29:26 +08:00
|
|
|
LWS_EXTERN char *
|
2011-05-23 10:00:03 +01:00
|
|
|
libwebsockets_generate_client_handshake(struct libwebsocket_context *context,
|
|
|
|
struct libwebsocket *wsi, char *pkt);
|
|
|
|
|
2013-02-13 09:29:26 +08:00
|
|
|
LWS_EXTERN int
|
2011-05-23 10:00:03 +01:00
|
|
|
lws_handle_POLLOUT_event(struct libwebsocket_context *context,
|
|
|
|
struct libwebsocket *wsi, struct pollfd *pollfd);
|
2013-01-20 17:08:31 +08:00
|
|
|
#ifndef LWS_NO_EXTENSIONS
|
2013-02-13 09:29:26 +08:00
|
|
|
LWS_EXTERN int
|
2011-05-23 10:00:03 +01:00
|
|
|
lws_any_extension_handled(struct libwebsocket_context *context,
|
2012-04-09 15:09:01 +08:00
|
|
|
struct libwebsocket *wsi,
|
|
|
|
enum libwebsocket_extension_callback_reasons r,
|
|
|
|
void *v, size_t len);
|
2011-05-23 10:00:03 +01:00
|
|
|
|
2013-02-13 09:29:26 +08:00
|
|
|
LWS_EXTERN void *
|
2011-05-23 10:00:03 +01:00
|
|
|
lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
|
2012-04-09 15:09:01 +08:00
|
|
|
struct libwebsocket_extension *ext);
|
2013-01-20 17:08:31 +08:00
|
|
|
#endif
|
2011-05-23 10:00:03 +01:00
|
|
|
|
2013-02-13 09:29:26 +08:00
|
|
|
LWS_EXTERN int
|
2011-05-23 10:00:03 +01:00
|
|
|
lws_client_interpret_server_handshake(struct libwebsocket_context *context,
|
|
|
|
struct libwebsocket *wsi);
|
|
|
|
|
2013-02-13 09:29:26 +08:00
|
|
|
LWS_EXTERN int
|
2011-05-23 10:00:03 +01:00
|
|
|
libwebsocket_rx_sm(struct libwebsocket *wsi, unsigned char c);
|
|
|
|
|
2013-02-13 09:29:26 +08:00
|
|
|
LWS_EXTERN int
|
2011-05-28 10:19:19 +01:00
|
|
|
lws_issue_raw_ext_access(struct libwebsocket *wsi,
|
|
|
|
unsigned char *buf, size_t len);
|
|
|
|
|
2013-02-13 09:29:26 +08:00
|
|
|
LWS_EXTERN int
|
2013-01-17 16:50:35 +08:00
|
|
|
_libwebsocket_rx_flow_control(struct libwebsocket *wsi);
|
|
|
|
|
2013-02-13 09:29:26 +08:00
|
|
|
LWS_EXTERN int
|
2013-02-11 17:13:32 +08:00
|
|
|
user_callback_handle_rxflow(callback_function,
|
|
|
|
struct libwebsocket_context *context,
|
2013-01-17 16:50:35 +08:00
|
|
|
struct libwebsocket *wsi,
|
|
|
|
enum libwebsocket_callback_reasons reason, void *user,
|
|
|
|
void *in, size_t len);
|
|
|
|
|
2013-02-13 09:29:26 +08:00
|
|
|
LWS_EXTERN int
|
2013-02-09 12:25:31 +08:00
|
|
|
lws_set_socket_options(struct libwebsocket_context *context, int fd);
|
|
|
|
|
2013-02-13 09:29:26 +08:00
|
|
|
LWS_EXTERN int
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
lws_allocate_header_table(struct libwebsocket *wsi);
|
|
|
|
|
2013-02-13 09:29:26 +08:00
|
|
|
LWS_EXTERN char *
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
lws_hdr_simple_ptr(struct libwebsocket *wsi, enum lws_token_indexes h);
|
|
|
|
|
2013-02-13 09:29:26 +08:00
|
|
|
LWS_EXTERN int
|
2013-02-11 17:13:32 +08:00
|
|
|
lws_hdr_simple_create(struct libwebsocket *wsi,
|
|
|
|
enum lws_token_indexes h, const char *s);
|
|
|
|
|
2013-02-18 16:30:10 +08:00
|
|
|
LWS_EXTERN int
|
|
|
|
libwebsocket_ensure_user_space(struct libwebsocket *wsi);
|
|
|
|
|
2013-02-11 17:13:32 +08:00
|
|
|
#ifndef LWS_NO_SERVER
|
2013-02-13 09:29:26 +08:00
|
|
|
LWS_EXTERN int handshake_0405(struct libwebsocket_context *context,
|
2013-02-11 17:13:32 +08:00
|
|
|
struct libwebsocket *wsi);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef LWS_NO_DAEMONIZE
|
2013-02-13 09:29:26 +08:00
|
|
|
LWS_EXTERN int get_daemonize_pid();
|
2013-02-11 17:13:32 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
extern int interface_to_sa(const char *ifname,
|
|
|
|
struct sockaddr_in *addr, size_t addrlen);
|
2013-02-11 13:04:45 +08:00
|
|
|
|
2011-02-14 17:52:39 +00:00
|
|
|
#ifndef LWS_OPENSSL_SUPPORT
|
|
|
|
|
|
|
|
unsigned char *
|
|
|
|
SHA1(const unsigned char *d, size_t n, unsigned char *md);
|
|
|
|
|
2013-02-11 17:13:32 +08:00
|
|
|
#else
|
|
|
|
|
2013-02-13 09:29:26 +08:00
|
|
|
LWS_EXTERN int openssl_websocket_private_data_index;
|
2013-02-11 17:13:32 +08:00
|
|
|
|
2011-02-14 17:52:39 +00:00
|
|
|
#endif
|