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
|
|
|
|
|
|
|
// 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>
|
2012-04-12 10:54:22 +08:00
|
|
|
#ifdef __MINGW64__
|
|
|
|
#else
|
|
|
|
#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-01-14 15:35:02 +08:00
|
|
|
#define compatible_close(fd) closesocket(fd);
|
2012-04-12 10:54:22 +08:00
|
|
|
#ifdef __MINGW64__
|
|
|
|
#else
|
|
|
|
#ifdef __MINGW32__
|
|
|
|
#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_NAME_LENGTH
|
2010-11-08 20:20:42 +00:00
|
|
|
#define LWS_MAX_HEADER_NAME_LENGTH 64
|
2013-01-12 23:42:17 +08:00
|
|
|
#endif
|
|
|
|
#ifndef LWS_MAX_HEADER_LEN
|
2010-11-08 20:20:42 +00:00
|
|
|
#define LWS_MAX_HEADER_LEN 4096
|
2013-01-12 23:42:17 +08:00
|
|
|
#endif
|
|
|
|
#ifndef LWS_INITIAL_HDR_ALLOC
|
2010-11-08 20:20:42 +00:00
|
|
|
#define LWS_INITIAL_HDR_ALLOC 256
|
2013-01-12 23:42:17 +08:00
|
|
|
#endif
|
|
|
|
#ifndef LWS_ADDITIONAL_HDR_ALLOC
|
2010-11-08 20:20:42 +00:00
|
|
|
#define LWS_ADDITIONAL_HDR_ALLOC 64
|
2013-01-12 23:42:17 +08:00
|
|
|
#endif
|
|
|
|
#ifndef MAX_USER_RX_BUFFER
|
2011-01-27 20:06:03 +00:00
|
|
|
#define MAX_USER_RX_BUFFER 4096
|
2013-01-12 23:42:17 +08:00
|
|
|
#endif
|
|
|
|
#ifndef LWS_MAX_PROTOCOLS
|
2010-12-18 15:13:50 +00:00
|
|
|
#define LWS_MAX_PROTOCOLS 10
|
2013-01-12 23:42:17 +08:00
|
|
|
#endif
|
|
|
|
#ifndef LWS_MAX_EXTENSIONS_ACTIVE
|
2011-03-05 16:12:15 +00:00
|
|
|
#define LWS_MAX_EXTENSIONS_ACTIVE 10
|
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-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,
|
|
|
|
|
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 */
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
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;
|
2011-01-27 22:01:43 +00:00
|
|
|
char http_proxy_address[256];
|
2011-01-28 10:00:18 +00:00
|
|
|
char canonical_hostname[1024];
|
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)
|
|
|
|
*/
|
|
|
|
unsigned char service_buffer[4096];
|
|
|
|
|
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-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
|
2012-10-19 11:21:56 +02:00
|
|
|
void *user_space;
|
2010-12-18 15:13:50 +00:00
|
|
|
};
|
|
|
|
|
2011-02-09 07:16:34 +00:00
|
|
|
|
2011-02-14 17:59:43 +00:00
|
|
|
enum pending_timeout {
|
|
|
|
NO_PENDING_TIMEOUT = 0,
|
2011-02-14 20:25:43 +00:00
|
|
|
PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE,
|
2011-02-14 17:59:43 +00:00
|
|
|
PENDING_TIMEOUT_ESTABLISH_WITH_SERVER,
|
2011-02-14 20:25:43 +00:00
|
|
|
PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE,
|
2011-02-14 17:59:43 +00:00
|
|
|
PENDING_TIMEOUT_AWAITING_PING,
|
2011-03-07 07:08:12 +00:00
|
|
|
PENDING_TIMEOUT_CLOSE_ACK,
|
2011-05-23 10:00:03 +01:00
|
|
|
PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE,
|
2013-01-13 11:05:30 +08:00
|
|
|
PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE,
|
2013-01-28 17:20:41 +08:00
|
|
|
PENDING_TIMEOUT_SSL_ACCEPT,
|
2011-02-14 17:59:43 +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
|
|
|
|
2013-01-21 11:04:23 +08:00
|
|
|
struct _lws_header_related {
|
2010-11-08 20:20:42 +00:00
|
|
|
char name_buffer[LWS_MAX_HEADER_NAME_LENGTH];
|
|
|
|
int name_buffer_pos;
|
2013-02-06 20:23:40 +09:00
|
|
|
struct lws_tokens hdrs[WSI_TOKEN_COUNT];
|
2013-01-18 01:55:48 +08:00
|
|
|
int lextable_pos;
|
2010-11-08 20:20:42 +00:00
|
|
|
enum lws_token_indexes parser_state;
|
2013-01-21 11:04:23 +08:00
|
|
|
int current_alloc_len;
|
|
|
|
#ifndef LWS_NO_CLIENT
|
|
|
|
char initial_handshake_hash_base64[30];
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _lws_websocket_related {
|
2010-11-11 09:22:22 +00:00
|
|
|
char rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING + MAX_USER_RX_BUFFER +
|
|
|
|
LWS_SEND_BUFFER_POST_PADDING];
|
|
|
|
int rx_user_buffer_head;
|
2011-01-19 12:20:27 +00:00
|
|
|
unsigned char masking_key_04[20];
|
|
|
|
unsigned char frame_masking_nonce_04[4];
|
|
|
|
unsigned char frame_mask_04[20];
|
|
|
|
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;
|
|
|
|
unsigned char final;
|
2013-01-09 18:06:55 +08:00
|
|
|
unsigned char rsv;
|
2013-01-18 09:49:20 +08:00
|
|
|
int frame_is_binary:1;
|
2011-01-19 12:20:27 +00:00
|
|
|
int pings_vs_pongs;
|
2011-02-10 09:22:35 +00:00
|
|
|
char all_zero_nonce;
|
2011-02-26 11:04:01 +00:00
|
|
|
enum lws_close_status close_reason;
|
2013-01-21 11:04:23 +08:00
|
|
|
unsigned char *rxflow_buffer;
|
|
|
|
int rxflow_len;
|
|
|
|
int rxflow_pos;
|
|
|
|
int rxflow_change_to;
|
2011-04-24 05:46:23 +01:00
|
|
|
char this_frame_masked;
|
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];
|
|
|
|
int count_active_extensions;
|
|
|
|
char extension_data_pending;
|
|
|
|
struct libwebsocket *extension_handles;
|
|
|
|
struct libwebsocket *candidate_children_list;
|
|
|
|
#endif
|
|
|
|
int ietf_spec_revision;
|
2011-04-24 05:46:23 +01:00
|
|
|
|
2013-01-18 11:43:21 +08:00
|
|
|
enum connection_mode mode;
|
2013-01-21 11:04:23 +08:00
|
|
|
enum lws_connection_states state;
|
|
|
|
enum lws_rx_parse_state lws_rx_parse_state;
|
|
|
|
|
|
|
|
enum pending_timeout pending_timeout;
|
|
|
|
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;
|
|
|
|
|
|
|
|
enum libwebsocket_write_protocol rx_frame_type;
|
2013-01-18 11:43:21 +08:00
|
|
|
|
|
|
|
#ifndef LWS_NO_CLIENT
|
2011-02-14 20:25:43 +00:00
|
|
|
char *c_path;
|
|
|
|
char *c_host;
|
|
|
|
char *c_origin;
|
|
|
|
char *c_protocol;
|
2012-04-20 12:13:50 +08:00
|
|
|
callback_function *c_callback;
|
2011-01-22 12:51:57 +00:00
|
|
|
|
2011-05-23 10:00:03 +01:00
|
|
|
char *c_address;
|
|
|
|
int c_port;
|
2013-01-18 11:43:21 +08:00
|
|
|
#endif
|
2011-05-23 10:00:03 +01: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;
|
2011-02-14 20:25:43 +00:00
|
|
|
int use_ssl;
|
2013-01-21 11:04:23 +08:00
|
|
|
#endif
|
2010-11-08 20:20:42 +00:00
|
|
|
};
|
|
|
|
|
2013-01-29 12:36:17 +08:00
|
|
|
#ifndef LWS_LATENCY
|
2013-02-06 15:28:07 +09: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
|
|
|
|
lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi, const char *action, int ret, int completion);
|
|
|
|
#endif
|
|
|
|
|
2011-01-22 12:51:57 +00:00
|
|
|
extern int
|
|
|
|
libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c);
|
|
|
|
|
2010-12-18 15:13:50 +00:00
|
|
|
extern int
|
|
|
|
libwebsocket_parse(struct libwebsocket *wsi, unsigned char c);
|
|
|
|
|
|
|
|
extern int
|
|
|
|
libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi,
|
|
|
|
unsigned char *buf, size_t len);
|
2010-12-19 22:13:26 +00:00
|
|
|
|
2011-01-18 15:39:02 +00:00
|
|
|
extern int
|
|
|
|
lws_b64_selftest(void);
|
2011-02-09 08:49:14 +00:00
|
|
|
|
2011-02-12 11:57:43 +00:00
|
|
|
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
|
|
|
|
|
|
|
extern int
|
2013-01-17 12:26:48 +08:00
|
|
|
insert_wsi_socket_into_fds(struct libwebsocket_context *context, struct libwebsocket *wsi);
|
2011-02-14 17:52:39 +00:00
|
|
|
|
2011-02-14 20:25:43 +00:00
|
|
|
extern void
|
|
|
|
libwebsocket_set_timeout(struct libwebsocket *wsi,
|
|
|
|
enum pending_timeout reason, int secs);
|
|
|
|
|
2011-03-06 10:29:38 +00:00
|
|
|
extern int
|
|
|
|
lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len);
|
|
|
|
|
2011-03-06 10:29:39 +00:00
|
|
|
|
2011-05-23 10:00:03 +01:00
|
|
|
extern void
|
|
|
|
libwebsocket_service_timeout_check(struct libwebsocket_context *context,
|
|
|
|
struct libwebsocket *wsi, unsigned int sec);
|
|
|
|
|
2012-04-09 15:09:01 +08:00
|
|
|
extern struct libwebsocket *
|
|
|
|
__libwebsocket_client_connect_2(struct libwebsocket_context *context,
|
2011-05-23 10:00:03 +01:00
|
|
|
struct libwebsocket *wsi);
|
|
|
|
|
|
|
|
extern struct libwebsocket *
|
|
|
|
libwebsocket_create_new_server_wsi(struct libwebsocket_context *context);
|
|
|
|
|
|
|
|
extern char *
|
|
|
|
libwebsockets_generate_client_handshake(struct libwebsocket_context *context,
|
|
|
|
struct libwebsocket *wsi, char *pkt);
|
|
|
|
|
|
|
|
extern int
|
|
|
|
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
|
2011-05-23 10:00:03 +01:00
|
|
|
extern int
|
|
|
|
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
|
|
|
|
|
|
|
extern void *
|
|
|
|
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
|
|
|
|
|
|
|
extern int
|
|
|
|
lws_client_interpret_server_handshake(struct libwebsocket_context *context,
|
|
|
|
struct libwebsocket *wsi);
|
|
|
|
|
|
|
|
extern int
|
|
|
|
libwebsocket_rx_sm(struct libwebsocket *wsi, unsigned char c);
|
|
|
|
|
2011-05-28 10:19:19 +01:00
|
|
|
extern int
|
|
|
|
lws_issue_raw_ext_access(struct libwebsocket *wsi,
|
|
|
|
unsigned char *buf, size_t len);
|
|
|
|
|
2013-01-17 16:50:35 +08:00
|
|
|
extern int
|
|
|
|
_libwebsocket_rx_flow_control(struct libwebsocket *wsi);
|
|
|
|
|
|
|
|
extern int
|
|
|
|
user_callback_handle_rxflow(callback_function, struct libwebsocket_context * context,
|
|
|
|
struct libwebsocket *wsi,
|
|
|
|
enum libwebsocket_callback_reasons reason, void *user,
|
|
|
|
void *in, size_t len);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
#endif
|