Andy Green | 58eaa74 | 2011-03-07 17:54:06 +0000 | [diff] [blame] | 1 | /* |
Andy Green | a0da8a8 | 2010-11-08 17:12:19 +0000 | [diff] [blame] | 2 | * libwebsockets - small server side websockets and web server implementation |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 3 | * |
Andy Green | a0da8a8 | 2010-11-08 17:12:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2010 Andy Green <andy@warmcat.com> |
| 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 |
Andy Green | 05a0a7b | 2010-10-31 17:51:39 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 22 | #include "private-libwebsockets.h" |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 23 | |
Andreas Pakulat | 68bd4bd | 2013-10-28 15:18:04 +0100 | [diff] [blame] | 24 | #if defined(WIN32) || defined(_WIN32) |
David Galeano | cb19368 | 2013-01-09 15:29:00 +0800 | [diff] [blame] | 25 | #include <tchar.h> |
| 26 | #include <io.h> |
Joakim Soderberg | 63ff120 | 2013-02-11 17:52:23 +0100 | [diff] [blame] | 27 | #include <mstcpip.h> |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 28 | #else |
David | c4ef7b1 | 2013-01-12 20:39:47 +0800 | [diff] [blame] | 29 | #ifdef LWS_BUILTIN_GETIFADDRS |
| 30 | #include <getifaddrs.h> |
| 31 | #else |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 32 | #include <ifaddrs.h> |
David | c4ef7b1 | 2013-01-12 20:39:47 +0800 | [diff] [blame] | 33 | #endif |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 34 | #include <syslog.h> |
Andy Green | 7627af5 | 2011-03-09 15:13:52 +0000 | [diff] [blame] | 35 | #include <sys/un.h> |
Andy Green | a69f051 | 2012-05-03 12:32:38 +0800 | [diff] [blame] | 36 | #include <sys/socket.h> |
| 37 | #include <netdb.h> |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 38 | #endif |
Andy Green | 2e24da0 | 2011-03-05 16:12:04 +0000 | [diff] [blame] | 39 | |
Andy Green | 3b3fa9e | 2013-12-25 16:34:37 +0800 | [diff] [blame] | 40 | #include <sys/types.h> |
| 41 | |
Andy Green | 2e24da0 | 2011-03-05 16:12:04 +0000 | [diff] [blame] | 42 | #ifdef LWS_OPENSSL_SUPPORT |
| 43 | int openssl_websocket_private_data_index; |
| 44 | #endif |
| 45 | |
Andy Green | aa6fc44 | 2012-04-12 13:26:49 +0800 | [diff] [blame] | 46 | #ifdef __MINGW32__ |
| 47 | #include "../win32port/win32helpers/websock-w32.c" |
| 48 | #else |
| 49 | #ifdef __MINGW64__ |
| 50 | #include "../win32port/win32helpers/websock-w32.c" |
| 51 | #endif |
| 52 | #endif |
| 53 | |
Andy Green | 7b40545 | 2013-02-01 10:50:15 +0800 | [diff] [blame] | 54 | #ifndef LWS_BUILD_HASH |
| 55 | #define LWS_BUILD_HASH "unknown-build-hash" |
| 56 | #endif |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 57 | |
Andy Green | 7c19c34 | 2013-01-19 12:18:07 +0800 | [diff] [blame] | 58 | static int log_level = LLL_ERR | LLL_WARN | LLL_NOTICE; |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 59 | static void lwsl_emit_stderr(int level, const char *line); |
| 60 | static void (*lwsl_emit)(int level, const char *line) = lwsl_emit_stderr; |
| 61 | |
Andy Green | 7b40545 | 2013-02-01 10:50:15 +0800 | [diff] [blame] | 62 | static const char *library_version = LWS_LIBRARY_VERSION " " LWS_BUILD_HASH; |
| 63 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 64 | static const char * const log_level_names[] = { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 65 | "ERR", |
| 66 | "WARN", |
Andy Green | 7c19c34 | 2013-01-19 12:18:07 +0800 | [diff] [blame] | 67 | "NOTICE", |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 68 | "INFO", |
| 69 | "DEBUG", |
| 70 | "PARSER", |
| 71 | "HEADER", |
| 72 | "EXTENSION", |
| 73 | "CLIENT", |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 74 | "LATENCY", |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 75 | }; |
| 76 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 77 | #ifndef LWS_NO_CLIENT |
| 78 | extern int lws_client_socket_service( |
| 79 | struct libwebsocket_context *context, |
| 80 | struct libwebsocket *wsi, struct pollfd *pollfd); |
| 81 | #endif |
| 82 | #ifndef LWS_NO_SERVER |
| 83 | extern int lws_server_socket_service( |
| 84 | struct libwebsocket_context *context, |
| 85 | struct libwebsocket *wsi, struct pollfd *pollfd); |
| 86 | #endif |
| 87 | |
Andy Green | 3b3fa9e | 2013-12-25 16:34:37 +0800 | [diff] [blame] | 88 | |
| 89 | #ifdef LWS_HAS_PPOLL |
| 90 | /* |
| 91 | * set to the Thread ID that's doing the service loop just before entry to ppoll |
| 92 | * indicates service thread likely idling in ppoll() |
| 93 | * volatile because other threads may check it as part of processing for pollfd |
| 94 | * event change. |
| 95 | */ |
| 96 | static volatile int lws_idling_ppoll_tid; |
| 97 | #endif |
| 98 | |
Andy Green | 7b40545 | 2013-02-01 10:50:15 +0800 | [diff] [blame] | 99 | /** |
| 100 | * lws_get_library_version: get version and git hash library built from |
| 101 | * |
| 102 | * returns a const char * to a string like "1.1 178d78c" |
| 103 | * representing the library version followed by the git head hash it |
| 104 | * was built from |
| 105 | */ |
| 106 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 107 | LWS_VISIBLE const char * |
Andy Green | 7b40545 | 2013-02-01 10:50:15 +0800 | [diff] [blame] | 108 | lws_get_library_version(void) |
| 109 | { |
| 110 | return library_version; |
| 111 | } |
| 112 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 113 | int |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 114 | insert_wsi_socket_into_fds(struct libwebsocket_context *context, |
| 115 | struct libwebsocket *wsi) |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 116 | { |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 117 | if (context->fds_count >= context->max_fds) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 118 | lwsl_err("Too many fds (%d)\n", context->max_fds); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 119 | return 1; |
| 120 | } |
| 121 | |
nononame | f162492 | 2014-01-11 13:12:34 +0800 | [diff] [blame] | 122 | if (wsi->sock >= context->max_fds) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 123 | lwsl_err("Socket fd %d is too high (%d)\n", |
| 124 | wsi->sock, context->max_fds); |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 125 | return 1; |
| 126 | } |
| 127 | |
| 128 | assert(wsi); |
Andy Green | 512c246 | 2013-09-18 12:59:33 +0800 | [diff] [blame] | 129 | assert(wsi->sock >= 0); |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 130 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 131 | lwsl_info("insert_wsi_socket_into_fds: wsi=%p, sock=%d, fds pos=%d\n", |
| 132 | wsi, wsi->sock, context->fds_count); |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 133 | |
Andy Green | 7a13279 | 2013-12-18 09:48:26 +0800 | [diff] [blame] | 134 | context->protocols[0].callback(context, wsi, |
| 135 | LWS_CALLBACK_LOCK_POLL, |
| 136 | wsi->user_space, (void *)(long)wsi->sock, 0); |
| 137 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 138 | context->lws_lookup[wsi->sock] = wsi; |
| 139 | wsi->position_in_fds_table = context->fds_count; |
| 140 | context->fds[context->fds_count].fd = wsi->sock; |
| 141 | context->fds[context->fds_count].events = POLLIN; |
| 142 | context->fds[context->fds_count++].revents = 0; |
| 143 | |
| 144 | /* external POLL support via protocol 0 */ |
| 145 | context->protocols[0].callback(context, wsi, |
| 146 | LWS_CALLBACK_ADD_POLL_FD, |
Andy Green | 50097dd | 2013-02-15 22:36:30 +0800 | [diff] [blame] | 147 | wsi->user_space, (void *)(long)wsi->sock, POLLIN); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 148 | |
Andy Green | 7a13279 | 2013-12-18 09:48:26 +0800 | [diff] [blame] | 149 | context->protocols[0].callback(context, wsi, |
| 150 | LWS_CALLBACK_UNLOCK_POLL, |
| 151 | wsi->user_space, (void *)(long)wsi->sock, 0); |
| 152 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 153 | return 0; |
| 154 | } |
| 155 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 156 | static int |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 157 | remove_wsi_socket_from_fds(struct libwebsocket_context *context, |
| 158 | struct libwebsocket *wsi) |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 159 | { |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 160 | int m; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 161 | |
Andy Green | 7a13279 | 2013-12-18 09:48:26 +0800 | [diff] [blame] | 162 | if (!--context->fds_count) { |
| 163 | context->protocols[0].callback(context, wsi, |
| 164 | LWS_CALLBACK_LOCK_POLL, |
| 165 | wsi->user_space, (void *)(long)wsi->sock, 0); |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 166 | goto do_ext; |
Andy Green | 7a13279 | 2013-12-18 09:48:26 +0800 | [diff] [blame] | 167 | } |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 168 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 169 | if (wsi->sock > context->max_fds) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 170 | lwsl_err("Socket fd %d too high (%d)\n", |
| 171 | wsi->sock, context->max_fds); |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 172 | return 1; |
| 173 | } |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 174 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 175 | lwsl_info("remove_wsi_socket_from_fds: wsi=%p, sock=%d, fds pos=%d\n", |
| 176 | wsi, wsi->sock, wsi->position_in_fds_table); |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 177 | |
Andy Green | 7a13279 | 2013-12-18 09:48:26 +0800 | [diff] [blame] | 178 | context->protocols[0].callback(context, wsi, |
| 179 | LWS_CALLBACK_LOCK_POLL, |
| 180 | wsi->user_space, (void *)(long)wsi->sock, 0); |
| 181 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 182 | m = wsi->position_in_fds_table; /* replace the contents for this */ |
| 183 | |
| 184 | /* have the last guy take up the vacant slot */ |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 185 | context->fds[m] = context->fds[context->fds_count]; |
| 186 | /* |
| 187 | * end guy's fds_lookup entry remains unchanged |
| 188 | * (still same fd pointing to same wsi) |
| 189 | */ |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 190 | /* end guy's "position in fds table" changed */ |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 191 | context->lws_lookup[context->fds[context->fds_count].fd]-> |
| 192 | position_in_fds_table = m; |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 193 | /* deletion guy's lws_lookup entry needs nuking */ |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 194 | context->lws_lookup[wsi->sock] = NULL; |
| 195 | /* removed wsi has no position any more */ |
| 196 | wsi->position_in_fds_table = -1; |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 197 | |
| 198 | do_ext: |
| 199 | /* remove also from external POLL support via protocol 0 */ |
| 200 | if (wsi->sock) |
| 201 | context->protocols[0].callback(context, wsi, |
Andy Green | 50097dd | 2013-02-15 22:36:30 +0800 | [diff] [blame] | 202 | LWS_CALLBACK_DEL_POLL_FD, wsi->user_space, |
| 203 | (void *)(long)wsi->sock, 0); |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 204 | |
Andy Green | 7a13279 | 2013-12-18 09:48:26 +0800 | [diff] [blame] | 205 | context->protocols[0].callback(context, wsi, |
| 206 | LWS_CALLBACK_UNLOCK_POLL, |
| 207 | wsi->user_space, (void *)(long)wsi->sock, 0); |
| 208 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 209 | return 0; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 212 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 213 | void |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 214 | libwebsocket_close_and_free_session(struct libwebsocket_context *context, |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 215 | struct libwebsocket *wsi, enum lws_close_status reason) |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 216 | { |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 217 | int n; |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 218 | int old_state; |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 219 | unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 + |
| 220 | LWS_SEND_BUFFER_POST_PADDING]; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 221 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 222 | int ret; |
| 223 | int m; |
| 224 | struct lws_tokens eff_buf; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 225 | struct libwebsocket_extension *ext; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 226 | #endif |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 227 | |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 228 | if (!wsi) |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 229 | return; |
| 230 | |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 231 | old_state = wsi->state; |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 232 | |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 233 | if (old_state == WSI_STATE_DEAD_SOCKET) |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 234 | return; |
| 235 | |
Andy Green | 22524a6 | 2013-02-15 22:48:58 +0800 | [diff] [blame] | 236 | /* we tried the polite way... */ |
| 237 | if (old_state == WSI_STATE_AWAITING_CLOSE_ACK) |
| 238 | goto just_kill_connection; |
| 239 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 240 | wsi->u.ws.close_reason = reason; |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 241 | |
Andy Green | 5dc62ea | 2013-09-20 20:26:12 +0800 | [diff] [blame] | 242 | if (wsi->mode == LWS_CONNMODE_WS_CLIENT_WAITING_CONNECT || |
| 243 | wsi->mode == LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE) { |
| 244 | |
| 245 | context->protocols[0].callback(context, wsi, |
| 246 | LWS_CALLBACK_CLIENT_CONNECTION_ERROR, NULL, NULL, 0); |
| 247 | |
| 248 | free(wsi->u.hdr.ah); |
| 249 | goto just_kill_connection; |
| 250 | } |
| 251 | |
| 252 | |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 253 | if (wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED) { |
| 254 | if (wsi->u.http.post_buffer) { |
| 255 | free(wsi->u.http.post_buffer); |
| 256 | wsi->u.http.post_buffer = NULL; |
| 257 | } |
| 258 | if (wsi->u.http.fd >= 0) { |
| 259 | lwsl_debug("closing http fd %d\n", wsi->u.http.fd); |
| 260 | close(wsi->u.http.fd); |
| 261 | wsi->u.http.fd = -1; |
| 262 | context->protocols[0].callback(context, wsi, |
| 263 | LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0); |
| 264 | } |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 265 | } |
| 266 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 267 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 268 | /* |
Andy Green | 68b4504 | 2011-05-25 21:41:57 +0100 | [diff] [blame] | 269 | * are his extensions okay with him closing? Eg he might be a mux |
| 270 | * parent and just his ch1 aspect is closing? |
| 271 | */ |
| 272 | |
Andy Green | 68b4504 | 2011-05-25 21:41:57 +0100 | [diff] [blame] | 273 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 274 | if (!wsi->active_extensions[n]->callback) |
| 275 | continue; |
| 276 | |
| 277 | m = wsi->active_extensions[n]->callback(context, |
| 278 | wsi->active_extensions[n], wsi, |
| 279 | LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE, |
| 280 | wsi->active_extensions_user[n], NULL, 0); |
| 281 | |
| 282 | /* |
| 283 | * if somebody vetoed actually closing him at this time.... |
| 284 | * up to the extension to track the attempted close, let's |
| 285 | * just bail |
| 286 | */ |
| 287 | |
| 288 | if (m) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 289 | lwsl_ext("extension vetoed close\n"); |
Andy Green | 68b4504 | 2011-05-25 21:41:57 +0100 | [diff] [blame] | 290 | return; |
| 291 | } |
| 292 | } |
| 293 | |
Andy Green | 68b4504 | 2011-05-25 21:41:57 +0100 | [diff] [blame] | 294 | /* |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 295 | * flush any tx pending from extensions, since we may send close packet |
| 296 | * if there are problems with send, just nuke the connection |
| 297 | */ |
| 298 | |
| 299 | ret = 1; |
| 300 | while (ret == 1) { |
| 301 | |
| 302 | /* default to nobody has more to spill */ |
| 303 | |
| 304 | ret = 0; |
| 305 | eff_buf.token = NULL; |
| 306 | eff_buf.token_len = 0; |
| 307 | |
| 308 | /* show every extension the new incoming data */ |
| 309 | |
| 310 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 311 | m = wsi->active_extensions[n]->callback( |
Andy Green | 46c2ea0 | 2011-03-22 09:04:01 +0000 | [diff] [blame] | 312 | wsi->protocol->owning_server, |
| 313 | wsi->active_extensions[n], wsi, |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 314 | LWS_EXT_CALLBACK_FLUSH_PENDING_TX, |
| 315 | wsi->active_extensions_user[n], &eff_buf, 0); |
| 316 | if (m < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 317 | lwsl_ext("Extension reports fatal error\n"); |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 318 | goto just_kill_connection; |
| 319 | } |
| 320 | if (m) |
| 321 | /* |
| 322 | * at least one extension told us he has more |
| 323 | * to spill, so we will go around again after |
| 324 | */ |
| 325 | ret = 1; |
| 326 | } |
| 327 | |
| 328 | /* assuming they left us something to send, send it */ |
| 329 | |
| 330 | if (eff_buf.token_len) |
| 331 | if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token, |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 332 | eff_buf.token_len) != eff_buf.token_len) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 333 | lwsl_debug("close: ext spill failed\n"); |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 334 | goto just_kill_connection; |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 335 | } |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 336 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 337 | #endif |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 338 | |
| 339 | /* |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 340 | * signal we are closing, libsocket_write will |
| 341 | * add any necessary version-specific stuff. If the write fails, |
| 342 | * no worries we are closing anyway. If we didn't initiate this |
| 343 | * close, then our state has been changed to |
| 344 | * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this. |
| 345 | * |
| 346 | * Likewise if it's a second call to close this connection after we |
| 347 | * sent the close indication to the peer already, we are in state |
| 348 | * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time. |
| 349 | */ |
| 350 | |
| 351 | if (old_state == WSI_STATE_ESTABLISHED && |
| 352 | reason != LWS_CLOSE_STATUS_NOSTATUS) { |
Andy Green | 66a16f3 | 2011-05-24 22:07:45 +0100 | [diff] [blame] | 353 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 354 | lwsl_debug("sending close indication...\n"); |
Andy Green | 66a16f3 | 2011-05-24 22:07:45 +0100 | [diff] [blame] | 355 | |
Andy Green | fdd305a | 2013-02-11 14:32:48 +0800 | [diff] [blame] | 356 | /* make valgrind happy */ |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 357 | memset(buf, 0, sizeof(buf)); |
| 358 | n = libwebsocket_write(wsi, |
| 359 | &buf[LWS_SEND_BUFFER_PRE_PADDING + 2], |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 360 | 0, LWS_WRITE_CLOSE); |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 361 | if (n >= 0) { |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 362 | /* |
| 363 | * we have sent a nice protocol level indication we |
| 364 | * now wish to close, we should not send anything more |
| 365 | */ |
| 366 | |
| 367 | wsi->state = WSI_STATE_AWAITING_CLOSE_ACK; |
| 368 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 369 | /* |
| 370 | * ...and we should wait for a reply for a bit |
| 371 | * out of politeness |
| 372 | */ |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 373 | |
| 374 | libwebsocket_set_timeout(wsi, |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 375 | PENDING_TIMEOUT_CLOSE_ACK, 1); |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 376 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 377 | lwsl_debug("sent close indication, awaiting ack\n"); |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 378 | |
| 379 | return; |
| 380 | } |
| 381 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 382 | lwsl_info("close: sending close packet failed, hanging up\n"); |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 383 | |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 384 | /* else, the send failed and we should just hang up */ |
| 385 | } |
| 386 | |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 387 | just_kill_connection: |
Andy Green | 66a16f3 | 2011-05-24 22:07:45 +0100 | [diff] [blame] | 388 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 389 | lwsl_debug("close: just_kill_connection\n"); |
Andy Green | 66a16f3 | 2011-05-24 22:07:45 +0100 | [diff] [blame] | 390 | |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 391 | /* |
| 392 | * we won't be servicing or receiving anything further from this guy |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 393 | * delete socket from the internal poll list if still present |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 394 | */ |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 395 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 396 | remove_wsi_socket_from_fds(context, wsi); |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 397 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 398 | wsi->state = WSI_STATE_DEAD_SOCKET; |
| 399 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 400 | if ((old_state == WSI_STATE_ESTABLISHED || |
| 401 | wsi->mode == LWS_CONNMODE_WS_SERVING || |
Andy Green | 3ee9b31 | 2013-02-12 12:52:39 +0800 | [diff] [blame] | 402 | wsi->mode == LWS_CONNMODE_WS_CLIENT)) { |
| 403 | |
| 404 | if (wsi->u.ws.rx_user_buffer) { |
| 405 | free(wsi->u.ws.rx_user_buffer); |
| 406 | wsi->u.ws.rx_user_buffer = NULL; |
| 407 | } |
| 408 | if (wsi->u.ws.rxflow_buffer) { |
| 409 | free(wsi->u.ws.rxflow_buffer); |
| 410 | wsi->u.ws.rxflow_buffer = NULL; |
| 411 | } |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 412 | if (wsi->truncated_send_malloc) { |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 413 | /* not going to be completed... nuke it */ |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 414 | free(wsi->truncated_send_malloc); |
| 415 | wsi->truncated_send_malloc = NULL; |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 416 | } |
Andy Green | 5449511 | 2013-02-06 21:10:16 +0900 | [diff] [blame] | 417 | } |
| 418 | |
Andy Green | 4b6fbe1 | 2011-02-14 08:03:48 +0000 | [diff] [blame] | 419 | /* tell the user it's all over for this guy */ |
| 420 | |
Andy Green | d430273 | 2011-02-28 07:45:29 +0000 | [diff] [blame] | 421 | if (wsi->protocol && wsi->protocol->callback && |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 422 | ((old_state == WSI_STATE_ESTABLISHED) || |
| 423 | (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) || |
| 424 | (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 425 | lwsl_debug("calling back CLOSED\n"); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 426 | wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED, |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 427 | wsi->user_space, NULL, 0); |
Niall T. Davidson | db761be | 2013-06-29 10:16:18 +0800 | [diff] [blame] | 428 | } else if ( wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED ) { |
| 429 | lwsl_debug("calling back CLOSED_HTTP\n"); |
| 430 | context->protocols[0].callback(context, wsi, |
| 431 | LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0 ); |
Andy Green | cc01247 | 2011-11-07 19:53:23 +0800 | [diff] [blame] | 432 | } else |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 433 | lwsl_debug("not calling back closed\n"); |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 434 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 435 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | ef660a9 | 2011-03-06 10:29:38 +0000 | [diff] [blame] | 436 | /* deallocate any active extension contexts */ |
| 437 | |
| 438 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 439 | if (!wsi->active_extensions[n]->callback) |
| 440 | continue; |
| 441 | |
Andy Green | 46c2ea0 | 2011-03-22 09:04:01 +0000 | [diff] [blame] | 442 | wsi->active_extensions[n]->callback(context, |
| 443 | wsi->active_extensions[n], wsi, |
| 444 | LWS_EXT_CALLBACK_DESTROY, |
| 445 | wsi->active_extensions_user[n], NULL, 0); |
Andy Green | ef660a9 | 2011-03-06 10:29:38 +0000 | [diff] [blame] | 446 | |
| 447 | free(wsi->active_extensions_user[n]); |
| 448 | } |
| 449 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 450 | /* |
| 451 | * inform all extensions in case they tracked this guy out of band |
| 452 | * even though not active on him specifically |
| 453 | */ |
| 454 | |
| 455 | ext = context->extensions; |
| 456 | while (ext && ext->callback) { |
| 457 | ext->callback(context, ext, wsi, |
| 458 | LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING, |
| 459 | NULL, NULL, 0); |
| 460 | ext++; |
| 461 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 462 | #endif |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 463 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 464 | /* lwsl_info("closing fd=%d\n", wsi->sock); */ |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 465 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 466 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 467 | if (wsi->ssl) { |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 468 | n = SSL_get_fd(wsi->ssl); |
| 469 | SSL_shutdown(wsi->ssl); |
Andy Green | 3fc2c65 | 2013-01-14 15:35:02 +0800 | [diff] [blame] | 470 | compatible_close(n); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 471 | SSL_free(wsi->ssl); |
| 472 | } else { |
| 473 | #endif |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 474 | if (wsi->sock) { |
| 475 | n = shutdown(wsi->sock, SHUT_RDWR); |
| 476 | if (n) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 477 | lwsl_debug("closing: shutdown returned %d\n", |
| 478 | errno); |
Andy Green | 3fc2c65 | 2013-01-14 15:35:02 +0800 | [diff] [blame] | 479 | |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 480 | n = compatible_close(wsi->sock); |
| 481 | if (n) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 482 | lwsl_debug("closing: close returned %d\n", |
| 483 | errno); |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 484 | } |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 485 | #ifdef LWS_OPENSSL_SUPPORT |
| 486 | } |
| 487 | #endif |
Andy Green | 76b6ea1 | 2014-02-15 19:25:50 +0800 | [diff] [blame^] | 488 | |
| 489 | /* outermost destroy notification for wsi (user_space still intact) */ |
| 490 | context->protocols[0].callback(context, wsi, |
| 491 | LWS_CALLBACK_WSI_DESTROY, wsi->user_space, NULL, 0); |
| 492 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 493 | if (wsi->protocol && wsi->protocol->per_session_data_size && |
| 494 | wsi->user_space) /* user code may own */ |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 495 | free(wsi->user_space); |
| 496 | |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 497 | free(wsi); |
| 498 | } |
| 499 | |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 500 | /** |
| 501 | * libwebsockets_get_peer_addresses() - Get client address information |
Andy Green | aaf0b9f | 2013-01-30 08:12:20 +0800 | [diff] [blame] | 502 | * @context: Libwebsockets context |
| 503 | * @wsi: Local struct libwebsocket associated with |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 504 | * @fd: Connection socket descriptor |
| 505 | * @name: Buffer to take client address name |
| 506 | * @name_len: Length of client address name buffer |
| 507 | * @rip: Buffer to take client address IP qotted quad |
| 508 | * @rip_len: Length of client address IP buffer |
| 509 | * |
| 510 | * This function fills in @name and @rip with the name and IP of |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 511 | * the client connected with socket descriptor @fd. Names may be |
| 512 | * truncated if there is not enough room. If either cannot be |
| 513 | * determined, they will be returned as valid zero-length strings. |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 514 | */ |
| 515 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 516 | LWS_VISIBLE void |
Andy Green | aaf0b9f | 2013-01-30 08:12:20 +0800 | [diff] [blame] | 517 | libwebsockets_get_peer_addresses(struct libwebsocket_context *context, |
| 518 | struct libwebsocket *wsi, int fd, char *name, int name_len, |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 519 | char *rip, int rip_len) |
| 520 | { |
Bob Roberts | ac04911 | 2013-04-25 09:16:30 +0800 | [diff] [blame] | 521 | socklen_t len; |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 522 | struct sockaddr_in sin; |
| 523 | struct hostent *host; |
| 524 | struct hostent *host1; |
| 525 | char ip[128]; |
Andy Green | f92def7 | 2011-03-09 15:02:20 +0000 | [diff] [blame] | 526 | unsigned char *p; |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 527 | int n; |
Andy Green | aaf0b9f | 2013-01-30 08:12:20 +0800 | [diff] [blame] | 528 | int ret = -1; |
David Galeano | cb19368 | 2013-01-09 15:29:00 +0800 | [diff] [blame] | 529 | #ifdef AF_LOCAL |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 530 | struct sockaddr_un *un; |
David Galeano | cb19368 | 2013-01-09 15:29:00 +0800 | [diff] [blame] | 531 | #endif |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 532 | |
| 533 | rip[0] = '\0'; |
| 534 | name[0] = '\0'; |
| 535 | |
Andy Green | aaf0b9f | 2013-01-30 08:12:20 +0800 | [diff] [blame] | 536 | lws_latency_pre(context, wsi); |
| 537 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 538 | len = sizeof(sin); |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 539 | if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) { |
| 540 | perror("getpeername"); |
Andy Green | aaf0b9f | 2013-01-30 08:12:20 +0800 | [diff] [blame] | 541 | goto bail; |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 542 | } |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 543 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 544 | host = gethostbyaddr((char *) &sin.sin_addr, sizeof(sin.sin_addr), |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 545 | AF_INET); |
| 546 | if (host == NULL) { |
| 547 | perror("gethostbyaddr"); |
Andy Green | aaf0b9f | 2013-01-30 08:12:20 +0800 | [diff] [blame] | 548 | goto bail; |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | strncpy(name, host->h_name, name_len); |
| 552 | name[name_len - 1] = '\0'; |
| 553 | |
| 554 | host1 = gethostbyname(host->h_name); |
| 555 | if (host1 == NULL) |
Andy Green | aaf0b9f | 2013-01-30 08:12:20 +0800 | [diff] [blame] | 556 | goto bail; |
Andy Green | f92def7 | 2011-03-09 15:02:20 +0000 | [diff] [blame] | 557 | p = (unsigned char *)host1; |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 558 | n = 0; |
| 559 | while (p != NULL) { |
Andy Green | f92def7 | 2011-03-09 15:02:20 +0000 | [diff] [blame] | 560 | p = (unsigned char *)host1->h_addr_list[n++]; |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 561 | if (p == NULL) |
| 562 | continue; |
Peter Hinz | bb45a90 | 2011-03-10 18:14:01 +0000 | [diff] [blame] | 563 | if ((host1->h_addrtype != AF_INET) |
| 564 | #ifdef AF_LOCAL |
| 565 | && (host1->h_addrtype != AF_LOCAL) |
| 566 | #endif |
| 567 | ) |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 568 | continue; |
| 569 | |
Andy Green | 7627af5 | 2011-03-09 15:13:52 +0000 | [diff] [blame] | 570 | if (host1->h_addrtype == AF_INET) |
| 571 | sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]); |
Peter Hinz | bb45a90 | 2011-03-10 18:14:01 +0000 | [diff] [blame] | 572 | #ifdef AF_LOCAL |
Andy Green | 7627af5 | 2011-03-09 15:13:52 +0000 | [diff] [blame] | 573 | else { |
| 574 | un = (struct sockaddr_un *)p; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 575 | strncpy(ip, un->sun_path, sizeof(ip) - 1); |
Andy Green | 7627af5 | 2011-03-09 15:13:52 +0000 | [diff] [blame] | 576 | ip[sizeof(ip) - 1] = '\0'; |
| 577 | } |
Peter Hinz | bb45a90 | 2011-03-10 18:14:01 +0000 | [diff] [blame] | 578 | #endif |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 579 | p = NULL; |
| 580 | strncpy(rip, ip, rip_len); |
| 581 | rip[rip_len - 1] = '\0'; |
| 582 | } |
Andy Green | aaf0b9f | 2013-01-30 08:12:20 +0800 | [diff] [blame] | 583 | |
| 584 | ret = 0; |
| 585 | bail: |
| 586 | lws_latency(context, wsi, "libwebsockets_get_peer_addresses", ret, 1); |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 587 | } |
Andy Green | 9f99034 | 2011-02-12 11:57:45 +0000 | [diff] [blame] | 588 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 589 | LWS_VISIBLE int libwebsockets_get_random(struct libwebsocket_context *context, |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 590 | void *buf, int len) |
| 591 | { |
| 592 | int n; |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 593 | char *p = (char *)buf; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 594 | |
Andreas Pakulat | 68bd4bd | 2013-10-28 15:18:04 +0100 | [diff] [blame] | 595 | #if defined(WIN32) || defined(_WIN32) |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 596 | for (n = 0; n < len; n++) |
| 597 | p[n] = (unsigned char)rand(); |
| 598 | #else |
| 599 | n = read(context->fd_random, p, len); |
| 600 | #endif |
| 601 | |
| 602 | return n; |
| 603 | } |
| 604 | |
Andy Green | a690cd0 | 2013-02-09 12:25:31 +0800 | [diff] [blame] | 605 | int lws_set_socket_options(struct libwebsocket_context *context, int fd) |
| 606 | { |
| 607 | int optval = 1; |
| 608 | socklen_t optlen = sizeof(optval); |
Andreas Pakulat | 68bd4bd | 2013-10-28 15:18:04 +0100 | [diff] [blame] | 609 | #if defined(WIN32) || defined(_WIN32) |
Andy Green | a690cd0 | 2013-02-09 12:25:31 +0800 | [diff] [blame] | 610 | unsigned long optl = 0; |
| 611 | #endif |
| 612 | #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) |
| 613 | struct protoent *tcp_proto; |
| 614 | #endif |
| 615 | |
| 616 | if (context->ka_time) { |
| 617 | /* enable keepalive on this socket */ |
| 618 | optval = 1; |
| 619 | if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, |
| 620 | (const void *)&optval, optlen) < 0) |
| 621 | return 1; |
| 622 | |
Bob Roberts | ac04911 | 2013-04-25 09:16:30 +0800 | [diff] [blame] | 623 | #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__CYGWIN__) |
Andy Green | a47865f | 2013-02-10 09:39:47 +0800 | [diff] [blame] | 624 | |
| 625 | /* |
| 626 | * didn't find a way to set these per-socket, need to |
| 627 | * tune kernel systemwide values |
| 628 | */ |
Joakim Soderberg | 63ff120 | 2013-02-11 17:52:23 +0100 | [diff] [blame] | 629 | #elif WIN32 |
| 630 | { |
| 631 | DWORD dwBytesRet; |
| 632 | struct tcp_keepalive alive; |
| 633 | alive.onoff = TRUE; |
| 634 | alive.keepalivetime = context->ka_time; |
| 635 | alive.keepaliveinterval = context->ka_interval; |
Andy Green | a47865f | 2013-02-10 09:39:47 +0800 | [diff] [blame] | 636 | |
Joakim Soderberg | 63ff120 | 2013-02-11 17:52:23 +0100 | [diff] [blame] | 637 | if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive), |
| 638 | NULL, 0, &dwBytesRet, NULL, NULL)) |
| 639 | return 1; |
| 640 | } |
Andy Green | a47865f | 2013-02-10 09:39:47 +0800 | [diff] [blame] | 641 | #else |
Andy Green | a690cd0 | 2013-02-09 12:25:31 +0800 | [diff] [blame] | 642 | /* set the keepalive conditions we want on it too */ |
| 643 | optval = context->ka_time; |
| 644 | if (setsockopt(fd, IPPROTO_IP, TCP_KEEPIDLE, |
| 645 | (const void *)&optval, optlen) < 0) |
| 646 | return 1; |
| 647 | |
Larry Hayes | bb66ac6 | 2013-02-22 09:16:20 +0800 | [diff] [blame] | 648 | optval = context->ka_interval; |
Andy Green | a690cd0 | 2013-02-09 12:25:31 +0800 | [diff] [blame] | 649 | if (setsockopt(fd, IPPROTO_IP, TCP_KEEPINTVL, |
| 650 | (const void *)&optval, optlen) < 0) |
| 651 | return 1; |
| 652 | |
Larry Hayes | bb66ac6 | 2013-02-22 09:16:20 +0800 | [diff] [blame] | 653 | optval = context->ka_probes; |
Andy Green | a690cd0 | 2013-02-09 12:25:31 +0800 | [diff] [blame] | 654 | if (setsockopt(fd, IPPROTO_IP, TCP_KEEPCNT, |
| 655 | (const void *)&optval, optlen) < 0) |
| 656 | return 1; |
Andy Green | a47865f | 2013-02-10 09:39:47 +0800 | [diff] [blame] | 657 | #endif |
Andy Green | a690cd0 | 2013-02-09 12:25:31 +0800 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | /* Disable Nagle */ |
| 661 | optval = 1; |
| 662 | #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__) |
| 663 | setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen); |
| 664 | #else |
| 665 | tcp_proto = getprotobyname("TCP"); |
| 666 | setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen); |
| 667 | #endif |
| 668 | |
| 669 | /* We are nonblocking... */ |
Andreas Pakulat | 68bd4bd | 2013-10-28 15:18:04 +0100 | [diff] [blame] | 670 | #if defined(WIN32) || defined(_WIN32) |
Andy Green | a690cd0 | 2013-02-09 12:25:31 +0800 | [diff] [blame] | 671 | ioctlsocket(fd, FIONBIO, &optl); |
| 672 | #else |
| 673 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 674 | #endif |
| 675 | |
| 676 | return 0; |
| 677 | } |
| 678 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 679 | LWS_VISIBLE int lws_send_pipe_choked(struct libwebsocket *wsi) |
Andy Green | 95a7b5d | 2011-03-06 10:29:39 +0000 | [diff] [blame] | 680 | { |
| 681 | struct pollfd fds; |
| 682 | |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 683 | /* treat the fact we got a truncated send pending as if we're choked */ |
| 684 | if (wsi->truncated_send_malloc) |
| 685 | return 1; |
| 686 | |
Andy Green | 95a7b5d | 2011-03-06 10:29:39 +0000 | [diff] [blame] | 687 | fds.fd = wsi->sock; |
| 688 | fds.events = POLLOUT; |
| 689 | fds.revents = 0; |
| 690 | |
| 691 | if (poll(&fds, 1, 0) != 1) |
| 692 | return 1; |
| 693 | |
| 694 | if ((fds.revents & POLLOUT) == 0) |
| 695 | return 1; |
| 696 | |
| 697 | /* okay to send another packet without blocking */ |
| 698 | |
| 699 | return 0; |
| 700 | } |
| 701 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 702 | int |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 703 | lws_handle_POLLOUT_event(struct libwebsocket_context *context, |
| 704 | struct libwebsocket *wsi, struct pollfd *pollfd) |
| 705 | { |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 706 | int n; |
Andy Green | 6f520a5 | 2013-01-29 17:57:39 +0800 | [diff] [blame] | 707 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 708 | #ifndef LWS_NO_EXTENSIONS |
| 709 | struct lws_tokens eff_buf; |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 710 | int ret; |
| 711 | int m; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 712 | int handled = 0; |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 713 | |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 714 | /* pending truncated sends have uber priority */ |
| 715 | |
Andy Green | 2764eba | 2013-12-09 14:16:17 +0800 | [diff] [blame] | 716 | if (wsi->truncated_send_malloc) { |
| 717 | lws_issue_raw(wsi, wsi->truncated_send_malloc + |
| 718 | wsi->truncated_send_offset, |
| 719 | wsi->truncated_send_len); |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 720 | /* leave POLLOUT active either way */ |
| 721 | return 0; |
| 722 | } |
| 723 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 724 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 725 | if (!wsi->active_extensions[n]->callback) |
| 726 | continue; |
| 727 | |
| 728 | m = wsi->active_extensions[n]->callback(context, |
| 729 | wsi->active_extensions[n], wsi, |
| 730 | LWS_EXT_CALLBACK_IS_WRITEABLE, |
| 731 | wsi->active_extensions_user[n], NULL, 0); |
| 732 | if (m > handled) |
| 733 | handled = m; |
| 734 | } |
| 735 | |
| 736 | if (handled == 1) |
| 737 | goto notify_action; |
| 738 | |
| 739 | if (!wsi->extension_data_pending || handled == 2) |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 740 | goto user_service; |
| 741 | |
| 742 | /* |
| 743 | * check in on the active extensions, see if they |
| 744 | * had pending stuff to spill... they need to get the |
| 745 | * first look-in otherwise sequence will be disordered |
| 746 | * |
| 747 | * NULL, zero-length eff_buf means just spill pending |
| 748 | */ |
| 749 | |
| 750 | ret = 1; |
| 751 | while (ret == 1) { |
| 752 | |
| 753 | /* default to nobody has more to spill */ |
| 754 | |
| 755 | ret = 0; |
| 756 | eff_buf.token = NULL; |
| 757 | eff_buf.token_len = 0; |
| 758 | |
| 759 | /* give every extension a chance to spill */ |
| 760 | |
| 761 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 762 | m = wsi->active_extensions[n]->callback( |
Andy Green | 46c2ea0 | 2011-03-22 09:04:01 +0000 | [diff] [blame] | 763 | wsi->protocol->owning_server, |
| 764 | wsi->active_extensions[n], wsi, |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 765 | LWS_EXT_CALLBACK_PACKET_TX_PRESEND, |
| 766 | wsi->active_extensions_user[n], &eff_buf, 0); |
| 767 | if (m < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 768 | lwsl_err("ext reports fatal error\n"); |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 769 | return -1; |
| 770 | } |
| 771 | if (m) |
| 772 | /* |
| 773 | * at least one extension told us he has more |
| 774 | * to spill, so we will go around again after |
| 775 | */ |
| 776 | ret = 1; |
| 777 | } |
| 778 | |
| 779 | /* assuming they gave us something to send, send it */ |
| 780 | |
| 781 | if (eff_buf.token_len) { |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 782 | n = lws_issue_raw(wsi, (unsigned char *)eff_buf.token, |
| 783 | eff_buf.token_len); |
| 784 | if (n < 0) |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 785 | return -1; |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 786 | /* |
| 787 | * Keep amount spilled small to minimize chance of this |
| 788 | */ |
| 789 | if (n != eff_buf.token_len) { |
| 790 | lwsl_err("Unable to spill ext %d vs %s\n", |
| 791 | eff_buf.token_len, n); |
| 792 | return -1; |
| 793 | } |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 794 | } else |
| 795 | continue; |
| 796 | |
| 797 | /* no extension has more to spill */ |
| 798 | |
| 799 | if (!ret) |
| 800 | continue; |
| 801 | |
| 802 | /* |
| 803 | * There's more to spill from an extension, but we just sent |
| 804 | * something... did that leave the pipe choked? |
| 805 | */ |
| 806 | |
| 807 | if (!lws_send_pipe_choked(wsi)) |
| 808 | /* no we could add more */ |
| 809 | continue; |
| 810 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 811 | lwsl_info("choked in POLLOUT service\n"); |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 812 | |
| 813 | /* |
| 814 | * Yes, he's choked. Leave the POLLOUT masked on so we will |
| 815 | * come back here when he is unchoked. Don't call the user |
| 816 | * callback to enforce ordering of spilling, he'll get called |
| 817 | * when we come back here and there's nothing more to spill. |
| 818 | */ |
| 819 | |
| 820 | return 0; |
| 821 | } |
| 822 | |
| 823 | wsi->extension_data_pending = 0; |
| 824 | |
| 825 | user_service: |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 826 | #endif |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 827 | /* one shot */ |
| 828 | |
Andy Green | 91f19d8 | 2013-12-21 11:18:34 +0800 | [diff] [blame] | 829 | if (pollfd) |
| 830 | lws_change_pollfd(wsi, POLLOUT, 0); |
Andy Green | 7a13279 | 2013-12-18 09:48:26 +0800 | [diff] [blame] | 831 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 832 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 833 | notify_action: |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 834 | #endif |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 835 | |
Andy Green | 9e4c2b6 | 2011-03-07 20:47:39 +0000 | [diff] [blame] | 836 | if (wsi->mode == LWS_CONNMODE_WS_CLIENT) |
| 837 | n = LWS_CALLBACK_CLIENT_WRITEABLE; |
| 838 | else |
| 839 | n = LWS_CALLBACK_SERVER_WRITEABLE; |
| 840 | |
Andy Green | b8b247d | 2013-01-22 07:20:08 +0800 | [diff] [blame] | 841 | return user_callback_handle_rxflow(wsi->protocol->callback, context, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 842 | wsi, (enum libwebsocket_callback_reasons) n, |
| 843 | wsi->user_space, NULL, 0); |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | |
| 847 | |
Andy Green | 1c6e142 | 2013-02-20 19:11:31 +0800 | [diff] [blame] | 848 | int |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 849 | libwebsocket_service_timeout_check(struct libwebsocket_context *context, |
| 850 | struct libwebsocket *wsi, unsigned int sec) |
| 851 | { |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 852 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 853 | int n; |
| 854 | |
| 855 | /* |
| 856 | * if extensions want in on it (eg, we are a mux parent) |
| 857 | * give them a chance to service child timeouts |
| 858 | */ |
| 859 | |
| 860 | for (n = 0; n < wsi->count_active_extensions; n++) |
| 861 | wsi->active_extensions[n]->callback( |
| 862 | context, wsi->active_extensions[n], |
| 863 | wsi, LWS_EXT_CALLBACK_1HZ, |
| 864 | wsi->active_extensions_user[n], NULL, sec); |
| 865 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 866 | #endif |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 867 | if (!wsi->pending_timeout) |
Andy Green | 1c6e142 | 2013-02-20 19:11:31 +0800 | [diff] [blame] | 868 | return 0; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 869 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 870 | /* |
| 871 | * if we went beyond the allowed time, kill the |
| 872 | * connection |
| 873 | */ |
| 874 | |
| 875 | if (sec > wsi->pending_timeout_limit) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 876 | lwsl_info("TIMEDOUT WAITING\n"); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 877 | libwebsocket_close_and_free_session(context, |
| 878 | wsi, LWS_CLOSE_STATUS_NOSTATUS); |
Andy Green | 1c6e142 | 2013-02-20 19:11:31 +0800 | [diff] [blame] | 879 | return 1; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 880 | } |
Andy Green | 1c6e142 | 2013-02-20 19:11:31 +0800 | [diff] [blame] | 881 | |
| 882 | return 0; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 883 | } |
| 884 | |
Andy Green | 9f99034 | 2011-02-12 11:57:45 +0000 | [diff] [blame] | 885 | /** |
| 886 | * libwebsocket_service_fd() - Service polled socket with something waiting |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 887 | * @context: Websocket context |
Andy Green | 9f99034 | 2011-02-12 11:57:45 +0000 | [diff] [blame] | 888 | * @pollfd: The pollfd entry describing the socket fd and which events |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 889 | * happened. |
Andy Green | 9f99034 | 2011-02-12 11:57:45 +0000 | [diff] [blame] | 890 | * |
Andy Green | 7500617 | 2013-01-22 12:32:11 +0800 | [diff] [blame] | 891 | * This function takes a pollfd that has POLLIN or POLLOUT activity and |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 892 | * services it according to the state of the associated |
| 893 | * struct libwebsocket. |
Andy Green | 7500617 | 2013-01-22 12:32:11 +0800 | [diff] [blame] | 894 | * |
| 895 | * The one call deals with all "service" that might happen on a socket |
| 896 | * including listen accepts, http files as well as websocket protocol. |
Andy Green | 2577c83 | 2013-03-11 20:37:03 +0800 | [diff] [blame] | 897 | * |
| 898 | * If a pollfd says it has something, you can just pass it to |
| 899 | * libwebsocket_serice_fd() whether it is a socket handled by lws or not. |
| 900 | * If it sees it is a lws socket, the traffic will be handled and |
| 901 | * pollfd->revents will be zeroed now. |
| 902 | * |
| 903 | * If the socket is foreign to lws, it leaves revents alone. So you can |
| 904 | * see if you should service yourself by checking the pollfd revents |
| 905 | * after letting lws try to service it. |
Andy Green | 9f99034 | 2011-02-12 11:57:45 +0000 | [diff] [blame] | 906 | */ |
| 907 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 908 | LWS_VISIBLE int |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 909 | libwebsocket_service_fd(struct libwebsocket_context *context, |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 910 | struct pollfd *pollfd) |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 911 | { |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 912 | struct libwebsocket *wsi; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 913 | int n; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 914 | int m; |
Andy Green | f0b79e2 | 2013-02-10 10:46:45 +0800 | [diff] [blame] | 915 | int listen_socket_fds_index = 0; |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 916 | struct timeval tv; |
Andy Green | 1c6e142 | 2013-02-20 19:11:31 +0800 | [diff] [blame] | 917 | int timed_out = 0; |
| 918 | int our_fd = 0; |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 919 | char draining_flow = 0; |
Andy Green | 6f520a5 | 2013-01-29 17:57:39 +0800 | [diff] [blame] | 920 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 921 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | 2366b1c | 2011-03-06 13:15:31 +0000 | [diff] [blame] | 922 | int more = 1; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 923 | #endif |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 924 | struct lws_tokens eff_buf; |
Andy Green | f0b79e2 | 2013-02-10 10:46:45 +0800 | [diff] [blame] | 925 | |
| 926 | if (context->listen_service_fd) |
| 927 | listen_socket_fds_index = context->lws_lookup[ |
| 928 | context->listen_service_fd]->position_in_fds_table; |
| 929 | |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 930 | /* |
| 931 | * you can call us with pollfd = NULL to just allow the once-per-second |
| 932 | * global timeout checks; if less than a second since the last check |
| 933 | * it returns immediately then. |
| 934 | */ |
| 935 | |
| 936 | gettimeofday(&tv, NULL); |
| 937 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 938 | if (context->last_timeout_check_s != tv.tv_sec) { |
| 939 | context->last_timeout_check_s = tv.tv_sec; |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 940 | |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 941 | #ifndef WIN32 |
Andy Green | 24cba92 | 2013-01-19 13:56:10 +0800 | [diff] [blame] | 942 | /* if our parent went down, don't linger around */ |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 943 | if (context->started_with_parent && |
| 944 | kill(context->started_with_parent, 0) < 0) |
Andy Green | 24cba92 | 2013-01-19 13:56:10 +0800 | [diff] [blame] | 945 | kill(getpid(), SIGTERM); |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 946 | #endif |
Andy Green | 24cba92 | 2013-01-19 13:56:10 +0800 | [diff] [blame] | 947 | |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 948 | /* global timeout check once per second */ |
| 949 | |
Andy Green | 1c6e142 | 2013-02-20 19:11:31 +0800 | [diff] [blame] | 950 | if (pollfd) |
| 951 | our_fd = pollfd->fd; |
| 952 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 953 | for (n = 0; n < context->fds_count; n++) { |
Andy Green | 1c6e142 | 2013-02-20 19:11:31 +0800 | [diff] [blame] | 954 | m = context->fds[n].fd; |
| 955 | wsi = context->lws_lookup[m]; |
| 956 | if (!wsi) |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 957 | continue; |
Andy Green | 1c6e142 | 2013-02-20 19:11:31 +0800 | [diff] [blame] | 958 | |
| 959 | if (libwebsocket_service_timeout_check(context, wsi, |
| 960 | tv.tv_sec)) |
| 961 | /* he did time out... */ |
Andy Green | b1a5e6c | 2013-03-09 12:34:30 +0800 | [diff] [blame] | 962 | if (m == our_fd) { |
Andy Green | 1c6e142 | 2013-02-20 19:11:31 +0800 | [diff] [blame] | 963 | /* it was the guy we came to service! */ |
| 964 | timed_out = 1; |
Andy Green | b1a5e6c | 2013-03-09 12:34:30 +0800 | [diff] [blame] | 965 | /* mark as handled */ |
| 966 | pollfd->revents = 0; |
| 967 | } |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 968 | } |
| 969 | } |
| 970 | |
Andy Green | 1c6e142 | 2013-02-20 19:11:31 +0800 | [diff] [blame] | 971 | /* the socket we came to service timed out, nothing to do */ |
| 972 | if (timed_out) |
| 973 | return 0; |
| 974 | |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 975 | /* just here for timeout management? */ |
Andy Green | a71eafc | 2011-02-14 17:59:43 +0000 | [diff] [blame] | 976 | if (pollfd == NULL) |
| 977 | return 0; |
| 978 | |
| 979 | /* no, here to service a socket descriptor */ |
Andy Green | b1a5e6c | 2013-03-09 12:34:30 +0800 | [diff] [blame] | 980 | wsi = context->lws_lookup[pollfd->fd]; |
| 981 | if (wsi == NULL) |
| 982 | /* not lws connection ... leave revents alone and return */ |
| 983 | return 0; |
| 984 | |
| 985 | /* |
| 986 | * so that caller can tell we handled, past here we need to |
| 987 | * zero down pollfd->revents after handling |
| 988 | */ |
| 989 | |
Andy Green | 65b0e91 | 2013-01-16 07:59:47 +0800 | [diff] [blame] | 990 | /* |
| 991 | * deal with listen service piggybacking |
| 992 | * every listen_service_modulo services of other fds, we |
| 993 | * sneak one in to service the listen socket if there's anything waiting |
| 994 | * |
| 995 | * To handle connection storms, as found in ab, if we previously saw a |
| 996 | * pending connection here, it causes us to check again next time. |
| 997 | */ |
| 998 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 999 | if (context->listen_service_fd && pollfd != |
| 1000 | &context->fds[listen_socket_fds_index]) { |
Andy Green | 65b0e91 | 2013-01-16 07:59:47 +0800 | [diff] [blame] | 1001 | context->listen_service_count++; |
| 1002 | if (context->listen_service_extraseen || |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1003 | context->listen_service_count == |
| 1004 | context->listen_service_modulo) { |
Andy Green | 65b0e91 | 2013-01-16 07:59:47 +0800 | [diff] [blame] | 1005 | context->listen_service_count = 0; |
| 1006 | m = 1; |
| 1007 | if (context->listen_service_extraseen > 5) |
| 1008 | m = 2; |
| 1009 | while (m--) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1010 | /* |
| 1011 | * even with extpoll, we prepared this |
| 1012 | * internal fds for listen |
| 1013 | */ |
| 1014 | n = poll(&context->fds[listen_socket_fds_index], |
| 1015 | 1, 0); |
| 1016 | if (n > 0) { /* there's a conn waiting for us */ |
| 1017 | libwebsocket_service_fd(context, |
| 1018 | &context-> |
| 1019 | fds[listen_socket_fds_index]); |
Andy Green | 65b0e91 | 2013-01-16 07:59:47 +0800 | [diff] [blame] | 1020 | context->listen_service_extraseen++; |
| 1021 | } else { |
| 1022 | if (context->listen_service_extraseen) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1023 | context-> |
| 1024 | listen_service_extraseen--; |
Andy Green | 65b0e91 | 2013-01-16 07:59:47 +0800 | [diff] [blame] | 1025 | break; |
| 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | } |
| 1031 | |
Graham Newton | b1aa1d3 | 2013-11-04 09:47:06 +0800 | [diff] [blame] | 1032 | /* handle session socket closed */ |
| 1033 | |
| 1034 | if ((!(pollfd->revents & POLLIN)) && |
| 1035 | (pollfd->revents & (POLLERR | POLLHUP))) { |
| 1036 | |
| 1037 | lwsl_debug("Session Socket %p (fd=%d) dead\n", |
| 1038 | (void *)wsi, pollfd->fd); |
| 1039 | |
| 1040 | goto close_and_handled; |
| 1041 | } |
| 1042 | |
Andy Green | 65b0e91 | 2013-01-16 07:59:47 +0800 | [diff] [blame] | 1043 | /* okay, what we came here to do... */ |
| 1044 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1045 | switch (wsi->mode) { |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 1046 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1047 | #ifndef LWS_NO_SERVER |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 1048 | case LWS_CONNMODE_HTTP_SERVING: |
Andy Green | 7cf6cb0 | 2013-05-19 14:04:10 +0800 | [diff] [blame] | 1049 | case LWS_CONNMODE_HTTP_SERVING_ACCEPTED: |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1050 | case LWS_CONNMODE_SERVER_LISTENER: |
Andy Green | e216071 | 2013-01-28 12:19:10 +0800 | [diff] [blame] | 1051 | case LWS_CONNMODE_SSL_ACK_PENDING: |
Andy Green | b1a5e6c | 2013-03-09 12:34:30 +0800 | [diff] [blame] | 1052 | n = lws_server_socket_service(context, wsi, pollfd); |
| 1053 | goto handled; |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1054 | #endif |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1055 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1056 | case LWS_CONNMODE_WS_SERVING: |
| 1057 | case LWS_CONNMODE_WS_CLIENT: |
| 1058 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1059 | /* the guy requested a callback when it was OK to write */ |
| 1060 | |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 1061 | if ((pollfd->revents & POLLOUT) && |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 1062 | wsi->state == WSI_STATE_ESTABLISHED && |
| 1063 | lws_handle_POLLOUT_event(context, wsi, pollfd) < 0) { |
Andy Green | 91f19d8 | 2013-12-21 11:18:34 +0800 | [diff] [blame] | 1064 | lwsl_info("libwebsocket_service_fd: closing\n"); |
| 1065 | goto close_and_handled; |
Graham Newton | b1aa1d3 | 2013-11-04 09:47:06 +0800 | [diff] [blame] | 1066 | } |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1067 | |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 1068 | if (wsi->u.ws.rxflow_buffer && |
| 1069 | (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)) { |
| 1070 | lwsl_info("draining rxflow\n"); |
| 1071 | /* well, drain it */ |
| 1072 | eff_buf.token = (char *)wsi->u.ws.rxflow_buffer + |
| 1073 | wsi->u.ws.rxflow_pos; |
| 1074 | eff_buf.token_len = wsi->u.ws.rxflow_len - |
| 1075 | wsi->u.ws.rxflow_pos; |
| 1076 | draining_flow = 1; |
| 1077 | goto drain; |
| 1078 | } |
| 1079 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1080 | /* any incoming data ready? */ |
| 1081 | |
| 1082 | if (!(pollfd->revents & POLLIN)) |
| 1083 | break; |
| 1084 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1085 | #ifdef LWS_OPENSSL_SUPPORT |
David Galeano | 7ffbe1b | 2013-01-10 10:35:32 +0800 | [diff] [blame] | 1086 | read_pending: |
Andy Green | 467c7ef | 2013-01-30 12:28:34 +0800 | [diff] [blame] | 1087 | if (wsi->ssl) { |
Andy Green | e84652c | 2013-02-08 13:01:02 +0800 | [diff] [blame] | 1088 | eff_buf.token_len = SSL_read(wsi->ssl, |
| 1089 | context->service_buffer, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1090 | sizeof(context->service_buffer)); |
Andy Green | 467c7ef | 2013-01-30 12:28:34 +0800 | [diff] [blame] | 1091 | if (!eff_buf.token_len) { |
| 1092 | n = SSL_get_error(wsi->ssl, eff_buf.token_len); |
Andy Green | e84652c | 2013-02-08 13:01:02 +0800 | [diff] [blame] | 1093 | lwsl_err("SSL_read returned 0 with reason %s\n", |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1094 | ERR_error_string(n, |
| 1095 | (char *)context->service_buffer)); |
Andy Green | 467c7ef | 2013-01-30 12:28:34 +0800 | [diff] [blame] | 1096 | } |
| 1097 | } else |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1098 | #endif |
Andy Green | e84652c | 2013-02-08 13:01:02 +0800 | [diff] [blame] | 1099 | eff_buf.token_len = recv(pollfd->fd, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1100 | context->service_buffer, |
| 1101 | sizeof(context->service_buffer), 0); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1102 | |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 1103 | if (eff_buf.token_len < 0) { |
Andy Green | 98ba9e0 | 2013-03-23 09:44:47 +0800 | [diff] [blame] | 1104 | lwsl_debug("service_fd read ret = %d, errno = %d\n", |
| 1105 | eff_buf.token_len, errno); |
Alon Levy | dc93b7f | 2012-10-19 11:21:57 +0200 | [diff] [blame] | 1106 | if (errno != EINTR && errno != EAGAIN) |
Andy Green | b1a5e6c | 2013-03-09 12:34:30 +0800 | [diff] [blame] | 1107 | goto close_and_handled; |
| 1108 | n = 0; |
| 1109 | goto handled; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1110 | } |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 1111 | if (!eff_buf.token_len) { |
Andy Green | 98ba9e0 | 2013-03-23 09:44:47 +0800 | [diff] [blame] | 1112 | lwsl_info("service_fd: closing due to 0 length read\n"); |
Andy Green | b1a5e6c | 2013-03-09 12:34:30 +0800 | [diff] [blame] | 1113 | goto close_and_handled; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 1116 | /* |
| 1117 | * give any active extensions a chance to munge the buffer |
| 1118 | * before parse. We pass in a pointer to an lws_tokens struct |
| 1119 | * prepared with the default buffer and content length that's in |
| 1120 | * there. Rather than rewrite the default buffer, extensions |
| 1121 | * that expect to grow the buffer can adapt .token to |
| 1122 | * point to their own per-connection buffer in the extension |
| 1123 | * user allocation. By default with no extensions or no |
| 1124 | * extension callback handling, just the normal input buffer is |
| 1125 | * used then so it is efficient. |
| 1126 | */ |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1127 | |
Andy Green | e84652c | 2013-02-08 13:01:02 +0800 | [diff] [blame] | 1128 | eff_buf.token = (char *)context->service_buffer; |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 1129 | drain: |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1130 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 1131 | more = 1; |
| 1132 | while (more) { |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1133 | |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 1134 | more = 0; |
| 1135 | |
| 1136 | for (n = 0; n < wsi->count_active_extensions; n++) { |
Andy Green | 46c2ea0 | 2011-03-22 09:04:01 +0000 | [diff] [blame] | 1137 | m = wsi->active_extensions[n]->callback(context, |
| 1138 | wsi->active_extensions[n], wsi, |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 1139 | LWS_EXT_CALLBACK_PACKET_RX_PREPARSE, |
Andy Green | 46c2ea0 | 2011-03-22 09:04:01 +0000 | [diff] [blame] | 1140 | wsi->active_extensions_user[n], |
| 1141 | &eff_buf, 0); |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 1142 | if (m < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1143 | lwsl_ext( |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1144 | "Extension reports fatal error\n"); |
Andy Green | b1a5e6c | 2013-03-09 12:34:30 +0800 | [diff] [blame] | 1145 | goto close_and_handled; |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 1146 | } |
| 1147 | if (m) |
| 1148 | more = 1; |
| 1149 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1150 | #endif |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 1151 | /* service incoming data */ |
| 1152 | |
| 1153 | if (eff_buf.token_len) { |
| 1154 | n = libwebsocket_read(context, wsi, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1155 | (unsigned char *)eff_buf.token, |
| 1156 | eff_buf.token_len); |
Andy Green | b1a5e6c | 2013-03-09 12:34:30 +0800 | [diff] [blame] | 1157 | if (n < 0) { |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 1158 | /* we closed wsi */ |
Andy Green | b1a5e6c | 2013-03-09 12:34:30 +0800 | [diff] [blame] | 1159 | n = 0; |
| 1160 | goto handled; |
| 1161 | } |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 1162 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1163 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 1164 | eff_buf.token = NULL; |
| 1165 | eff_buf.token_len = 0; |
| 1166 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1167 | #endif |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 1168 | if (draining_flow && wsi->u.ws.rxflow_buffer && |
| 1169 | wsi->u.ws.rxflow_pos == wsi->u.ws.rxflow_len) { |
| 1170 | lwsl_info("flow buffer: drained\n"); |
| 1171 | free(wsi->u.ws.rxflow_buffer); |
| 1172 | wsi->u.ws.rxflow_buffer = NULL; |
| 1173 | /* having drained the rxflow buffer, can rearm POLLIN */ |
| 1174 | _libwebsocket_rx_flow_control(wsi); |
| 1175 | } |
David Galeano | 7ffbe1b | 2013-01-10 10:35:32 +0800 | [diff] [blame] | 1176 | |
| 1177 | #ifdef LWS_OPENSSL_SUPPORT |
| 1178 | if (wsi->ssl && SSL_pending(wsi->ssl)) |
| 1179 | goto read_pending; |
| 1180 | #endif |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 1181 | break; |
Andy Green | 76f61e7 | 2013-01-16 11:53:05 +0800 | [diff] [blame] | 1182 | |
| 1183 | default: |
Andy Green | 03674a6 | 2013-01-16 11:47:40 +0800 | [diff] [blame] | 1184 | #ifdef LWS_NO_CLIENT |
| 1185 | break; |
| 1186 | #else |
Andy Green | b1a5e6c | 2013-03-09 12:34:30 +0800 | [diff] [blame] | 1187 | n = lws_client_socket_service(context, wsi, pollfd); |
| 1188 | goto handled; |
Andy Green | 03674a6 | 2013-01-16 11:47:40 +0800 | [diff] [blame] | 1189 | #endif |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Andy Green | b1a5e6c | 2013-03-09 12:34:30 +0800 | [diff] [blame] | 1192 | n = 0; |
| 1193 | goto handled; |
| 1194 | |
| 1195 | close_and_handled: |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 1196 | libwebsocket_close_and_free_session(context, wsi, |
| 1197 | LWS_CLOSE_STATUS_NOSTATUS); |
| 1198 | n = 1; |
Andy Green | b1a5e6c | 2013-03-09 12:34:30 +0800 | [diff] [blame] | 1199 | |
| 1200 | handled: |
| 1201 | pollfd->revents = 0; |
| 1202 | return n; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1205 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1206 | /** |
| 1207 | * libwebsocket_context_destroy() - Destroy the websocket context |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1208 | * @context: Websocket context |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1209 | * |
| 1210 | * This function closes any active connections and then frees the |
| 1211 | * context. After calling this, any further use of the context is |
| 1212 | * undefined. |
| 1213 | */ |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1214 | LWS_VISIBLE void |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1215 | libwebsocket_context_destroy(struct libwebsocket_context *context) |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1216 | { |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1217 | int n; |
arnaudviala | 7c6f26c | 2014-02-15 14:39:40 +0800 | [diff] [blame] | 1218 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1219 | int m; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1220 | struct libwebsocket_extension *ext; |
arnaudviala | 7c6f26c | 2014-02-15 14:39:40 +0800 | [diff] [blame] | 1221 | #endif /* ndef LWS_NO_EXTENSIONS */ |
Andy Green | a7109e6 | 2013-02-11 12:05:54 +0800 | [diff] [blame] | 1222 | struct libwebsocket_protocols *protocol = context->protocols; |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1223 | |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 1224 | #ifdef LWS_LATENCY |
| 1225 | if (context->worst_latency_info[0]) |
| 1226 | lwsl_notice("Worst latency: %s\n", context->worst_latency_info); |
| 1227 | #endif |
| 1228 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1229 | for (n = 0; n < context->fds_count; n++) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1230 | struct libwebsocket *wsi = |
| 1231 | context->lws_lookup[context->fds[n].fd]; |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1232 | libwebsocket_close_and_free_session(context, |
Andy Green | 7b92205 | 2013-02-11 11:43:05 +0800 | [diff] [blame] | 1233 | wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */); |
| 1234 | n--; |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1235 | } |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1236 | |
arnaudviala | 7c6f26c | 2014-02-15 14:39:40 +0800 | [diff] [blame] | 1237 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1238 | /* |
| 1239 | * give all extensions a chance to clean up any per-context |
| 1240 | * allocations they might have made |
| 1241 | */ |
| 1242 | |
| 1243 | ext = context->extensions; |
| 1244 | m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT; |
| 1245 | if (context->listen_port) |
| 1246 | m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT; |
Paulo Roberto Urio | 1f680ab | 2012-06-04 08:40:28 +0800 | [diff] [blame] | 1247 | while (ext && ext->callback) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1248 | ext->callback(context, ext, NULL, |
| 1249 | (enum libwebsocket_extension_callback_reasons)m, |
| 1250 | NULL, NULL, 0); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1251 | ext++; |
| 1252 | } |
arnaudviala | 7c6f26c | 2014-02-15 14:39:40 +0800 | [diff] [blame] | 1253 | #endif /* ndef LWS_NO_EXTENSIONS */ |
Andy Green | a7109e6 | 2013-02-11 12:05:54 +0800 | [diff] [blame] | 1254 | |
| 1255 | /* |
| 1256 | * inform all the protocols that they are done and will have no more |
| 1257 | * callbacks |
| 1258 | */ |
| 1259 | |
| 1260 | while (protocol->callback) { |
| 1261 | protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY, |
| 1262 | NULL, NULL, 0); |
| 1263 | protocol++; |
| 1264 | } |
| 1265 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1266 | |
Andreas Pakulat | 68bd4bd | 2013-10-28 15:18:04 +0100 | [diff] [blame] | 1267 | #if defined(WIN32) || defined(_WIN32) |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1268 | #else |
| 1269 | close(context->fd_random); |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1270 | #endif |
| 1271 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1272 | #ifdef LWS_OPENSSL_SUPPORT |
| 1273 | if (context->ssl_ctx) |
| 1274 | SSL_CTX_free(context->ssl_ctx); |
| 1275 | if (context->ssl_client_ctx) |
| 1276 | SSL_CTX_free(context->ssl_client_ctx); |
Andy Green | ad68639 | 2013-02-11 14:50:45 +0800 | [diff] [blame] | 1277 | |
| 1278 | ERR_remove_state(0); |
| 1279 | ERR_free_strings(); |
| 1280 | EVP_cleanup(); |
| 1281 | CRYPTO_cleanup_all_ex_data(); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1282 | #endif |
| 1283 | |
Andy Green | 4659648 | 2013-02-11 11:04:01 +0800 | [diff] [blame] | 1284 | if (context->fds) |
| 1285 | free(context->fds); |
| 1286 | if (context->lws_lookup) |
| 1287 | free(context->lws_lookup); |
| 1288 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1289 | free(context); |
| 1290 | |
Andreas Pakulat | 68bd4bd | 2013-10-28 15:18:04 +0100 | [diff] [blame] | 1291 | #if defined(WIN32) || defined(_WIN32) |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1292 | WSACleanup(); |
| 1293 | #endif |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
Andy Green | d88146d | 2013-01-22 12:40:35 +0800 | [diff] [blame] | 1296 | /** |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1297 | * libwebsocket_context_user() - get the user data associated with the context |
Andy Green | d88146d | 2013-01-22 12:40:35 +0800 | [diff] [blame] | 1298 | * @context: Websocket context |
| 1299 | * |
| 1300 | * This returns the optional user allocation that can be attached to |
| 1301 | * the context the sockets live in at context_create time. It's a way |
| 1302 | * to let all sockets serviced in the same context share data without |
| 1303 | * using globals statics in the user code. |
| 1304 | */ |
Alon Levy | 0291eb3 | 2012-10-19 11:21:56 +0200 | [diff] [blame] | 1305 | LWS_EXTERN void * |
| 1306 | libwebsocket_context_user(struct libwebsocket_context *context) |
| 1307 | { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1308 | return context->user_space; |
Alon Levy | 0291eb3 | 2012-10-19 11:21:56 +0200 | [diff] [blame] | 1309 | } |
| 1310 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1311 | /** |
| 1312 | * libwebsocket_service() - Service any pending websocket activity |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1313 | * @context: Websocket context |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1314 | * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed |
| 1315 | * service otherwise block and service immediately, returning |
| 1316 | * after the timeout if nothing needed service. |
| 1317 | * |
| 1318 | * This function deals with any pending websocket traffic, for three |
| 1319 | * kinds of event. It handles these events on both server and client |
| 1320 | * types of connection the same. |
| 1321 | * |
| 1322 | * 1) Accept new connections to our context's server |
| 1323 | * |
Andy Green | 6f520a5 | 2013-01-29 17:57:39 +0800 | [diff] [blame] | 1324 | * 2) Call the receive callback for incoming frame data received by |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1325 | * server or client connections. |
| 1326 | * |
| 1327 | * You need to call this service function periodically to all the above |
| 1328 | * functions to happen; if your application is single-threaded you can |
| 1329 | * just call it in your main event loop. |
| 1330 | * |
| 1331 | * Alternatively you can fork a new process that asynchronously handles |
| 1332 | * calling this service in a loop. In that case you are happy if this |
| 1333 | * call blocks your thread until it needs to take care of something and |
| 1334 | * would call it with a large nonzero timeout. Your loop then takes no |
| 1335 | * CPU while there is nothing happening. |
| 1336 | * |
| 1337 | * If you are calling it in a single-threaded app, you don't want it to |
| 1338 | * wait around blocking other things in your loop from happening, so you |
| 1339 | * would call it with a timeout_ms of 0, so it returns immediately if |
| 1340 | * nothing is pending, or as soon as it services whatever was pending. |
| 1341 | */ |
| 1342 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1343 | LWS_VISIBLE int |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1344 | libwebsocket_service(struct libwebsocket_context *context, int timeout_ms) |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1345 | { |
| 1346 | int n; |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 1347 | int m; |
Andy Green | 3b3fa9e | 2013-12-25 16:34:37 +0800 | [diff] [blame] | 1348 | #ifdef LWS_HAS_PPOLL |
| 1349 | struct timespec timeout_ts; |
| 1350 | sigset_t sigmask; |
| 1351 | #endif |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1352 | |
| 1353 | /* stay dead once we are dead */ |
| 1354 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1355 | if (context == NULL) |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1356 | return 1; |
| 1357 | |
Andy Green | 3b3fa9e | 2013-12-25 16:34:37 +0800 | [diff] [blame] | 1358 | #ifdef LWS_HAS_PPOLL |
| 1359 | lws_idling_ppoll_tid = context->protocols[0].callback(context, NULL, |
| 1360 | LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0); |
| 1361 | |
| 1362 | timeout_ts.tv_sec = timeout_ms / 1000; |
| 1363 | timeout_ts.tv_nsec = timeout_ms % 1000; |
Andy Green | ed451d5 | 2014-01-11 12:37:07 +0800 | [diff] [blame] | 1364 | |
| 1365 | sigprocmask(SIG_BLOCK, NULL, &sigmask); |
| 1366 | sigdelset(&sigmask, SIGUSR2); |
Andy Green | 3b3fa9e | 2013-12-25 16:34:37 +0800 | [diff] [blame] | 1367 | |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1368 | /* wait for something to need service */ |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1369 | |
Andy Green | 3b3fa9e | 2013-12-25 16:34:37 +0800 | [diff] [blame] | 1370 | n = ppoll(context->fds, context->fds_count, &timeout_ts, &sigmask); |
| 1371 | lws_idling_ppoll_tid = 0; |
| 1372 | #else |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1373 | n = poll(context->fds, context->fds_count, timeout_ms); |
Andy Green | 3b3fa9e | 2013-12-25 16:34:37 +0800 | [diff] [blame] | 1374 | #endif |
Andy Green | 5dc62ea | 2013-09-20 20:26:12 +0800 | [diff] [blame] | 1375 | if (n == 0) /* poll timeout */ { |
| 1376 | libwebsocket_service_fd(context, NULL); |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 1377 | return 0; |
Andy Green | 5dc62ea | 2013-09-20 20:26:12 +0800 | [diff] [blame] | 1378 | } |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1379 | |
Andy Green | ed451d5 | 2014-01-11 12:37:07 +0800 | [diff] [blame] | 1380 | if (n < 0) { |
| 1381 | if (errno != EINTR) |
| 1382 | return -1; |
| 1383 | else |
| 1384 | return 0; |
| 1385 | } |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1386 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1387 | /* any socket with events to service? */ |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1388 | |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 1389 | for (n = 0; n < context->fds_count; n++) { |
| 1390 | if (!context->fds[n].revents) |
| 1391 | continue; |
| 1392 | m = libwebsocket_service_fd(context, &context->fds[n]); |
| 1393 | if (m < 0) |
| 1394 | return -1; |
| 1395 | /* if something closed, retry this slot */ |
| 1396 | if (m) |
| 1397 | n--; |
| 1398 | } |
| 1399 | |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1400 | return 0; |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1403 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1404 | int |
| 1405 | lws_any_extension_handled(struct libwebsocket_context *context, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1406 | struct libwebsocket *wsi, |
| 1407 | enum libwebsocket_extension_callback_reasons r, |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1408 | void *v, size_t len) |
| 1409 | { |
| 1410 | int n; |
| 1411 | int handled = 0; |
| 1412 | |
| 1413 | /* maybe an extension will take care of it for us */ |
| 1414 | |
| 1415 | for (n = 0; n < wsi->count_active_extensions && !handled; n++) { |
| 1416 | if (!wsi->active_extensions[n]->callback) |
| 1417 | continue; |
| 1418 | |
| 1419 | handled |= wsi->active_extensions[n]->callback(context, |
| 1420 | wsi->active_extensions[n], wsi, |
| 1421 | r, wsi->active_extensions_user[n], v, len); |
| 1422 | } |
| 1423 | |
| 1424 | return handled; |
| 1425 | } |
| 1426 | |
| 1427 | |
| 1428 | void * |
| 1429 | lws_get_extension_user_matching_ext(struct libwebsocket *wsi, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1430 | struct libwebsocket_extension *ext) |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1431 | { |
| 1432 | int n = 0; |
| 1433 | |
Andy Green | 68b4504 | 2011-05-25 21:41:57 +0100 | [diff] [blame] | 1434 | if (wsi == NULL) |
| 1435 | return NULL; |
| 1436 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1437 | while (n < wsi->count_active_extensions) { |
| 1438 | if (wsi->active_extensions[n] != ext) { |
| 1439 | n++; |
| 1440 | continue; |
| 1441 | } |
| 1442 | return wsi->active_extensions_user[n]; |
| 1443 | } |
| 1444 | |
| 1445 | return NULL; |
| 1446 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1447 | #endif |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1448 | |
Andy Green | 91f19d8 | 2013-12-21 11:18:34 +0800 | [diff] [blame] | 1449 | void |
| 1450 | lws_change_pollfd(struct libwebsocket *wsi, int _and, int _or) |
| 1451 | { |
| 1452 | struct libwebsocket_context *context = wsi->protocol->owning_server; |
Andy Green | 3b3fa9e | 2013-12-25 16:34:37 +0800 | [diff] [blame] | 1453 | int events; |
| 1454 | #ifdef LWS_HAS_PPOLL |
| 1455 | int tid; |
| 1456 | int sampled_ppoll_tid; |
| 1457 | #endif |
Andy Green | 91f19d8 | 2013-12-21 11:18:34 +0800 | [diff] [blame] | 1458 | |
| 1459 | context->protocols[0].callback(context, wsi, |
| 1460 | LWS_CALLBACK_LOCK_POLL, |
| 1461 | wsi->user_space, (void *)(long)wsi->sock, 0); |
| 1462 | |
Andy Green | 3b3fa9e | 2013-12-25 16:34:37 +0800 | [diff] [blame] | 1463 | events = context->fds[wsi->position_in_fds_table].events; |
| 1464 | |
| 1465 | context->fds[wsi->position_in_fds_table].events = (events & ~_and) | _or; |
Andy Green | 91f19d8 | 2013-12-21 11:18:34 +0800 | [diff] [blame] | 1466 | |
| 1467 | /* external POLL support via protocol 0 */ |
| 1468 | if (_and) |
| 1469 | context->protocols[0].callback(context, wsi, |
| 1470 | LWS_CALLBACK_CLEAR_MODE_POLL_FD, |
| 1471 | wsi->user_space, (void *)(long)wsi->sock, _and); |
| 1472 | |
| 1473 | if (_or) |
| 1474 | context->protocols[0].callback(context, wsi, |
| 1475 | LWS_CALLBACK_SET_MODE_POLL_FD, |
| 1476 | wsi->user_space, (void *)(long)wsi->sock, _or); |
| 1477 | |
Andy Green | 3b3fa9e | 2013-12-25 16:34:37 +0800 | [diff] [blame] | 1478 | #ifdef LWS_HAS_PPOLL |
| 1479 | /* |
| 1480 | * if we changed something in this pollfd... |
| 1481 | * ... and we're running in a different thread context |
| 1482 | * than the service thread... |
| 1483 | * ... and the service thread is waiting in ppoll()... |
| 1484 | * then fire a SIGUSR2 at the service thread to force it to |
| 1485 | * restart the ppoll() with our changed events |
| 1486 | */ |
| 1487 | if (events != context->fds[wsi->position_in_fds_table].events) { |
| 1488 | sampled_ppoll_tid = lws_idling_ppoll_tid; |
| 1489 | if (sampled_ppoll_tid) { |
| 1490 | tid = context->protocols[0].callback(context, NULL, |
| 1491 | LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0); |
| 1492 | if (tid != sampled_ppoll_tid) |
| 1493 | kill(sampled_ppoll_tid, SIGUSR2); |
| 1494 | } |
| 1495 | } |
| 1496 | #endif |
| 1497 | |
Andy Green | 91f19d8 | 2013-12-21 11:18:34 +0800 | [diff] [blame] | 1498 | context->protocols[0].callback(context, wsi, |
| 1499 | LWS_CALLBACK_UNLOCK_POLL, |
| 1500 | wsi->user_space, (void *)(long)wsi->sock, 0); |
| 1501 | } |
| 1502 | |
| 1503 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1504 | /** |
| 1505 | * libwebsocket_callback_on_writable() - Request a callback when this socket |
| 1506 | * becomes able to be written to without |
| 1507 | * blocking |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 1508 | * |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1509 | * @context: libwebsockets context |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1510 | * @wsi: Websocket connection instance to get callback for |
| 1511 | */ |
| 1512 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1513 | LWS_VISIBLE int |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1514 | libwebsocket_callback_on_writable(struct libwebsocket_context *context, |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1515 | struct libwebsocket *wsi) |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1516 | { |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1517 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1518 | int n; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1519 | int handled = 0; |
| 1520 | |
| 1521 | /* maybe an extension will take care of it for us */ |
| 1522 | |
| 1523 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 1524 | if (!wsi->active_extensions[n]->callback) |
| 1525 | continue; |
| 1526 | |
| 1527 | handled |= wsi->active_extensions[n]->callback(context, |
| 1528 | wsi->active_extensions[n], wsi, |
| 1529 | LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE, |
| 1530 | wsi->active_extensions_user[n], NULL, 0); |
| 1531 | } |
| 1532 | |
| 1533 | if (handled) |
| 1534 | return 1; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1535 | #endif |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1536 | if (wsi->position_in_fds_table < 0) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1537 | lwsl_err("libwebsocket_callback_on_writable: failed to find socket %d\n", |
| 1538 | wsi->sock); |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1539 | return -1; |
| 1540 | } |
| 1541 | |
Andy Green | 91f19d8 | 2013-12-21 11:18:34 +0800 | [diff] [blame] | 1542 | lws_change_pollfd(wsi, 0, POLLOUT); |
Andy Green | 7a13279 | 2013-12-18 09:48:26 +0800 | [diff] [blame] | 1543 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1544 | return 1; |
| 1545 | } |
| 1546 | |
| 1547 | /** |
| 1548 | * libwebsocket_callback_on_writable_all_protocol() - Request a callback for |
| 1549 | * all connections using the given protocol when it |
| 1550 | * becomes possible to write to each socket without |
| 1551 | * blocking in turn. |
| 1552 | * |
| 1553 | * @protocol: Protocol whose connections will get callbacks |
| 1554 | */ |
| 1555 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1556 | LWS_VISIBLE int |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1557 | libwebsocket_callback_on_writable_all_protocol( |
| 1558 | const struct libwebsocket_protocols *protocol) |
| 1559 | { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1560 | struct libwebsocket_context *context = protocol->owning_server; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1561 | int n; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1562 | struct libwebsocket *wsi; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1563 | |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1564 | for (n = 0; n < context->fds_count; n++) { |
| 1565 | wsi = context->lws_lookup[context->fds[n].fd]; |
| 1566 | if (!wsi) |
| 1567 | continue; |
| 1568 | if (wsi->protocol == protocol) |
| 1569 | libwebsocket_callback_on_writable(context, wsi); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1570 | } |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1571 | |
| 1572 | return 0; |
| 1573 | } |
| 1574 | |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1575 | /** |
Andy Green | e39e6ef | 2014-02-15 16:36:38 +0800 | [diff] [blame] | 1576 | * libwebsocket_callback_all_protocol() - Callback all connections using |
| 1577 | * the given protocol with the given reason |
| 1578 | * |
| 1579 | * @protocol: Protocol whose connections will get callbacks |
| 1580 | * @reason: Callback reason index |
| 1581 | */ |
| 1582 | |
| 1583 | LWS_VISIBLE int |
| 1584 | libwebsocket_callback_all_protocol( |
| 1585 | const struct libwebsocket_protocols *protocol, int reason) |
| 1586 | { |
| 1587 | struct libwebsocket_context *context = protocol->owning_server; |
| 1588 | int n; |
| 1589 | struct libwebsocket *wsi; |
| 1590 | |
| 1591 | for (n = 0; n < context->fds_count; n++) { |
| 1592 | wsi = context->lws_lookup[context->fds[n].fd]; |
| 1593 | if (!wsi) |
| 1594 | continue; |
| 1595 | if (wsi->protocol == protocol) |
| 1596 | protocol->callback(context, wsi, |
| 1597 | reason, wsi->user_space, NULL, 0); |
| 1598 | } |
| 1599 | |
| 1600 | return 0; |
| 1601 | } |
| 1602 | |
| 1603 | /** |
Andy Green | be93fef | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 1604 | * libwebsocket_set_timeout() - marks the wsi as subject to a timeout |
| 1605 | * |
| 1606 | * You will not need this unless you are doing something special |
| 1607 | * |
| 1608 | * @wsi: Websocket connection instance |
| 1609 | * @reason: timeout reason |
| 1610 | * @secs: how many seconds |
| 1611 | */ |
| 1612 | |
| 1613 | void |
| 1614 | libwebsocket_set_timeout(struct libwebsocket *wsi, |
| 1615 | enum pending_timeout reason, int secs) |
| 1616 | { |
| 1617 | struct timeval tv; |
| 1618 | |
| 1619 | gettimeofday(&tv, NULL); |
| 1620 | |
| 1621 | wsi->pending_timeout_limit = tv.tv_sec + secs; |
| 1622 | wsi->pending_timeout = reason; |
| 1623 | } |
| 1624 | |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 1625 | |
| 1626 | /** |
| 1627 | * libwebsocket_get_socket_fd() - returns the socket file descriptor |
| 1628 | * |
| 1629 | * You will not need this unless you are doing something special |
| 1630 | * |
| 1631 | * @wsi: Websocket connection instance |
| 1632 | */ |
| 1633 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1634 | LWS_VISIBLE int |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 1635 | libwebsocket_get_socket_fd(struct libwebsocket *wsi) |
| 1636 | { |
| 1637 | return wsi->sock; |
| 1638 | } |
| 1639 | |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 1640 | #ifdef LWS_LATENCY |
| 1641 | void |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1642 | lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi, |
| 1643 | const char *action, int ret, int completed) |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 1644 | { |
| 1645 | struct timeval tv; |
| 1646 | unsigned long u; |
| 1647 | char buf[256]; |
| 1648 | |
| 1649 | gettimeofday(&tv, NULL); |
| 1650 | |
| 1651 | u = (tv.tv_sec * 1000000) + tv.tv_usec; |
| 1652 | |
| 1653 | if (action) { |
| 1654 | if (completed) { |
| 1655 | if (wsi->action_start == wsi->latency_start) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1656 | sprintf(buf, |
| 1657 | "Completion first try lat %luus: %p: ret %d: %s\n", |
| 1658 | u - wsi->latency_start, |
| 1659 | (void *)wsi, ret, action); |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 1660 | else |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1661 | sprintf(buf, |
| 1662 | "Completion %luus: lat %luus: %p: ret %d: %s\n", |
| 1663 | u - wsi->action_start, |
| 1664 | u - wsi->latency_start, |
| 1665 | (void *)wsi, ret, action); |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 1666 | wsi->action_start = 0; |
| 1667 | } else |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1668 | sprintf(buf, "lat %luus: %p: ret %d: %s\n", |
| 1669 | u - wsi->latency_start, |
| 1670 | (void *)wsi, ret, action); |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 1671 | if (u - wsi->latency_start > context->worst_latency) { |
| 1672 | context->worst_latency = u - wsi->latency_start; |
| 1673 | strcpy(context->worst_latency_info, buf); |
| 1674 | } |
| 1675 | lwsl_latency("%s", buf); |
| 1676 | } else { |
| 1677 | wsi->latency_start = u; |
| 1678 | if (!wsi->action_start) |
| 1679 | wsi->action_start = u; |
| 1680 | } |
| 1681 | } |
| 1682 | #endif |
| 1683 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1684 | #ifdef LWS_NO_SERVER |
| 1685 | int |
Andy Green | 8d15cf4 | 2013-10-24 21:47:06 +0800 | [diff] [blame] | 1686 | _libwebsocket_rx_flow_control(struct libwebsocket *wsi) |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1687 | { |
| 1688 | return 0; |
| 1689 | } |
| 1690 | #else |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1691 | int |
| 1692 | _libwebsocket_rx_flow_control(struct libwebsocket *wsi) |
| 1693 | { |
| 1694 | struct libwebsocket_context *context = wsi->protocol->owning_server; |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1695 | |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 1696 | /* there is no pending change */ |
| 1697 | if (!(wsi->u.ws.rxflow_change_to & LWS_RXFLOW_PENDING_CHANGE)) |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1698 | return 0; |
| 1699 | |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 1700 | /* stuff is still buffered, not ready to really accept new input */ |
| 1701 | if (wsi->u.ws.rxflow_buffer) { |
| 1702 | /* get ourselves called back to deal with stashed buffer */ |
| 1703 | libwebsocket_callback_on_writable(context, wsi); |
| 1704 | return 0; |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1705 | } |
| 1706 | |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 1707 | /* pending is cleared, we can change rxflow state */ |
| 1708 | |
| 1709 | wsi->u.ws.rxflow_change_to &= ~LWS_RXFLOW_PENDING_CHANGE; |
| 1710 | |
| 1711 | lwsl_info("rxflow: wsi %p change_to %d\n", wsi, |
| 1712 | wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW); |
| 1713 | |
| 1714 | /* adjust the pollfd for this wsi */ |
| 1715 | |
| 1716 | if (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW) |
Andy Green | 91f19d8 | 2013-12-21 11:18:34 +0800 | [diff] [blame] | 1717 | lws_change_pollfd(wsi, 0, POLLIN); |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1718 | else |
Andy Green | 91f19d8 | 2013-12-21 11:18:34 +0800 | [diff] [blame] | 1719 | lws_change_pollfd(wsi, POLLIN, 0); |
Andy Green | 7a13279 | 2013-12-18 09:48:26 +0800 | [diff] [blame] | 1720 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1721 | return 1; |
| 1722 | } |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 1723 | #endif |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1724 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1725 | /** |
| 1726 | * libwebsocket_rx_flow_control() - Enable and disable socket servicing for |
| 1727 | * receieved packets. |
| 1728 | * |
| 1729 | * If the output side of a server process becomes choked, this allows flow |
| 1730 | * control for the input side. |
| 1731 | * |
| 1732 | * @wsi: Websocket connection instance to get callback for |
| 1733 | * @enable: 0 = disable read servicing for this connection, 1 = enable |
| 1734 | */ |
| 1735 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1736 | LWS_VISIBLE int |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1737 | libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable) |
| 1738 | { |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 1739 | if (enable == (wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)) |
| 1740 | return 0; |
| 1741 | |
| 1742 | lwsl_info("libwebsocket_rx_flow_control(0x%p, %d)\n", wsi, enable); |
| 1743 | wsi->u.ws.rxflow_change_to = LWS_RXFLOW_PENDING_CHANGE | !!enable; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1744 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1745 | return 0; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1746 | } |
| 1747 | |
Andy Green | b55451c | 2013-03-16 12:32:27 +0800 | [diff] [blame] | 1748 | /** |
| 1749 | * libwebsocket_rx_flow_allow_all_protocol() - Allow all connections with this protocol to receive |
| 1750 | * |
| 1751 | * When the user server code realizes it can accept more input, it can |
| 1752 | * call this to have the RX flow restriction removed from all connections using |
| 1753 | * the given protocol. |
| 1754 | * |
| 1755 | * @protocol: all connections using this protocol will be allowed to receive |
| 1756 | */ |
| 1757 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1758 | LWS_VISIBLE void |
Andy Green | b55451c | 2013-03-16 12:32:27 +0800 | [diff] [blame] | 1759 | libwebsocket_rx_flow_allow_all_protocol( |
| 1760 | const struct libwebsocket_protocols *protocol) |
| 1761 | { |
| 1762 | struct libwebsocket_context *context = protocol->owning_server; |
| 1763 | int n; |
| 1764 | struct libwebsocket *wsi; |
| 1765 | |
| 1766 | for (n = 0; n < context->fds_count; n++) { |
| 1767 | wsi = context->lws_lookup[context->fds[n].fd]; |
| 1768 | if (!wsi) |
| 1769 | continue; |
| 1770 | if (wsi->protocol == protocol) |
| 1771 | libwebsocket_rx_flow_control(wsi, LWS_RXFLOW_ALLOW); |
| 1772 | } |
| 1773 | } |
| 1774 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1775 | |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 1776 | /** |
| 1777 | * libwebsocket_canonical_hostname() - returns this host's hostname |
| 1778 | * |
| 1779 | * This is typically used by client code to fill in the host parameter |
| 1780 | * when making a client connection. You can only call it after the context |
| 1781 | * has been created. |
| 1782 | * |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1783 | * @context: Websocket context |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 1784 | */ |
| 1785 | |
| 1786 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1787 | LWS_VISIBLE extern const char * |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1788 | libwebsocket_canonical_hostname(struct libwebsocket_context *context) |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 1789 | { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1790 | return (const char *)context->canonical_hostname; |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1794 | static void sigpipe_handler(int x) |
| 1795 | { |
| 1796 | } |
| 1797 | |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 1798 | #ifdef LWS_OPENSSL_SUPPORT |
| 1799 | static int |
| 1800 | OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx) |
| 1801 | { |
| 1802 | |
| 1803 | SSL *ssl; |
| 1804 | int n; |
Andy Green | 2e24da0 | 2011-03-05 16:12:04 +0000 | [diff] [blame] | 1805 | struct libwebsocket_context *context; |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 1806 | |
| 1807 | ssl = X509_STORE_CTX_get_ex_data(x509_ctx, |
| 1808 | SSL_get_ex_data_X509_STORE_CTX_idx()); |
| 1809 | |
| 1810 | /* |
Andy Green | 2e24da0 | 2011-03-05 16:12:04 +0000 | [diff] [blame] | 1811 | * !!! nasty openssl requires the index to come as a library-scope |
| 1812 | * static |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 1813 | */ |
Andy Green | 2e24da0 | 2011-03-05 16:12:04 +0000 | [diff] [blame] | 1814 | context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index); |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1815 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1816 | n = context->protocols[0].callback(NULL, NULL, |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 1817 | LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION, |
| 1818 | x509_ctx, ssl, preverify_ok); |
| 1819 | |
| 1820 | /* convert return code from 0 = OK to 1 = OK */ |
| 1821 | |
| 1822 | if (!n) |
| 1823 | n = 1; |
| 1824 | else |
| 1825 | n = 0; |
| 1826 | |
| 1827 | return n; |
| 1828 | } |
| 1829 | #endif |
| 1830 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1831 | int user_callback_handle_rxflow(callback_function callback_function, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1832 | struct libwebsocket_context *context, |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1833 | struct libwebsocket *wsi, |
| 1834 | enum libwebsocket_callback_reasons reason, void *user, |
| 1835 | void *in, size_t len) |
| 1836 | { |
| 1837 | int n; |
| 1838 | |
| 1839 | n = callback_function(context, wsi, reason, user, in, len); |
Andy Green | aedc953 | 2013-02-10 21:21:24 +0800 | [diff] [blame] | 1840 | if (!n) |
| 1841 | n = _libwebsocket_rx_flow_control(wsi); |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1842 | |
Andy Green | aedc953 | 2013-02-10 21:21:24 +0800 | [diff] [blame] | 1843 | return n; |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1844 | } |
| 1845 | |
Andy Green | 3b3fa9e | 2013-12-25 16:34:37 +0800 | [diff] [blame] | 1846 | /* |
| 1847 | * This is just used to interrupt poll waiting |
| 1848 | * we don't have to do anything with it. |
| 1849 | */ |
arnaudviala | 7c6f26c | 2014-02-15 14:39:40 +0800 | [diff] [blame] | 1850 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | 3b3fa9e | 2013-12-25 16:34:37 +0800 | [diff] [blame] | 1851 | static void lws_sigusr2(int sig) |
| 1852 | { |
| 1853 | } |
arnaudviala | 7c6f26c | 2014-02-15 14:39:40 +0800 | [diff] [blame] | 1854 | #endif |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1855 | |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 1856 | /** |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1857 | * libwebsocket_create_context() - Create the websocket handler |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 1858 | * @info: pointer to struct with parameters |
Andy Green | 05464c6 | 2010-11-12 10:44:18 +0000 | [diff] [blame] | 1859 | * |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 1860 | * This function creates the listening socket (if serving) and takes care |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1861 | * of all initialization in one step. |
| 1862 | * |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1863 | * After initialization, it returns a struct libwebsocket_context * that |
| 1864 | * represents this server. After calling, user code needs to take care |
| 1865 | * of calling libwebsocket_service() with the context pointer to get the |
| 1866 | * server's sockets serviced. This can be done in the same process context |
| 1867 | * or a forked process, or another thread, |
Andy Green | 05464c6 | 2010-11-12 10:44:18 +0000 | [diff] [blame] | 1868 | * |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 1869 | * The protocol callback functions are called for a handful of events |
| 1870 | * including http requests coming in, websocket connections becoming |
| 1871 | * established, and data arriving; it's also called periodically to allow |
| 1872 | * async transmission. |
| 1873 | * |
| 1874 | * HTTP requests are sent always to the FIRST protocol in @protocol, since |
| 1875 | * at that time websocket protocol has not been negotiated. Other |
| 1876 | * protocols after the first one never see any HTTP callack activity. |
| 1877 | * |
| 1878 | * The server created is a simple http server by default; part of the |
| 1879 | * websocket standard is upgrading this http connection to a websocket one. |
| 1880 | * |
| 1881 | * This allows the same server to provide files like scripts and favicon / |
| 1882 | * images or whatever over http and dynamic data over websockets all in |
| 1883 | * one place; they're all handled in the user callback. |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 1884 | */ |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame] | 1885 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1886 | LWS_VISIBLE struct libwebsocket_context * |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 1887 | libwebsocket_create_context(struct lws_context_creation_info *info) |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 1888 | { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1889 | struct libwebsocket_context *context = NULL; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 1890 | char *p; |
Andy Green | 14f4729 | 2013-02-11 19:36:15 +0800 | [diff] [blame] | 1891 | int n; |
Joakim Soderberg | 3baa08c | 2013-02-22 09:27:59 +0800 | [diff] [blame] | 1892 | #ifndef LWS_NO_SERVER |
Andy Green | cbb3122 | 2013-01-31 09:57:05 +0800 | [diff] [blame] | 1893 | int opt = 1; |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 1894 | struct libwebsocket *wsi; |
Andy Green | cbb3122 | 2013-01-31 09:57:05 +0800 | [diff] [blame] | 1895 | struct sockaddr_in serv_addr; |
| 1896 | #endif |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1897 | #ifndef LWS_NO_EXTENSIONS |
| 1898 | int m; |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 1899 | struct libwebsocket_extension *ext; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1900 | #endif |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 1901 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1902 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | f2f54d5 | 2010-11-15 22:08:00 +0000 | [diff] [blame] | 1903 | SSL_METHOD *method; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 1904 | #endif |
| 1905 | |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 1906 | #ifndef LWS_NO_DAEMONIZE |
Joakim Söderberg | 68e8d73 | 2013-02-06 15:27:39 +0900 | [diff] [blame] | 1907 | int pid_daemon = get_daemonize_pid(); |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 1908 | #endif |
| 1909 | |
Andy Green | b3a614a | 2013-01-19 13:08:17 +0800 | [diff] [blame] | 1910 | lwsl_notice("Initial logging level %d\n", log_level); |
Andy Green | 7b40545 | 2013-02-01 10:50:15 +0800 | [diff] [blame] | 1911 | lwsl_notice("Library version: %s\n", library_version); |
Andy Green | c0d6b63 | 2013-01-12 23:42:17 +0800 | [diff] [blame] | 1912 | lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN); |
Andy Green | c0d6b63 | 2013-01-12 23:42:17 +0800 | [diff] [blame] | 1913 | lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS); |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1914 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1915 | lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n", |
| 1916 | LWS_MAX_EXTENSIONS_ACTIVE); |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1917 | #else |
| 1918 | lwsl_notice(" Configured without extension support\n"); |
| 1919 | #endif |
Andy Green | c0d6b63 | 2013-01-12 23:42:17 +0800 | [diff] [blame] | 1920 | lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED); |
| 1921 | lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT); |
Andy Green | 2672fb2 | 2013-02-22 09:54:35 +0800 | [diff] [blame] | 1922 | if (info->ssl_cipher_list) |
| 1923 | lwsl_info(" SSL ciphers: '%s'\n", info->ssl_cipher_list); |
Andy Green | c0d6b63 | 2013-01-12 23:42:17 +0800 | [diff] [blame] | 1924 | lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH); |
| 1925 | lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER); |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1926 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1927 | #ifdef _WIN32 |
| 1928 | { |
| 1929 | WORD wVersionRequested; |
| 1930 | WSADATA wsaData; |
| 1931 | int err; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1932 | HMODULE wsdll; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1933 | |
| 1934 | /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */ |
| 1935 | wVersionRequested = MAKEWORD(2, 2); |
| 1936 | |
| 1937 | err = WSAStartup(wVersionRequested, &wsaData); |
| 1938 | if (err != 0) { |
| 1939 | /* Tell the user that we could not find a usable */ |
| 1940 | /* Winsock DLL. */ |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1941 | lwsl_err("WSAStartup failed with error: %d\n", err); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1942 | return NULL; |
| 1943 | } |
David Galeano | 7b11fec | 2011-10-04 19:55:18 +0800 | [diff] [blame] | 1944 | |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1945 | /* default to a poll() made out of select() */ |
| 1946 | poll = emulated_poll; |
David Galeano | 7b11fec | 2011-10-04 19:55:18 +0800 | [diff] [blame] | 1947 | |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1948 | /* if windows socket lib available, use his WSAPoll */ |
David Galeano | cb19368 | 2013-01-09 15:29:00 +0800 | [diff] [blame] | 1949 | wsdll = GetModuleHandle(_T("Ws2_32.dll")); |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 1950 | if (wsdll) |
| 1951 | poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll"); |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 1952 | |
Joakim Söderberg | 68e8d73 | 2013-02-06 15:27:39 +0900 | [diff] [blame] | 1953 | /* Finally fall back to emulated poll if all else fails */ |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 1954 | if (!poll) |
| 1955 | poll = emulated_poll; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1956 | } |
| 1957 | #endif |
| 1958 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1959 | context = (struct libwebsocket_context *) |
| 1960 | malloc(sizeof(struct libwebsocket_context)); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1961 | if (!context) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 1962 | lwsl_err("No memory for websocket context\n"); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1963 | return NULL; |
| 1964 | } |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 1965 | memset(context, 0, sizeof(*context)); |
Andy Green | 35f332b | 2013-01-21 13:06:38 +0800 | [diff] [blame] | 1966 | #ifndef LWS_NO_DAEMONIZE |
Andy Green | 24cba92 | 2013-01-19 13:56:10 +0800 | [diff] [blame] | 1967 | context->started_with_parent = pid_daemon; |
| 1968 | lwsl_notice(" Started with daemon pid %d\n", pid_daemon); |
| 1969 | #endif |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1970 | |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 1971 | context->listen_service_extraseen = 0; |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 1972 | context->protocols = info->protocols; |
| 1973 | context->listen_port = info->port; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1974 | context->http_proxy_port = 0; |
| 1975 | context->http_proxy_address[0] = '\0'; |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 1976 | context->options = info->options; |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1977 | /* to reduce this allocation, */ |
| 1978 | context->max_fds = getdtablesize(); |
Andy Green | a86f634 | 2013-02-11 11:04:56 +0800 | [diff] [blame] | 1979 | lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n", |
| 1980 | sizeof(struct libwebsocket_context), |
| 1981 | sizeof(struct pollfd) + sizeof(struct libwebsocket *), |
| 1982 | context->max_fds, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1983 | sizeof(struct libwebsocket_context) + |
| 1984 | ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) * |
| 1985 | context->max_fds)); |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1986 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1987 | context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) * |
| 1988 | context->max_fds); |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1989 | if (context->fds == NULL) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1990 | lwsl_err("Unable to allocate fds array for %d connections\n", |
| 1991 | context->max_fds); |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1992 | free(context); |
| 1993 | return NULL; |
| 1994 | } |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1995 | context->lws_lookup = (struct libwebsocket **) |
| 1996 | malloc(sizeof(struct libwebsocket *) * context->max_fds); |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 1997 | if (context->lws_lookup == NULL) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1998 | lwsl_err( |
| 1999 | "Unable to allocate lws_lookup array for %d connections\n", |
| 2000 | context->max_fds); |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 2001 | free(context->fds); |
| 2002 | free(context); |
| 2003 | return NULL; |
| 2004 | } |
Andy Green | b1a5e6c | 2013-03-09 12:34:30 +0800 | [diff] [blame] | 2005 | memset(context->lws_lookup, 0, sizeof(struct libwebsocket *) * |
| 2006 | context->max_fds); |
Andy Green | a17c692 | 2013-01-20 20:21:54 +0800 | [diff] [blame] | 2007 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2008 | context->fds_count = 0; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 2009 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2010 | context->extensions = info->extensions; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 2011 | #endif |
Paulo Roberto Urio | 1e32663 | 2012-06-04 10:52:19 +0800 | [diff] [blame] | 2012 | context->last_timeout_check_s = 0; |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2013 | context->user_space = info->user; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 2014 | |
Andreas Pakulat | 68bd4bd | 2013-10-28 15:18:04 +0100 | [diff] [blame] | 2015 | #if defined(WIN32) || defined(_WIN32) |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2016 | context->fd_random = 0; |
| 2017 | #else |
| 2018 | context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY); |
| 2019 | if (context->fd_random < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2020 | lwsl_err("Unable to open random device %s %d\n", |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2021 | SYSTEM_RANDOM_FILEPATH, context->fd_random); |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 2022 | goto bail; |
Andy Green | 44eee68 | 2011-02-10 09:32:24 +0000 | [diff] [blame] | 2023 | } |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2024 | #endif |
Andy Green | 44eee68 | 2011-02-10 09:32:24 +0000 | [diff] [blame] | 2025 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2026 | #ifdef LWS_OPENSSL_SUPPORT |
| 2027 | context->use_ssl = 0; |
James Devine | 5b34c97 | 2013-12-14 11:41:29 +0800 | [diff] [blame] | 2028 | context->allow_non_ssl_on_ssl_port = 0; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2029 | context->ssl_ctx = NULL; |
| 2030 | context->ssl_client_ctx = NULL; |
Andy Green | 2e24da0 | 2011-03-05 16:12:04 +0000 | [diff] [blame] | 2031 | openssl_websocket_private_data_index = 0; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2032 | #endif |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 2033 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 2034 | strcpy(context->canonical_hostname, "unknown"); |
Andy Green | a69f051 | 2012-05-03 12:32:38 +0800 | [diff] [blame] | 2035 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 2036 | #ifndef LWS_NO_SERVER |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2037 | if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) { |
Andy Green | 788c4a8 | 2012-10-22 12:29:57 +0100 | [diff] [blame] | 2038 | /* find canonical hostname */ |
Andy Green | adc7146 | 2013-02-14 17:11:22 +0800 | [diff] [blame] | 2039 | gethostname((char *)context->canonical_hostname, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2040 | sizeof(context->canonical_hostname) - 1); |
Andy Green | 788c4a8 | 2012-10-22 12:29:57 +0100 | [diff] [blame] | 2041 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2042 | lwsl_notice(" canonical_hostname = %s\n", |
| 2043 | context->canonical_hostname); |
Andy Green | a69f051 | 2012-05-03 12:32:38 +0800 | [diff] [blame] | 2044 | } |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 2045 | #endif |
Andy Green | a69f051 | 2012-05-03 12:32:38 +0800 | [diff] [blame] | 2046 | |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 2047 | /* split the proxy ads:port if given */ |
| 2048 | |
| 2049 | p = getenv("http_proxy"); |
| 2050 | if (p) { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2051 | strncpy(context->http_proxy_address, p, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2052 | sizeof(context->http_proxy_address) - 1); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2053 | context->http_proxy_address[ |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2054 | sizeof(context->http_proxy_address) - 1] = '\0'; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 2055 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2056 | p = strchr(context->http_proxy_address, ':'); |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 2057 | if (p == NULL) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2058 | lwsl_err("http_proxy needs to be ads:port\n"); |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 2059 | goto bail; |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 2060 | } |
| 2061 | *p = '\0'; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2062 | context->http_proxy_port = atoi(p + 1); |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 2063 | |
Andy Green | b3a614a | 2013-01-19 13:08:17 +0800 | [diff] [blame] | 2064 | lwsl_notice(" Proxy %s:%u\n", |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2065 | context->http_proxy_address, |
| 2066 | context->http_proxy_port); |
Andy Green | 9659f37 | 2011-01-27 22:01:43 +0000 | [diff] [blame] | 2067 | } |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2068 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 2069 | #ifndef LWS_NO_SERVER |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2070 | if (info->port) { |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2071 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2072 | #ifdef LWS_OPENSSL_SUPPORT |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2073 | context->use_ssl = info->ssl_cert_filepath != NULL && |
| 2074 | info->ssl_private_key_filepath != NULL; |
Andy Green | 23c5f2e | 2013-02-06 15:43:00 +0900 | [diff] [blame] | 2075 | #ifdef USE_CYASSL |
| 2076 | lwsl_notice(" Compiled with CYASSL support\n"); |
| 2077 | #else |
| 2078 | lwsl_notice(" Compiled with OpenSSL support\n"); |
| 2079 | #endif |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2080 | if (context->use_ssl) |
Andy Green | 23c5f2e | 2013-02-06 15:43:00 +0900 | [diff] [blame] | 2081 | lwsl_notice(" Using SSL mode\n"); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2082 | else |
Andy Green | 23c5f2e | 2013-02-06 15:43:00 +0900 | [diff] [blame] | 2083 | lwsl_notice(" Using non-SSL mode\n"); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2084 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2085 | #else |
Andy Green | 2b40b79 | 2013-02-10 22:22:01 +0800 | [diff] [blame] | 2086 | if (info->ssl_cert_filepath != NULL && |
| 2087 | info->ssl_private_key_filepath != NULL) { |
Andy Green | b3a614a | 2013-01-19 13:08:17 +0800 | [diff] [blame] | 2088 | lwsl_notice(" Not compiled for OpenSSl support!\n"); |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 2089 | goto bail; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2090 | } |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2091 | lwsl_notice(" Compiled without SSL support\n"); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2092 | #endif |
Andy Green | a17c692 | 2013-01-20 20:21:54 +0800 | [diff] [blame] | 2093 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2094 | lwsl_notice( |
| 2095 | " per-conn mem: %u + %u headers + protocol rx buf\n", |
| 2096 | sizeof(struct libwebsocket), |
| 2097 | sizeof(struct allocated_headers)); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2098 | } |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 2099 | #endif |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2100 | |
| 2101 | /* ignore SIGPIPE */ |
Andreas Pakulat | 68bd4bd | 2013-10-28 15:18:04 +0100 | [diff] [blame] | 2102 | #if defined(WIN32) || defined(_WIN32) |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2103 | #else |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2104 | signal(SIGPIPE, sigpipe_handler); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2105 | #endif |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2106 | |
| 2107 | |
| 2108 | #ifdef LWS_OPENSSL_SUPPORT |
| 2109 | |
| 2110 | /* basic openssl init */ |
| 2111 | |
| 2112 | SSL_library_init(); |
| 2113 | |
| 2114 | OpenSSL_add_all_algorithms(); |
| 2115 | SSL_load_error_strings(); |
| 2116 | |
Andy Green | 2e24da0 | 2011-03-05 16:12:04 +0000 | [diff] [blame] | 2117 | openssl_websocket_private_data_index = |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 2118 | SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL); |
| 2119 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2120 | /* |
| 2121 | * Firefox insists on SSLv23 not SSLv3 |
| 2122 | * Konq disables SSLv2 by default now, SSLv23 works |
| 2123 | */ |
| 2124 | |
| 2125 | method = (SSL_METHOD *)SSLv23_server_method(); |
| 2126 | if (!method) { |
Jonti | e01cb9a | 2013-11-22 13:14:26 +0200 | [diff] [blame] | 2127 | int error = ERR_get_error(); |
Joakim Soderberg | b82b0dd | 2013-02-22 09:28:15 +0800 | [diff] [blame] | 2128 | lwsl_err("problem creating ssl method %lu: %s\n", |
Jonti | e01cb9a | 2013-11-22 13:14:26 +0200 | [diff] [blame] | 2129 | error, |
| 2130 | ERR_error_string(error, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2131 | (char *)context->service_buffer)); |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 2132 | goto bail; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2133 | } |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2134 | context->ssl_ctx = SSL_CTX_new(method); /* create context */ |
| 2135 | if (!context->ssl_ctx) { |
Jonti | e01cb9a | 2013-11-22 13:14:26 +0200 | [diff] [blame] | 2136 | int error = ERR_get_error(); |
Joakim Soderberg | b82b0dd | 2013-02-22 09:28:15 +0800 | [diff] [blame] | 2137 | lwsl_err("problem creating ssl context %lu: %s\n", |
Jonti | e01cb9a | 2013-11-22 13:14:26 +0200 | [diff] [blame] | 2138 | error, |
| 2139 | ERR_error_string(error, |
| 2140 | (char *)context->service_buffer)); |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 2141 | goto bail; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2142 | } |
| 2143 | |
martell | 19c73f3 | 2014-01-22 18:17:03 +0000 | [diff] [blame] | 2144 | #if defined(WIN32) || defined(_WIN32) |
| 2145 | #else |
Andy Green | 3b3fa9e | 2013-12-25 16:34:37 +0800 | [diff] [blame] | 2146 | signal(SIGUSR2, lws_sigusr2); |
Andy Green | ed451d5 | 2014-01-11 12:37:07 +0800 | [diff] [blame] | 2147 | { |
| 2148 | sigset_t mask; |
| 2149 | sigemptyset (&mask); |
| 2150 | sigaddset (&mask, SIGUSR2); |
| 2151 | |
| 2152 | sigprocmask(SIG_BLOCK, &mask, NULL); |
| 2153 | } |
martell | 19c73f3 | 2014-01-22 18:17:03 +0000 | [diff] [blame] | 2154 | #endif |
Andy Green | 3b3fa9e | 2013-12-25 16:34:37 +0800 | [diff] [blame] | 2155 | |
David Galeano | cc148e4 | 2013-01-10 10:18:59 +0800 | [diff] [blame] | 2156 | #ifdef SSL_OP_NO_COMPRESSION |
David Galeano | c72f6f9 | 2013-01-10 10:11:57 +0800 | [diff] [blame] | 2157 | SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION); |
David Galeano | cc148e4 | 2013-01-10 10:18:59 +0800 | [diff] [blame] | 2158 | #endif |
David Galeano | 77a677c | 2013-01-10 10:14:12 +0800 | [diff] [blame] | 2159 | SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE); |
Andy Green | 2672fb2 | 2013-02-22 09:54:35 +0800 | [diff] [blame] | 2160 | if (info->ssl_cipher_list) |
| 2161 | SSL_CTX_set_cipher_list(context->ssl_ctx, |
| 2162 | info->ssl_cipher_list); |
David Galeano | c72f6f9 | 2013-01-10 10:11:57 +0800 | [diff] [blame] | 2163 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 2164 | #ifndef LWS_NO_CLIENT |
| 2165 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2166 | /* client context */ |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2167 | |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2168 | if (info->port == CONTEXT_PORT_NO_LISTEN) { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2169 | method = (SSL_METHOD *)SSLv23_client_method(); |
| 2170 | if (!method) { |
Jonti | e01cb9a | 2013-11-22 13:14:26 +0200 | [diff] [blame] | 2171 | int error = ERR_get_error(); |
Joakim Soderberg | b82b0dd | 2013-02-22 09:28:15 +0800 | [diff] [blame] | 2172 | lwsl_err("problem creating ssl method %lu: %s\n", |
Jonti | e01cb9a | 2013-11-22 13:14:26 +0200 | [diff] [blame] | 2173 | error, |
| 2174 | ERR_error_string(error, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2175 | (char *)context->service_buffer)); |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 2176 | goto bail; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2177 | } |
| 2178 | /* create context */ |
| 2179 | context->ssl_client_ctx = SSL_CTX_new(method); |
| 2180 | if (!context->ssl_client_ctx) { |
Jonti | e01cb9a | 2013-11-22 13:14:26 +0200 | [diff] [blame] | 2181 | int error = ERR_get_error(); |
Joakim Soderberg | b82b0dd | 2013-02-22 09:28:15 +0800 | [diff] [blame] | 2182 | lwsl_err("problem creating ssl context %lu: %s\n", |
Jonti | e01cb9a | 2013-11-22 13:14:26 +0200 | [diff] [blame] | 2183 | error, |
| 2184 | ERR_error_string(error, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2185 | (char *)context->service_buffer)); |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 2186 | goto bail; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2187 | } |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2188 | |
David Galeano | cc148e4 | 2013-01-10 10:18:59 +0800 | [diff] [blame] | 2189 | #ifdef SSL_OP_NO_COMPRESSION |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2190 | SSL_CTX_set_options(context->ssl_client_ctx, |
| 2191 | SSL_OP_NO_COMPRESSION); |
David Galeano | cc148e4 | 2013-01-10 10:18:59 +0800 | [diff] [blame] | 2192 | #endif |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2193 | SSL_CTX_set_options(context->ssl_client_ctx, |
| 2194 | SSL_OP_CIPHER_SERVER_PREFERENCE); |
Andy Green | 2672fb2 | 2013-02-22 09:54:35 +0800 | [diff] [blame] | 2195 | if (info->ssl_cipher_list) |
| 2196 | SSL_CTX_set_cipher_list(context->ssl_client_ctx, |
| 2197 | info->ssl_cipher_list); |
David Galeano | c72f6f9 | 2013-01-10 10:11:57 +0800 | [diff] [blame] | 2198 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2199 | /* openssl init for cert verification (for client sockets) */ |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2200 | if (!info->ssl_ca_filepath) { |
David Galeano | 2f82be8 | 2013-01-09 16:25:54 +0800 | [diff] [blame] | 2201 | if (!SSL_CTX_load_verify_locations( |
| 2202 | context->ssl_client_ctx, NULL, |
| 2203 | LWS_OPENSSL_CLIENT_CERTS)) |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2204 | lwsl_err( |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2205 | "Unable to load SSL Client certs from %s " |
| 2206 | "(set by --with-client-cert-dir= " |
| 2207 | "in configure) -- client ssl isn't " |
| 2208 | "going to work", LWS_OPENSSL_CLIENT_CERTS); |
David Galeano | 2f82be8 | 2013-01-09 16:25:54 +0800 | [diff] [blame] | 2209 | } else |
| 2210 | if (!SSL_CTX_load_verify_locations( |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2211 | context->ssl_client_ctx, info->ssl_ca_filepath, |
David Galeano | 2f82be8 | 2013-01-09 16:25:54 +0800 | [diff] [blame] | 2212 | NULL)) |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2213 | lwsl_err( |
David Galeano | 2f82be8 | 2013-01-09 16:25:54 +0800 | [diff] [blame] | 2214 | "Unable to load SSL Client certs " |
| 2215 | "file from %s -- client ssl isn't " |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2216 | "going to work", info->ssl_ca_filepath); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2217 | |
| 2218 | /* |
| 2219 | * callback allowing user code to load extra verification certs |
| 2220 | * helping the client to verify server identity |
| 2221 | */ |
| 2222 | |
prasannateamf1 | b353a45 | 2013-11-12 17:21:01 -0800 | [diff] [blame] | 2223 | /* support for client-side certificate authentication */ |
| 2224 | if (info->ssl_cert_filepath) { |
| 2225 | n = SSL_CTX_use_certificate_chain_file( |
| 2226 | context->ssl_client_ctx, |
| 2227 | info->ssl_cert_filepath); |
| 2228 | if (n != 1) { |
| 2229 | lwsl_err("problem getting cert '%s' %lu: %s\n", |
| 2230 | info->ssl_cert_filepath, |
| 2231 | ERR_get_error(), |
| 2232 | ERR_error_string(ERR_get_error(), |
| 2233 | (char *)context->service_buffer)); |
| 2234 | goto bail; |
| 2235 | } |
| 2236 | } |
| 2237 | if (info->ssl_private_key_filepath) { |
| 2238 | /* set the private key from KeyFile */ |
| 2239 | if (SSL_CTX_use_PrivateKey_file(context->ssl_client_ctx, |
| 2240 | info->ssl_private_key_filepath, |
| 2241 | SSL_FILETYPE_PEM) != 1) { |
| 2242 | lwsl_err("use_PrivateKey_file '%s' %lu: %s\n", |
| 2243 | info->ssl_private_key_filepath, |
| 2244 | ERR_get_error(), |
| 2245 | ERR_error_string(ERR_get_error(), |
| 2246 | (char *)context->service_buffer)); |
| 2247 | goto bail; |
| 2248 | } |
| 2249 | |
| 2250 | /* verify private key */ |
| 2251 | if (!SSL_CTX_check_private_key(context->ssl_client_ctx)) { |
| 2252 | lwsl_err("Private SSL key doesn't match cert\n"); |
| 2253 | goto bail; |
| 2254 | } |
| 2255 | } |
| 2256 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2257 | context->protocols[0].callback(context, NULL, |
| 2258 | LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS, |
| 2259 | context->ssl_client_ctx, NULL, 0); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2260 | } |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 2261 | #endif |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2262 | |
Andy Green | c6bf2c2 | 2011-02-20 11:10:47 +0000 | [diff] [blame] | 2263 | /* as a server, are we requiring clients to identify themselves? */ |
| 2264 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2265 | if (info->options & |
| 2266 | LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) { |
Andy Green | c6bf2c2 | 2011-02-20 11:10:47 +0000 | [diff] [blame] | 2267 | |
| 2268 | /* absolutely require the client cert */ |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2269 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2270 | SSL_CTX_set_verify(context->ssl_ctx, |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 2271 | SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, |
| 2272 | OpenSSL_verify_callback); |
Andy Green | c6bf2c2 | 2011-02-20 11:10:47 +0000 | [diff] [blame] | 2273 | |
| 2274 | /* |
| 2275 | * give user code a chance to load certs into the server |
| 2276 | * allowing it to verify incoming client certs |
| 2277 | */ |
| 2278 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2279 | context->protocols[0].callback(context, NULL, |
Andy Green | c6bf2c2 | 2011-02-20 11:10:47 +0000 | [diff] [blame] | 2280 | LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS, |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2281 | context->ssl_ctx, NULL, 0); |
Andy Green | c6bf2c2 | 2011-02-20 11:10:47 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
James Devine | 5b34c97 | 2013-12-14 11:41:29 +0800 | [diff] [blame] | 2284 | if(info->options & LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT) { |
| 2285 | /* Normally SSL listener rejects non-ssl, optionally allow */ |
| 2286 | context->allow_non_ssl_on_ssl_port = 1; |
| 2287 | } |
| 2288 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2289 | if (context->use_ssl) { |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 2290 | |
| 2291 | /* openssl init for server sockets */ |
| 2292 | |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2293 | /* set the local certificate from CertFile */ |
David Galeano | 9b3d4b2 | 2013-01-10 10:11:21 +0800 | [diff] [blame] | 2294 | n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx, |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2295 | info->ssl_cert_filepath); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2296 | if (n != 1) { |
Jonti | e01cb9a | 2013-11-22 13:14:26 +0200 | [diff] [blame] | 2297 | int error = ERR_get_error(); |
Joakim Soderberg | b82b0dd | 2013-02-22 09:28:15 +0800 | [diff] [blame] | 2298 | lwsl_err("problem getting cert '%s' %lu: %s\n", |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2299 | info->ssl_cert_filepath, |
Jonti | e01cb9a | 2013-11-22 13:14:26 +0200 | [diff] [blame] | 2300 | error, |
| 2301 | ERR_error_string(error, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2302 | (char *)context->service_buffer)); |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 2303 | goto bail; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2304 | } |
| 2305 | /* set the private key from KeyFile */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2306 | if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx, |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2307 | info->ssl_private_key_filepath, |
| 2308 | SSL_FILETYPE_PEM) != 1) { |
Jonti | e01cb9a | 2013-11-22 13:14:26 +0200 | [diff] [blame] | 2309 | int error = ERR_get_error(); |
Joakim Soderberg | b82b0dd | 2013-02-22 09:28:15 +0800 | [diff] [blame] | 2310 | lwsl_err("ssl problem getting key '%s' %lu: %s\n", |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2311 | info->ssl_private_key_filepath, |
Jonti | e01cb9a | 2013-11-22 13:14:26 +0200 | [diff] [blame] | 2312 | error, |
| 2313 | ERR_error_string(error, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2314 | (char *)context->service_buffer)); |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 2315 | goto bail; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2316 | } |
| 2317 | /* verify private key */ |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2318 | if (!SSL_CTX_check_private_key(context->ssl_ctx)) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2319 | lwsl_err("Private SSL key doesn't match cert\n"); |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 2320 | goto bail; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 2321 | } |
| 2322 | |
| 2323 | /* SSL is happy and has a cert it's content with */ |
| 2324 | } |
| 2325 | #endif |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2326 | |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 2327 | #ifndef LWS_NO_SERVER |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2328 | /* set up our external listening socket we serve on */ |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 2329 | |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2330 | if (info->port) { |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 2331 | int sockfd; |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 2332 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 2333 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
| 2334 | if (sockfd < 0) { |
Andy Green | f7609e9 | 2013-01-14 13:10:55 +0800 | [diff] [blame] | 2335 | lwsl_err("ERROR opening socket\n"); |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 2336 | goto bail; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 2337 | } |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 2338 | |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 2339 | #ifndef WIN32 |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2340 | /* |
| 2341 | * allow us to restart even if old sockets in TIME_WAIT |
| 2342 | * (REUSEADDR on Unix means, "don't hang on to this |
| 2343 | * address after the listener is closed." On Windows, though, |
| 2344 | * it means "don't keep other processes from binding to |
| 2345 | * this address while we're using it) |
| 2346 | */ |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2347 | setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, |
| 2348 | (const void *)&opt, sizeof(opt)); |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 2349 | #endif |
Andy Green | 6c93955 | 2011-03-08 08:56:57 +0000 | [diff] [blame] | 2350 | |
| 2351 | /* Disable Nagle */ |
| 2352 | opt = 1; |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2353 | setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, |
| 2354 | (const void *)&opt, sizeof(opt)); |
Andy Green | 6c93955 | 2011-03-08 08:56:57 +0000 | [diff] [blame] | 2355 | |
Andreas Pakulat | 68bd4bd | 2013-10-28 15:18:04 +0100 | [diff] [blame] | 2356 | #if defined(WIN32) || defined(_WIN32) |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 2357 | opt = 0; |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2358 | ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt); |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 2359 | #else |
Andy Green | e216071 | 2013-01-28 12:19:10 +0800 | [diff] [blame] | 2360 | fcntl(sockfd, F_SETFL, O_NONBLOCK); |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 2361 | #endif |
Andy Green | e216071 | 2013-01-28 12:19:10 +0800 | [diff] [blame] | 2362 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 2363 | bzero((char *) &serv_addr, sizeof(serv_addr)); |
| 2364 | serv_addr.sin_family = AF_INET; |
Joakim Soderberg | 63ff120 | 2013-02-11 17:52:23 +0100 | [diff] [blame] | 2365 | if (info->iface == NULL) |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 2366 | serv_addr.sin_addr.s_addr = INADDR_ANY; |
| 2367 | else |
Andy Green | d1eac60 | 2013-11-09 08:07:38 +0800 | [diff] [blame] | 2368 | if (interface_to_sa(info->iface, &serv_addr, |
| 2369 | sizeof(serv_addr)) < 0) { |
| 2370 | lwsl_err("Unable to find interface %s\n", |
| 2371 | info->iface); |
| 2372 | compatible_close(sockfd); |
| 2373 | goto bail; |
| 2374 | } |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2375 | serv_addr.sin_port = htons(info->port); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 2376 | |
| 2377 | n = bind(sockfd, (struct sockaddr *) &serv_addr, |
| 2378 | sizeof(serv_addr)); |
| 2379 | if (n < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2380 | lwsl_err("ERROR on binding to port %d (%d %d)\n", |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2381 | info->port, n, errno); |
Andy Green | 8b2d6f0 | 2013-10-22 06:49:30 +0800 | [diff] [blame] | 2382 | compatible_close(sockfd); |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 2383 | goto bail; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 2384 | } |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2385 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2386 | wsi = (struct libwebsocket *)malloc( |
| 2387 | sizeof(struct libwebsocket)); |
Andy Green | 41c5803 | 2013-01-12 13:21:08 +0800 | [diff] [blame] | 2388 | if (wsi == NULL) { |
| 2389 | lwsl_err("Out of mem\n"); |
Andy Green | 8b2d6f0 | 2013-10-22 06:49:30 +0800 | [diff] [blame] | 2390 | compatible_close(sockfd); |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 2391 | goto bail; |
Andy Green | 41c5803 | 2013-01-12 13:21:08 +0800 | [diff] [blame] | 2392 | } |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2393 | memset(wsi, 0, sizeof(struct libwebsocket)); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2394 | wsi->sock = sockfd; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 2395 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 2396 | wsi->count_active_extensions = 0; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 2397 | #endif |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2398 | wsi->mode = LWS_CONNMODE_SERVER_LISTENER; |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 2399 | |
| 2400 | insert_wsi_socket_into_fds(context, wsi); |
Andy Green | 0d33833 | 2011-02-12 11:57:43 +0000 | [diff] [blame] | 2401 | |
Andy Green | 65b0e91 | 2013-01-16 07:59:47 +0800 | [diff] [blame] | 2402 | context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO; |
| 2403 | context->listen_service_count = 0; |
| 2404 | context->listen_service_fd = sockfd; |
| 2405 | |
Andy Green | a824d18 | 2013-01-15 20:52:29 +0800 | [diff] [blame] | 2406 | listen(sockfd, LWS_SOMAXCONN); |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2407 | lwsl_notice(" Listening on port %d\n", info->port); |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 2408 | } |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 2409 | #endif |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2410 | |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2411 | /* |
| 2412 | * drop any root privs for this process |
| 2413 | * to listen on port < 1023 we would have needed root, but now we are |
| 2414 | * listening, we don't want the power for anything else |
| 2415 | */ |
Andreas Pakulat | 68bd4bd | 2013-10-28 15:18:04 +0100 | [diff] [blame] | 2416 | #if defined(WIN32) || defined(_WIN32) |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2417 | #else |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2418 | if (info->gid != -1) |
| 2419 | if (setgid(info->gid)) |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2420 | lwsl_warn("setgid: %s\n", strerror(errno)); |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2421 | if (info->uid != -1) |
| 2422 | if (setuid(info->uid)) |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2423 | lwsl_warn("setuid: %s\n", strerror(errno)); |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2424 | #endif |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2425 | |
Andy Green | 6f520a5 | 2013-01-29 17:57:39 +0800 | [diff] [blame] | 2426 | /* initialize supported protocols */ |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2427 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2428 | for (context->count_protocols = 0; |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2429 | info->protocols[context->count_protocols].callback; |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2430 | context->count_protocols++) { |
Andy Green | 2d1301e | 2011-05-24 10:14:41 +0100 | [diff] [blame] | 2431 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2432 | lwsl_parser(" Protocol: %s\n", |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2433 | info->protocols[context->count_protocols].name); |
Andy Green | 2d1301e | 2011-05-24 10:14:41 +0100 | [diff] [blame] | 2434 | |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2435 | info->protocols[context->count_protocols].owning_server = |
| 2436 | context; |
| 2437 | info->protocols[context->count_protocols].protocol_index = |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2438 | context->count_protocols; |
Andy Green | a7109e6 | 2013-02-11 12:05:54 +0800 | [diff] [blame] | 2439 | |
| 2440 | /* |
| 2441 | * inform all the protocols that they are doing their one-time |
| 2442 | * initialization if they want to |
| 2443 | */ |
| 2444 | info->protocols[context->count_protocols].callback(context, |
| 2445 | NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0); |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2446 | } |
Andy Green | f5bc130 | 2013-01-21 09:09:52 +0800 | [diff] [blame] | 2447 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 2448 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 2449 | /* |
| 2450 | * give all extensions a chance to create any per-context |
| 2451 | * allocations they need |
| 2452 | */ |
| 2453 | |
| 2454 | m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT; |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2455 | if (info->port) |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 2456 | m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT; |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2457 | |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2458 | if (info->extensions) { |
| 2459 | ext = info->extensions; |
| 2460 | while (ext->callback) { |
| 2461 | lwsl_ext(" Extension: %s\n", ext->name); |
| 2462 | ext->callback(context, ext, NULL, |
Aaron Zinman | 4550f1d | 2013-01-10 12:35:18 +0800 | [diff] [blame] | 2463 | (enum libwebsocket_extension_callback_reasons)m, |
| 2464 | NULL, NULL, 0); |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 2465 | ext++; |
| 2466 | } |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 2467 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 2468 | #endif |
Andy Green | 3b3fa9e | 2013-12-25 16:34:37 +0800 | [diff] [blame] | 2469 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 2470 | return context; |
Peter Pentchev | 3b233cb | 2013-02-07 16:31:19 +0200 | [diff] [blame] | 2471 | |
| 2472 | bail: |
| 2473 | libwebsocket_context_destroy(context); |
| 2474 | return NULL; |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 2475 | } |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2476 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2477 | /** |
shys | b4e800e | 2013-10-24 22:12:03 +0800 | [diff] [blame] | 2478 | * libwebsocket_set_proxy() - Setups proxy to libwebsocket_context. |
| 2479 | * @context: pointer to struct libwebsocket_context you want set proxy to |
| 2480 | * @proxy: pointer to c string containing proxy in format address:port |
| 2481 | * |
| 2482 | * Returns 0 if proxy string was parsed and proxy was setup. |
| 2483 | * Returns -1 if @proxy is NULL or has incorrect format. |
| 2484 | * |
| 2485 | * This is only required if your OS does not provide the http_proxy |
| 2486 | * enviroment variable (eg, OSX) |
| 2487 | * |
| 2488 | * IMPORTANT! You should call this function right after creation of the |
| 2489 | * libwebsocket_context and before call to connect. If you call this |
| 2490 | * function after connect behavior is undefined. |
| 2491 | * This function will override proxy settings made on libwebsocket_context |
| 2492 | * creation with genenv() call. |
| 2493 | */ |
| 2494 | |
| 2495 | LWS_VISIBLE int |
| 2496 | libwebsocket_set_proxy(struct libwebsocket_context *context, const char *proxy) |
| 2497 | { |
| 2498 | char *p; |
| 2499 | |
| 2500 | if (!proxy) |
| 2501 | return -1; |
| 2502 | |
| 2503 | strncpy(context->http_proxy_address, proxy, |
| 2504 | sizeof(context->http_proxy_address) - 1); |
| 2505 | context->http_proxy_address[ |
| 2506 | sizeof(context->http_proxy_address) - 1] = '\0'; |
| 2507 | |
| 2508 | p = strchr(context->http_proxy_address, ':'); |
| 2509 | if (!p) { |
| 2510 | lwsl_err("http_proxy needs to be ads:port\n"); |
| 2511 | |
| 2512 | return -1; |
| 2513 | } |
| 2514 | *p = '\0'; |
| 2515 | context->http_proxy_port = atoi(p + 1); |
| 2516 | |
| 2517 | lwsl_notice(" Proxy %s:%u\n", context->http_proxy_address, |
| 2518 | context->http_proxy_port); |
| 2519 | |
| 2520 | return 0; |
| 2521 | } |
| 2522 | |
| 2523 | /** |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2524 | * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 2525 | * connection. |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2526 | * @wsi: pointer to struct websocket you want to know the protocol of |
| 2527 | * |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 2528 | * |
Andy Green | 6f520a5 | 2013-01-29 17:57:39 +0800 | [diff] [blame] | 2529 | * Some apis can act on all live connections of a given protocol, |
| 2530 | * this is how you can get a pointer to the active protocol if needed. |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2531 | */ |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 2532 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 2533 | LWS_VISIBLE const struct libwebsocket_protocols * |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 2534 | libwebsockets_get_protocol(struct libwebsocket *wsi) |
| 2535 | { |
| 2536 | return wsi->protocol; |
| 2537 | } |
| 2538 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 2539 | LWS_VISIBLE int |
Andy Green | 82c3d54 | 2011-03-07 21:16:31 +0000 | [diff] [blame] | 2540 | libwebsocket_is_final_fragment(struct libwebsocket *wsi) |
| 2541 | { |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 2542 | return wsi->u.ws.final; |
Andy Green | 82c3d54 | 2011-03-07 21:16:31 +0000 | [diff] [blame] | 2543 | } |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 2544 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 2545 | LWS_VISIBLE unsigned char |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 2546 | libwebsocket_get_reserved_bits(struct libwebsocket *wsi) |
| 2547 | { |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 2548 | return wsi->u.ws.rsv; |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 2549 | } |
| 2550 | |
Andy Green | 2af4d5b | 2013-02-18 16:30:10 +0800 | [diff] [blame] | 2551 | int |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 2552 | libwebsocket_ensure_user_space(struct libwebsocket *wsi) |
| 2553 | { |
Andy Green | 67d556c | 2013-02-15 22:31:55 +0800 | [diff] [blame] | 2554 | if (!wsi->protocol) |
Andy Green | 2af4d5b | 2013-02-18 16:30:10 +0800 | [diff] [blame] | 2555 | return 1; |
Andy Green | 67d556c | 2013-02-15 22:31:55 +0800 | [diff] [blame] | 2556 | |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 2557 | /* allocate the per-connection user memory (if any) */ |
| 2558 | |
| 2559 | if (wsi->protocol->per_session_data_size && !wsi->user_space) { |
| 2560 | wsi->user_space = malloc( |
| 2561 | wsi->protocol->per_session_data_size); |
| 2562 | if (wsi->user_space == NULL) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2563 | lwsl_err("Out of memory for conn user space\n"); |
Andy Green | 2af4d5b | 2013-02-18 16:30:10 +0800 | [diff] [blame] | 2564 | return 1; |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 2565 | } |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 2566 | memset(wsi->user_space, 0, |
| 2567 | wsi->protocol->per_session_data_size); |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 2568 | } |
Andy Green | 2af4d5b | 2013-02-18 16:30:10 +0800 | [diff] [blame] | 2569 | return 0; |
Alex Bligh | 49146db | 2011-11-07 17:19:25 +0800 | [diff] [blame] | 2570 | } |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2571 | |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 2572 | static void lwsl_emit_stderr(int level, const char *line) |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 2573 | { |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 2574 | char buf[300]; |
| 2575 | struct timeval tv; |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 2576 | int n; |
| 2577 | |
| 2578 | gettimeofday(&tv, NULL); |
| 2579 | |
| 2580 | buf[0] = '\0'; |
| 2581 | for (n = 0; n < LLL_COUNT; n++) |
| 2582 | if (level == (1 << n)) { |
Andy Green | 058ba81 | 2013-01-19 11:32:18 +0800 | [diff] [blame] | 2583 | sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2584 | (int)(tv.tv_usec / 100), log_level_names[n]); |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 2585 | break; |
| 2586 | } |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2587 | |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 2588 | fprintf(stderr, "%s%s", buf, line); |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 2589 | } |
| 2590 | |
Andreas Pakulat | 68bd4bd | 2013-10-28 15:18:04 +0100 | [diff] [blame] | 2591 | #if defined(WIN32) || defined(_WIN32) |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 2592 | LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line) |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 2593 | { |
| 2594 | lwsl_emit_stderr(level, line); |
| 2595 | } |
| 2596 | #else |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 2597 | LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line) |
Andy Green | c11db20 | 2013-01-19 11:12:16 +0800 | [diff] [blame] | 2598 | { |
| 2599 | int syslog_level = LOG_DEBUG; |
| 2600 | |
| 2601 | switch (level) { |
| 2602 | case LLL_ERR: |
| 2603 | syslog_level = LOG_ERR; |
| 2604 | break; |
| 2605 | case LLL_WARN: |
| 2606 | syslog_level = LOG_WARNING; |
| 2607 | break; |
| 2608 | case LLL_NOTICE: |
| 2609 | syslog_level = LOG_NOTICE; |
| 2610 | break; |
| 2611 | case LLL_INFO: |
| 2612 | syslog_level = LOG_INFO; |
| 2613 | break; |
| 2614 | } |
Edwin van den Oetelaar | f6eeabc | 2013-01-19 20:01:01 +0800 | [diff] [blame] | 2615 | syslog(syslog_level, "%s", line); |
Andy Green | c11db20 | 2013-01-19 11:12:16 +0800 | [diff] [blame] | 2616 | } |
Joakim Soderberg | 4c53123 | 2013-02-06 15:26:58 +0900 | [diff] [blame] | 2617 | #endif |
Andy Green | c11db20 | 2013-01-19 11:12:16 +0800 | [diff] [blame] | 2618 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 2619 | LWS_VISIBLE void _lws_log(int filter, const char *format, ...) |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2620 | { |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 2621 | char buf[256]; |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2622 | va_list ap; |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2623 | |
| 2624 | if (!(log_level & filter)) |
| 2625 | return; |
| 2626 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2627 | va_start(ap, format); |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2628 | vsnprintf(buf, sizeof(buf), format, ap); |
| 2629 | buf[sizeof(buf) - 1] = '\0'; |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 2630 | va_end(ap); |
| 2631 | |
Andy Green | 0b31909 | 2013-01-19 11:17:56 +0800 | [diff] [blame] | 2632 | lwsl_emit(filter, buf); |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2633 | } |
| 2634 | |
| 2635 | /** |
| 2636 | * lws_set_log_level() - Set the logging bitfield |
| 2637 | * @level: OR together the LLL_ debug contexts you want output from |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 2638 | * @log_emit_function: NULL to leave it as it is, or a user-supplied |
| 2639 | * function to perform log string emission instead of |
| 2640 | * the default stderr one. |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2641 | * |
Andy Green | c6511a0 | 2013-02-19 10:01:48 +0800 | [diff] [blame] | 2642 | * log level defaults to "err", "warn" and "notice" contexts enabled and |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 2643 | * emission on stderr. |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2644 | */ |
| 2645 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 2646 | LWS_VISIBLE void lws_set_log_level(int level, void (*log_emit_function)(int level, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 2647 | const char *line)) |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2648 | { |
| 2649 | log_level = level; |
Andy Green | de8f27a | 2013-01-12 09:17:42 +0800 | [diff] [blame] | 2650 | if (log_emit_function) |
| 2651 | lwsl_emit = log_emit_function; |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 2652 | } |