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