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