blob: 560fb09f78c070d3ed9c9bc2b09459ed7af1e7f3 [file] [log] [blame]
Andy Green58eaa742011-03-07 17:54:06 +00001/*
Andy Greena0da8a82010-11-08 17:12:19 +00002 * libwebsockets - small server side websockets and web server implementation
Andy Green8f037e42010-12-19 22:13:26 +00003 *
Andy Greena0da8a82010-11-08 17:12:19 +00004 * 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 Green05a0a7b2010-10-31 17:51:39 +000020 */
21
Andy Green7c212cc2010-11-08 20:20:42 +000022#include "private-libwebsockets.h"
Andy Greenff95d7a2010-10-28 22:36:01 +010023
Peter Hinz56885f32011-03-02 22:03:47 +000024#ifdef WIN32
David Galeanocb193682013-01-09 15:29:00 +080025#include <tchar.h>
26#include <io.h>
Peter Hinz56885f32011-03-02 22:03:47 +000027#else
Davidc4ef7b12013-01-12 20:39:47 +080028#ifdef LWS_BUILTIN_GETIFADDRS
29#include <getifaddrs.h>
30#else
Peter Hinz56885f32011-03-02 22:03:47 +000031#include <ifaddrs.h>
Davidc4ef7b12013-01-12 20:39:47 +080032#endif
Joakim Soderberg4c531232013-02-06 15:26:58 +090033#include <syslog.h>
Andy Green7627af52011-03-09 15:13:52 +000034#include <sys/un.h>
Andy Greena69f0512012-05-03 12:32:38 +080035#include <sys/socket.h>
36#include <netdb.h>
Peter Hinz56885f32011-03-02 22:03:47 +000037#endif
Andy Green2e24da02011-03-05 16:12:04 +000038
39#ifdef LWS_OPENSSL_SUPPORT
40int openssl_websocket_private_data_index;
41#endif
42
Andy Greenaa6fc442012-04-12 13:26:49 +080043#ifdef __MINGW32__
44#include "../win32port/win32helpers/websock-w32.c"
45#else
46#ifdef __MINGW64__
47#include "../win32port/win32helpers/websock-w32.c"
48#endif
49#endif
50
Andy Green7b405452013-02-01 10:50:15 +080051#ifndef LWS_BUILD_HASH
52#define LWS_BUILD_HASH "unknown-build-hash"
53#endif
Andy Green3182ece2013-01-20 17:08:31 +080054
Andy Green7c19c342013-01-19 12:18:07 +080055static int log_level = LLL_ERR | LLL_WARN | LLL_NOTICE;
Andy Green058ba812013-01-19 11:32:18 +080056static void lwsl_emit_stderr(int level, const char *line);
57static void (*lwsl_emit)(int level, const char *line) = lwsl_emit_stderr;
58
Andy Green7b405452013-02-01 10:50:15 +080059static const char *library_version = LWS_LIBRARY_VERSION " " LWS_BUILD_HASH;
60
Andy Greenb5b23192013-02-11 17:13:32 +080061static const char * const log_level_names[] = {
Andy Green43db0452013-01-10 19:50:35 +080062 "ERR",
63 "WARN",
Andy Green7c19c342013-01-19 12:18:07 +080064 "NOTICE",
Andy Green43db0452013-01-10 19:50:35 +080065 "INFO",
66 "DEBUG",
67 "PARSER",
68 "HEADER",
69 "EXTENSION",
70 "CLIENT",
Andy Greend636e352013-01-29 12:36:17 +080071 "LATENCY",
Andy Green43db0452013-01-10 19:50:35 +080072};
73
Andy Greenb5b23192013-02-11 17:13:32 +080074#ifndef LWS_NO_CLIENT
75 extern int lws_client_socket_service(
76 struct libwebsocket_context *context,
77 struct libwebsocket *wsi, struct pollfd *pollfd);
78#endif
79#ifndef LWS_NO_SERVER
80 extern int lws_server_socket_service(
81 struct libwebsocket_context *context,
82 struct libwebsocket *wsi, struct pollfd *pollfd);
83#endif
84
Andy Green7b405452013-02-01 10:50:15 +080085/**
86 * lws_get_library_version: get version and git hash library built from
87 *
88 * returns a const char * to a string like "1.1 178d78c"
89 * representing the library version followed by the git head hash it
90 * was built from
91 */
92
93const char *
94lws_get_library_version(void)
95{
96 return library_version;
97}
98
Andy Green0d338332011-02-12 11:57:43 +000099int
Andy Greenb5b23192013-02-11 17:13:32 +0800100insert_wsi_socket_into_fds(struct libwebsocket_context *context,
101 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000102{
Andy Greendfb23042013-01-17 12:26:48 +0800103 if (context->fds_count >= context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800104 lwsl_err("Too many fds (%d)\n", context->max_fds);
Andy Green0d338332011-02-12 11:57:43 +0000105 return 1;
106 }
107
Andy Greendfb23042013-01-17 12:26:48 +0800108 if (wsi->sock > context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800109 lwsl_err("Socket fd %d is too high (%d)\n",
110 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800111 return 1;
112 }
113
114 assert(wsi);
115 assert(wsi->sock);
116
Andy Greenb5b23192013-02-11 17:13:32 +0800117 lwsl_info("insert_wsi_socket_into_fds: wsi=%p, sock=%d, fds pos=%d\n",
118 wsi, wsi->sock, context->fds_count);
Andy Greendfb23042013-01-17 12:26:48 +0800119
120 context->lws_lookup[wsi->sock] = wsi;
121 wsi->position_in_fds_table = context->fds_count;
122 context->fds[context->fds_count].fd = wsi->sock;
123 context->fds[context->fds_count].events = POLLIN;
124 context->fds[context->fds_count++].revents = 0;
125
126 /* external POLL support via protocol 0 */
127 context->protocols[0].callback(context, wsi,
128 LWS_CALLBACK_ADD_POLL_FD,
129 (void *)(long)wsi->sock, NULL, POLLIN);
Andy Green0d338332011-02-12 11:57:43 +0000130
131 return 0;
132}
133
Andy Greendfb23042013-01-17 12:26:48 +0800134static int
Andy Greenb5b23192013-02-11 17:13:32 +0800135remove_wsi_socket_from_fds(struct libwebsocket_context *context,
136 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000137{
Andy Greendfb23042013-01-17 12:26:48 +0800138 int m;
Andy Green0d338332011-02-12 11:57:43 +0000139
Andy Greendfb23042013-01-17 12:26:48 +0800140 if (!--context->fds_count)
141 goto do_ext;
Andy Green0d338332011-02-12 11:57:43 +0000142
Andy Greendfb23042013-01-17 12:26:48 +0800143 if (wsi->sock > context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800144 lwsl_err("Socket fd %d too high (%d)\n",
145 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800146 return 1;
147 }
Andy Green0d338332011-02-12 11:57:43 +0000148
Andy Greenb5b23192013-02-11 17:13:32 +0800149 lwsl_info("remove_wsi_socket_from_fds: wsi=%p, sock=%d, fds pos=%d\n",
150 wsi, wsi->sock, wsi->position_in_fds_table);
Andy Greendfb23042013-01-17 12:26:48 +0800151
152 m = wsi->position_in_fds_table; /* replace the contents for this */
153
154 /* have the last guy take up the vacant slot */
Andy Greenb5b23192013-02-11 17:13:32 +0800155 context->fds[m] = context->fds[context->fds_count];
156 /*
157 * end guy's fds_lookup entry remains unchanged
158 * (still same fd pointing to same wsi)
159 */
Andy Greendfb23042013-01-17 12:26:48 +0800160 /* end guy's "position in fds table" changed */
Andy Greenb5b23192013-02-11 17:13:32 +0800161 context->lws_lookup[context->fds[context->fds_count].fd]->
162 position_in_fds_table = m;
Andy Greendfb23042013-01-17 12:26:48 +0800163 /* deletion guy's lws_lookup entry needs nuking */
Andy Greenb5b23192013-02-11 17:13:32 +0800164 context->lws_lookup[wsi->sock] = NULL;
165 /* removed wsi has no position any more */
166 wsi->position_in_fds_table = -1;
Andy Greendfb23042013-01-17 12:26:48 +0800167
168do_ext:
169 /* remove also from external POLL support via protocol 0 */
170 if (wsi->sock)
171 context->protocols[0].callback(context, wsi,
172 LWS_CALLBACK_DEL_POLL_FD, (void *)(long)wsi->sock, NULL, 0);
173
174 return 0;
Andy Green0d338332011-02-12 11:57:43 +0000175}
176
Andy Green32375b72011-02-19 08:32:53 +0000177
Andy Green8f037e42010-12-19 22:13:26 +0000178void
Peter Hinz56885f32011-03-02 22:03:47 +0000179libwebsocket_close_and_free_session(struct libwebsocket_context *context,
Andy Green687b0182011-02-26 11:04:01 +0000180 struct libwebsocket *wsi, enum lws_close_status reason)
Andy Green251f6fa2010-11-03 11:13:06 +0000181{
Andy Greenb45993c2010-12-18 15:13:50 +0000182 int n;
Andy Green62c54d22011-02-14 09:14:25 +0000183 int old_state;
Andy Green5e1fa172011-02-10 09:07:05 +0000184 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 +
185 LWS_SEND_BUFFER_POST_PADDING];
Andy Green3182ece2013-01-20 17:08:31 +0800186#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000187 int ret;
188 int m;
189 struct lws_tokens eff_buf;
Andy Greena41314f2011-05-23 10:00:03 +0100190 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +0800191#endif
Andy Greenb45993c2010-12-18 15:13:50 +0000192
Andy Green4b6fbe12011-02-14 08:03:48 +0000193 if (!wsi)
Andy Greenb45993c2010-12-18 15:13:50 +0000194 return;
195
Andy Green62c54d22011-02-14 09:14:25 +0000196 old_state = wsi->state;
Andy Green251f6fa2010-11-03 11:13:06 +0000197
Andy Green62c54d22011-02-14 09:14:25 +0000198 if (old_state == WSI_STATE_DEAD_SOCKET)
Andy Green5e1fa172011-02-10 09:07:05 +0000199 return;
200
Andy Green623a98d2013-01-21 11:04:23 +0800201 wsi->u.ws.close_reason = reason;
Andy Greenda527df2011-03-07 07:08:12 +0000202
Andy Greenb8b247d2013-01-22 07:20:08 +0800203 if (wsi->mode == LWS_CONNMODE_HTTP_SERVING && wsi->u.http.fd) {
204 close(wsi->u.http.fd);
205 wsi->u.http.fd = 0;
206 }
207
Andy Green3182ece2013-01-20 17:08:31 +0800208#ifndef LWS_NO_EXTENSIONS
Andy Greenda527df2011-03-07 07:08:12 +0000209 /*
Andy Green68b45042011-05-25 21:41:57 +0100210 * are his extensions okay with him closing? Eg he might be a mux
211 * parent and just his ch1 aspect is closing?
212 */
213
Andy Green68b45042011-05-25 21:41:57 +0100214 for (n = 0; n < wsi->count_active_extensions; n++) {
215 if (!wsi->active_extensions[n]->callback)
216 continue;
217
218 m = wsi->active_extensions[n]->callback(context,
219 wsi->active_extensions[n], wsi,
220 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
221 wsi->active_extensions_user[n], NULL, 0);
222
223 /*
224 * if somebody vetoed actually closing him at this time....
225 * up to the extension to track the attempted close, let's
226 * just bail
227 */
228
229 if (m) {
Andy Green43db0452013-01-10 19:50:35 +0800230 lwsl_ext("extension vetoed close\n");
Andy Green68b45042011-05-25 21:41:57 +0100231 return;
232 }
233 }
234
Andy Green68b45042011-05-25 21:41:57 +0100235 /*
Andy Greenc44159f2011-03-07 07:08:18 +0000236 * flush any tx pending from extensions, since we may send close packet
237 * if there are problems with send, just nuke the connection
238 */
239
240 ret = 1;
241 while (ret == 1) {
242
243 /* default to nobody has more to spill */
244
245 ret = 0;
246 eff_buf.token = NULL;
247 eff_buf.token_len = 0;
248
249 /* show every extension the new incoming data */
250
251 for (n = 0; n < wsi->count_active_extensions; n++) {
252 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000253 wsi->protocol->owning_server,
254 wsi->active_extensions[n], wsi,
Andy Greenc44159f2011-03-07 07:08:18 +0000255 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
256 wsi->active_extensions_user[n], &eff_buf, 0);
257 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800258 lwsl_ext("Extension reports fatal error\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000259 goto just_kill_connection;
260 }
261 if (m)
262 /*
263 * at least one extension told us he has more
264 * to spill, so we will go around again after
265 */
266 ret = 1;
267 }
268
269 /* assuming they left us something to send, send it */
270
271 if (eff_buf.token_len)
272 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
Andy Greenb5b23192013-02-11 17:13:32 +0800273 eff_buf.token_len)) {
274 lwsl_debug("close: ext spill failed\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000275 goto just_kill_connection;
Andy Green0303db42013-01-17 14:46:43 +0800276 }
Andy Greenc44159f2011-03-07 07:08:18 +0000277 }
Andy Green3182ece2013-01-20 17:08:31 +0800278#endif
Andy Greenc44159f2011-03-07 07:08:18 +0000279
280 /*
Andy Greenda527df2011-03-07 07:08:12 +0000281 * signal we are closing, libsocket_write will
282 * add any necessary version-specific stuff. If the write fails,
283 * no worries we are closing anyway. If we didn't initiate this
284 * close, then our state has been changed to
285 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this.
286 *
287 * Likewise if it's a second call to close this connection after we
288 * sent the close indication to the peer already, we are in state
289 * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time.
290 */
291
292 if (old_state == WSI_STATE_ESTABLISHED &&
293 reason != LWS_CLOSE_STATUS_NOSTATUS) {
Andy Green66a16f32011-05-24 22:07:45 +0100294
Andy Green43db0452013-01-10 19:50:35 +0800295 lwsl_debug("sending close indication...\n");
Andy Green66a16f32011-05-24 22:07:45 +0100296
Andy Greenfdd305a2013-02-11 14:32:48 +0800297 /* make valgrind happy */
Andy Greenb5b23192013-02-11 17:13:32 +0800298 memset(buf, 0, sizeof(buf));
299 n = libwebsocket_write(wsi,
300 &buf[LWS_SEND_BUFFER_PRE_PADDING + 2],
Andy Greenda527df2011-03-07 07:08:12 +0000301 0, LWS_WRITE_CLOSE);
302 if (!n) {
303 /*
304 * we have sent a nice protocol level indication we
305 * now wish to close, we should not send anything more
306 */
307
308 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
309
Andy Greenb5b23192013-02-11 17:13:32 +0800310 /*
311 * ...and we should wait for a reply for a bit
312 * out of politeness
313 */
Andy Greenda527df2011-03-07 07:08:12 +0000314
315 libwebsocket_set_timeout(wsi,
Andy Green0303db42013-01-17 14:46:43 +0800316 PENDING_TIMEOUT_CLOSE_ACK, 1);
Andy Greenda527df2011-03-07 07:08:12 +0000317
Andy Green43db0452013-01-10 19:50:35 +0800318 lwsl_debug("sent close indication, awaiting ack\n");
Andy Greenda527df2011-03-07 07:08:12 +0000319
320 return;
321 }
322
Andy Greenb5b23192013-02-11 17:13:32 +0800323 lwsl_info("close: sending close packet failed, hanging up\n");
Andy Green0303db42013-01-17 14:46:43 +0800324
Andy Greenda527df2011-03-07 07:08:12 +0000325 /* else, the send failed and we should just hang up */
326 }
327
Andy Green3182ece2013-01-20 17:08:31 +0800328#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000329just_kill_connection:
Andy Green3182ece2013-01-20 17:08:31 +0800330#endif
Andy Green66a16f32011-05-24 22:07:45 +0100331
Andy Greenb5b23192013-02-11 17:13:32 +0800332 lwsl_debug("close: just_kill_connection\n");
Andy Green66a16f32011-05-24 22:07:45 +0100333
Andy Greenda527df2011-03-07 07:08:12 +0000334 /*
335 * we won't be servicing or receiving anything further from this guy
Andy Greendfb23042013-01-17 12:26:48 +0800336 * delete socket from the internal poll list if still present
Andy Greenda527df2011-03-07 07:08:12 +0000337 */
Andy Green4b6fbe12011-02-14 08:03:48 +0000338
Andy Greendfb23042013-01-17 12:26:48 +0800339 remove_wsi_socket_from_fds(context, wsi);
Andy Green4b6fbe12011-02-14 08:03:48 +0000340
Andy Green251f6fa2010-11-03 11:13:06 +0000341 wsi->state = WSI_STATE_DEAD_SOCKET;
342
Andy Greenb5b23192013-02-11 17:13:32 +0800343 if ((old_state == WSI_STATE_ESTABLISHED ||
344 wsi->mode == LWS_CONNMODE_WS_SERVING ||
345 wsi->mode == LWS_CONNMODE_WS_CLIENT) && wsi->u.ws.rx_user_buffer) {
Andy Green54495112013-02-06 21:10:16 +0900346 free(wsi->u.ws.rx_user_buffer);
347 wsi->u.ws.rx_user_buffer = NULL;
348 }
349
Andy Green4b6fbe12011-02-14 08:03:48 +0000350 /* tell the user it's all over for this guy */
351
Andy Greend4302732011-02-28 07:45:29 +0000352 if (wsi->protocol && wsi->protocol->callback &&
Andy Green6ee372f2012-04-09 15:09:01 +0800353 ((old_state == WSI_STATE_ESTABLISHED) ||
354 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
355 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
Andy Green43db0452013-01-10 19:50:35 +0800356 lwsl_debug("calling back CLOSED\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000357 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000358 wsi->user_space, NULL, 0);
Andy Greencc012472011-11-07 19:53:23 +0800359 } else
Andy Greenb5b23192013-02-11 17:13:32 +0800360 lwsl_debug("not calling back closed\n");
Andy Green251f6fa2010-11-03 11:13:06 +0000361
Andy Green3182ece2013-01-20 17:08:31 +0800362#ifndef LWS_NO_EXTENSIONS
Andy Greenef660a92011-03-06 10:29:38 +0000363 /* deallocate any active extension contexts */
364
365 for (n = 0; n < wsi->count_active_extensions; n++) {
366 if (!wsi->active_extensions[n]->callback)
367 continue;
368
Andy Green46c2ea02011-03-22 09:04:01 +0000369 wsi->active_extensions[n]->callback(context,
370 wsi->active_extensions[n], wsi,
371 LWS_EXT_CALLBACK_DESTROY,
372 wsi->active_extensions_user[n], NULL, 0);
Andy Greenef660a92011-03-06 10:29:38 +0000373
374 free(wsi->active_extensions_user[n]);
375 }
376
Andy Greena41314f2011-05-23 10:00:03 +0100377 /*
378 * inform all extensions in case they tracked this guy out of band
379 * even though not active on him specifically
380 */
381
382 ext = context->extensions;
383 while (ext && ext->callback) {
384 ext->callback(context, ext, wsi,
385 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
386 NULL, NULL, 0);
387 ext++;
388 }
Andy Green3182ece2013-01-20 17:08:31 +0800389#endif
Andy Greena41314f2011-05-23 10:00:03 +0100390
Andy Green623a98d2013-01-21 11:04:23 +0800391 if (wsi->u.ws.rxflow_buffer)
392 free(wsi->u.ws.rxflow_buffer);
Andy Green706961d2013-01-17 16:50:35 +0800393
Andy Green43db0452013-01-10 19:50:35 +0800394/* lwsl_info("closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000395
Andy Green3faa9c72010-11-08 17:03:03 +0000396#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000397 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000398 n = SSL_get_fd(wsi->ssl);
399 SSL_shutdown(wsi->ssl);
Andy Green3fc2c652013-01-14 15:35:02 +0800400 compatible_close(n);
Andy Green3faa9c72010-11-08 17:03:03 +0000401 SSL_free(wsi->ssl);
402 } else {
403#endif
Andy Green0303db42013-01-17 14:46:43 +0800404 if (wsi->sock) {
405 n = shutdown(wsi->sock, SHUT_RDWR);
406 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800407 lwsl_debug("closing: shutdown returned %d\n",
408 errno);
Andy Green3fc2c652013-01-14 15:35:02 +0800409
Andy Green0303db42013-01-17 14:46:43 +0800410 n = compatible_close(wsi->sock);
411 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800412 lwsl_debug("closing: close returned %d\n",
413 errno);
Andy Green0303db42013-01-17 14:46:43 +0800414 }
Andy Green3faa9c72010-11-08 17:03:03 +0000415#ifdef LWS_OPENSSL_SUPPORT
416 }
417#endif
Andy Greenb5b23192013-02-11 17:13:32 +0800418 if (wsi->protocol && wsi->protocol->per_session_data_size &&
419 wsi->user_space) /* user code may own */
Andy Green4f3943a2010-11-12 10:44:16 +0000420 free(wsi->user_space);
421
Andy Green251f6fa2010-11-03 11:13:06 +0000422 free(wsi);
423}
424
Andy Green07034092011-02-13 08:37:12 +0000425/**
Andy Greenf7ee5492011-02-13 09:04:21 +0000426 * libwebsockets_hangup_on_client() - Server calls to terminate client
Andy Green6ee372f2012-04-09 15:09:01 +0800427 * connection
Peter Hinz56885f32011-03-02 22:03:47 +0000428 * @context: libwebsockets context
Andy Greenf7ee5492011-02-13 09:04:21 +0000429 * @fd: Connection socket descriptor
430 */
431
432void
Peter Hinz56885f32011-03-02 22:03:47 +0000433libwebsockets_hangup_on_client(struct libwebsocket_context *context, int fd)
Andy Greenf7ee5492011-02-13 09:04:21 +0000434{
Andy Greendfb23042013-01-17 12:26:48 +0800435 struct libwebsocket *wsi = context->lws_lookup[fd];
Andy Greenf7ee5492011-02-13 09:04:21 +0000436
Andy Greendfb23042013-01-17 12:26:48 +0800437 if (wsi) {
Andy Greenb5b23192013-02-11 17:13:32 +0800438 lwsl_info("close connection at hangup_on_client:\n");
Andy Greendfb23042013-01-17 12:26:48 +0800439 libwebsocket_close_and_free_session(context,
440 wsi, LWS_CLOSE_STATUS_NOSTATUS);
441 } else
442 close(fd);
Andy Greenf7ee5492011-02-13 09:04:21 +0000443}
444
445
446/**
Andy Green07034092011-02-13 08:37:12 +0000447 * libwebsockets_get_peer_addresses() - Get client address information
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800448 * @context: Libwebsockets context
449 * @wsi: Local struct libwebsocket associated with
Andy Green07034092011-02-13 08:37:12 +0000450 * @fd: Connection socket descriptor
451 * @name: Buffer to take client address name
452 * @name_len: Length of client address name buffer
453 * @rip: Buffer to take client address IP qotted quad
454 * @rip_len: Length of client address IP buffer
455 *
456 * This function fills in @name and @rip with the name and IP of
Andy Green6ee372f2012-04-09 15:09:01 +0800457 * the client connected with socket descriptor @fd. Names may be
458 * truncated if there is not enough room. If either cannot be
459 * determined, they will be returned as valid zero-length strings.
Andy Green07034092011-02-13 08:37:12 +0000460 */
461
462void
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800463libwebsockets_get_peer_addresses(struct libwebsocket_context *context,
464 struct libwebsocket *wsi, int fd, char *name, int name_len,
Andy Green07034092011-02-13 08:37:12 +0000465 char *rip, int rip_len)
466{
467 unsigned int len;
468 struct sockaddr_in sin;
469 struct hostent *host;
470 struct hostent *host1;
471 char ip[128];
Andy Greenf92def72011-03-09 15:02:20 +0000472 unsigned char *p;
Andy Green07034092011-02-13 08:37:12 +0000473 int n;
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800474 int ret = -1;
David Galeanocb193682013-01-09 15:29:00 +0800475#ifdef AF_LOCAL
Andy Greenb5b23192013-02-11 17:13:32 +0800476 struct sockaddr_un *un;
David Galeanocb193682013-01-09 15:29:00 +0800477#endif
Andy Green07034092011-02-13 08:37:12 +0000478
479 rip[0] = '\0';
480 name[0] = '\0';
481
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800482 lws_latency_pre(context, wsi);
483
Andy Greenb5b23192013-02-11 17:13:32 +0800484 len = sizeof(sin);
Andy Green07034092011-02-13 08:37:12 +0000485 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
486 perror("getpeername");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800487 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000488 }
Andy Green6ee372f2012-04-09 15:09:01 +0800489
Andy Greenb5b23192013-02-11 17:13:32 +0800490 host = gethostbyaddr((char *) &sin.sin_addr, sizeof(sin.sin_addr),
Andy Green07034092011-02-13 08:37:12 +0000491 AF_INET);
492 if (host == NULL) {
493 perror("gethostbyaddr");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800494 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000495 }
496
497 strncpy(name, host->h_name, name_len);
498 name[name_len - 1] = '\0';
499
500 host1 = gethostbyname(host->h_name);
501 if (host1 == NULL)
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800502 goto bail;
Andy Greenf92def72011-03-09 15:02:20 +0000503 p = (unsigned char *)host1;
Andy Green07034092011-02-13 08:37:12 +0000504 n = 0;
505 while (p != NULL) {
Andy Greenf92def72011-03-09 15:02:20 +0000506 p = (unsigned char *)host1->h_addr_list[n++];
Andy Green07034092011-02-13 08:37:12 +0000507 if (p == NULL)
508 continue;
Peter Hinzbb45a902011-03-10 18:14:01 +0000509 if ((host1->h_addrtype != AF_INET)
510#ifdef AF_LOCAL
511 && (host1->h_addrtype != AF_LOCAL)
512#endif
513 )
Andy Green07034092011-02-13 08:37:12 +0000514 continue;
515
Andy Green7627af52011-03-09 15:13:52 +0000516 if (host1->h_addrtype == AF_INET)
517 sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
Peter Hinzbb45a902011-03-10 18:14:01 +0000518#ifdef AF_LOCAL
Andy Green7627af52011-03-09 15:13:52 +0000519 else {
520 un = (struct sockaddr_un *)p;
Andy Green6ee372f2012-04-09 15:09:01 +0800521 strncpy(ip, un->sun_path, sizeof(ip) - 1);
Andy Green7627af52011-03-09 15:13:52 +0000522 ip[sizeof(ip) - 1] = '\0';
523 }
Peter Hinzbb45a902011-03-10 18:14:01 +0000524#endif
Andy Green07034092011-02-13 08:37:12 +0000525 p = NULL;
526 strncpy(rip, ip, rip_len);
527 rip[rip_len - 1] = '\0';
528 }
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800529
530 ret = 0;
531bail:
532 lws_latency(context, wsi, "libwebsockets_get_peer_addresses", ret, 1);
Andy Green07034092011-02-13 08:37:12 +0000533}
Andy Green9f990342011-02-12 11:57:45 +0000534
Peter Hinz56885f32011-03-02 22:03:47 +0000535int libwebsockets_get_random(struct libwebsocket_context *context,
536 void *buf, int len)
537{
538 int n;
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800539 char *p = (char *)buf;
Peter Hinz56885f32011-03-02 22:03:47 +0000540
541#ifdef WIN32
542 for (n = 0; n < len; n++)
543 p[n] = (unsigned char)rand();
544#else
545 n = read(context->fd_random, p, len);
546#endif
547
548 return n;
549}
550
Andy Greena690cd02013-02-09 12:25:31 +0800551int lws_set_socket_options(struct libwebsocket_context *context, int fd)
552{
553 int optval = 1;
554 socklen_t optlen = sizeof(optval);
555#ifdef WIN32
556 unsigned long optl = 0;
557#endif
558#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
559 struct protoent *tcp_proto;
560#endif
561
562 if (context->ka_time) {
563 /* enable keepalive on this socket */
564 optval = 1;
565 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
566 (const void *)&optval, optlen) < 0)
567 return 1;
568
Andy Greena47865f2013-02-10 09:39:47 +0800569#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
570
571 /*
572 * didn't find a way to set these per-socket, need to
573 * tune kernel systemwide values
574 */
575
576#else
Andy Greena690cd02013-02-09 12:25:31 +0800577 /* set the keepalive conditions we want on it too */
578 optval = context->ka_time;
579 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPIDLE,
580 (const void *)&optval, optlen) < 0)
581 return 1;
582
583 optval = context->ka_probes;
584 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPINTVL,
585 (const void *)&optval, optlen) < 0)
586 return 1;
587
588 optval = context->ka_interval;
589 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPCNT,
590 (const void *)&optval, optlen) < 0)
591 return 1;
Andy Greena47865f2013-02-10 09:39:47 +0800592#endif
Andy Greena690cd02013-02-09 12:25:31 +0800593 }
594
595 /* Disable Nagle */
596 optval = 1;
597#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
598 setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen);
599#else
600 tcp_proto = getprotobyname("TCP");
601 setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen);
602#endif
603
604 /* We are nonblocking... */
605#ifdef WIN32
606 ioctlsocket(fd, FIONBIO, &optl);
607#else
608 fcntl(fd, F_SETFL, O_NONBLOCK);
609#endif
610
611 return 0;
612}
613
Andy Green95a7b5d2011-03-06 10:29:39 +0000614int lws_send_pipe_choked(struct libwebsocket *wsi)
615{
616 struct pollfd fds;
617
618 fds.fd = wsi->sock;
619 fds.events = POLLOUT;
620 fds.revents = 0;
621
622 if (poll(&fds, 1, 0) != 1)
623 return 1;
624
625 if ((fds.revents & POLLOUT) == 0)
626 return 1;
627
628 /* okay to send another packet without blocking */
629
630 return 0;
631}
632
Andy Greena41314f2011-05-23 10:00:03 +0100633int
Andy Green3b84c002011-03-06 13:14:42 +0000634lws_handle_POLLOUT_event(struct libwebsocket_context *context,
635 struct libwebsocket *wsi, struct pollfd *pollfd)
636{
Andy Green3b84c002011-03-06 13:14:42 +0000637 int n;
Andy Green6f520a52013-01-29 17:57:39 +0800638
Andy Green3182ece2013-01-20 17:08:31 +0800639#ifndef LWS_NO_EXTENSIONS
640 struct lws_tokens eff_buf;
Andy Green3b84c002011-03-06 13:14:42 +0000641 int ret;
642 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100643 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000644
Andy Greena41314f2011-05-23 10:00:03 +0100645 for (n = 0; n < wsi->count_active_extensions; n++) {
646 if (!wsi->active_extensions[n]->callback)
647 continue;
648
649 m = wsi->active_extensions[n]->callback(context,
650 wsi->active_extensions[n], wsi,
651 LWS_EXT_CALLBACK_IS_WRITEABLE,
652 wsi->active_extensions_user[n], NULL, 0);
653 if (m > handled)
654 handled = m;
655 }
656
657 if (handled == 1)
658 goto notify_action;
659
660 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000661 goto user_service;
662
663 /*
664 * check in on the active extensions, see if they
665 * had pending stuff to spill... they need to get the
666 * first look-in otherwise sequence will be disordered
667 *
668 * NULL, zero-length eff_buf means just spill pending
669 */
670
671 ret = 1;
672 while (ret == 1) {
673
674 /* default to nobody has more to spill */
675
676 ret = 0;
677 eff_buf.token = NULL;
678 eff_buf.token_len = 0;
679
680 /* give every extension a chance to spill */
681
682 for (n = 0; n < wsi->count_active_extensions; n++) {
683 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000684 wsi->protocol->owning_server,
685 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000686 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
687 wsi->active_extensions_user[n], &eff_buf, 0);
688 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800689 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000690 return -1;
691 }
692 if (m)
693 /*
694 * at least one extension told us he has more
695 * to spill, so we will go around again after
696 */
697 ret = 1;
698 }
699
700 /* assuming they gave us something to send, send it */
701
702 if (eff_buf.token_len) {
703 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
704 eff_buf.token_len))
705 return -1;
706 } else
707 continue;
708
709 /* no extension has more to spill */
710
711 if (!ret)
712 continue;
713
714 /*
715 * There's more to spill from an extension, but we just sent
716 * something... did that leave the pipe choked?
717 */
718
719 if (!lws_send_pipe_choked(wsi))
720 /* no we could add more */
721 continue;
722
Andy Green43db0452013-01-10 19:50:35 +0800723 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000724
725 /*
726 * Yes, he's choked. Leave the POLLOUT masked on so we will
727 * come back here when he is unchoked. Don't call the user
728 * callback to enforce ordering of spilling, he'll get called
729 * when we come back here and there's nothing more to spill.
730 */
731
732 return 0;
733 }
734
735 wsi->extension_data_pending = 0;
736
737user_service:
Andy Green3182ece2013-01-20 17:08:31 +0800738#endif
Andy Green3b84c002011-03-06 13:14:42 +0000739 /* one shot */
740
Andy Greena41314f2011-05-23 10:00:03 +0100741 if (pollfd) {
742 pollfd->events &= ~POLLOUT;
Andy Green3b84c002011-03-06 13:14:42 +0000743
Andy Greena41314f2011-05-23 10:00:03 +0100744 /* external POLL support via protocol 0 */
745 context->protocols[0].callback(context, wsi,
746 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
747 (void *)(long)wsi->sock, NULL, POLLOUT);
748 }
Andy Green3182ece2013-01-20 17:08:31 +0800749#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100750notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800751#endif
Andy Green3b84c002011-03-06 13:14:42 +0000752
Andy Green9e4c2b62011-03-07 20:47:39 +0000753 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
754 n = LWS_CALLBACK_CLIENT_WRITEABLE;
755 else
756 n = LWS_CALLBACK_SERVER_WRITEABLE;
757
Andy Greenb8b247d2013-01-22 07:20:08 +0800758 return user_callback_handle_rxflow(wsi->protocol->callback, context,
Andy Greenb5b23192013-02-11 17:13:32 +0800759 wsi, (enum libwebsocket_callback_reasons) n,
760 wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000761}
762
763
764
Andy Greena41314f2011-05-23 10:00:03 +0100765void
766libwebsocket_service_timeout_check(struct libwebsocket_context *context,
767 struct libwebsocket *wsi, unsigned int sec)
768{
Andy Green3182ece2013-01-20 17:08:31 +0800769#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100770 int n;
771
772 /*
773 * if extensions want in on it (eg, we are a mux parent)
774 * give them a chance to service child timeouts
775 */
776
777 for (n = 0; n < wsi->count_active_extensions; n++)
778 wsi->active_extensions[n]->callback(
779 context, wsi->active_extensions[n],
780 wsi, LWS_EXT_CALLBACK_1HZ,
781 wsi->active_extensions_user[n], NULL, sec);
782
Andy Green3182ece2013-01-20 17:08:31 +0800783#endif
Andy Greena41314f2011-05-23 10:00:03 +0100784 if (!wsi->pending_timeout)
785 return;
Andy Green6ee372f2012-04-09 15:09:01 +0800786
Andy Greena41314f2011-05-23 10:00:03 +0100787 /*
788 * if we went beyond the allowed time, kill the
789 * connection
790 */
791
792 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800793 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100794 libwebsocket_close_and_free_session(context,
795 wsi, LWS_CLOSE_STATUS_NOSTATUS);
796 }
797}
798
Andy Green9f990342011-02-12 11:57:45 +0000799/**
800 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000801 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000802 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800803 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000804 *
Andy Green75006172013-01-22 12:32:11 +0800805 * This function takes a pollfd that has POLLIN or POLLOUT activity and
Andy Greenb5b23192013-02-11 17:13:32 +0800806 * services it according to the state of the associated
807 * struct libwebsocket.
Andy Green75006172013-01-22 12:32:11 +0800808 *
809 * The one call deals with all "service" that might happen on a socket
810 * including listen accepts, http files as well as websocket protocol.
Andy Green9f990342011-02-12 11:57:45 +0000811 */
812
813int
Peter Hinz56885f32011-03-02 22:03:47 +0000814libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000815 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000816{
Andy Greena1ce6be2013-01-18 11:43:21 +0800817 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000818 int n;
Andy Green0d338332011-02-12 11:57:43 +0000819 int m;
Andy Greenf0b79e22013-02-10 10:46:45 +0800820 int listen_socket_fds_index = 0;
Andy Greena71eafc2011-02-14 17:59:43 +0000821 struct timeval tv;
Andy Green6f520a52013-01-29 17:57:39 +0800822
Andy Green3182ece2013-01-20 17:08:31 +0800823#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000824 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800825#endif
Andy Green98a717c2011-03-06 13:14:15 +0000826 struct lws_tokens eff_buf;
Andy Greenf0b79e22013-02-10 10:46:45 +0800827
828 if (context->listen_service_fd)
829 listen_socket_fds_index = context->lws_lookup[
830 context->listen_service_fd]->position_in_fds_table;
831
Andy Greena71eafc2011-02-14 17:59:43 +0000832 /*
833 * you can call us with pollfd = NULL to just allow the once-per-second
834 * global timeout checks; if less than a second since the last check
835 * it returns immediately then.
836 */
837
838 gettimeofday(&tv, NULL);
839
Peter Hinz56885f32011-03-02 22:03:47 +0000840 if (context->last_timeout_check_s != tv.tv_sec) {
841 context->last_timeout_check_s = tv.tv_sec;
Andy Greena71eafc2011-02-14 17:59:43 +0000842
Joakim Soderberg4c531232013-02-06 15:26:58 +0900843 #ifndef WIN32
Andy Green24cba922013-01-19 13:56:10 +0800844 /* if our parent went down, don't linger around */
Andy Greenb5b23192013-02-11 17:13:32 +0800845 if (context->started_with_parent &&
846 kill(context->started_with_parent, 0) < 0)
Andy Green24cba922013-01-19 13:56:10 +0800847 kill(getpid(), SIGTERM);
Joakim Soderberg4c531232013-02-06 15:26:58 +0900848 #endif
Andy Green24cba922013-01-19 13:56:10 +0800849
Andy Greena71eafc2011-02-14 17:59:43 +0000850 /* global timeout check once per second */
851
Peter Hinz56885f32011-03-02 22:03:47 +0000852 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +0800853 struct libwebsocket *new_wsi =
854 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +0800855 if (!new_wsi)
856 continue;
857 libwebsocket_service_timeout_check(context,
858 new_wsi, tv.tv_sec);
Andy Greena71eafc2011-02-14 17:59:43 +0000859 }
860 }
861
862 /* just here for timeout management? */
863
864 if (pollfd == NULL)
865 return 0;
866
867 /* no, here to service a socket descriptor */
868
Andy Green65b0e912013-01-16 07:59:47 +0800869 /*
870 * deal with listen service piggybacking
871 * every listen_service_modulo services of other fds, we
872 * sneak one in to service the listen socket if there's anything waiting
873 *
874 * To handle connection storms, as found in ab, if we previously saw a
875 * pending connection here, it causes us to check again next time.
876 */
877
Andy Greenb5b23192013-02-11 17:13:32 +0800878 if (context->listen_service_fd && pollfd !=
879 &context->fds[listen_socket_fds_index]) {
Andy Green65b0e912013-01-16 07:59:47 +0800880 context->listen_service_count++;
881 if (context->listen_service_extraseen ||
Andy Greenb5b23192013-02-11 17:13:32 +0800882 context->listen_service_count ==
883 context->listen_service_modulo) {
Andy Green65b0e912013-01-16 07:59:47 +0800884 context->listen_service_count = 0;
885 m = 1;
886 if (context->listen_service_extraseen > 5)
887 m = 2;
888 while (m--) {
Andy Greenb5b23192013-02-11 17:13:32 +0800889 /*
890 * even with extpoll, we prepared this
891 * internal fds for listen
892 */
893 n = poll(&context->fds[listen_socket_fds_index],
894 1, 0);
895 if (n > 0) { /* there's a conn waiting for us */
896 libwebsocket_service_fd(context,
897 &context->
898 fds[listen_socket_fds_index]);
Andy Green65b0e912013-01-16 07:59:47 +0800899 context->listen_service_extraseen++;
900 } else {
901 if (context->listen_service_extraseen)
Andy Greenb5b23192013-02-11 17:13:32 +0800902 context->
903 listen_service_extraseen--;
Andy Green65b0e912013-01-16 07:59:47 +0800904 break;
905 }
906 }
907 }
908
909 }
910
911 /* okay, what we came here to do... */
912
Andy Greendfb23042013-01-17 12:26:48 +0800913 wsi = context->lws_lookup[pollfd->fd];
Andy Greend280b6e2013-01-15 13:40:23 +0800914 if (wsi == NULL) {
Andy Greendfb23042013-01-17 12:26:48 +0800915 if (pollfd->fd > 11)
Andy Greenb5b23192013-02-11 17:13:32 +0800916 lwsl_err("unexpected NULL wsi fd=%d fds_count=%d\n",
917 pollfd->fd, context->fds_count);
Andy Greenfa3f4052012-10-07 20:40:35 +0800918 return 0;
Andy Greend280b6e2013-01-15 13:40:23 +0800919 }
Andy Green8f037e42010-12-19 22:13:26 +0000920
Andy Green0d338332011-02-12 11:57:43 +0000921 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +0800922
Andy Greena1ce6be2013-01-18 11:43:21 +0800923#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +0800924 case LWS_CONNMODE_HTTP_SERVING:
Andy Green0d338332011-02-12 11:57:43 +0000925 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +0800926 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greena1ce6be2013-01-18 11:43:21 +0800927 return lws_server_socket_service(context, wsi, pollfd);
928#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000929
Andy Green0d338332011-02-12 11:57:43 +0000930 case LWS_CONNMODE_WS_SERVING:
931 case LWS_CONNMODE_WS_CLIENT:
932
933 /* handle session socket closed */
934
935 if (pollfd->revents & (POLLERR | POLLHUP)) {
936
Andy Green43db0452013-01-10 19:50:35 +0800937 lwsl_debug("Session Socket %p (fd=%d) dead\n",
Andy Green0d338332011-02-12 11:57:43 +0000938 (void *)wsi, pollfd->fd);
939
Peter Hinz56885f32011-03-02 22:03:47 +0000940 libwebsocket_close_and_free_session(context, wsi,
Andy Green687b0182011-02-26 11:04:01 +0000941 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800942 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000943 }
944
Andy Green0d338332011-02-12 11:57:43 +0000945 /* the guy requested a callback when it was OK to write */
946
Andy Greenda527df2011-03-07 07:08:12 +0000947 if ((pollfd->revents & POLLOUT) &&
948 wsi->state == WSI_STATE_ESTABLISHED)
949 if (lws_handle_POLLOUT_event(context, wsi,
950 pollfd) < 0) {
Andy Green16ab3182013-02-10 18:02:31 +0800951 lwsl_info("libwebsocket_service_fd: closing");
Andy Greenda527df2011-03-07 07:08:12 +0000952 libwebsocket_close_and_free_session(
953 context, wsi, LWS_CLOSE_STATUS_NORMAL);
Andy Green040d2ef2013-01-16 13:40:43 +0800954 return 0;
Andy Green3b84c002011-03-06 13:14:42 +0000955 }
Andy Green0d338332011-02-12 11:57:43 +0000956
Andy Green0d338332011-02-12 11:57:43 +0000957
958 /* any incoming data ready? */
959
960 if (!(pollfd->revents & POLLIN))
961 break;
962
Andy Greenb45993c2010-12-18 15:13:50 +0000963#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +0800964read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +0800965 if (wsi->ssl) {
Andy Greene84652c2013-02-08 13:01:02 +0800966 eff_buf.token_len = SSL_read(wsi->ssl,
967 context->service_buffer,
Andy Greenb5b23192013-02-11 17:13:32 +0800968 sizeof(context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +0800969 if (!eff_buf.token_len) {
970 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
Andy Greene84652c2013-02-08 13:01:02 +0800971 lwsl_err("SSL_read returned 0 with reason %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +0800972 ERR_error_string(n,
973 (char *)context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +0800974 }
975 } else
Andy Greenb45993c2010-12-18 15:13:50 +0000976#endif
Andy Greene84652c2013-02-08 13:01:02 +0800977 eff_buf.token_len = recv(pollfd->fd,
Andy Greenb5b23192013-02-11 17:13:32 +0800978 context->service_buffer,
979 sizeof(context->service_buffer), 0);
Andy Greenb45993c2010-12-18 15:13:50 +0000980
Andy Green98a717c2011-03-06 13:14:15 +0000981 if (eff_buf.token_len < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800982 lwsl_debug("Socket read returned %d\n",
Andy Green98a717c2011-03-06 13:14:15 +0000983 eff_buf.token_len);
Alon Levydc93b7f2012-10-19 11:21:57 +0200984 if (errno != EINTR && errno != EAGAIN)
Andy Green6ee372f2012-04-09 15:09:01 +0800985 libwebsocket_close_and_free_session(context,
986 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800987 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000988 }
Andy Green98a717c2011-03-06 13:14:15 +0000989 if (!eff_buf.token_len) {
Andy Greenb5b23192013-02-11 17:13:32 +0800990 lwsl_info("closing connection due to 0 length read\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000991 libwebsocket_close_and_free_session(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +0800992 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenfa3f4052012-10-07 20:40:35 +0800993 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000994 }
995
Andy Green98a717c2011-03-06 13:14:15 +0000996 /*
997 * give any active extensions a chance to munge the buffer
998 * before parse. We pass in a pointer to an lws_tokens struct
999 * prepared with the default buffer and content length that's in
1000 * there. Rather than rewrite the default buffer, extensions
1001 * that expect to grow the buffer can adapt .token to
1002 * point to their own per-connection buffer in the extension
1003 * user allocation. By default with no extensions or no
1004 * extension callback handling, just the normal input buffer is
1005 * used then so it is efficient.
1006 */
Andy Greenb45993c2010-12-18 15:13:50 +00001007
Andy Greene84652c2013-02-08 13:01:02 +08001008 eff_buf.token = (char *)context->service_buffer;
Andy Green3182ece2013-01-20 17:08:31 +08001009#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001010 more = 1;
1011 while (more) {
Andy Green0d338332011-02-12 11:57:43 +00001012
Andy Green98a717c2011-03-06 13:14:15 +00001013 more = 0;
1014
1015 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +00001016 m = wsi->active_extensions[n]->callback(context,
1017 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +00001018 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +00001019 wsi->active_extensions_user[n],
1020 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +00001021 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001022 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +08001023 "Extension reports fatal error\n");
1024 libwebsocket_close_and_free_session(
1025 context, wsi,
1026 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +08001027 return 0;
Andy Green98a717c2011-03-06 13:14:15 +00001028 }
1029 if (m)
1030 more = 1;
1031 }
Andy Green3182ece2013-01-20 17:08:31 +08001032#endif
Andy Green98a717c2011-03-06 13:14:15 +00001033 /* service incoming data */
1034
1035 if (eff_buf.token_len) {
1036 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001037 (unsigned char *)eff_buf.token,
1038 eff_buf.token_len);
Andy Green98a717c2011-03-06 13:14:15 +00001039 if (n < 0)
1040 /* we closed wsi */
Andy Green040d2ef2013-01-16 13:40:43 +08001041 return 0;
Andy Green98a717c2011-03-06 13:14:15 +00001042 }
Andy Green3182ece2013-01-20 17:08:31 +08001043#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001044 eff_buf.token = NULL;
1045 eff_buf.token_len = 0;
1046 }
Andy Green3182ece2013-01-20 17:08:31 +08001047#endif
David Galeano7ffbe1b2013-01-10 10:35:32 +08001048
1049#ifdef LWS_OPENSSL_SUPPORT
1050 if (wsi->ssl && SSL_pending(wsi->ssl))
1051 goto read_pending;
1052#endif
Andy Green98a717c2011-03-06 13:14:15 +00001053 break;
Andy Green76f61e72013-01-16 11:53:05 +08001054
1055 default:
Andy Green03674a62013-01-16 11:47:40 +08001056#ifdef LWS_NO_CLIENT
1057 break;
1058#else
Andy Green76f61e72013-01-16 11:53:05 +08001059 return lws_client_socket_service(context, wsi, pollfd);
Andy Green03674a62013-01-16 11:47:40 +08001060#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001061 }
1062
1063 return 0;
1064}
1065
Andy Green0d338332011-02-12 11:57:43 +00001066
Andy Green6964bb52011-01-23 16:50:33 +00001067/**
1068 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +00001069 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001070 *
1071 * This function closes any active connections and then frees the
1072 * context. After calling this, any further use of the context is
1073 * undefined.
1074 */
1075void
Peter Hinz56885f32011-03-02 22:03:47 +00001076libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +00001077{
Andy Green3182ece2013-01-20 17:08:31 +08001078#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +00001079 int n;
1080 int m;
Andy Greena41314f2011-05-23 10:00:03 +01001081 struct libwebsocket_extension *ext;
Andy Greena7109e62013-02-11 12:05:54 +08001082 struct libwebsocket_protocols *protocol = context->protocols;
Andy Green6964bb52011-01-23 16:50:33 +00001083
Andy Greend636e352013-01-29 12:36:17 +08001084#ifdef LWS_LATENCY
1085 if (context->worst_latency_info[0])
1086 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1087#endif
1088
Andy Greendfb23042013-01-17 12:26:48 +08001089 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +08001090 struct libwebsocket *wsi =
1091 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +08001092 libwebsocket_close_and_free_session(context,
Andy Green7b922052013-02-11 11:43:05 +08001093 wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */);
1094 n--;
Andy Greendfb23042013-01-17 12:26:48 +08001095 }
Andy Green6964bb52011-01-23 16:50:33 +00001096
Andy Greena41314f2011-05-23 10:00:03 +01001097 /*
1098 * give all extensions a chance to clean up any per-context
1099 * allocations they might have made
1100 */
1101
1102 ext = context->extensions;
1103 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
1104 if (context->listen_port)
1105 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +08001106 while (ext && ext->callback) {
Andy Greenb5b23192013-02-11 17:13:32 +08001107 ext->callback(context, ext, NULL,
1108 (enum libwebsocket_extension_callback_reasons)m,
1109 NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001110 ext++;
1111 }
Andy Greena7109e62013-02-11 12:05:54 +08001112
1113 /*
1114 * inform all the protocols that they are done and will have no more
1115 * callbacks
1116 */
1117
1118 while (protocol->callback) {
1119 protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY,
1120 NULL, NULL, 0);
1121 protocol++;
1122 }
1123
Andy Green3182ece2013-01-20 17:08:31 +08001124#endif
Andy Greena41314f2011-05-23 10:00:03 +01001125
Peter Hinz56885f32011-03-02 22:03:47 +00001126#ifdef WIN32
1127#else
1128 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00001129#endif
1130
Peter Hinz56885f32011-03-02 22:03:47 +00001131#ifdef LWS_OPENSSL_SUPPORT
1132 if (context->ssl_ctx)
1133 SSL_CTX_free(context->ssl_ctx);
1134 if (context->ssl_client_ctx)
1135 SSL_CTX_free(context->ssl_client_ctx);
Andy Greenad686392013-02-11 14:50:45 +08001136
1137 ERR_remove_state(0);
1138 ERR_free_strings();
1139 EVP_cleanup();
1140 CRYPTO_cleanup_all_ex_data();
Peter Hinz56885f32011-03-02 22:03:47 +00001141#endif
1142
Andy Green46596482013-02-11 11:04:01 +08001143 if (context->fds)
1144 free(context->fds);
1145 if (context->lws_lookup)
1146 free(context->lws_lookup);
1147
Peter Hinz56885f32011-03-02 22:03:47 +00001148 free(context);
1149
1150#ifdef WIN32
1151 WSACleanup();
1152#endif
Andy Green6964bb52011-01-23 16:50:33 +00001153}
1154
Andy Greend88146d2013-01-22 12:40:35 +08001155/**
Andy Greenb5b23192013-02-11 17:13:32 +08001156 * libwebsocket_context_user() - get the user data associated with the context
Andy Greend88146d2013-01-22 12:40:35 +08001157 * @context: Websocket context
1158 *
1159 * This returns the optional user allocation that can be attached to
1160 * the context the sockets live in at context_create time. It's a way
1161 * to let all sockets serviced in the same context share data without
1162 * using globals statics in the user code.
1163 */
Alon Levy0291eb32012-10-19 11:21:56 +02001164LWS_EXTERN void *
1165libwebsocket_context_user(struct libwebsocket_context *context)
1166{
Andy Greenb5b23192013-02-11 17:13:32 +08001167 return context->user_space;
Alon Levy0291eb32012-10-19 11:21:56 +02001168}
1169
Andy Green6964bb52011-01-23 16:50:33 +00001170/**
1171 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001172 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001173 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1174 * service otherwise block and service immediately, returning
1175 * after the timeout if nothing needed service.
1176 *
1177 * This function deals with any pending websocket traffic, for three
1178 * kinds of event. It handles these events on both server and client
1179 * types of connection the same.
1180 *
1181 * 1) Accept new connections to our context's server
1182 *
Andy Green6f520a52013-01-29 17:57:39 +08001183 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001184 * server or client connections.
1185 *
1186 * You need to call this service function periodically to all the above
1187 * functions to happen; if your application is single-threaded you can
1188 * just call it in your main event loop.
1189 *
1190 * Alternatively you can fork a new process that asynchronously handles
1191 * calling this service in a loop. In that case you are happy if this
1192 * call blocks your thread until it needs to take care of something and
1193 * would call it with a large nonzero timeout. Your loop then takes no
1194 * CPU while there is nothing happening.
1195 *
1196 * If you are calling it in a single-threaded app, you don't want it to
1197 * wait around blocking other things in your loop from happening, so you
1198 * would call it with a timeout_ms of 0, so it returns immediately if
1199 * nothing is pending, or as soon as it services whatever was pending.
1200 */
1201
Andy Greene92cd172011-01-19 13:11:55 +00001202int
Peter Hinz56885f32011-03-02 22:03:47 +00001203libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001204{
1205 int n;
Andy Greene92cd172011-01-19 13:11:55 +00001206
1207 /* stay dead once we are dead */
1208
Peter Hinz56885f32011-03-02 22:03:47 +00001209 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001210 return 1;
1211
Andy Green0d338332011-02-12 11:57:43 +00001212 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001213
Peter Hinz56885f32011-03-02 22:03:47 +00001214 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green3221f922011-02-12 13:14:11 +00001215 if (n == 0) /* poll timeout */
1216 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001217
Andy Greendfb23042013-01-17 12:26:48 +08001218 if (n < 0)
Andy Green3928f612012-07-20 12:58:38 +08001219 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001220
Andy Greendfb23042013-01-17 12:26:48 +08001221 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001222
Peter Hinz56885f32011-03-02 22:03:47 +00001223 for (n = 0; n < context->fds_count; n++)
1224 if (context->fds[n].revents)
Andy Green3928f612012-07-20 12:58:38 +08001225 if (libwebsocket_service_fd(context,
1226 &context->fds[n]) < 0)
1227 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001228 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001229}
1230
Andy Green3182ece2013-01-20 17:08:31 +08001231#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001232int
1233lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001234 struct libwebsocket *wsi,
1235 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001236 void *v, size_t len)
1237{
1238 int n;
1239 int handled = 0;
1240
1241 /* maybe an extension will take care of it for us */
1242
1243 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1244 if (!wsi->active_extensions[n]->callback)
1245 continue;
1246
1247 handled |= wsi->active_extensions[n]->callback(context,
1248 wsi->active_extensions[n], wsi,
1249 r, wsi->active_extensions_user[n], v, len);
1250 }
1251
1252 return handled;
1253}
1254
1255
1256void *
1257lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001258 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001259{
1260 int n = 0;
1261
Andy Green68b45042011-05-25 21:41:57 +01001262 if (wsi == NULL)
1263 return NULL;
1264
Andy Greena41314f2011-05-23 10:00:03 +01001265 while (n < wsi->count_active_extensions) {
1266 if (wsi->active_extensions[n] != ext) {
1267 n++;
1268 continue;
1269 }
1270 return wsi->active_extensions_user[n];
1271 }
1272
1273 return NULL;
1274}
Andy Green3182ece2013-01-20 17:08:31 +08001275#endif
Andy Greena41314f2011-05-23 10:00:03 +01001276
Andy Green90c7cbc2011-01-27 06:26:52 +00001277/**
1278 * libwebsocket_callback_on_writable() - Request a callback when this socket
1279 * becomes able to be written to without
1280 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001281 *
Peter Hinz56885f32011-03-02 22:03:47 +00001282 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001283 * @wsi: Websocket connection instance to get callback for
1284 */
1285
1286int
Peter Hinz56885f32011-03-02 22:03:47 +00001287libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001288 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001289{
Andy Green3182ece2013-01-20 17:08:31 +08001290#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001291 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001292 int handled = 0;
1293
1294 /* maybe an extension will take care of it for us */
1295
1296 for (n = 0; n < wsi->count_active_extensions; n++) {
1297 if (!wsi->active_extensions[n]->callback)
1298 continue;
1299
1300 handled |= wsi->active_extensions[n]->callback(context,
1301 wsi->active_extensions[n], wsi,
1302 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1303 wsi->active_extensions_user[n], NULL, 0);
1304 }
1305
1306 if (handled)
1307 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001308#endif
Andy Greendfb23042013-01-17 12:26:48 +08001309 if (wsi->position_in_fds_table < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001310 lwsl_err("libwebsocket_callback_on_writable: failed to find socket %d\n",
1311 wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001312 return -1;
1313 }
1314
1315 context->fds[wsi->position_in_fds_table].events |= POLLOUT;
Andy Greena41314f2011-05-23 10:00:03 +01001316
Andy Green3221f922011-02-12 13:14:11 +00001317 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00001318 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001319 LWS_CALLBACK_SET_MODE_POLL_FD,
1320 (void *)(long)wsi->sock, NULL, POLLOUT);
1321
Andy Green90c7cbc2011-01-27 06:26:52 +00001322 return 1;
1323}
1324
1325/**
1326 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1327 * all connections using the given protocol when it
1328 * becomes possible to write to each socket without
1329 * blocking in turn.
1330 *
1331 * @protocol: Protocol whose connections will get callbacks
1332 */
1333
1334int
1335libwebsocket_callback_on_writable_all_protocol(
1336 const struct libwebsocket_protocols *protocol)
1337{
Peter Hinz56885f32011-03-02 22:03:47 +00001338 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001339 int n;
Andy Green0d338332011-02-12 11:57:43 +00001340 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001341
Andy Greendfb23042013-01-17 12:26:48 +08001342 for (n = 0; n < context->fds_count; n++) {
1343 wsi = context->lws_lookup[context->fds[n].fd];
1344 if (!wsi)
1345 continue;
1346 if (wsi->protocol == protocol)
1347 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001348 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001349
1350 return 0;
1351}
1352
Andy Greenbe93fef2011-02-14 20:25:43 +00001353/**
1354 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1355 *
1356 * You will not need this unless you are doing something special
1357 *
1358 * @wsi: Websocket connection instance
1359 * @reason: timeout reason
1360 * @secs: how many seconds
1361 */
1362
1363void
1364libwebsocket_set_timeout(struct libwebsocket *wsi,
1365 enum pending_timeout reason, int secs)
1366{
1367 struct timeval tv;
1368
1369 gettimeofday(&tv, NULL);
1370
1371 wsi->pending_timeout_limit = tv.tv_sec + secs;
1372 wsi->pending_timeout = reason;
1373}
1374
Andy Greena6cbece2011-01-27 20:06:03 +00001375
1376/**
1377 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1378 *
1379 * You will not need this unless you are doing something special
1380 *
1381 * @wsi: Websocket connection instance
1382 */
1383
1384int
1385libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1386{
1387 return wsi->sock;
1388}
1389
Andy Greend636e352013-01-29 12:36:17 +08001390#ifdef LWS_LATENCY
1391void
Andy Greenb5b23192013-02-11 17:13:32 +08001392lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi,
1393 const char *action, int ret, int completed)
Andy Greend636e352013-01-29 12:36:17 +08001394{
1395 struct timeval tv;
1396 unsigned long u;
1397 char buf[256];
1398
1399 gettimeofday(&tv, NULL);
1400
1401 u = (tv.tv_sec * 1000000) + tv.tv_usec;
1402
1403 if (action) {
1404 if (completed) {
1405 if (wsi->action_start == wsi->latency_start)
Andy Greenb5b23192013-02-11 17:13:32 +08001406 sprintf(buf,
1407 "Completion first try lat %luus: %p: ret %d: %s\n",
1408 u - wsi->latency_start,
1409 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001410 else
Andy Greenb5b23192013-02-11 17:13:32 +08001411 sprintf(buf,
1412 "Completion %luus: lat %luus: %p: ret %d: %s\n",
1413 u - wsi->action_start,
1414 u - wsi->latency_start,
1415 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001416 wsi->action_start = 0;
1417 } else
Andy Greenb5b23192013-02-11 17:13:32 +08001418 sprintf(buf, "lat %luus: %p: ret %d: %s\n",
1419 u - wsi->latency_start,
1420 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001421 if (u - wsi->latency_start > context->worst_latency) {
1422 context->worst_latency = u - wsi->latency_start;
1423 strcpy(context->worst_latency_info, buf);
1424 }
1425 lwsl_latency("%s", buf);
1426 } else {
1427 wsi->latency_start = u;
1428 if (!wsi->action_start)
1429 wsi->action_start = u;
1430 }
1431}
1432#endif
1433
Andy Greena1ce6be2013-01-18 11:43:21 +08001434#ifdef LWS_NO_SERVER
1435int
1436_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1437{
1438 return 0;
1439}
1440#else
Andy Green706961d2013-01-17 16:50:35 +08001441int
1442_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1443{
1444 struct libwebsocket_context *context = wsi->protocol->owning_server;
1445 int n;
1446
Andy Green623a98d2013-01-21 11:04:23 +08001447 if (!(wsi->u.ws.rxflow_change_to & 2))
Andy Green706961d2013-01-17 16:50:35 +08001448 return 0;
1449
Andy Green623a98d2013-01-21 11:04:23 +08001450 wsi->u.ws.rxflow_change_to &= ~2;
Andy Green706961d2013-01-17 16:50:35 +08001451
Andy Greenb5b23192013-02-11 17:13:32 +08001452 lwsl_info("rxflow: wsi %p change_to %d\n",
1453 wsi, wsi->u.ws.rxflow_change_to);
Andy Green706961d2013-01-17 16:50:35 +08001454
1455 /* if we're letting it come again, did we interrupt anything? */
Andy Green623a98d2013-01-21 11:04:23 +08001456 if ((wsi->u.ws.rxflow_change_to & 1) && wsi->u.ws.rxflow_buffer) {
Andy Green706961d2013-01-17 16:50:35 +08001457 n = libwebsocket_interpret_incoming_packet(wsi, NULL, 0);
1458 if (n < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001459 lwsl_info("libwebsocket_rx_flow_control: close req\n");
Andy Green706961d2013-01-17 16:50:35 +08001460 return -1;
1461 }
1462 if (n)
1463 /* oh he stuck again, do nothing */
1464 return 0;
1465 }
1466
Andy Green623a98d2013-01-21 11:04:23 +08001467 if (wsi->u.ws.rxflow_change_to & 1)
Andy Green706961d2013-01-17 16:50:35 +08001468 context->fds[wsi->position_in_fds_table].events |= POLLIN;
1469 else
1470 context->fds[wsi->position_in_fds_table].events &= ~POLLIN;
1471
Andy Green623a98d2013-01-21 11:04:23 +08001472 if (wsi->u.ws.rxflow_change_to & 1)
Andy Green706961d2013-01-17 16:50:35 +08001473 /* external POLL support via protocol 0 */
1474 context->protocols[0].callback(context, wsi,
1475 LWS_CALLBACK_SET_MODE_POLL_FD,
1476 (void *)(long)wsi->sock, NULL, POLLIN);
1477 else
1478 /* external POLL support via protocol 0 */
1479 context->protocols[0].callback(context, wsi,
1480 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
1481 (void *)(long)wsi->sock, NULL, POLLIN);
1482
1483 return 1;
1484}
Andy Greena1ce6be2013-01-18 11:43:21 +08001485#endif
Andy Green706961d2013-01-17 16:50:35 +08001486
Andy Green90c7cbc2011-01-27 06:26:52 +00001487/**
1488 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1489 * receieved packets.
1490 *
1491 * If the output side of a server process becomes choked, this allows flow
1492 * control for the input side.
1493 *
1494 * @wsi: Websocket connection instance to get callback for
1495 * @enable: 0 = disable read servicing for this connection, 1 = enable
1496 */
1497
1498int
1499libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1500{
Andy Green623a98d2013-01-21 11:04:23 +08001501 wsi->u.ws.rxflow_change_to = 2 | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001502
Andy Green706961d2013-01-17 16:50:35 +08001503 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001504}
1505
Andy Green706961d2013-01-17 16:50:35 +08001506
Andy Green2ac5a6f2011-01-28 10:00:18 +00001507/**
1508 * libwebsocket_canonical_hostname() - returns this host's hostname
1509 *
1510 * This is typically used by client code to fill in the host parameter
1511 * when making a client connection. You can only call it after the context
1512 * has been created.
1513 *
Peter Hinz56885f32011-03-02 22:03:47 +00001514 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001515 */
1516
1517
1518extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001519libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001520{
Peter Hinz56885f32011-03-02 22:03:47 +00001521 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001522}
1523
1524
Andy Green90c7cbc2011-01-27 06:26:52 +00001525static void sigpipe_handler(int x)
1526{
1527}
1528
Andy Green6901cb32011-02-21 08:06:47 +00001529#ifdef LWS_OPENSSL_SUPPORT
1530static int
1531OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1532{
1533
1534 SSL *ssl;
1535 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001536 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001537
1538 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1539 SSL_get_ex_data_X509_STORE_CTX_idx());
1540
1541 /*
Andy Green2e24da02011-03-05 16:12:04 +00001542 * !!! nasty openssl requires the index to come as a library-scope
1543 * static
Andy Green6901cb32011-02-21 08:06:47 +00001544 */
Andy Green2e24da02011-03-05 16:12:04 +00001545 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001546
Peter Hinz56885f32011-03-02 22:03:47 +00001547 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001548 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1549 x509_ctx, ssl, preverify_ok);
1550
1551 /* convert return code from 0 = OK to 1 = OK */
1552
1553 if (!n)
1554 n = 1;
1555 else
1556 n = 0;
1557
1558 return n;
1559}
1560#endif
1561
Andy Green706961d2013-01-17 16:50:35 +08001562int user_callback_handle_rxflow(callback_function callback_function,
Andy Greenb5b23192013-02-11 17:13:32 +08001563 struct libwebsocket_context *context,
Andy Green706961d2013-01-17 16:50:35 +08001564 struct libwebsocket *wsi,
1565 enum libwebsocket_callback_reasons reason, void *user,
1566 void *in, size_t len)
1567{
1568 int n;
1569
1570 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenaedc9532013-02-10 21:21:24 +08001571 if (!n)
1572 n = _libwebsocket_rx_flow_control(wsi);
Andy Green706961d2013-01-17 16:50:35 +08001573
Andy Greenaedc9532013-02-10 21:21:24 +08001574 return n;
Andy Green706961d2013-01-17 16:50:35 +08001575}
1576
Andy Greenb45993c2010-12-18 15:13:50 +00001577
Andy Greenab990e42010-10-31 12:42:52 +00001578/**
Andy Green4739e5c2011-01-22 12:51:57 +00001579 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08001580 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00001581 *
Andy Green1b265272013-02-09 14:01:09 +08001582 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00001583 * of all initialization in one step.
1584 *
Andy Greene92cd172011-01-19 13:11:55 +00001585 * After initialization, it returns a struct libwebsocket_context * that
1586 * represents this server. After calling, user code needs to take care
1587 * of calling libwebsocket_service() with the context pointer to get the
1588 * server's sockets serviced. This can be done in the same process context
1589 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001590 *
Andy Green8f037e42010-12-19 22:13:26 +00001591 * The protocol callback functions are called for a handful of events
1592 * including http requests coming in, websocket connections becoming
1593 * established, and data arriving; it's also called periodically to allow
1594 * async transmission.
1595 *
1596 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1597 * at that time websocket protocol has not been negotiated. Other
1598 * protocols after the first one never see any HTTP callack activity.
1599 *
1600 * The server created is a simple http server by default; part of the
1601 * websocket standard is upgrading this http connection to a websocket one.
1602 *
1603 * This allows the same server to provide files like scripts and favicon /
1604 * images or whatever over http and dynamic data over websockets all in
1605 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001606 */
Andy Green4ea60062010-10-30 12:15:07 +01001607
Andy Greene92cd172011-01-19 13:11:55 +00001608struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08001609libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01001610{
1611 int n;
Peter Hinz56885f32011-03-02 22:03:47 +00001612 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001613 char *p;
Andy Greencbb31222013-01-31 09:57:05 +08001614#ifndef LWS_NO_SERVER
1615 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00001616 struct libwebsocket *wsi;
Andy Greencbb31222013-01-31 09:57:05 +08001617 struct sockaddr_in serv_addr;
1618#endif
Andy Green3182ece2013-01-20 17:08:31 +08001619#ifndef LWS_NO_EXTENSIONS
1620 int m;
Andy Green1b265272013-02-09 14:01:09 +08001621 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08001622#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001623
Andy Green3faa9c72010-11-08 17:03:03 +00001624#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001625 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001626#endif
1627
Joakim Soderberg4c531232013-02-06 15:26:58 +09001628#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001629 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09001630#endif
1631
Andy Greenb3a614a2013-01-19 13:08:17 +08001632 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08001633 lwsl_notice("Library version: %s\n", library_version);
Andy Greenb5b23192013-02-11 17:13:32 +08001634 lwsl_info(" LWS_MAX_HEADER_NAME_LENGTH: %u\n",
1635 LWS_MAX_HEADER_NAME_LENGTH);
Andy Greenc0d6b632013-01-12 23:42:17 +08001636 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
Andy Greenc0d6b632013-01-12 23:42:17 +08001637 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001638#ifndef LWS_NO_EXTENSIONS
Andy Greenb5b23192013-02-11 17:13:32 +08001639 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n",
1640 LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001641#else
1642 lwsl_notice(" Configured without extension support\n");
1643#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001644 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1645 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
1646 lwsl_info(" CIPHERS_LIST_STRING: '%s'\n", CIPHERS_LIST_STRING);
1647 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1648 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001649
Peter Hinz56885f32011-03-02 22:03:47 +00001650#ifdef _WIN32
1651 {
1652 WORD wVersionRequested;
1653 WSADATA wsaData;
1654 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001655 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001656
1657 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1658 wVersionRequested = MAKEWORD(2, 2);
1659
1660 err = WSAStartup(wVersionRequested, &wsaData);
1661 if (err != 0) {
1662 /* Tell the user that we could not find a usable */
1663 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001664 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001665 return NULL;
1666 }
David Galeano7b11fec2011-10-04 19:55:18 +08001667
Andy Green6ee372f2012-04-09 15:09:01 +08001668 /* default to a poll() made out of select() */
1669 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001670
Andy Green6ee372f2012-04-09 15:09:01 +08001671 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001672 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001673 if (wsdll)
1674 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09001675
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001676 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09001677 if (!poll)
1678 poll = emulated_poll;
Peter Hinz56885f32011-03-02 22:03:47 +00001679 }
1680#endif
1681
Andy Greenb5b23192013-02-11 17:13:32 +08001682 context = (struct libwebsocket_context *)
1683 malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001684 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001685 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001686 return NULL;
1687 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001688 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08001689#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001690 context->started_with_parent = pid_daemon;
1691 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1692#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001693
Joakim Soderberg4c531232013-02-06 15:26:58 +09001694 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08001695 context->protocols = info->protocols;
1696 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00001697 context->http_proxy_port = 0;
1698 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08001699 context->options = info->options;
Andy Greendfb23042013-01-17 12:26:48 +08001700 /* to reduce this allocation, */
1701 context->max_fds = getdtablesize();
Andy Greena86f6342013-02-11 11:04:56 +08001702 lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n",
1703 sizeof(struct libwebsocket_context),
1704 sizeof(struct pollfd) + sizeof(struct libwebsocket *),
1705 context->max_fds,
Andy Greenb5b23192013-02-11 17:13:32 +08001706 sizeof(struct libwebsocket_context) +
1707 ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) *
1708 context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08001709
Andy Greenb5b23192013-02-11 17:13:32 +08001710 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) *
1711 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001712 if (context->fds == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001713 lwsl_err("Unable to allocate fds array for %d connections\n",
1714 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001715 free(context);
1716 return NULL;
1717 }
Andy Greenb5b23192013-02-11 17:13:32 +08001718 context->lws_lookup = (struct libwebsocket **)
1719 malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001720 if (context->lws_lookup == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001721 lwsl_err(
1722 "Unable to allocate lws_lookup array for %d connections\n",
1723 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001724 free(context->fds);
1725 free(context);
1726 return NULL;
1727 }
Andy Greena17c6922013-01-20 20:21:54 +08001728
Peter Hinz56885f32011-03-02 22:03:47 +00001729 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001730#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08001731 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08001732#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08001733 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08001734 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00001735
Peter Hinz56885f32011-03-02 22:03:47 +00001736#ifdef WIN32
1737 context->fd_random = 0;
1738#else
1739 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
1740 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001741 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001742 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001743 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00001744 }
Peter Hinz56885f32011-03-02 22:03:47 +00001745#endif
Andy Green44eee682011-02-10 09:32:24 +00001746
Peter Hinz56885f32011-03-02 22:03:47 +00001747#ifdef LWS_OPENSSL_SUPPORT
1748 context->use_ssl = 0;
1749 context->ssl_ctx = NULL;
1750 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00001751 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001752#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00001753
Andy Greena1ce6be2013-01-18 11:43:21 +08001754 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08001755
Andy Greena1ce6be2013-01-18 11:43:21 +08001756#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001757 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Greena1ce6be2013-01-18 11:43:21 +08001758 struct sockaddr sa;
Andy Greene310b0c2013-02-10 15:10:10 +08001759 context->service_buffer[0] = '\0';
Andy Green788c4a82012-10-22 12:29:57 +01001760
1761 /* find canonical hostname */
1762
Andy Greenb5b23192013-02-11 17:13:32 +08001763 context->service_buffer[
1764 sizeof(context->service_buffer) - 1] = '\0';
Andy Green788c4a82012-10-22 12:29:57 +01001765 memset(&sa, 0, sizeof(sa));
1766 sa.sa_family = AF_INET;
Andy Greenb5b23192013-02-11 17:13:32 +08001767 sa.sa_data[sizeof(sa.sa_data) - 1] = '\0';
1768 gethostname((char *)context->service_buffer,
1769 sizeof(context->service_buffer) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01001770
1771 n = 0;
1772
Andy Greenb5b23192013-02-11 17:13:32 +08001773 if (strlen((char *)context->service_buffer) <
1774 sizeof(sa.sa_data) - 1) {
Andy Greene310b0c2013-02-10 15:10:10 +08001775 strcpy(sa.sa_data, (char *)context->service_buffer);
Andy Greend09d7d42013-01-31 10:05:43 +08001776 lwsl_debug("my host name is %s\n", sa.sa_data);
Andy Greenb5b23192013-02-11 17:13:32 +08001777 n = getnameinfo(&sa, sizeof(sa),
1778 (char *)context->service_buffer,
1779 sizeof(context->service_buffer) - 1,
1780 NULL, 0, NI_NAMEREQD);
Andy Green788c4a82012-10-22 12:29:57 +01001781 }
1782
1783 if (!n) {
Andy Greenb5b23192013-02-11 17:13:32 +08001784 strncpy(context->canonical_hostname,
1785 (char *)context->service_buffer,
1786 sizeof(context->canonical_hostname) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01001787 context->canonical_hostname[
Andy Greenb5b23192013-02-11 17:13:32 +08001788 sizeof(context->canonical_hostname) - 1] = '\0';
Andy Green788c4a82012-10-22 12:29:57 +01001789 } else
Andy Greenb5b23192013-02-11 17:13:32 +08001790 strncpy(context->canonical_hostname,
1791 (char *)context->service_buffer,
1792 sizeof(context->canonical_hostname) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01001793
Andy Greenb5b23192013-02-11 17:13:32 +08001794 lwsl_notice(" canonical_hostname = %s\n",
1795 context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08001796 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001797#endif
Andy Greena69f0512012-05-03 12:32:38 +08001798
Andy Green9659f372011-01-27 22:01:43 +00001799 /* split the proxy ads:port if given */
1800
1801 p = getenv("http_proxy");
1802 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00001803 strncpy(context->http_proxy_address, p,
Andy Greenb5b23192013-02-11 17:13:32 +08001804 sizeof(context->http_proxy_address) - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00001805 context->http_proxy_address[
Andy Greenb5b23192013-02-11 17:13:32 +08001806 sizeof(context->http_proxy_address) - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00001807
Peter Hinz56885f32011-03-02 22:03:47 +00001808 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00001809 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001810 lwsl_err("http_proxy needs to be ads:port\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001811 goto bail;
Andy Green9659f372011-01-27 22:01:43 +00001812 }
1813 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00001814 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00001815
Andy Greenb3a614a2013-01-19 13:08:17 +08001816 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001817 context->http_proxy_address,
1818 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00001819 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001820
Andy Greena1ce6be2013-01-18 11:43:21 +08001821#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001822 if (info->port) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001823
Andy Green3faa9c72010-11-08 17:03:03 +00001824#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08001825 context->use_ssl = info->ssl_cert_filepath != NULL &&
1826 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09001827#ifdef USE_CYASSL
1828 lwsl_notice(" Compiled with CYASSL support\n");
1829#else
1830 lwsl_notice(" Compiled with OpenSSL support\n");
1831#endif
Peter Hinz56885f32011-03-02 22:03:47 +00001832 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09001833 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001834 else
Andy Green23c5f2e2013-02-06 15:43:00 +09001835 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00001836
Andy Green90c7cbc2011-01-27 06:26:52 +00001837#else
Andy Green2b40b792013-02-10 22:22:01 +08001838 if (info->ssl_cert_filepath != NULL &&
1839 info->ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08001840 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001841 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001842 }
Andy Greenb5b23192013-02-11 17:13:32 +08001843 lwsl_notice(" Compiled without SSL support\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001844#endif
Andy Greena17c6922013-01-20 20:21:54 +08001845
Andy Greenb5b23192013-02-11 17:13:32 +08001846 lwsl_notice(
1847 " per-conn mem: %u + %u headers + protocol rx buf\n",
1848 sizeof(struct libwebsocket),
1849 sizeof(struct allocated_headers));
Andy Green90c7cbc2011-01-27 06:26:52 +00001850 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001851#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001852
1853 /* ignore SIGPIPE */
Peter Hinz56885f32011-03-02 22:03:47 +00001854#ifdef WIN32
1855#else
Andy Green90c7cbc2011-01-27 06:26:52 +00001856 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00001857#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001858
1859
1860#ifdef LWS_OPENSSL_SUPPORT
1861
1862 /* basic openssl init */
1863
1864 SSL_library_init();
1865
1866 OpenSSL_add_all_algorithms();
1867 SSL_load_error_strings();
1868
Andy Green2e24da02011-03-05 16:12:04 +00001869 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00001870 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
1871
Andy Green90c7cbc2011-01-27 06:26:52 +00001872 /*
1873 * Firefox insists on SSLv23 not SSLv3
1874 * Konq disables SSLv2 by default now, SSLv23 works
1875 */
1876
1877 method = (SSL_METHOD *)SSLv23_server_method();
1878 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001879 lwsl_err("problem creating ssl method: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001880 ERR_error_string(ERR_get_error(),
1881 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001882 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001883 }
Peter Hinz56885f32011-03-02 22:03:47 +00001884 context->ssl_ctx = SSL_CTX_new(method); /* create context */
1885 if (!context->ssl_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001886 lwsl_err("problem creating ssl context: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001887 ERR_error_string(ERR_get_error(),
1888 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001889 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001890 }
1891
David Galeanocc148e42013-01-10 10:18:59 +08001892#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001893 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001894#endif
David Galeano77a677c2013-01-10 10:14:12 +08001895 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
David Galeanof177f2a2013-01-10 10:15:19 +08001896 SSL_CTX_set_cipher_list(context->ssl_ctx, CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001897
Andy Greena1ce6be2013-01-18 11:43:21 +08001898#ifndef LWS_NO_CLIENT
1899
Andy Green90c7cbc2011-01-27 06:26:52 +00001900 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08001901
Andy Green1b265272013-02-09 14:01:09 +08001902 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00001903 method = (SSL_METHOD *)SSLv23_client_method();
1904 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001905 lwsl_err("problem creating ssl method: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001906 ERR_error_string(ERR_get_error(),
1907 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001908 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001909 }
1910 /* create context */
1911 context->ssl_client_ctx = SSL_CTX_new(method);
1912 if (!context->ssl_client_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001913 lwsl_err("problem creating ssl context: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001914 ERR_error_string(ERR_get_error(),
1915 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001916 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001917 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001918
David Galeanocc148e42013-01-10 10:18:59 +08001919#ifdef SSL_OP_NO_COMPRESSION
Andy Greenb5b23192013-02-11 17:13:32 +08001920 SSL_CTX_set_options(context->ssl_client_ctx,
1921 SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001922#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001923 SSL_CTX_set_options(context->ssl_client_ctx,
1924 SSL_OP_CIPHER_SERVER_PREFERENCE);
1925 SSL_CTX_set_cipher_list(context->ssl_client_ctx,
1926 CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001927
Peter Hinz56885f32011-03-02 22:03:47 +00001928 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08001929 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08001930 if (!SSL_CTX_load_verify_locations(
1931 context->ssl_client_ctx, NULL,
1932 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08001933 lwsl_err(
Andy Greenb5b23192013-02-11 17:13:32 +08001934 "Unable to load SSL Client certs from %s "
1935 "(set by --with-client-cert-dir= "
1936 "in configure) -- client ssl isn't "
1937 "going to work", LWS_OPENSSL_CLIENT_CERTS);
David Galeano2f82be82013-01-09 16:25:54 +08001938 } else
1939 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08001940 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08001941 NULL))
Andy Green43db0452013-01-10 19:50:35 +08001942 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08001943 "Unable to load SSL Client certs "
1944 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08001945 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00001946
1947 /*
1948 * callback allowing user code to load extra verification certs
1949 * helping the client to verify server identity
1950 */
1951
1952 context->protocols[0].callback(context, NULL,
1953 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
1954 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00001955 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001956#endif
Andy Green6ee372f2012-04-09 15:09:01 +08001957
Andy Greenc6bf2c22011-02-20 11:10:47 +00001958 /* as a server, are we requiring clients to identify themselves? */
1959
Andy Greenb5b23192013-02-11 17:13:32 +08001960 if (info->options &
1961 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00001962
1963 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08001964
Peter Hinz56885f32011-03-02 22:03:47 +00001965 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00001966 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1967 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001968
1969 /*
1970 * give user code a chance to load certs into the server
1971 * allowing it to verify incoming client certs
1972 */
1973
Peter Hinz56885f32011-03-02 22:03:47 +00001974 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00001975 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00001976 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001977 }
1978
Peter Hinz56885f32011-03-02 22:03:47 +00001979 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001980
1981 /* openssl init for server sockets */
1982
Andy Green3faa9c72010-11-08 17:03:03 +00001983 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08001984 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08001985 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00001986 if (n != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001987 lwsl_err("problem getting cert '%s': %s\n",
Andy Green1b265272013-02-09 14:01:09 +08001988 info->ssl_cert_filepath,
Andy Greenb5b23192013-02-11 17:13:32 +08001989 ERR_error_string(ERR_get_error(),
1990 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001991 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001992 }
1993 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00001994 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08001995 info->ssl_private_key_filepath,
1996 SSL_FILETYPE_PEM) != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001997 lwsl_err("ssl problem getting key '%s': %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001998 info->ssl_private_key_filepath,
1999 ERR_error_string(ERR_get_error(),
2000 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002001 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002002 }
2003 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00002004 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08002005 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002006 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002007 }
2008
2009 /* SSL is happy and has a cert it's content with */
2010 }
2011#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002012
Andy Greendf736162011-01-18 15:39:02 +00002013 /* selftest */
2014
2015 if (lws_b64_selftest())
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002016 goto bail;
Andy Greendf736162011-01-18 15:39:02 +00002017
Andy Greena1ce6be2013-01-18 11:43:21 +08002018#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00002019 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00002020
Andy Green1b265272013-02-09 14:01:09 +08002021 if (info->port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08002022 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00002023
Andy Green4739e5c2011-01-22 12:51:57 +00002024 sockfd = socket(AF_INET, SOCK_STREAM, 0);
2025 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08002026 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002027 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002028 }
Andy Green775c0dd2010-10-29 14:15:22 +01002029
Joakim Soderberg4c531232013-02-06 15:26:58 +09002030#ifndef WIN32
Andy Greenb5b23192013-02-11 17:13:32 +08002031 /*
2032 * allow us to restart even if old sockets in TIME_WAIT
2033 * (REUSEADDR on Unix means, "don't hang on to this
2034 * address after the listener is closed." On Windows, though,
2035 * it means "don't keep other processes from binding to
2036 * this address while we're using it)
2037 */
Andy Green6ee372f2012-04-09 15:09:01 +08002038 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
2039 (const void *)&opt, sizeof(opt));
Joakim Soderberg4c531232013-02-06 15:26:58 +09002040#endif
Andy Green6c939552011-03-08 08:56:57 +00002041
2042 /* Disable Nagle */
2043 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08002044 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
2045 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002046
Joakim Soderberg4c531232013-02-06 15:26:58 +09002047 #ifdef WIN32
2048 opt = 0;
Andy Greenb5b23192013-02-11 17:13:32 +08002049 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002050 #else
Andy Greene2160712013-01-28 12:19:10 +08002051 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002052 #endif
Andy Greene2160712013-01-28 12:19:10 +08002053
Andy Green4739e5c2011-01-22 12:51:57 +00002054 bzero((char *) &serv_addr, sizeof(serv_addr));
2055 serv_addr.sin_family = AF_INET;
Andy Green1b265272013-02-09 14:01:09 +08002056 if (info->interface == NULL)
Andy Green32375b72011-02-19 08:32:53 +00002057 serv_addr.sin_addr.s_addr = INADDR_ANY;
2058 else
Andy Green1b265272013-02-09 14:01:09 +08002059 interface_to_sa(info->interface, &serv_addr,
Andy Green32375b72011-02-19 08:32:53 +00002060 sizeof(serv_addr));
Andy Green1b265272013-02-09 14:01:09 +08002061 serv_addr.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00002062
2063 n = bind(sockfd, (struct sockaddr *) &serv_addr,
2064 sizeof(serv_addr));
2065 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002066 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green1b265272013-02-09 14:01:09 +08002067 info->port, n, errno);
Andy Green41c58032013-01-12 13:21:08 +08002068 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002069 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002070 }
Andy Green0d338332011-02-12 11:57:43 +00002071
Andy Greenb5b23192013-02-11 17:13:32 +08002072 wsi = (struct libwebsocket *)malloc(
2073 sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08002074 if (wsi == NULL) {
2075 lwsl_err("Out of mem\n");
2076 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002077 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08002078 }
Andy Greenb5b23192013-02-11 17:13:32 +08002079 memset(wsi, 0, sizeof(struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00002080 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08002081#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00002082 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002083#endif
Andy Green0d338332011-02-12 11:57:43 +00002084 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08002085
2086 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002087
Andy Green65b0e912013-01-16 07:59:47 +08002088 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
2089 context->listen_service_count = 0;
2090 context->listen_service_fd = sockfd;
2091
Andy Greena824d182013-01-15 20:52:29 +08002092 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08002093 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00002094 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002095#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002096
Andy Green6ee372f2012-04-09 15:09:01 +08002097 /*
2098 * drop any root privs for this process
2099 * to listen on port < 1023 we would have needed root, but now we are
2100 * listening, we don't want the power for anything else
2101 */
Peter Hinz56885f32011-03-02 22:03:47 +00002102#ifdef WIN32
2103#else
Andy Green1b265272013-02-09 14:01:09 +08002104 if (info->gid != -1)
2105 if (setgid(info->gid))
Andy Green43db0452013-01-10 19:50:35 +08002106 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green1b265272013-02-09 14:01:09 +08002107 if (info->uid != -1)
2108 if (setuid(info->uid))
Andy Green43db0452013-01-10 19:50:35 +08002109 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00002110#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002111
Andy Green6f520a52013-01-29 17:57:39 +08002112 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00002113
Peter Hinz56885f32011-03-02 22:03:47 +00002114 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08002115 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00002116 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002117
Andy Green43db0452013-01-10 19:50:35 +08002118 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002119 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002120
Andy Green1b265272013-02-09 14:01:09 +08002121 info->protocols[context->count_protocols].owning_server =
2122 context;
2123 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00002124 context->count_protocols;
Andy Greena7109e62013-02-11 12:05:54 +08002125
2126 /*
2127 * inform all the protocols that they are doing their one-time
2128 * initialization if they want to
2129 */
2130 info->protocols[context->count_protocols].callback(context,
2131 NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00002132 }
Andy Greenf5bc1302013-01-21 09:09:52 +08002133
Andy Green3182ece2013-01-20 17:08:31 +08002134#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01002135 /*
2136 * give all extensions a chance to create any per-context
2137 * allocations they need
2138 */
2139
2140 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andy Green1b265272013-02-09 14:01:09 +08002141 if (info->port)
Andy Greena41314f2011-05-23 10:00:03 +01002142 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andy Greenb5b23192013-02-11 17:13:32 +08002143
Andy Green1b265272013-02-09 14:01:09 +08002144 if (info->extensions) {
2145 ext = info->extensions;
2146 while (ext->callback) {
2147 lwsl_ext(" Extension: %s\n", ext->name);
2148 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002149 (enum libwebsocket_extension_callback_reasons)m,
2150 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08002151 ext++;
2152 }
Andy Greena41314f2011-05-23 10:00:03 +01002153 }
Andy Green3182ece2013-01-20 17:08:31 +08002154#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002155 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002156
2157bail:
2158 libwebsocket_context_destroy(context);
2159 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00002160}
Andy Greenb45993c2010-12-18 15:13:50 +00002161
Andy Greenb45993c2010-12-18 15:13:50 +00002162/**
2163 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002164 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002165 * @wsi: pointer to struct websocket you want to know the protocol of
2166 *
Andy Green8f037e42010-12-19 22:13:26 +00002167 *
Andy Green6f520a52013-01-29 17:57:39 +08002168 * Some apis can act on all live connections of a given protocol,
2169 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002170 */
Andy Greenab990e42010-10-31 12:42:52 +00002171
Andy Greenb45993c2010-12-18 15:13:50 +00002172const struct libwebsocket_protocols *
2173libwebsockets_get_protocol(struct libwebsocket *wsi)
2174{
2175 return wsi->protocol;
2176}
2177
Andy Green82c3d542011-03-07 21:16:31 +00002178int
2179libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2180{
Andy Green623a98d2013-01-21 11:04:23 +08002181 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002182}
Alex Bligh49146db2011-11-07 17:19:25 +08002183
David Galeanoe2cf9922013-01-09 18:06:55 +08002184unsigned char
2185libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2186{
Andy Green623a98d2013-01-21 11:04:23 +08002187 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002188}
2189
Alex Bligh49146db2011-11-07 17:19:25 +08002190void *
2191libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2192{
2193 /* allocate the per-connection user memory (if any) */
2194
2195 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2196 wsi->user_space = malloc(
2197 wsi->protocol->per_session_data_size);
2198 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002199 lwsl_err("Out of memory for conn user space\n");
Alex Bligh49146db2011-11-07 17:19:25 +08002200 return NULL;
2201 }
Andy Green6ee372f2012-04-09 15:09:01 +08002202 memset(wsi->user_space, 0,
2203 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002204 }
2205 return wsi->user_space;
2206}
Andy Green43db0452013-01-10 19:50:35 +08002207
Andy Green0b319092013-01-19 11:17:56 +08002208static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002209{
Andy Green0b319092013-01-19 11:17:56 +08002210 char buf[300];
2211 struct timeval tv;
Andy Green0b319092013-01-19 11:17:56 +08002212 int n;
2213
2214 gettimeofday(&tv, NULL);
2215
2216 buf[0] = '\0';
2217 for (n = 0; n < LLL_COUNT; n++)
2218 if (level == (1 << n)) {
Andy Green058ba812013-01-19 11:32:18 +08002219 sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
Andy Greenb5b23192013-02-11 17:13:32 +08002220 (int)(tv.tv_usec / 100), log_level_names[n]);
Andy Green0b319092013-01-19 11:17:56 +08002221 break;
2222 }
Andy Greenb5b23192013-02-11 17:13:32 +08002223
Andy Green0b319092013-01-19 11:17:56 +08002224 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002225}
2226
Joakim Soderberg4c531232013-02-06 15:26:58 +09002227#ifdef WIN32
2228void lwsl_emit_syslog(int level, const char *line)
2229{
2230 lwsl_emit_stderr(level, line);
2231}
2232#else
Andy Greenc11db202013-01-19 11:12:16 +08002233void lwsl_emit_syslog(int level, const char *line)
2234{
2235 int syslog_level = LOG_DEBUG;
2236
2237 switch (level) {
2238 case LLL_ERR:
2239 syslog_level = LOG_ERR;
2240 break;
2241 case LLL_WARN:
2242 syslog_level = LOG_WARNING;
2243 break;
2244 case LLL_NOTICE:
2245 syslog_level = LOG_NOTICE;
2246 break;
2247 case LLL_INFO:
2248 syslog_level = LOG_INFO;
2249 break;
2250 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002251 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002252}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002253#endif
Andy Greenc11db202013-01-19 11:12:16 +08002254
Andy Green43db0452013-01-10 19:50:35 +08002255void _lws_log(int filter, const char *format, ...)
2256{
Andy Greende8f27a2013-01-12 09:17:42 +08002257 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002258 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002259
2260 if (!(log_level & filter))
2261 return;
2262
Andy Green43db0452013-01-10 19:50:35 +08002263 va_start(ap, format);
Andy Greenb5b23192013-02-11 17:13:32 +08002264 vsnprintf(buf, sizeof(buf), format, ap);
2265 buf[sizeof(buf) - 1] = '\0';
Andy Greende8f27a2013-01-12 09:17:42 +08002266 va_end(ap);
2267
Andy Green0b319092013-01-19 11:17:56 +08002268 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002269}
2270
2271/**
2272 * lws_set_log_level() - Set the logging bitfield
2273 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002274 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2275 * function to perform log string emission instead of
2276 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002277 *
Andy Greende8f27a2013-01-12 09:17:42 +08002278 * log level defaults to "err" and "warn" contexts enabled only and
2279 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002280 */
2281
Andy Greenb5b23192013-02-11 17:13:32 +08002282void lws_set_log_level(int level, void (*log_emit_function)(int level,
2283 const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002284{
2285 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002286 if (log_emit_function)
2287 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002288}