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
|
|
|
*
|
2010-11-08 20:20:42 +00:00
|
|
|
* Copyright (C) 2010 Andy Green <andy@warmcat.com>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2010-12-20 09:35:03 +00:00
|
|
|
#include <unistd.h>
|
2010-11-08 20:20:42 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2010-12-20 09:35:03 +00:00
|
|
|
#include <strings.h>
|
2010-11-08 20:20:42 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
2010-11-13 10:03:47 +00:00
|
|
|
#include <sys/types.h>
|
2010-12-20 09:35:03 +00:00
|
|
|
#include <sys/stat.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
|
2010-12-18 15:13:50 +00:00
|
|
|
#include <sys/prctl.h>
|
2011-01-20 10:23:50 +00:00
|
|
|
#endif
|
2010-11-08 20:20:42 +00:00
|
|
|
#include <netinet/in.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>
|
|
|
|
|
|
|
|
#ifdef LWS_OPENSSL_SUPPORT
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#endif
|
|
|
|
|
2010-12-20 09:35:03 +00:00
|
|
|
#include <openssl/md5.h>
|
2011-01-18 17:14:03 +00:00
|
|
|
#include <openssl/sha.h>
|
2010-11-08 20:20:42 +00:00
|
|
|
#include "libwebsockets.h"
|
|
|
|
|
2011-01-23 16:50:33 +00:00
|
|
|
#if 0
|
|
|
|
#define DEBUG
|
|
|
|
#endif
|
2010-11-08 20:20:42 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2011-01-23 16:50:33 +00:00
|
|
|
static inline void debug(const char *format, ...)
|
|
|
|
{
|
2011-01-22 12:51:57 +00:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap);
|
|
|
|
}
|
2010-11-08 20:20:42 +00:00
|
|
|
#else
|
2011-01-23 16:50:33 +00:00
|
|
|
static inline void debug(const char *format, ...)
|
|
|
|
{
|
|
|
|
}
|
2010-11-08 20:20:42 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#define MAX_CLIENTS 100
|
|
|
|
#define LWS_MAX_HEADER_NAME_LENGTH 64
|
|
|
|
#define LWS_MAX_HEADER_LEN 4096
|
|
|
|
#define LWS_INITIAL_HDR_ALLOC 256
|
|
|
|
#define LWS_ADDITIONAL_HDR_ALLOC 64
|
2011-01-27 20:06:03 +00:00
|
|
|
#define MAX_USER_RX_BUFFER 4096
|
2011-01-27 06:26:52 +00:00
|
|
|
#define MAX_BROADCAST_PAYLOAD 2048
|
2010-12-18 15:13:50 +00:00
|
|
|
#define LWS_MAX_PROTOCOLS 10
|
2010-11-08 20:20:42 +00:00
|
|
|
|
2011-01-18 17:14:03 +00:00
|
|
|
#define MAX_WEBSOCKET_04_KEY_LEN 128
|
2011-01-22 12:51:57 +00:00
|
|
|
#define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
|
2011-01-18 17:14:03 +00:00
|
|
|
|
2011-01-19 12:20:27 +00:00
|
|
|
enum lws_websocket_opcodes_04 {
|
|
|
|
LWS_WS_OPCODE_04__CONTINUATION = 0,
|
|
|
|
LWS_WS_OPCODE_04__CLOSE = 1,
|
|
|
|
LWS_WS_OPCODE_04__PING = 2,
|
|
|
|
LWS_WS_OPCODE_04__PONG = 3,
|
|
|
|
LWS_WS_OPCODE_04__TEXT_FRAME = 4,
|
|
|
|
LWS_WS_OPCODE_04__BINARY_FRAME = 5,
|
|
|
|
};
|
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
enum lws_connection_states {
|
|
|
|
WSI_STATE_HTTP,
|
|
|
|
WSI_STATE_HTTP_HEADERS,
|
|
|
|
WSI_STATE_DEAD_SOCKET,
|
2011-01-22 12:51:57 +00:00
|
|
|
WSI_STATE_ESTABLISHED,
|
|
|
|
WSI_STATE_CLIENT_UNCONNECTED
|
2010-11-08 20:20:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum lws_token_indexes {
|
|
|
|
WSI_TOKEN_GET_URI,
|
|
|
|
WSI_TOKEN_HOST,
|
|
|
|
WSI_TOKEN_CONNECTION,
|
|
|
|
WSI_TOKEN_KEY1,
|
|
|
|
WSI_TOKEN_KEY2,
|
|
|
|
WSI_TOKEN_PROTOCOL,
|
|
|
|
WSI_TOKEN_UPGRADE,
|
|
|
|
WSI_TOKEN_ORIGIN,
|
2010-11-11 12:48:13 +00:00
|
|
|
WSI_TOKEN_DRAFT,
|
2010-11-08 20:20:42 +00:00
|
|
|
WSI_TOKEN_CHALLENGE,
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2011-01-18 15:39:02 +00:00
|
|
|
/* new for 04 */
|
|
|
|
WSI_TOKEN_KEY,
|
|
|
|
WSI_TOKEN_VERSION,
|
2011-01-22 12:51:57 +00:00
|
|
|
WSI_TOKEN_SWORIGIN,
|
|
|
|
|
|
|
|
/* client receives these */
|
|
|
|
WSI_TOKEN_ACCEPT,
|
|
|
|
WSI_TOKEN_NONCE,
|
|
|
|
WSI_TOKEN_HTTP,
|
2011-01-18 15:39:02 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
/* always last real token index*/
|
|
|
|
WSI_TOKEN_COUNT,
|
|
|
|
/* parser state additions */
|
|
|
|
WSI_TOKEN_NAME_PART,
|
|
|
|
WSI_TOKEN_SKIPPING,
|
|
|
|
WSI_TOKEN_SKIPPING_SAW_CR,
|
|
|
|
WSI_PARSING_COMPLETE
|
|
|
|
};
|
|
|
|
|
|
|
|
enum lws_rx_parse_state {
|
|
|
|
LWS_RXPS_NEW,
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
LWS_RXPS_SEEN_76_FF,
|
|
|
|
LWS_RXPS_PULLING_76_LENGTH,
|
2010-11-11 09:22:22 +00:00
|
|
|
LWS_RXPS_EAT_UNTIL_76_FF,
|
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
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct lws_tokens {
|
2010-11-13 10:03:47 +00:00
|
|
|
char *token;
|
2010-11-08 20:20:42 +00:00
|
|
|
int token_len;
|
|
|
|
};
|
|
|
|
|
2011-01-19 13:11:55 +00:00
|
|
|
struct libwebsocket_protocols;
|
|
|
|
|
2010-12-18 15:13:50 +00:00
|
|
|
struct libwebsocket_context {
|
|
|
|
struct libwebsocket *wsi[MAX_CLIENTS + 1];
|
|
|
|
struct pollfd fds[MAX_CLIENTS + 1];
|
|
|
|
int fds_count;
|
2011-01-22 12:51:57 +00:00
|
|
|
int listen_port;
|
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;
|
|
|
|
};
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct libwebsocket {
|
2010-11-12 10:44:16 +00:00
|
|
|
const struct libwebsocket_protocols *protocol;
|
2010-11-08 20:20:42 +00:00
|
|
|
|
|
|
|
enum lws_connection_states state;
|
|
|
|
|
|
|
|
char name_buffer[LWS_MAX_HEADER_NAME_LENGTH];
|
|
|
|
int name_buffer_pos;
|
|
|
|
int current_alloc_len;
|
|
|
|
enum lws_token_indexes parser_state;
|
|
|
|
struct lws_tokens utf8_token[WSI_TOKEN_COUNT];
|
|
|
|
int ietf_spec_revision;
|
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;
|
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
int sock;
|
|
|
|
|
|
|
|
enum lws_rx_parse_state lws_rx_parse_state;
|
2011-01-19 12:20:27 +00:00
|
|
|
|
|
|
|
/* 04 protocol specific */
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
int pings_vs_pongs;
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2011-01-22 12:51:57 +00:00
|
|
|
/* client support */
|
|
|
|
char initial_handshake_hash_base64[30];
|
|
|
|
int client_mode;
|
|
|
|
|
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;
|
2010-11-08 20:20:42 +00:00
|
|
|
#endif
|
|
|
|
|
2010-11-12 10:44:16 +00:00
|
|
|
void *user_space;
|
2010-11-08 20:20:42 +00:00
|
|
|
};
|
|
|
|
|
2011-01-22 12:51:57 +00:00
|
|
|
extern int
|
|
|
|
libwebsocket_client_rx_sm(struct libwebsocket *wsi, unsigned char c);
|
|
|
|
|
2010-11-13 10:03:47 +00:00
|
|
|
extern void
|
2010-11-08 20:20:42 +00:00
|
|
|
libwebsocket_close_and_free_session(struct libwebsocket *wsi);
|
2010-11-13 10:03:47 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
extern int
|
|
|
|
libwebsocket_read(struct libwebsocket *wsi, unsigned char * buf, size_t len);
|
2011-01-18 15:39:02 +00:00
|
|
|
|
|
|
|
extern int
|
2011-01-22 12:51:57 +00:00
|
|
|
lws_b64_encode_string(const char *in, int in_len, char *out, int out_size);
|
2011-01-18 15:39:02 +00:00
|
|
|
|
|
|
|
extern int
|
|
|
|
lws_b64_decode_string(const char *in, char *out, int out_size);
|
|
|
|
|
|
|
|
extern int
|
|
|
|
lws_b64_selftest(void);
|