Andy Green | a0da8a8 | 2010-11-08 17:12:19 +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 | 7b40545 | 2013-02-01 10:50:15 +0800 | [diff] [blame] | 4 | * Copyright (C) 2010-2013 Andy Green <andy@warmcat.com> |
Andy Green | a0da8a8 | 2010-11-08 17:12:19 +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 | */ |
| 21 | |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 22 | #ifndef __LIBWEBSOCKET_H__ |
| 23 | #define __LIBWEBSOCKET_H__ |
| 24 | |
Andy Green | a11fe94 | 2011-09-25 10:30:26 +0100 | [diff] [blame] | 25 | #ifdef __cplusplus |
| 26 | extern "C" { |
Paulo Roberto Urio | 4144e83 | 2012-06-04 09:04:33 +0800 | [diff] [blame] | 27 | #include <cstddef> |
Andy Green | a11fe94 | 2011-09-25 10:30:26 +0100 | [diff] [blame] | 28 | #endif |
| 29 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 30 | #ifdef WIN32 |
| 31 | |
| 32 | #ifndef WIN32_LEAN_AND_MEAN |
| 33 | #define WIN32_LEAN_AND_MEAN |
| 34 | #endif |
| 35 | #include <winsock2.h> |
| 36 | #include <ws2tcpip.h> |
Paulo Roberto Urio | 4144e83 | 2012-06-04 09:04:33 +0800 | [diff] [blame] | 37 | #include <stddef.h> |
Andy Green | 2a480ea | 2012-04-12 10:54:22 +0800 | [diff] [blame] | 38 | #include "../win32port/win32helpers/websock-w32.h" |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 39 | |
Andy Green | a314788 | 2012-04-12 11:02:06 +0800 | [diff] [blame] | 40 | #include "../win32port/win32helpers/gettimeofday.h" |
Michel Archambault | 03bec39 | 2011-05-24 17:15:25 +0100 | [diff] [blame] | 41 | |
Oleg Golosovskiy | 2ef7505 | 2011-10-04 20:20:14 +0800 | [diff] [blame] | 42 | #define strcasecmp stricmp |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 43 | #define getdtablesize() 30000 |
Oleg Golosovskiy | 2ef7505 | 2011-10-04 20:20:14 +0800 | [diff] [blame] | 44 | |
David Brooks | 06fbcee | 2012-05-02 06:02:45 +0800 | [diff] [blame] | 45 | typedef int ssize_t; |
| 46 | |
Andy Green | 3661d7b | 2013-04-26 07:37:16 +0800 | [diff] [blame] | 47 | #define LWS_VISIBLE |
| 48 | |
David Galeano | 9454e21 | 2011-09-26 12:17:20 +0100 | [diff] [blame] | 49 | #ifdef LWS_DLL |
| 50 | #ifdef LWS_INTERNAL |
| 51 | #define LWS_EXTERN extern __declspec(dllexport) |
| 52 | #else |
| 53 | #define LWS_EXTERN extern __declspec(dllimport) |
| 54 | #endif |
Andy Green | 3661d7b | 2013-04-26 07:37:16 +0800 | [diff] [blame] | 55 | #else |
| 56 | #define LWS_EXTERN |
David Galeano | 9454e21 | 2011-09-26 12:17:20 +0100 | [diff] [blame] | 57 | #endif |
| 58 | |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 59 | #else // NOT WIN32 |
Andy Green | 9f99034 | 2011-02-12 11:57:45 +0000 | [diff] [blame] | 60 | #include <poll.h> |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 61 | #include <unistd.h> |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 62 | |
| 63 | #if defined(__GNUC__) |
| 64 | #define LWS_VISIBLE __attribute__((visibility("default"))) |
| 65 | #else |
| 66 | #define LWS_VISIBLE |
| 67 | #endif |
| 68 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 69 | #endif |
Andy Green | 9f99034 | 2011-02-12 11:57:45 +0000 | [diff] [blame] | 70 | |
Peter Young | 27b3237 | 2013-01-17 10:10:10 +0800 | [diff] [blame] | 71 | #include <assert.h> |
| 72 | |
David Galeano | 9454e21 | 2011-09-26 12:17:20 +0100 | [diff] [blame] | 73 | #ifndef LWS_EXTERN |
| 74 | #define LWS_EXTERN extern |
| 75 | #endif |
| 76 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 77 | #define CONTEXT_PORT_NO_LISTEN 0 |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 78 | #define MAX_MUX_RECURSION 2 |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 79 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 80 | enum lws_log_levels { |
| 81 | LLL_ERR = 1 << 0, |
| 82 | LLL_WARN = 1 << 1, |
Andy Green | 7c19c34 | 2013-01-19 12:18:07 +0800 | [diff] [blame] | 83 | LLL_NOTICE = 1 << 2, |
| 84 | LLL_INFO = 1 << 3, |
| 85 | LLL_DEBUG = 1 << 4, |
| 86 | LLL_PARSER = 1 << 5, |
| 87 | LLL_HEADER = 1 << 6, |
| 88 | LLL_EXT = 1 << 7, |
| 89 | LLL_CLIENT = 1 << 8, |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 90 | LLL_LATENCY = 1 << 9, |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 91 | |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 92 | LLL_COUNT = 10 /* set to count of valid flags */ |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 93 | }; |
| 94 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 95 | LWS_VISIBLE LWS_EXTERN void _lws_log(int filter, const char *format, ...); |
Andy Green | 69e4364 | 2013-01-19 11:58:07 +0800 | [diff] [blame] | 96 | |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 97 | /* notice, warn and log are always compiled in */ |
Andy Green | 69e4364 | 2013-01-19 11:58:07 +0800 | [diff] [blame] | 98 | #define lwsl_notice(...) _lws_log(LLL_NOTICE, __VA_ARGS__) |
| 99 | #define lwsl_warn(...) _lws_log(LLL_WARN, __VA_ARGS__) |
| 100 | #define lwsl_err(...) _lws_log(LLL_ERR, __VA_ARGS__) |
Andy Green | 69e4364 | 2013-01-19 11:58:07 +0800 | [diff] [blame] | 101 | /* |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 102 | * weaker logging can be deselected at configure time using --disable-debug |
Andy Green | 69e4364 | 2013-01-19 11:58:07 +0800 | [diff] [blame] | 103 | * that gets rid of the overhead of checking while keeping _warn and _err |
| 104 | * active |
| 105 | */ |
| 106 | #ifdef _DEBUG |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 107 | |
Andy Green | 69e4364 | 2013-01-19 11:58:07 +0800 | [diff] [blame] | 108 | #define lwsl_info(...) _lws_log(LLL_INFO, __VA_ARGS__) |
| 109 | #define lwsl_debug(...) _lws_log(LLL_DEBUG, __VA_ARGS__) |
| 110 | #define lwsl_parser(...) _lws_log(LLL_PARSER, __VA_ARGS__) |
| 111 | #define lwsl_header(...) _lws_log(LLL_HEADER, __VA_ARGS__) |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 112 | #define lwsl_ext(...) _lws_log(LLL_EXT, __VA_ARGS__) |
Andy Green | 69e4364 | 2013-01-19 11:58:07 +0800 | [diff] [blame] | 113 | #define lwsl_client(...) _lws_log(LLL_CLIENT, __VA_ARGS__) |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 114 | #define lwsl_latency(...) _lws_log(LLL_LATENCY, __VA_ARGS__) |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 115 | LWS_VISIBLE LWS_EXTERN void lwsl_hexdump(void *buf, size_t len); |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 116 | |
Andy Green | 69e4364 | 2013-01-19 11:58:07 +0800 | [diff] [blame] | 117 | #else /* no debug */ |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 118 | |
Andy Green | 69e4364 | 2013-01-19 11:58:07 +0800 | [diff] [blame] | 119 | #define lwsl_info(...) |
| 120 | #define lwsl_debug(...) |
| 121 | #define lwsl_parser(...) |
| 122 | #define lwsl_header(...) |
| 123 | #define lwsl_ext(...) |
| 124 | #define lwsl_client(...) |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 125 | #define lwsl_latency(...) |
Andy Green | 69e4364 | 2013-01-19 11:58:07 +0800 | [diff] [blame] | 126 | #define lwsl_hexdump(a, b) |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 127 | |
Andy Green | 69e4364 | 2013-01-19 11:58:07 +0800 | [diff] [blame] | 128 | #endif |
| 129 | |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 130 | enum libwebsocket_context_options { |
Andy Green | c6bf2c2 | 2011-02-20 11:10:47 +0000 | [diff] [blame] | 131 | LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT = 2, |
Andy Green | 788c4a8 | 2012-10-22 12:29:57 +0100 | [diff] [blame] | 132 | LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME = 4, |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 133 | }; |
Andy Green | 8014b29 | 2011-01-30 20:57:25 +0000 | [diff] [blame] | 134 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 135 | enum libwebsocket_callback_reasons { |
| 136 | LWS_CALLBACK_ESTABLISHED, |
David Brooks | 80a4497 | 2012-04-20 12:18:47 +0800 | [diff] [blame] | 137 | LWS_CALLBACK_CLIENT_CONNECTION_ERROR, |
Andy Green | 2b57a34 | 2013-02-06 15:15:25 +0900 | [diff] [blame] | 138 | LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH, |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 139 | LWS_CALLBACK_CLIENT_ESTABLISHED, |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 140 | LWS_CALLBACK_CLOSED, |
Andy Green | 0c9563b | 2013-06-10 22:54:40 +0800 | [diff] [blame] | 141 | LWS_CALLBACK_CLOSED_HTTP, |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 142 | LWS_CALLBACK_RECEIVE, |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 143 | LWS_CALLBACK_CLIENT_RECEIVE, |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 144 | LWS_CALLBACK_CLIENT_RECEIVE_PONG, |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 145 | LWS_CALLBACK_CLIENT_WRITEABLE, |
Andy Green | 9e4c2b6 | 2011-03-07 20:47:39 +0000 | [diff] [blame] | 146 | LWS_CALLBACK_SERVER_WRITEABLE, |
Andy Green | a2b0ab0 | 2010-11-11 12:28:29 +0000 | [diff] [blame] | 147 | LWS_CALLBACK_HTTP, |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 148 | LWS_CALLBACK_HTTP_FILE_COMPLETION, |
Andy Green | 54cb346 | 2013-02-14 22:23:54 +0800 | [diff] [blame] | 149 | LWS_CALLBACK_HTTP_WRITEABLE, |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 150 | LWS_CALLBACK_FILTER_NETWORK_CONNECTION, |
Andy Green | c85619d | 2011-02-13 08:25:26 +0000 | [diff] [blame] | 151 | LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION, |
Andy Green | 0894bda | 2011-02-19 09:09:11 +0000 | [diff] [blame] | 152 | LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS, |
Andy Green | c6bf2c2 | 2011-02-20 11:10:47 +0000 | [diff] [blame] | 153 | LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS, |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 154 | LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION, |
Andy Green | 385e7ad | 2011-03-01 21:06:02 +0000 | [diff] [blame] | 155 | LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER, |
Andy Green | c511482 | 2011-03-06 10:29:35 +0000 | [diff] [blame] | 156 | LWS_CALLBACK_CONFIRM_EXTENSION_OKAY, |
Andy Green | c6517fa | 2011-03-06 13:15:29 +0000 | [diff] [blame] | 157 | LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED, |
Andy Green | a7109e6 | 2013-02-11 12:05:54 +0800 | [diff] [blame] | 158 | LWS_CALLBACK_PROTOCOL_INIT, |
| 159 | LWS_CALLBACK_PROTOCOL_DESTROY, |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 160 | /* external poll() management support */ |
| 161 | LWS_CALLBACK_ADD_POLL_FD, |
| 162 | LWS_CALLBACK_DEL_POLL_FD, |
| 163 | LWS_CALLBACK_SET_MODE_POLL_FD, |
| 164 | LWS_CALLBACK_CLEAR_MODE_POLL_FD, |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 165 | }; |
| 166 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 167 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | c511482 | 2011-03-06 10:29:35 +0000 | [diff] [blame] | 168 | enum libwebsocket_extension_callback_reasons { |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 169 | LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT, |
| 170 | LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT, |
| 171 | LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT, |
| 172 | LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT, |
Andy Green | c511482 | 2011-03-06 10:29:35 +0000 | [diff] [blame] | 173 | LWS_EXT_CALLBACK_CONSTRUCT, |
Andy Green | 2366b1c | 2011-03-06 13:15:31 +0000 | [diff] [blame] | 174 | LWS_EXT_CALLBACK_CLIENT_CONSTRUCT, |
Andy Green | 68b4504 | 2011-05-25 21:41:57 +0100 | [diff] [blame] | 175 | LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE, |
Andy Green | 0922650 | 2011-05-28 10:19:19 +0100 | [diff] [blame] | 176 | LWS_EXT_CALLBACK_CHECK_OK_TO_PROPOSE_EXTENSION, |
Andy Green | c511482 | 2011-03-06 10:29:35 +0000 | [diff] [blame] | 177 | LWS_EXT_CALLBACK_DESTROY, |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 178 | LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING, |
| 179 | LWS_EXT_CALLBACK_ANY_WSI_ESTABLISHED, |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 180 | LWS_EXT_CALLBACK_PACKET_RX_PREPARSE, |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 181 | LWS_EXT_CALLBACK_PACKET_TX_PRESEND, |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 182 | LWS_EXT_CALLBACK_PACKET_TX_DO_SEND, |
| 183 | LWS_EXT_CALLBACK_HANDSHAKE_REPLY_TX, |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 184 | LWS_EXT_CALLBACK_FLUSH_PENDING_TX, |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 185 | LWS_EXT_CALLBACK_EXTENDED_PAYLOAD_RX, |
| 186 | LWS_EXT_CALLBACK_CAN_PROXY_CLIENT_CONNECTION, |
| 187 | LWS_EXT_CALLBACK_1HZ, |
| 188 | LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE, |
| 189 | LWS_EXT_CALLBACK_IS_WRITEABLE, |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 190 | LWS_EXT_CALLBACK_PAYLOAD_TX, |
| 191 | LWS_EXT_CALLBACK_PAYLOAD_RX, |
Andy Green | c511482 | 2011-03-06 10:29:35 +0000 | [diff] [blame] | 192 | }; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 193 | #endif |
Andy Green | c511482 | 2011-03-06 10:29:35 +0000 | [diff] [blame] | 194 | |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 195 | enum libwebsocket_write_protocol { |
| 196 | LWS_WRITE_TEXT, |
| 197 | LWS_WRITE_BINARY, |
Andy Green | 5d9d94b | 2011-03-07 20:47:41 +0000 | [diff] [blame] | 198 | LWS_WRITE_CONTINUATION, |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 199 | LWS_WRITE_HTTP, |
| 200 | |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 201 | /* special 04+ opcodes */ |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 202 | |
| 203 | LWS_WRITE_CLOSE, |
| 204 | LWS_WRITE_PING, |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 205 | LWS_WRITE_PONG, |
| 206 | |
Andy Green | bd96d80 | 2011-01-30 08:24:31 +0000 | [diff] [blame] | 207 | /* flags */ |
| 208 | |
| 209 | LWS_WRITE_NO_FIN = 0x40, |
| 210 | /* |
| 211 | * client packet payload goes out on wire unmunged |
| 212 | * only useful for security tests since normal servers cannot |
| 213 | * decode the content if used |
| 214 | */ |
| 215 | LWS_WRITE_CLIENT_IGNORE_XOR_MASK = 0x80 |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 216 | }; |
| 217 | |
Andy Green | c85619d | 2011-02-13 08:25:26 +0000 | [diff] [blame] | 218 | /* |
| 219 | * you need these to look at headers that have been parsed if using the |
| 220 | * LWS_CALLBACK_FILTER_CONNECTION callback. If a header from the enum |
| 221 | * list below is absent, .token = NULL and token_len = 0. Otherwise .token |
| 222 | * points to .token_len chars containing that header content. |
| 223 | */ |
| 224 | |
| 225 | struct lws_tokens { |
| 226 | char *token; |
| 227 | int token_len; |
| 228 | }; |
| 229 | |
| 230 | enum lws_token_indexes { |
| 231 | WSI_TOKEN_GET_URI, |
| 232 | WSI_TOKEN_HOST, |
| 233 | WSI_TOKEN_CONNECTION, |
| 234 | WSI_TOKEN_KEY1, |
| 235 | WSI_TOKEN_KEY2, |
| 236 | WSI_TOKEN_PROTOCOL, |
| 237 | WSI_TOKEN_UPGRADE, |
| 238 | WSI_TOKEN_ORIGIN, |
| 239 | WSI_TOKEN_DRAFT, |
| 240 | WSI_TOKEN_CHALLENGE, |
| 241 | |
| 242 | /* new for 04 */ |
| 243 | WSI_TOKEN_KEY, |
| 244 | WSI_TOKEN_VERSION, |
| 245 | WSI_TOKEN_SWORIGIN, |
| 246 | |
| 247 | /* new for 05 */ |
| 248 | WSI_TOKEN_EXTENSIONS, |
| 249 | |
| 250 | /* client receives these */ |
| 251 | WSI_TOKEN_ACCEPT, |
| 252 | WSI_TOKEN_NONCE, |
| 253 | WSI_TOKEN_HTTP, |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 254 | WSI_TOKEN_MUXURL, |
Andy Green | c85619d | 2011-02-13 08:25:26 +0000 | [diff] [blame] | 255 | |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 256 | /* use token storage to stash these */ |
| 257 | |
| 258 | _WSI_TOKEN_CLIENT_SENT_PROTOCOLS, |
| 259 | _WSI_TOKEN_CLIENT_PEER_ADDRESS, |
| 260 | _WSI_TOKEN_CLIENT_URI, |
| 261 | _WSI_TOKEN_CLIENT_HOST, |
| 262 | _WSI_TOKEN_CLIENT_ORIGIN, |
| 263 | |
Andy Green | c85619d | 2011-02-13 08:25:26 +0000 | [diff] [blame] | 264 | /* always last real token index*/ |
| 265 | WSI_TOKEN_COUNT, |
| 266 | /* parser state additions */ |
| 267 | WSI_TOKEN_NAME_PART, |
| 268 | WSI_TOKEN_SKIPPING, |
| 269 | WSI_TOKEN_SKIPPING_SAW_CR, |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 270 | WSI_PARSING_COMPLETE, |
| 271 | WSI_INIT_TOKEN_MUXURL, |
Andy Green | c85619d | 2011-02-13 08:25:26 +0000 | [diff] [blame] | 272 | }; |
| 273 | |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 274 | /* |
David Galeano | 2915a80 | 2013-01-09 15:26:08 +0800 | [diff] [blame] | 275 | * From RFC 6455 |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 276 | 1000 |
| 277 | |
David Galeano | 2915a80 | 2013-01-09 15:26:08 +0800 | [diff] [blame] | 278 | 1000 indicates a normal closure, meaning that the purpose for |
| 279 | which the connection was established has been fulfilled. |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 280 | |
| 281 | 1001 |
| 282 | |
| 283 | 1001 indicates that an endpoint is "going away", such as a server |
David Galeano | 2915a80 | 2013-01-09 15:26:08 +0800 | [diff] [blame] | 284 | going down or a browser having navigated away from a page. |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 285 | |
| 286 | 1002 |
| 287 | |
| 288 | 1002 indicates that an endpoint is terminating the connection due |
| 289 | to a protocol error. |
| 290 | |
| 291 | 1003 |
| 292 | |
| 293 | 1003 indicates that an endpoint is terminating the connection |
David Galeano | 2915a80 | 2013-01-09 15:26:08 +0800 | [diff] [blame] | 294 | because it has received a type of data it cannot accept (e.g., an |
| 295 | endpoint that understands only text data MAY send this if it |
| 296 | receives a binary message). |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 297 | |
| 298 | 1004 |
| 299 | |
David Galeano | 2915a80 | 2013-01-09 15:26:08 +0800 | [diff] [blame] | 300 | Reserved. The specific meaning might be defined in the future. |
| 301 | |
| 302 | 1005 |
| 303 | |
| 304 | 1005 is a reserved value and MUST NOT be set as a status code in a |
| 305 | Close control frame by an endpoint. It is designated for use in |
| 306 | applications expecting a status code to indicate that no status |
| 307 | code was actually present. |
| 308 | |
| 309 | 1006 |
| 310 | |
| 311 | 1006 is a reserved value and MUST NOT be set as a status code in a |
| 312 | Close control frame by an endpoint. It is designated for use in |
| 313 | applications expecting a status code to indicate that the |
| 314 | connection was closed abnormally, e.g., without sending or |
| 315 | receiving a Close control frame. |
| 316 | |
| 317 | 1007 |
| 318 | |
| 319 | 1007 indicates that an endpoint is terminating the connection |
| 320 | because it has received data within a message that was not |
| 321 | consistent with the type of the message (e.g., non-UTF-8 [RFC3629] |
| 322 | data within a text message). |
| 323 | |
| 324 | 1008 |
| 325 | |
| 326 | 1008 indicates that an endpoint is terminating the connection |
| 327 | because it has received a message that violates its policy. This |
| 328 | is a generic status code that can be returned when there is no |
| 329 | other more suitable status code (e.g., 1003 or 1009) or if there |
| 330 | is a need to hide specific details about the policy. |
| 331 | |
| 332 | 1009 |
| 333 | |
| 334 | 1009 indicates that an endpoint is terminating the connection |
| 335 | because it has received a message that is too big for it to |
| 336 | process. |
| 337 | |
| 338 | 1010 |
| 339 | |
| 340 | 1010 indicates that an endpoint (client) is terminating the |
| 341 | connection because it has expected the server to negotiate one or |
| 342 | more extension, but the server didn't return them in the response |
| 343 | message of the WebSocket handshake. The list of extensions that |
| 344 | are needed SHOULD appear in the /reason/ part of the Close frame. |
| 345 | Note that this status code is not used by the server, because it |
| 346 | can fail the WebSocket handshake instead. |
| 347 | |
| 348 | 1011 |
| 349 | |
| 350 | 1011 indicates that a server is terminating the connection because |
| 351 | it encountered an unexpected condition that prevented it from |
| 352 | fulfilling the request. |
| 353 | |
| 354 | 1015 |
| 355 | |
| 356 | 1015 is a reserved value and MUST NOT be set as a status code in a |
| 357 | Close control frame by an endpoint. It is designated for use in |
| 358 | applications expecting a status code to indicate that the |
| 359 | connection was closed due to a failure to perform a TLS handshake |
| 360 | (e.g., the server certificate can't be verified). |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 361 | */ |
| 362 | |
| 363 | enum lws_close_status { |
| 364 | LWS_CLOSE_STATUS_NOSTATUS = 0, |
| 365 | LWS_CLOSE_STATUS_NORMAL = 1000, |
| 366 | LWS_CLOSE_STATUS_GOINGAWAY = 1001, |
| 367 | LWS_CLOSE_STATUS_PROTOCOL_ERR = 1002, |
| 368 | LWS_CLOSE_STATUS_UNACCEPTABLE_OPCODE = 1003, |
David Galeano | 2915a80 | 2013-01-09 15:26:08 +0800 | [diff] [blame] | 369 | LWS_CLOSE_STATUS_RESERVED = 1004, |
| 370 | LWS_CLOSE_STATUS_NO_STATUS = 1005, |
| 371 | LWS_CLOSE_STATUS_ABNORMAL_CLOSE = 1006, |
| 372 | LWS_CLOSE_STATUS_INVALID_PAYLOAD = 1007, |
| 373 | LWS_CLOSE_STATUS_POLICY_VIOLATION = 1008, |
| 374 | LWS_CLOSE_STATUS_MESSAGE_TOO_LARGE = 1009, |
| 375 | LWS_CLOSE_STATUS_EXTENSION_REQUIRED = 1010, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 376 | LWS_CLOSE_STATUS_UNEXPECTED_CONDITION = 1011, |
| 377 | LWS_CLOSE_STATUS_TLS_FAILURE = 1015, |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 378 | }; |
| 379 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 380 | struct libwebsocket; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 381 | struct libwebsocket_context; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 382 | /* needed even with extensions disabled for create context */ |
Andy Green | 46c2ea0 | 2011-03-22 09:04:01 +0000 | [diff] [blame] | 383 | struct libwebsocket_extension; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 384 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 385 | /** |
David Brooks | 6c6a3d3 | 2012-04-20 12:13:50 +0800 | [diff] [blame] | 386 | * callback_function() - User server actions |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 387 | * @context: Websockets context |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 388 | * @wsi: Opaque websocket instance pointer |
| 389 | * @reason: The reason for the call |
| 390 | * @user: Pointer to per-session user data allocated by library |
| 391 | * @in: Pointer used for some callback reasons |
| 392 | * @len: Length set for some callback reasons |
| 393 | * |
| 394 | * This callback is the way the user controls what is served. All the |
| 395 | * protocol detail is hidden and handled by the library. |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 396 | * |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 397 | * For each connection / session there is user data allocated that is |
| 398 | * pointed to by "user". You set the size of this user data area when |
| 399 | * the library is initialized with libwebsocket_create_server. |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 400 | * |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 401 | * You get an opportunity to initialize user data when called back with |
| 402 | * LWS_CALLBACK_ESTABLISHED reason. |
| 403 | * |
Andy Green | 2b57a34 | 2013-02-06 15:15:25 +0900 | [diff] [blame] | 404 | * LWS_CALLBACK_ESTABLISHED: after the server completes a handshake with |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 405 | * an incoming client |
| 406 | * |
David Brooks | 80a4497 | 2012-04-20 12:18:47 +0800 | [diff] [blame] | 407 | * LWS_CALLBACK_CLIENT_CONNECTION_ERROR: the request client connection has |
| 408 | * been unable to complete a handshake with the remote server |
| 409 | * |
Andy Green | 2b57a34 | 2013-02-06 15:15:25 +0900 | [diff] [blame] | 410 | * LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH: this is the last chance for the |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 411 | * client user code to examine the http headers |
| 412 | * and decide to reject the connection. If the |
| 413 | * content in the headers is interesting to the |
| 414 | * client (url, etc) it needs to copy it out at |
| 415 | * this point since it will be destroyed before |
| 416 | * the CLIENT_ESTABLISHED call |
Andy Green | 2b57a34 | 2013-02-06 15:15:25 +0900 | [diff] [blame] | 417 | * |
David Brooks | 80a4497 | 2012-04-20 12:18:47 +0800 | [diff] [blame] | 418 | * LWS_CALLBACK_CLIENT_ESTABLISHED: after your client connection completed |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 419 | * a handshake with the remote server |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 420 | * |
| 421 | * LWS_CALLBACK_CLOSED: when the websocket session ends |
| 422 | * |
Andy Green | 0c9563b | 2013-06-10 22:54:40 +0800 | [diff] [blame] | 423 | * LWS_CALLBACK_CLOSED_HTTP: when a HTTP (non-websocket) session ends |
| 424 | * |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 425 | * LWS_CALLBACK_RECEIVE: data has appeared for this server endpoint from a |
| 426 | * remote client, it can be found at *in and is |
| 427 | * len bytes long |
| 428 | * |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 429 | * LWS_CALLBACK_CLIENT_RECEIVE_PONG: if you elected to see PONG packets, |
| 430 | * they appear with this callback reason. PONG |
| 431 | * packets only exist in 04+ protocol |
| 432 | * |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 433 | * LWS_CALLBACK_CLIENT_RECEIVE: data has appeared from the server for the |
| 434 | * client connection, it can be found at *in and |
| 435 | * is len bytes long |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 436 | * |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 437 | * LWS_CALLBACK_HTTP: an http request has come from a client that is not |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 438 | * asking to upgrade the connection to a websocket |
| 439 | * one. This is a chance to serve http content, |
| 440 | * for example, to send a script to the client |
| 441 | * which will then open the websockets connection. |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 442 | * @in points to the URI path requested and |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 443 | * libwebsockets_serve_http_file() makes it very |
| 444 | * simple to send back a file to the client. |
Andy Green | 24b588b | 2013-01-13 09:53:18 +0800 | [diff] [blame] | 445 | * Normally after sending the file you are done |
| 446 | * with the http connection, since the rest of the |
| 447 | * activity will come by websockets from the script |
| 448 | * that was delivered by http, so you will want to |
| 449 | * return 1; to close and free up the connection. |
| 450 | * That's important because it uses a slot in the |
| 451 | * total number of client connections allowed set |
| 452 | * by MAX_CLIENTS. |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 453 | * |
Andy Green | 54cb346 | 2013-02-14 22:23:54 +0800 | [diff] [blame] | 454 | * LWS_CALLBACK_HTTP_WRITEABLE: you can write more down the http protocol |
| 455 | * link now. |
| 456 | * |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 457 | * LWS_CALLBACK_HTTP_FILE_COMPLETION: a file requested to be send down |
| 458 | * http link has completed. |
| 459 | * |
Andy Green | 9e4c2b6 | 2011-03-07 20:47:39 +0000 | [diff] [blame] | 460 | * LWS_CALLBACK_CLIENT_WRITEABLE: |
| 461 | * LWS_CALLBACK_SERVER_WRITEABLE: If you call |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 462 | * libwebsocket_callback_on_writable() on a connection, you will |
Andy Green | 9e4c2b6 | 2011-03-07 20:47:39 +0000 | [diff] [blame] | 463 | * get one of these callbacks coming when the connection socket |
| 464 | * is able to accept another write packet without blocking. |
| 465 | * If it already was able to take another packet without blocking, |
| 466 | * you'll get this callback at the next call to the service loop |
| 467 | * function. Notice that CLIENTs get LWS_CALLBACK_CLIENT_WRITEABLE |
| 468 | * and servers get LWS_CALLBACK_SERVER_WRITEABLE. |
Andy Green | e7981dc | 2011-02-12 21:24:03 +0000 | [diff] [blame] | 469 | * |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 470 | * LWS_CALLBACK_FILTER_NETWORK_CONNECTION: called when a client connects to |
| 471 | * the server at network level; the connection is accepted but then |
| 472 | * passed to this callback to decide whether to hang up immediately |
Edwin van den Oetelaar | 8c8a8e1 | 2013-02-20 20:56:59 +0800 | [diff] [blame] | 473 | * or not, based on the client IP. @in contains the connection |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 474 | * socket's descriptor. Return non-zero to terminate |
| 475 | * the connection before sending or receiving anything. |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 476 | * Because this happens immediately after the network connection |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 477 | * from the client, there's no websocket protocol selected yet so |
| 478 | * this callback is issued only to protocol 0. |
| 479 | * |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 480 | * LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: called when the handshake has |
| 481 | * been received and parsed from the client, but the response is |
| 482 | * not sent yet. Return non-zero to disallow the connection. |
Andy Green | 96d48fd | 2013-09-18 08:32:55 +0800 | [diff] [blame^] | 483 | * @user is a pointer to the connection user space allocation, |
| 484 | * @in is the requested protocol name |
| 485 | * In your handler you can use the public APIs |
| 486 | * lws_hdr_total_length() / lws_hdr_copy() to access all of the |
| 487 | * headers using the header enums lws_token_indexes from |
| 488 | * libwebsockets.h to check for and read the supported header |
| 489 | * presence and content before deciding to allow the handshake |
| 490 | * to proceed or to kill the connection. |
Andy Green | c85619d | 2011-02-13 08:25:26 +0000 | [diff] [blame] | 491 | * |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 492 | * LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS: if configured for |
| 493 | * including OpenSSL support, this callback allows your user code |
| 494 | * to perform extra SSL_CTX_load_verify_locations() or similar |
Andy Green | 0894bda | 2011-02-19 09:09:11 +0000 | [diff] [blame] | 495 | * calls to direct OpenSSL where to find certificates the client |
| 496 | * can use to confirm the remote server identity. @user is the |
| 497 | * OpenSSL SSL_CTX* |
Andy Green | c85619d | 2011-02-13 08:25:26 +0000 | [diff] [blame] | 498 | * |
Andy Green | c6bf2c2 | 2011-02-20 11:10:47 +0000 | [diff] [blame] | 499 | * LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS: if configured for |
| 500 | * including OpenSSL support, this callback allows your user code |
| 501 | * to load extra certifcates into the server which allow it to |
| 502 | * verify the validity of certificates returned by clients. @user |
| 503 | * is the server's OpenSSL SSL_CTX* |
| 504 | * |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 505 | * LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION: if the |
| 506 | * libwebsockets context was created with the option |
| 507 | * LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT, then this |
| 508 | * callback is generated during OpenSSL verification of the cert |
| 509 | * sent from the client. It is sent to protocol[0] callback as |
| 510 | * no protocol has been negotiated on the connection yet. |
| 511 | * Notice that the libwebsockets context and wsi are both NULL |
| 512 | * during this callback. See |
| 513 | * http://www.openssl.org/docs/ssl/SSL_CTX_set_verify.html |
| 514 | * to understand more detail about the OpenSSL callback that |
| 515 | * generates this libwebsockets callback and the meanings of the |
| 516 | * arguments passed. In this callback, @user is the x509_ctx, |
| 517 | * @in is the ssl pointer and @len is preverify_ok |
| 518 | * Notice that this callback maintains libwebsocket return |
| 519 | * conventions, return 0 to mean the cert is OK or 1 to fail it. |
| 520 | * This also means that if you don't handle this callback then |
| 521 | * the default callback action of returning 0 allows the client |
| 522 | * certificates. |
| 523 | * |
Andy Green | 385e7ad | 2011-03-01 21:06:02 +0000 | [diff] [blame] | 524 | * LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER: this callback happens |
| 525 | * when a client handshake is being compiled. @user is NULL, |
| 526 | * @in is a char **, it's pointing to a char * which holds the |
| 527 | * next location in the header buffer where you can add |
| 528 | * headers, and @len is the remaining space in the header buffer, |
| 529 | * which is typically some hundreds of bytes. So, to add a canned |
| 530 | * cookie, your handler code might look similar to: |
| 531 | * |
| 532 | * char **p = (char **)in; |
| 533 | * |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 534 | * if (len < 100) |
| 535 | * return 1; |
Andy Green | 385e7ad | 2011-03-01 21:06:02 +0000 | [diff] [blame] | 536 | * |
| 537 | * *p += sprintf(*p, "Cookie: a=b\x0d\x0a"); |
| 538 | * |
| 539 | * return 0; |
| 540 | * |
| 541 | * Notice if you add anything, you just have to take care about |
| 542 | * the CRLF on the line you added. Obviously this callback is |
| 543 | * optional, if you don't handle it everything is fine. |
| 544 | * |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 545 | * Notice the callback is coming to protocols[0] all the time, |
Andy Green | 385e7ad | 2011-03-01 21:06:02 +0000 | [diff] [blame] | 546 | * because there is no specific protocol handshook yet. |
| 547 | * |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 548 | * LWS_CALLBACK_CONFIRM_EXTENSION_OKAY: When the server handshake code |
Andy Green | c511482 | 2011-03-06 10:29:35 +0000 | [diff] [blame] | 549 | * sees that it does support a requested extension, before |
| 550 | * accepting the extension by additing to the list sent back to |
| 551 | * the client it gives this callback just to check that it's okay |
| 552 | * to use that extension. It calls back to the requested protocol |
| 553 | * and with @in being the extension name, @len is 0 and @user is |
| 554 | * valid. Note though at this time the ESTABLISHED callback hasn't |
| 555 | * happened yet so if you initialize @user content there, @user |
| 556 | * content during this callback might not be useful for anything. |
| 557 | * Notice this callback comes to protocols[0]. |
| 558 | * |
Andy Green | c6517fa | 2011-03-06 13:15:29 +0000 | [diff] [blame] | 559 | * LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED: When a client |
| 560 | * connection is being prepared to start a handshake to a server, |
| 561 | * each supported extension is checked with protocols[0] callback |
| 562 | * with this reason, giving the user code a chance to suppress the |
| 563 | * claim to support that extension by returning non-zero. If |
| 564 | * unhandled, by default 0 will be returned and the extension |
| 565 | * support included in the header to the server. Notice this |
| 566 | * callback comes to protocols[0]. |
| 567 | * |
Andy Green | a7109e6 | 2013-02-11 12:05:54 +0800 | [diff] [blame] | 568 | * LWS_CALLBACK_PROTOCOL_INIT: One-time call per protocol so it can |
| 569 | * do initial setup / allocations etc |
| 570 | * |
| 571 | * LWS_CALLBACK_PROTOCOL_DESTROY: One-time call per protocol indicating |
| 572 | * this protocol won't get used at all after this callback, the |
| 573 | * context is getting destroyed. Take the opportunity to |
| 574 | * deallocate everything that was allocated by the protocol. |
| 575 | * |
Andy Green | e7981dc | 2011-02-12 21:24:03 +0000 | [diff] [blame] | 576 | * The next four reasons are optional and only need taking care of if you |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 577 | * will be integrating libwebsockets sockets into an external polling |
| 578 | * array. |
| 579 | * |
| 580 | * LWS_CALLBACK_ADD_POLL_FD: libwebsocket deals with its poll() loop |
| 581 | * internally, but in the case you are integrating with another |
| 582 | * server you will need to have libwebsocket sockets share a |
| 583 | * polling array with the other server. This and the other |
| 584 | * POLL_FD related callbacks let you put your specialized |
| 585 | * poll array interface code in the callback for protocol 0, the |
| 586 | * first protocol you support, usually the HTTP protocol in the |
| 587 | * serving case. This callback happens when a socket needs to be |
Andy Green | 50097dd | 2013-02-15 22:36:30 +0800 | [diff] [blame] | 588 | * added to the polling loop: @in contains the fd, and |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 589 | * @len is the events bitmap (like, POLLIN). If you are using the |
Andy Green | e7981dc | 2011-02-12 21:24:03 +0000 | [diff] [blame] | 590 | * internal polling loop (the "service" callback), you can just |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 591 | * ignore these callbacks. |
Andy Green | e7981dc | 2011-02-12 21:24:03 +0000 | [diff] [blame] | 592 | * |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 593 | * LWS_CALLBACK_DEL_POLL_FD: This callback happens when a socket descriptor |
Andy Green | 50097dd | 2013-02-15 22:36:30 +0800 | [diff] [blame] | 594 | * needs to be removed from an external polling array. @in is |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 595 | * the socket desricptor. If you are using the internal polling |
| 596 | * loop, you can just ignore it. |
Andy Green | e7981dc | 2011-02-12 21:24:03 +0000 | [diff] [blame] | 597 | * |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 598 | * LWS_CALLBACK_SET_MODE_POLL_FD: This callback happens when libwebsockets |
Andy Green | 50097dd | 2013-02-15 22:36:30 +0800 | [diff] [blame] | 599 | * wants to modify the events for the socket descriptor in @in. |
Andy Green | e7981dc | 2011-02-12 21:24:03 +0000 | [diff] [blame] | 600 | * The handler should OR @len on to the events member of the pollfd |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 601 | * struct for this socket descriptor. If you are using the |
Andy Green | e7981dc | 2011-02-12 21:24:03 +0000 | [diff] [blame] | 602 | * internal polling loop, you can just ignore it. |
| 603 | * |
| 604 | * LWS_CALLBACK_CLEAR_MODE_POLL_FD: This callback occurs when libwebsockets |
Andy Green | 50097dd | 2013-02-15 22:36:30 +0800 | [diff] [blame] | 605 | * wants to modify the events for the socket descriptor in @in. |
Andy Green | e7981dc | 2011-02-12 21:24:03 +0000 | [diff] [blame] | 606 | * The handler should AND ~@len on to the events member of the |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 607 | * pollfd struct for this socket descriptor. If you are using the |
Andy Green | e7981dc | 2011-02-12 21:24:03 +0000 | [diff] [blame] | 608 | * internal polling loop, you can just ignore it. |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 609 | */ |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 610 | LWS_VISIBLE LWS_EXTERN int callback(struct libwebsocket_context *context, |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 611 | struct libwebsocket *wsi, |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 612 | enum libwebsocket_callback_reasons reason, void *user, |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 613 | void *in, size_t len); |
| 614 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 615 | typedef int (callback_function)(struct libwebsocket_context *context, |
David Brooks | 6c6a3d3 | 2012-04-20 12:13:50 +0800 | [diff] [blame] | 616 | struct libwebsocket *wsi, |
| 617 | enum libwebsocket_callback_reasons reason, void *user, |
| 618 | void *in, size_t len); |
| 619 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 620 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | 57b4e9a | 2011-03-06 13:14:46 +0000 | [diff] [blame] | 621 | /** |
David Brooks | 6c6a3d3 | 2012-04-20 12:13:50 +0800 | [diff] [blame] | 622 | * extension_callback_function() - Hooks to allow extensions to operate |
Andy Green | 57b4e9a | 2011-03-06 13:14:46 +0000 | [diff] [blame] | 623 | * @context: Websockets context |
Andy Green | 46c2ea0 | 2011-03-22 09:04:01 +0000 | [diff] [blame] | 624 | * @ext: This extension |
Andy Green | 57b4e9a | 2011-03-06 13:14:46 +0000 | [diff] [blame] | 625 | * @wsi: Opaque websocket instance pointer |
| 626 | * @reason: The reason for the call |
| 627 | * @user: Pointer to per-session user data allocated by library |
| 628 | * @in: Pointer used for some callback reasons |
| 629 | * @len: Length set for some callback reasons |
| 630 | * |
| 631 | * Each extension that is active on a particular connection receives |
| 632 | * callbacks during the connection lifetime to allow the extension to |
| 633 | * operate on websocket data and manage itself. |
| 634 | * |
| 635 | * Libwebsockets takes care of allocating and freeing "user" memory for |
| 636 | * each active extension on each connection. That is what is pointed to |
| 637 | * by the @user parameter. |
| 638 | * |
| 639 | * LWS_EXT_CALLBACK_CONSTRUCT: called when the server has decided to |
| 640 | * select this extension from the list provided by the client, |
| 641 | * just before the server will send back the handshake accepting |
| 642 | * the connection with this extension active. This gives the |
| 643 | * extension a chance to initialize its connection context found |
| 644 | * in @user. |
| 645 | * |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 646 | * LWS_EXT_CALLBACK_CLIENT_CONSTRUCT: same as LWS_EXT_CALLBACK_CONSTRUCT |
Andy Green | 2366b1c | 2011-03-06 13:15:31 +0000 | [diff] [blame] | 647 | * but called when client is instantiating this extension. Some |
| 648 | * extensions will work the same on client and server side and then |
| 649 | * you can just merge handlers for both CONSTRUCTS. |
| 650 | * |
Andy Green | 57b4e9a | 2011-03-06 13:14:46 +0000 | [diff] [blame] | 651 | * LWS_EXT_CALLBACK_DESTROY: called when the connection the extension was |
| 652 | * being used on is about to be closed and deallocated. It's the |
| 653 | * last chance for the extension to deallocate anything it has |
| 654 | * allocated in the user data (pointed to by @user) before the |
Andy Green | 2366b1c | 2011-03-06 13:15:31 +0000 | [diff] [blame] | 655 | * user data is deleted. This same callback is used whether you |
| 656 | * are in client or server instantiation context. |
Andy Green | 57b4e9a | 2011-03-06 13:14:46 +0000 | [diff] [blame] | 657 | * |
| 658 | * LWS_EXT_CALLBACK_PACKET_RX_PREPARSE: when this extension was active on |
| 659 | * a connection, and a packet of data arrived at the connection, |
| 660 | * it is passed to this callback to give the extension a chance to |
| 661 | * change the data, eg, decompress it. @user is pointing to the |
| 662 | * extension's private connection context data, @in is pointing |
| 663 | * to an lws_tokens struct, it consists of a char * pointer called |
| 664 | * token, and an int called token_len. At entry, these are |
| 665 | * set to point to the received buffer and set to the content |
| 666 | * length. If the extension will grow the content, it should use |
| 667 | * a new buffer allocated in its private user context data and |
| 668 | * set the pointed-to lws_tokens members to point to its buffer. |
| 669 | * |
| 670 | * LWS_EXT_CALLBACK_PACKET_TX_PRESEND: this works the same way as |
| 671 | * LWS_EXT_CALLBACK_PACKET_RX_PREPARSE above, except it gives the |
| 672 | * extension a chance to change websocket data just before it will |
| 673 | * be sent out. Using the same lws_token pointer scheme in @in, |
| 674 | * the extension can change the buffer and the length to be |
| 675 | * transmitted how it likes. Again if it wants to grow the |
| 676 | * buffer safely, it should copy the data into its own buffer and |
| 677 | * set the lws_tokens token pointer to it. |
| 678 | */ |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 679 | LWS_VISIBLE LWS_EXTERN int extension_callback(struct libwebsocket_context *context, |
Andy Green | 46c2ea0 | 2011-03-22 09:04:01 +0000 | [diff] [blame] | 680 | struct libwebsocket_extension *ext, |
Andy Green | 57b4e9a | 2011-03-06 13:14:46 +0000 | [diff] [blame] | 681 | struct libwebsocket *wsi, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 682 | enum libwebsocket_extension_callback_reasons reason, |
| 683 | void *user, void *in, size_t len); |
Andy Green | 57b4e9a | 2011-03-06 13:14:46 +0000 | [diff] [blame] | 684 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 685 | typedef int (extension_callback_function)(struct libwebsocket_context *context, |
David Brooks | 6c6a3d3 | 2012-04-20 12:13:50 +0800 | [diff] [blame] | 686 | struct libwebsocket_extension *ext, |
| 687 | struct libwebsocket *wsi, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 688 | enum libwebsocket_extension_callback_reasons reason, |
| 689 | void *user, void *in, size_t len); |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 690 | #endif |
Andy Green | 57b4e9a | 2011-03-06 13:14:46 +0000 | [diff] [blame] | 691 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 692 | /** |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 693 | * struct libwebsocket_protocols - List of protocols and handlers server |
| 694 | * supports. |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 695 | * @name: Protocol name that must match the one given in the client |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 696 | * Javascript new WebSocket(url, 'protocol') name |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 697 | * @callback: The service callback used for this protocol. It allows the |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 698 | * service action for an entire protocol to be encapsulated in |
| 699 | * the protocol-specific callback |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 700 | * @per_session_data_size: Each new connection using this protocol gets |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 701 | * this much memory allocated on connection establishment and |
| 702 | * freed on connection takedown. A pointer to this per-connection |
| 703 | * allocation is passed into the callback in the 'user' parameter |
Andy Green | 5449511 | 2013-02-06 21:10:16 +0900 | [diff] [blame] | 704 | * @rx_buffer_size: if you want atomic frames delivered to the callback, you |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 705 | * should set this to the size of the biggest legal frame that |
| 706 | * you support. If the frame size is exceeded, there is no |
| 707 | * error, but the buffer will spill to the user callback when |
| 708 | * full, which you can detect by using |
| 709 | * libwebsockets_remaining_packet_payload(). Notice that you |
| 710 | * just talk about frame size here, the LWS_SEND_BUFFER_PRE_PADDING |
| 711 | * and post-padding are automatically also allocated on top. |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 712 | * @owning_server: the server init call fills in this opaque pointer when |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 713 | * registering this protocol with the server. |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 714 | * @protocol_index: which protocol we are starting from zero |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 715 | * |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 716 | * This structure represents one protocol supported by the server. An |
| 717 | * array of these structures is passed to libwebsocket_create_server() |
| 718 | * allows as many protocols as you like to be handled by one server. |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 719 | */ |
| 720 | |
| 721 | struct libwebsocket_protocols { |
| 722 | const char *name; |
David Brooks | 6c6a3d3 | 2012-04-20 12:13:50 +0800 | [diff] [blame] | 723 | callback_function *callback; |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 724 | size_t per_session_data_size; |
Andy Green | 5449511 | 2013-02-06 21:10:16 +0900 | [diff] [blame] | 725 | size_t rx_buffer_size; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 726 | |
| 727 | /* |
| 728 | * below are filled in on server init and can be left uninitialized, |
| 729 | * no need for user to use them directly either |
| 730 | */ |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 731 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 732 | struct libwebsocket_context *owning_server; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 733 | int protocol_index; |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 734 | }; |
| 735 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 736 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 737 | /** |
| 738 | * struct libwebsocket_extension - An extension we know how to cope with |
| 739 | * |
| 740 | * @name: Formal extension name, eg, "deflate-stream" |
| 741 | * @callback: Service callback |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 742 | * @per_session_data_size: Libwebsockets will auto-malloc this much |
| 743 | * memory for the use of the extension, a pointer |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 744 | * to it comes in the @user callback parameter |
Peter Pentchev | c74964e | 2013-02-07 16:17:13 +0200 | [diff] [blame] | 745 | * @per_context_private_data: Optional storage for this extension that |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 746 | * is per-context, so it can track stuff across |
| 747 | * all sessions, etc, if it wants |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 748 | */ |
| 749 | |
| 750 | struct libwebsocket_extension { |
| 751 | const char *name; |
David Brooks | 6c6a3d3 | 2012-04-20 12:13:50 +0800 | [diff] [blame] | 752 | extension_callback_function *callback; |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 753 | size_t per_session_data_size; |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 754 | void *per_context_private_data; |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 755 | }; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 756 | #endif |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 757 | |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 758 | /** |
| 759 | * struct lws_context_creation_info: parameters to create context with |
| 760 | * |
| 761 | * @port: Port to listen on... you can use 0 to suppress listening on |
| 762 | * any port, that's what you want if you are not running a |
| 763 | * websocket server at all but just using it as a client |
Andy Green | 7a506ba | 2013-02-12 10:13:02 +0800 | [diff] [blame] | 764 | * @iface: NULL to bind the listen socket to all interfaces, or the |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 765 | * interface name, eg, "eth2" |
| 766 | * @protocols: Array of structures listing supported protocols and a protocol- |
| 767 | * specific callback for each one. The list is ended with an |
| 768 | * entry that has a NULL callback pointer. |
| 769 | * It's not const because we write the owning_server member |
| 770 | * @extensions: NULL or array of libwebsocket_extension structs listing the |
| 771 | * extensions this context supports. If you configured with |
| 772 | * --without-extensions, you should give NULL here. |
| 773 | * @ssl_cert_filepath: If libwebsockets was compiled to use ssl, and you want |
| 774 | * to listen using SSL, set to the filepath to fetch the |
| 775 | * server cert from, otherwise NULL for unencrypted |
| 776 | * @ssl_private_key_filepath: filepath to private key if wanting SSL mode, |
| 777 | * else ignored |
| 778 | * @ssl_ca_filepath: CA certificate filepath or NULL |
Andy Green | 2672fb2 | 2013-02-22 09:54:35 +0800 | [diff] [blame] | 779 | * @ssl_cipher_list: List of valid ciphers to use (eg, |
| 780 | * "RC4-MD5:RC4-SHA:AES128-SHA:AES256-SHA:HIGH:!DSS:!aNULL" |
| 781 | * or you can leave it as NULL to get "DEFAULT" |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 782 | * @gid: group id to change to after setting listen socket, or -1. |
| 783 | * @uid: user id to change to after setting listen socket, or -1. |
| 784 | * @options: 0, or LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK |
| 785 | * @user: optional user pointer that can be recovered via the context |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 786 | * pointer using libwebsocket_context_user |
Andy Green | a690cd0 | 2013-02-09 12:25:31 +0800 | [diff] [blame] | 787 | * @ka_time: 0 for no keepalive, otherwise apply this keepalive timeout to |
| 788 | * all libwebsocket sockets, client or server |
| 789 | * @ka_probes: if ka_time was nonzero, after the timeout expires how many |
| 790 | * times to try to get a response from the peer before giving up |
| 791 | * and killing the connection |
| 792 | * @ka_interval: if ka_time was nonzero, how long to wait before each ka_probes |
| 793 | * attempt |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 794 | */ |
| 795 | |
| 796 | struct lws_context_creation_info { |
| 797 | int port; |
Joakim Soderberg | 63ff120 | 2013-02-11 17:52:23 +0100 | [diff] [blame] | 798 | const char *iface; |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 799 | struct libwebsocket_protocols *protocols; |
| 800 | struct libwebsocket_extension *extensions; |
| 801 | const char *ssl_cert_filepath; |
| 802 | const char *ssl_private_key_filepath; |
| 803 | const char *ssl_ca_filepath; |
Andy Green | 2672fb2 | 2013-02-22 09:54:35 +0800 | [diff] [blame] | 804 | const char *ssl_cipher_list; |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 805 | int gid; |
| 806 | int uid; |
| 807 | unsigned int options; |
| 808 | void *user; |
Andy Green | a690cd0 | 2013-02-09 12:25:31 +0800 | [diff] [blame] | 809 | int ka_time; |
| 810 | int ka_probes; |
| 811 | int ka_interval; |
| 812 | |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 813 | }; |
| 814 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 815 | LWS_VISIBLE LWS_EXTERN |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 816 | void lws_set_log_level(int level, |
| 817 | void (*log_emit_function)(int level, const char *line)); |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 818 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 819 | LWS_VISIBLE LWS_EXTERN void |
Andy Green | c11db20 | 2013-01-19 11:12:16 +0800 | [diff] [blame] | 820 | lwsl_emit_syslog(int level, const char *line); |
| 821 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 822 | LWS_VISIBLE LWS_EXTERN struct libwebsocket_context * |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 823 | libwebsocket_create_context(struct lws_context_creation_info *info); |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 824 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 825 | LWS_VISIBLE LWS_EXTERN void |
Darin Willits | c19456f | 2011-02-14 17:52:39 +0000 | [diff] [blame] | 826 | libwebsocket_context_destroy(struct libwebsocket_context *context); |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 827 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 828 | LWS_VISIBLE LWS_EXTERN int |
Darin Willits | c19456f | 2011-02-14 17:52:39 +0000 | [diff] [blame] | 829 | libwebsocket_service(struct libwebsocket_context *context, int timeout_ms); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 830 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 831 | LWS_VISIBLE LWS_EXTERN int |
Darin Willits | c19456f | 2011-02-14 17:52:39 +0000 | [diff] [blame] | 832 | libwebsocket_service_fd(struct libwebsocket_context *context, |
Andy Green | 9f99034 | 2011-02-12 11:57:45 +0000 | [diff] [blame] | 833 | struct pollfd *pollfd); |
| 834 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 835 | LWS_VISIBLE LWS_EXTERN void * |
Alon Levy | 0291eb3 | 2012-10-19 11:21:56 +0200 | [diff] [blame] | 836 | libwebsocket_context_user(struct libwebsocket_context *context); |
| 837 | |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame] | 838 | /* |
| 839 | * IMPORTANT NOTICE! |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 840 | * |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 841 | * When sending with websocket protocol (LWS_WRITE_TEXT or LWS_WRITE_BINARY) |
| 842 | * the send buffer has to have LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame] | 843 | * buf, and LWS_SEND_BUFFER_POST_PADDING bytes valid AFTER (buf + len). |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 844 | * |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame] | 845 | * This allows us to add protocol info before and after the data, and send as |
| 846 | * one packet on the network without payload copying, for maximum efficiency. |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 847 | * |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame] | 848 | * So for example you need this kind of code to use libwebsocket_write with a |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 849 | * 128-byte payload |
| 850 | * |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 851 | * char buf[LWS_SEND_BUFFER_PRE_PADDING + 128 + LWS_SEND_BUFFER_POST_PADDING]; |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 852 | * |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 853 | * // fill your part of the buffer... for example here it's all zeros |
| 854 | * memset(&buf[LWS_SEND_BUFFER_PRE_PADDING], 0, 128); |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 855 | * |
Alex Rhatushnyak | 9f2246e | 2013-03-09 12:01:47 +0800 | [diff] [blame] | 856 | * libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], 128, |
| 857 | * LWS_WRITE_TEXT); |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 858 | * |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 859 | * When sending LWS_WRITE_HTTP, there is no protocol addition and you can just |
| 860 | * use the whole buffer without taking care of the above. |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame] | 861 | */ |
| 862 | |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 863 | /* |
| 864 | * this is the frame nonce plus two header plus 8 length |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 865 | * there's an additional two for mux extension per mux nesting level |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 866 | * 2 byte prepend on close will already fit because control frames cannot use |
| 867 | * the big length style |
| 868 | */ |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 869 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 870 | #define LWS_SEND_BUFFER_PRE_PADDING (4 + 10 + (2 * MAX_MUX_RECURSION)) |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 871 | #define LWS_SEND_BUFFER_POST_PADDING 4 |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame] | 872 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 873 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 874 | libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf, size_t len, |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 875 | enum libwebsocket_write_protocol protocol); |
Andy Green | a2b0ab0 | 2010-11-11 12:28:29 +0000 | [diff] [blame] | 876 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 877 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 878 | libwebsockets_serve_http_file(struct libwebsocket_context *context, |
| 879 | struct libwebsocket *wsi, const char *file, |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 880 | const char *content_type); |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 881 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 882 | libwebsockets_serve_http_file_fragment(struct libwebsocket_context *context, |
| 883 | struct libwebsocket *wsi); |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 884 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 885 | LWS_VISIBLE LWS_EXTERN const struct libwebsocket_protocols * |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 886 | libwebsockets_get_protocol(struct libwebsocket *wsi); |
| 887 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 888 | LWS_VISIBLE LWS_EXTERN int |
Darin Willits | c19456f | 2011-02-14 17:52:39 +0000 | [diff] [blame] | 889 | libwebsocket_callback_on_writable(struct libwebsocket_context *context, |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 890 | struct libwebsocket *wsi); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 891 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 892 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 893 | libwebsocket_callback_on_writable_all_protocol( |
| 894 | const struct libwebsocket_protocols *protocol); |
| 895 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 896 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 897 | libwebsocket_get_socket_fd(struct libwebsocket *wsi); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 898 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 899 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | 82c3d54 | 2011-03-07 21:16:31 +0000 | [diff] [blame] | 900 | libwebsocket_is_final_fragment(struct libwebsocket *wsi); |
| 901 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 902 | LWS_VISIBLE LWS_EXTERN unsigned char |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 903 | libwebsocket_get_reserved_bits(struct libwebsocket *wsi); |
| 904 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 905 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 906 | libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable); |
| 907 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 908 | LWS_VISIBLE LWS_EXTERN void |
Andy Green | b55451c | 2013-03-16 12:32:27 +0800 | [diff] [blame] | 909 | libwebsocket_rx_flow_allow_all_protocol( |
| 910 | const struct libwebsocket_protocols *protocol); |
| 911 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 912 | LWS_VISIBLE LWS_EXTERN size_t |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 913 | libwebsockets_remaining_packet_payload(struct libwebsocket *wsi); |
| 914 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 915 | LWS_VISIBLE LWS_EXTERN struct libwebsocket * |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 916 | libwebsocket_client_connect(struct libwebsocket_context *clients, |
| 917 | const char *address, |
| 918 | int port, |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 919 | int ssl_connection, |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 920 | const char *path, |
| 921 | const char *host, |
| 922 | const char *origin, |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 923 | const char *protocol, |
| 924 | int ietf_version_or_minus_one); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 925 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 926 | LWS_VISIBLE LWS_EXTERN struct libwebsocket * |
David Brooks | 2c60d95 | 2012-04-20 12:19:01 +0800 | [diff] [blame] | 927 | libwebsocket_client_connect_extended(struct libwebsocket_context *clients, |
| 928 | const char *address, |
| 929 | int port, |
| 930 | int ssl_connection, |
| 931 | const char *path, |
| 932 | const char *host, |
| 933 | const char *origin, |
| 934 | const char *protocol, |
| 935 | int ietf_version_or_minus_one, |
| 936 | void *userdata); |
| 937 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 938 | LWS_VISIBLE LWS_EXTERN const char * |
Darin Willits | c19456f | 2011-02-14 17:52:39 +0000 | [diff] [blame] | 939 | libwebsocket_canonical_hostname(struct libwebsocket_context *context); |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 940 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 941 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 942 | LWS_VISIBLE LWS_EXTERN void |
Andy Green | aaf0b9f | 2013-01-30 08:12:20 +0800 | [diff] [blame] | 943 | libwebsockets_get_peer_addresses(struct libwebsocket_context *context, |
| 944 | struct libwebsocket *wsi, int fd, char *name, int name_len, |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 945 | char *rip, int rip_len); |
| 946 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 947 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | 8acc194 | 2011-03-07 20:47:40 +0000 | [diff] [blame] | 948 | libwebsockets_get_random(struct libwebsocket_context *context, |
| 949 | void *buf, int len); |
| 950 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 951 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | 279a303 | 2013-01-17 10:05:39 +0800 | [diff] [blame] | 952 | lws_daemonize(const char *_lock_path); |
| 953 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 954 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | 043a0ba | 2011-04-25 23:54:11 +0800 | [diff] [blame] | 955 | lws_send_pipe_choked(struct libwebsocket *wsi); |
| 956 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 957 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | 2fd3f2f | 2013-01-18 09:49:20 +0800 | [diff] [blame] | 958 | lws_frame_is_binary(struct libwebsocket *wsi); |
| 959 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 960 | LWS_VISIBLE LWS_EXTERN unsigned char * |
Andy Green | 2836c64 | 2011-03-07 20:47:41 +0000 | [diff] [blame] | 961 | libwebsockets_SHA1(const unsigned char *d, size_t n, unsigned char *md); |
| 962 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 963 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | f94e058 | 2011-05-14 15:07:56 +0200 | [diff] [blame] | 964 | lws_b64_encode_string(const char *in, int in_len, char *out, int out_size); |
| 965 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 966 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | f94e058 | 2011-05-14 15:07:56 +0200 | [diff] [blame] | 967 | lws_b64_decode_string(const char *in, char *out, int out_size); |
| 968 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 969 | LWS_VISIBLE LWS_EXTERN const char * |
Andy Green | 7b40545 | 2013-02-01 10:50:15 +0800 | [diff] [blame] | 970 | lws_get_library_version(void); |
| 971 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 972 | /* access to headers... only valid while headers valid */ |
| 973 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 974 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 975 | lws_hdr_total_length(struct libwebsocket *wsi, enum lws_token_indexes h); |
| 976 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 977 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 978 | lws_hdr_copy(struct libwebsocket *wsi, char *dest, int len, |
| 979 | enum lws_token_indexes h); |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 980 | |
Andy Green | ea0642a | 2013-01-29 06:52:00 +0800 | [diff] [blame] | 981 | /* |
| 982 | * Note: this is not normally needed as a user api. It's provided in case it is |
| 983 | * useful when integrating with other app poll loop service code. |
| 984 | */ |
| 985 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 986 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | ea0642a | 2013-01-29 06:52:00 +0800 | [diff] [blame] | 987 | libwebsocket_read(struct libwebsocket_context *context, |
| 988 | struct libwebsocket *wsi, |
| 989 | unsigned char *buf, size_t len); |
| 990 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 991 | #ifndef LWS_NO_EXTENSIONS |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 992 | LWS_VISIBLE LWS_EXTERN struct libwebsocket_extension *libwebsocket_get_internal_extensions(); |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 993 | #endif |
Andy Green | 4cd87a0 | 2011-03-06 13:15:32 +0000 | [diff] [blame] | 994 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 995 | #ifdef __cplusplus |
| 996 | } |
| 997 | #endif |
Andy Green | a11fe94 | 2011-09-25 10:30:26 +0100 | [diff] [blame] | 998 | |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 999 | #endif |