2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010 Andy Green <andy@warmcat.com>
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.
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.
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,
28 #include <sys/types.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/prctl.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
42 #ifdef LWS_OPENSSL_SUPPORT
43 #include <openssl/ssl.h>
44 #include <openssl/evp.h>
45 #include <openssl/err.h>
48 #include "libwebsockets.h"
54 #define debug(format, args...) \
55 fprintf(stderr, format , ## args)
57 #define debug(format, args...)
60 #ifdef LWS_OPENSSL_SUPPORT
61 extern SSL_CTX *ssl_ctx;
66 #define MAX_CLIENTS 100
67 #define LWS_MAX_HEADER_NAME_LENGTH 64
68 #define LWS_MAX_HEADER_LEN 4096
69 #define LWS_INITIAL_HDR_ALLOC 256
70 #define LWS_ADDITIONAL_HDR_ALLOC 64
71 #define MAX_USER_RX_BUFFER 512
72 #define MAX_BROADCAST_PAYLOAD 1024
73 #define LWS_MAX_PROTOCOLS 10
75 enum lws_connection_states {
77 WSI_STATE_HTTP_HEADERS,
78 WSI_STATE_DEAD_SOCKET,
82 enum lws_token_indexes {
94 /* always last real token index*/
96 /* parser state additions */
99 WSI_TOKEN_SKIPPING_SAW_CR,
103 enum lws_rx_parse_state {
107 LWS_RXPS_PULLING_76_LENGTH,
108 LWS_RXPS_EAT_UNTIL_76_FF,
110 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
119 struct libwebsocket_context {
120 struct libwebsocket *wsi[MAX_CLIENTS + 1];
121 struct pollfd fds[MAX_CLIENTS + 1];
123 #ifdef LWS_OPENSSL_SUPPORT
131 * This is totally opaque to code using the library. It's exported as a
132 * forward-reference pointer-only declaration; the user can use the pointer with
133 * other APIs to get information out of it.
136 struct libwebsocket {
137 const struct libwebsocket_protocols *protocol;
139 enum lws_connection_states state;
141 char name_buffer[LWS_MAX_HEADER_NAME_LENGTH];
143 int current_alloc_len;
144 enum lws_token_indexes parser_state;
145 struct lws_tokens utf8_token[WSI_TOKEN_COUNT];
146 int ietf_spec_revision;
147 char rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING + MAX_USER_RX_BUFFER +
148 LWS_SEND_BUFFER_POST_PADDING];
149 int rx_user_buffer_head;
153 enum lws_rx_parse_state lws_rx_parse_state;
154 size_t rx_packet_length;
156 #ifdef LWS_OPENSSL_SUPPORT
164 libwebsocket_close_and_free_session(struct libwebsocket *wsi);
167 libwebsockets_md5(const unsigned char *input, int ilen, unsigned char *output);
170 libwebsocket_parse(struct libwebsocket *wsi, unsigned char c);
173 libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi,
174 unsigned char *buf, size_t len);