blob: ed0db66fe263ebc49e9e4115a539d2fb5eb19aa1 [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
Andy Green7627af52011-03-09 15:13:52 +000033#include <sys/un.h>
Andy Greena69f0512012-05-03 12:32:38 +080034#include <sys/socket.h>
35#include <netdb.h>
Peter Hinz56885f32011-03-02 22:03:47 +000036#endif
Andy Green2e24da02011-03-05 16:12:04 +000037
38#ifdef LWS_OPENSSL_SUPPORT
39int openssl_websocket_private_data_index;
40#endif
41
Andy Greenaa6fc442012-04-12 13:26:49 +080042#ifdef __MINGW32__
43#include "../win32port/win32helpers/websock-w32.c"
44#else
45#ifdef __MINGW64__
46#include "../win32port/win32helpers/websock-w32.c"
47#endif
48#endif
49
Andy Green7c19c342013-01-19 12:18:07 +080050static int log_level = LLL_ERR | LLL_WARN | LLL_NOTICE;
Andy Greende8f27a2013-01-12 09:17:42 +080051static void lwsl_emit_stderr(const char *line);
52static void (*lwsl_emit)(const char *line) = lwsl_emit_stderr;
Andy Green43db0452013-01-10 19:50:35 +080053static const char *log_level_names[] = {
54 "ERR",
55 "WARN",
Andy Green7c19c342013-01-19 12:18:07 +080056 "NOTICE",
Andy Green43db0452013-01-10 19:50:35 +080057 "INFO",
58 "DEBUG",
59 "PARSER",
60 "HEADER",
61 "EXTENSION",
62 "CLIENT",
63};
64
Andy Green0d338332011-02-12 11:57:43 +000065int
Andy Greendfb23042013-01-17 12:26:48 +080066insert_wsi_socket_into_fds(struct libwebsocket_context *context, struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +000067{
Andy Greendfb23042013-01-17 12:26:48 +080068 if (context->fds_count >= context->max_fds) {
69 lwsl_err("Reached limit of fds tracking (%d)\n", context->max_fds);
Andy Green0d338332011-02-12 11:57:43 +000070 return 1;
71 }
72
Andy Greendfb23042013-01-17 12:26:48 +080073 if (wsi->sock > context->max_fds) {
74 lwsl_err("Socket fd %d is beyond what we can index (%d)\n", wsi->sock, context->max_fds);
75 return 1;
76 }
77
78 assert(wsi);
79 assert(wsi->sock);
80
81 lwsl_info("insert_wsi_socket_into_fds: wsi=%p, sock=%d, fds pos=%d\n", wsi, wsi->sock, context->fds_count);
82
83 context->lws_lookup[wsi->sock] = wsi;
84 wsi->position_in_fds_table = context->fds_count;
85 context->fds[context->fds_count].fd = wsi->sock;
86 context->fds[context->fds_count].events = POLLIN;
87 context->fds[context->fds_count++].revents = 0;
88
89 /* external POLL support via protocol 0 */
90 context->protocols[0].callback(context, wsi,
91 LWS_CALLBACK_ADD_POLL_FD,
92 (void *)(long)wsi->sock, NULL, POLLIN);
Andy Green0d338332011-02-12 11:57:43 +000093
94 return 0;
95}
96
Andy Greendfb23042013-01-17 12:26:48 +080097static int
98remove_wsi_socket_from_fds(struct libwebsocket_context *context, struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +000099{
Andy Greendfb23042013-01-17 12:26:48 +0800100 int m;
Andy Green0d338332011-02-12 11:57:43 +0000101
Andy Greendfb23042013-01-17 12:26:48 +0800102 if (!--context->fds_count)
103 goto do_ext;
Andy Green0d338332011-02-12 11:57:43 +0000104
Andy Greendfb23042013-01-17 12:26:48 +0800105 if (wsi->sock > context->max_fds) {
106 lwsl_err("Socket fd %d is beyond what we can index (%d)\n", wsi->sock, context->max_fds);
107 return 1;
108 }
Andy Green0d338332011-02-12 11:57:43 +0000109
Andy Greendfb23042013-01-17 12:26:48 +0800110 lwsl_info("remove_wsi_socket_from_fds: wsi=%p, sock=%d, fds pos=%d\n", wsi, wsi->sock, wsi->position_in_fds_table);
111
112 m = wsi->position_in_fds_table; /* replace the contents for this */
113
114 /* have the last guy take up the vacant slot */
115 context->fds[m] = context->fds[context->fds_count]; /* vacant fds slot filled with end one */
116 /* end guy's fds_lookup entry remains unchanged (still same fd pointing to same wsi) */
117 /* end guy's "position in fds table" changed */
118 context->lws_lookup[context->fds[context->fds_count].fd]->position_in_fds_table = m;
119 /* deletion guy's lws_lookup entry needs nuking */
120 context->lws_lookup[wsi->sock] = NULL; /* no WSI for the socket of the wsi being removed*/
121 wsi->position_in_fds_table = -1; /* removed wsi has no position any more */
122
123do_ext:
124 /* remove also from external POLL support via protocol 0 */
125 if (wsi->sock)
126 context->protocols[0].callback(context, wsi,
127 LWS_CALLBACK_DEL_POLL_FD, (void *)(long)wsi->sock, NULL, 0);
128
129 return 0;
Andy Green0d338332011-02-12 11:57:43 +0000130}
131
Andy Green32375b72011-02-19 08:32:53 +0000132
Andy Green8f037e42010-12-19 22:13:26 +0000133void
Peter Hinz56885f32011-03-02 22:03:47 +0000134libwebsocket_close_and_free_session(struct libwebsocket_context *context,
Andy Green687b0182011-02-26 11:04:01 +0000135 struct libwebsocket *wsi, enum lws_close_status reason)
Andy Green251f6fa2010-11-03 11:13:06 +0000136{
Andy Greenb45993c2010-12-18 15:13:50 +0000137 int n;
Andy Green62c54d22011-02-14 09:14:25 +0000138 int old_state;
Andy Green5e1fa172011-02-10 09:07:05 +0000139 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 +
140 LWS_SEND_BUFFER_POST_PADDING];
Andy Greenc44159f2011-03-07 07:08:18 +0000141 int ret;
142 int m;
143 struct lws_tokens eff_buf;
Andy Greena41314f2011-05-23 10:00:03 +0100144 struct libwebsocket_extension *ext;
Andy Greenb45993c2010-12-18 15:13:50 +0000145
Andy Green4b6fbe12011-02-14 08:03:48 +0000146 if (!wsi)
Andy Greenb45993c2010-12-18 15:13:50 +0000147 return;
148
Andy Green62c54d22011-02-14 09:14:25 +0000149 old_state = wsi->state;
Andy Green251f6fa2010-11-03 11:13:06 +0000150
Andy Green62c54d22011-02-14 09:14:25 +0000151 if (old_state == WSI_STATE_DEAD_SOCKET)
Andy Green5e1fa172011-02-10 09:07:05 +0000152 return;
153
Andy Greenda527df2011-03-07 07:08:12 +0000154 wsi->close_reason = reason;
155
156 /*
Andy Green68b45042011-05-25 21:41:57 +0100157 * are his extensions okay with him closing? Eg he might be a mux
158 * parent and just his ch1 aspect is closing?
159 */
160
161
162 for (n = 0; n < wsi->count_active_extensions; n++) {
163 if (!wsi->active_extensions[n]->callback)
164 continue;
165
166 m = wsi->active_extensions[n]->callback(context,
167 wsi->active_extensions[n], wsi,
168 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
169 wsi->active_extensions_user[n], NULL, 0);
170
171 /*
172 * if somebody vetoed actually closing him at this time....
173 * up to the extension to track the attempted close, let's
174 * just bail
175 */
176
177 if (m) {
Andy Green43db0452013-01-10 19:50:35 +0800178 lwsl_ext("extension vetoed close\n");
Andy Green68b45042011-05-25 21:41:57 +0100179 return;
180 }
181 }
182
183
184
185 /*
Andy Greenc44159f2011-03-07 07:08:18 +0000186 * flush any tx pending from extensions, since we may send close packet
187 * if there are problems with send, just nuke the connection
188 */
189
190 ret = 1;
191 while (ret == 1) {
192
193 /* default to nobody has more to spill */
194
195 ret = 0;
196 eff_buf.token = NULL;
197 eff_buf.token_len = 0;
198
199 /* show every extension the new incoming data */
200
201 for (n = 0; n < wsi->count_active_extensions; n++) {
202 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000203 wsi->protocol->owning_server,
204 wsi->active_extensions[n], wsi,
Andy Greenc44159f2011-03-07 07:08:18 +0000205 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
206 wsi->active_extensions_user[n], &eff_buf, 0);
207 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800208 lwsl_ext("Extension reports fatal error\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000209 goto just_kill_connection;
210 }
211 if (m)
212 /*
213 * at least one extension told us he has more
214 * to spill, so we will go around again after
215 */
216 ret = 1;
217 }
218
219 /* assuming they left us something to send, send it */
220
221 if (eff_buf.token_len)
222 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
Andy Green0303db42013-01-17 14:46:43 +0800223 eff_buf.token_len)) {
224 lwsl_debug("close: sending final extension spill had problems\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000225 goto just_kill_connection;
Andy Green0303db42013-01-17 14:46:43 +0800226 }
Andy Greenc44159f2011-03-07 07:08:18 +0000227 }
228
229 /*
Andy Greenda527df2011-03-07 07:08:12 +0000230 * signal we are closing, libsocket_write will
231 * add any necessary version-specific stuff. If the write fails,
232 * no worries we are closing anyway. If we didn't initiate this
233 * close, then our state has been changed to
234 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this.
235 *
236 * Likewise if it's a second call to close this connection after we
237 * sent the close indication to the peer already, we are in state
238 * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time.
239 */
240
241 if (old_state == WSI_STATE_ESTABLISHED &&
242 reason != LWS_CLOSE_STATUS_NOSTATUS) {
Andy Green66a16f32011-05-24 22:07:45 +0100243
Andy Green43db0452013-01-10 19:50:35 +0800244 lwsl_debug("sending close indication...\n");
Andy Green66a16f32011-05-24 22:07:45 +0100245
Andy Greenda527df2011-03-07 07:08:12 +0000246 n = libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING],
247 0, LWS_WRITE_CLOSE);
248 if (!n) {
249 /*
250 * we have sent a nice protocol level indication we
251 * now wish to close, we should not send anything more
252 */
253
254 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
255
Andy Green0303db42013-01-17 14:46:43 +0800256 /* and we should wait for a reply for a bit out of politeness */
Andy Greenda527df2011-03-07 07:08:12 +0000257
258 libwebsocket_set_timeout(wsi,
Andy Green0303db42013-01-17 14:46:43 +0800259 PENDING_TIMEOUT_CLOSE_ACK, 1);
Andy Greenda527df2011-03-07 07:08:12 +0000260
Andy Green43db0452013-01-10 19:50:35 +0800261 lwsl_debug("sent close indication, awaiting ack\n");
Andy Greenda527df2011-03-07 07:08:12 +0000262
263 return;
264 }
265
Andy Green0303db42013-01-17 14:46:43 +0800266 lwsl_info("close: sending the close packet failed, hanging up\n");
267
Andy Greenda527df2011-03-07 07:08:12 +0000268 /* else, the send failed and we should just hang up */
269 }
270
Andy Greenc44159f2011-03-07 07:08:18 +0000271just_kill_connection:
Andy Green66a16f32011-05-24 22:07:45 +0100272
Andy Green43db0452013-01-10 19:50:35 +0800273 lwsl_debug("libwebsocket_close_and_free_session: just_kill_connection\n");
Andy Green66a16f32011-05-24 22:07:45 +0100274
Andy Greenda527df2011-03-07 07:08:12 +0000275 /*
276 * we won't be servicing or receiving anything further from this guy
Andy Greendfb23042013-01-17 12:26:48 +0800277 * delete socket from the internal poll list if still present
Andy Greenda527df2011-03-07 07:08:12 +0000278 */
Andy Green4b6fbe12011-02-14 08:03:48 +0000279
Andy Greendfb23042013-01-17 12:26:48 +0800280 remove_wsi_socket_from_fds(context, wsi);
Andy Green4b6fbe12011-02-14 08:03:48 +0000281
Andy Green251f6fa2010-11-03 11:13:06 +0000282 wsi->state = WSI_STATE_DEAD_SOCKET;
283
Andy Green4b6fbe12011-02-14 08:03:48 +0000284 /* tell the user it's all over for this guy */
285
Andy Greend4302732011-02-28 07:45:29 +0000286 if (wsi->protocol && wsi->protocol->callback &&
Andy Green6ee372f2012-04-09 15:09:01 +0800287 ((old_state == WSI_STATE_ESTABLISHED) ||
288 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
289 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
Andy Green43db0452013-01-10 19:50:35 +0800290 lwsl_debug("calling back CLOSED\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000291 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000292 wsi->user_space, NULL, 0);
Andy Greencc012472011-11-07 19:53:23 +0800293 } else
Andy Green43db0452013-01-10 19:50:35 +0800294 lwsl_debug("not calling back closed, old_state=%d\n", old_state);
Andy Green251f6fa2010-11-03 11:13:06 +0000295
Andy Greenef660a92011-03-06 10:29:38 +0000296 /* deallocate any active extension contexts */
297
298 for (n = 0; n < wsi->count_active_extensions; n++) {
299 if (!wsi->active_extensions[n]->callback)
300 continue;
301
Andy Green46c2ea02011-03-22 09:04:01 +0000302 wsi->active_extensions[n]->callback(context,
303 wsi->active_extensions[n], wsi,
304 LWS_EXT_CALLBACK_DESTROY,
305 wsi->active_extensions_user[n], NULL, 0);
Andy Greenef660a92011-03-06 10:29:38 +0000306
307 free(wsi->active_extensions_user[n]);
308 }
309
Andy Greena41314f2011-05-23 10:00:03 +0100310 /*
311 * inform all extensions in case they tracked this guy out of band
312 * even though not active on him specifically
313 */
314
315 ext = context->extensions;
316 while (ext && ext->callback) {
317 ext->callback(context, ext, wsi,
318 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
319 NULL, NULL, 0);
320 ext++;
321 }
322
Andy Greenef660a92011-03-06 10:29:38 +0000323 /* free up his parsing allocations */
Andy Green4b6fbe12011-02-14 08:03:48 +0000324
Andy Green251f6fa2010-11-03 11:13:06 +0000325 for (n = 0; n < WSI_TOKEN_COUNT; n++)
326 if (wsi->utf8_token[n].token)
327 free(wsi->utf8_token[n].token);
Andy Greena1ce6be2013-01-18 11:43:21 +0800328#ifndef LWS_NO_CLIENT
Andy Greena41314f2011-05-23 10:00:03 +0100329 if (wsi->c_address)
330 free(wsi->c_address);
Andy Greena1ce6be2013-01-18 11:43:21 +0800331#endif
Andy Green706961d2013-01-17 16:50:35 +0800332 if (wsi->rxflow_buffer)
333 free(wsi->rxflow_buffer);
334
Andy Green43db0452013-01-10 19:50:35 +0800335/* lwsl_info("closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000336
Andy Green3faa9c72010-11-08 17:03:03 +0000337#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000338 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000339 n = SSL_get_fd(wsi->ssl);
340 SSL_shutdown(wsi->ssl);
Andy Green3fc2c652013-01-14 15:35:02 +0800341 compatible_close(n);
Andy Green3faa9c72010-11-08 17:03:03 +0000342 SSL_free(wsi->ssl);
343 } else {
344#endif
Andy Green0303db42013-01-17 14:46:43 +0800345 if (wsi->sock) {
346 n = shutdown(wsi->sock, SHUT_RDWR);
347 if (n)
348 lwsl_debug("closing: shutdown returned %d\n", errno);
Andy Green3fc2c652013-01-14 15:35:02 +0800349
Andy Green0303db42013-01-17 14:46:43 +0800350 n = compatible_close(wsi->sock);
351 if (n)
352 lwsl_debug("closing: close returned %d\n", errno);
353 }
Andy Green3faa9c72010-11-08 17:03:03 +0000354#ifdef LWS_OPENSSL_SUPPORT
355 }
356#endif
David Brooks2c60d952012-04-20 12:19:01 +0800357 if (wsi->protocol && wsi->protocol->per_session_data_size && wsi->user_space) /* user code may own */
Andy Green4f3943a2010-11-12 10:44:16 +0000358 free(wsi->user_space);
359
Andy Green251f6fa2010-11-03 11:13:06 +0000360 free(wsi);
361}
362
Andy Green07034092011-02-13 08:37:12 +0000363/**
Andy Greenf7ee5492011-02-13 09:04:21 +0000364 * libwebsockets_hangup_on_client() - Server calls to terminate client
Andy Green6ee372f2012-04-09 15:09:01 +0800365 * connection
Peter Hinz56885f32011-03-02 22:03:47 +0000366 * @context: libwebsockets context
Andy Greenf7ee5492011-02-13 09:04:21 +0000367 * @fd: Connection socket descriptor
368 */
369
370void
Peter Hinz56885f32011-03-02 22:03:47 +0000371libwebsockets_hangup_on_client(struct libwebsocket_context *context, int fd)
Andy Greenf7ee5492011-02-13 09:04:21 +0000372{
Andy Greendfb23042013-01-17 12:26:48 +0800373 struct libwebsocket *wsi = context->lws_lookup[fd];
Andy Greenf7ee5492011-02-13 09:04:21 +0000374
Andy Greendfb23042013-01-17 12:26:48 +0800375 if (wsi) {
376 libwebsocket_close_and_free_session(context,
377 wsi, LWS_CLOSE_STATUS_NOSTATUS);
378 } else
379 close(fd);
Andy Greenf7ee5492011-02-13 09:04:21 +0000380}
381
382
383/**
Andy Green07034092011-02-13 08:37:12 +0000384 * libwebsockets_get_peer_addresses() - Get client address information
385 * @fd: Connection socket descriptor
386 * @name: Buffer to take client address name
387 * @name_len: Length of client address name buffer
388 * @rip: Buffer to take client address IP qotted quad
389 * @rip_len: Length of client address IP buffer
390 *
391 * This function fills in @name and @rip with the name and IP of
Andy Green6ee372f2012-04-09 15:09:01 +0800392 * the client connected with socket descriptor @fd. Names may be
393 * truncated if there is not enough room. If either cannot be
394 * determined, they will be returned as valid zero-length strings.
Andy Green07034092011-02-13 08:37:12 +0000395 */
396
397void
398libwebsockets_get_peer_addresses(int fd, char *name, int name_len,
399 char *rip, int rip_len)
400{
401 unsigned int len;
402 struct sockaddr_in sin;
403 struct hostent *host;
404 struct hostent *host1;
405 char ip[128];
Andy Greenf92def72011-03-09 15:02:20 +0000406 unsigned char *p;
Andy Green07034092011-02-13 08:37:12 +0000407 int n;
David Galeanocb193682013-01-09 15:29:00 +0800408#ifdef AF_LOCAL
409 struct sockaddr_un *un;
410#endif
Andy Green07034092011-02-13 08:37:12 +0000411
412 rip[0] = '\0';
413 name[0] = '\0';
414
415 len = sizeof sin;
416 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
417 perror("getpeername");
418 return;
419 }
Andy Green6ee372f2012-04-09 15:09:01 +0800420
Andy Green07034092011-02-13 08:37:12 +0000421 host = gethostbyaddr((char *) &sin.sin_addr, sizeof sin.sin_addr,
422 AF_INET);
423 if (host == NULL) {
424 perror("gethostbyaddr");
425 return;
426 }
427
428 strncpy(name, host->h_name, name_len);
429 name[name_len - 1] = '\0';
430
431 host1 = gethostbyname(host->h_name);
432 if (host1 == NULL)
433 return;
Andy Greenf92def72011-03-09 15:02:20 +0000434 p = (unsigned char *)host1;
Andy Green07034092011-02-13 08:37:12 +0000435 n = 0;
436 while (p != NULL) {
Andy Greenf92def72011-03-09 15:02:20 +0000437 p = (unsigned char *)host1->h_addr_list[n++];
Andy Green07034092011-02-13 08:37:12 +0000438 if (p == NULL)
439 continue;
Peter Hinzbb45a902011-03-10 18:14:01 +0000440 if ((host1->h_addrtype != AF_INET)
441#ifdef AF_LOCAL
442 && (host1->h_addrtype != AF_LOCAL)
443#endif
444 )
Andy Green07034092011-02-13 08:37:12 +0000445 continue;
446
Andy Green7627af52011-03-09 15:13:52 +0000447 if (host1->h_addrtype == AF_INET)
448 sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
Peter Hinzbb45a902011-03-10 18:14:01 +0000449#ifdef AF_LOCAL
Andy Green7627af52011-03-09 15:13:52 +0000450 else {
451 un = (struct sockaddr_un *)p;
Andy Green6ee372f2012-04-09 15:09:01 +0800452 strncpy(ip, un->sun_path, sizeof(ip) - 1);
Andy Green7627af52011-03-09 15:13:52 +0000453 ip[sizeof(ip) - 1] = '\0';
454 }
Peter Hinzbb45a902011-03-10 18:14:01 +0000455#endif
Andy Green07034092011-02-13 08:37:12 +0000456 p = NULL;
457 strncpy(rip, ip, rip_len);
458 rip[rip_len - 1] = '\0';
459 }
460}
Andy Green9f990342011-02-12 11:57:45 +0000461
Peter Hinz56885f32011-03-02 22:03:47 +0000462int libwebsockets_get_random(struct libwebsocket_context *context,
463 void *buf, int len)
464{
465 int n;
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800466 char *p = (char *)buf;
Peter Hinz56885f32011-03-02 22:03:47 +0000467
468#ifdef WIN32
469 for (n = 0; n < len; n++)
470 p[n] = (unsigned char)rand();
471#else
472 n = read(context->fd_random, p, len);
473#endif
474
475 return n;
476}
477
Andy Green2836c642011-03-07 20:47:41 +0000478unsigned char *
479libwebsockets_SHA1(const unsigned char *d, size_t n, unsigned char *md)
480{
481 return SHA1(d, n, md);
482}
483
Andy Green95a7b5d2011-03-06 10:29:39 +0000484int lws_send_pipe_choked(struct libwebsocket *wsi)
485{
486 struct pollfd fds;
487
488 fds.fd = wsi->sock;
489 fds.events = POLLOUT;
490 fds.revents = 0;
491
492 if (poll(&fds, 1, 0) != 1)
493 return 1;
494
495 if ((fds.revents & POLLOUT) == 0)
496 return 1;
497
498 /* okay to send another packet without blocking */
499
500 return 0;
501}
502
Andy Greena41314f2011-05-23 10:00:03 +0100503int
Andy Green3b84c002011-03-06 13:14:42 +0000504lws_handle_POLLOUT_event(struct libwebsocket_context *context,
505 struct libwebsocket *wsi, struct pollfd *pollfd)
506{
507 struct lws_tokens eff_buf;
508 int n;
509 int ret;
510 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100511 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000512
Andy Greena41314f2011-05-23 10:00:03 +0100513 for (n = 0; n < wsi->count_active_extensions; n++) {
514 if (!wsi->active_extensions[n]->callback)
515 continue;
516
517 m = wsi->active_extensions[n]->callback(context,
518 wsi->active_extensions[n], wsi,
519 LWS_EXT_CALLBACK_IS_WRITEABLE,
520 wsi->active_extensions_user[n], NULL, 0);
521 if (m > handled)
522 handled = m;
523 }
524
525 if (handled == 1)
526 goto notify_action;
527
528 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000529 goto user_service;
530
531 /*
532 * check in on the active extensions, see if they
533 * had pending stuff to spill... they need to get the
534 * first look-in otherwise sequence will be disordered
535 *
536 * NULL, zero-length eff_buf means just spill pending
537 */
538
539 ret = 1;
540 while (ret == 1) {
541
542 /* default to nobody has more to spill */
543
544 ret = 0;
545 eff_buf.token = NULL;
546 eff_buf.token_len = 0;
547
548 /* give every extension a chance to spill */
549
550 for (n = 0; n < wsi->count_active_extensions; n++) {
551 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000552 wsi->protocol->owning_server,
553 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000554 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
555 wsi->active_extensions_user[n], &eff_buf, 0);
556 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800557 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000558 return -1;
559 }
560 if (m)
561 /*
562 * at least one extension told us he has more
563 * to spill, so we will go around again after
564 */
565 ret = 1;
566 }
567
568 /* assuming they gave us something to send, send it */
569
570 if (eff_buf.token_len) {
571 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
572 eff_buf.token_len))
573 return -1;
574 } else
575 continue;
576
577 /* no extension has more to spill */
578
579 if (!ret)
580 continue;
581
582 /*
583 * There's more to spill from an extension, but we just sent
584 * something... did that leave the pipe choked?
585 */
586
587 if (!lws_send_pipe_choked(wsi))
588 /* no we could add more */
589 continue;
590
Andy Green43db0452013-01-10 19:50:35 +0800591 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000592
593 /*
594 * Yes, he's choked. Leave the POLLOUT masked on so we will
595 * come back here when he is unchoked. Don't call the user
596 * callback to enforce ordering of spilling, he'll get called
597 * when we come back here and there's nothing more to spill.
598 */
599
600 return 0;
601 }
602
603 wsi->extension_data_pending = 0;
604
605user_service:
606 /* one shot */
607
Andy Greena41314f2011-05-23 10:00:03 +0100608 if (pollfd) {
609 pollfd->events &= ~POLLOUT;
Andy Green3b84c002011-03-06 13:14:42 +0000610
Andy Greena41314f2011-05-23 10:00:03 +0100611 /* external POLL support via protocol 0 */
612 context->protocols[0].callback(context, wsi,
613 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
614 (void *)(long)wsi->sock, NULL, POLLOUT);
615 }
616
617notify_action:
Andy Green3b84c002011-03-06 13:14:42 +0000618
Andy Green9e4c2b62011-03-07 20:47:39 +0000619 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
620 n = LWS_CALLBACK_CLIENT_WRITEABLE;
621 else
622 n = LWS_CALLBACK_SERVER_WRITEABLE;
623
Andy Green706961d2013-01-17 16:50:35 +0800624 user_callback_handle_rxflow(wsi->protocol->callback, context,
625 wsi, (enum libwebsocket_callback_reasons) n, wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000626
627 return 0;
628}
629
630
631
Andy Greena41314f2011-05-23 10:00:03 +0100632void
633libwebsocket_service_timeout_check(struct libwebsocket_context *context,
634 struct libwebsocket *wsi, unsigned int sec)
635{
636 int n;
637
638 /*
639 * if extensions want in on it (eg, we are a mux parent)
640 * give them a chance to service child timeouts
641 */
642
643 for (n = 0; n < wsi->count_active_extensions; n++)
644 wsi->active_extensions[n]->callback(
645 context, wsi->active_extensions[n],
646 wsi, LWS_EXT_CALLBACK_1HZ,
647 wsi->active_extensions_user[n], NULL, sec);
648
649 if (!wsi->pending_timeout)
650 return;
Andy Green6ee372f2012-04-09 15:09:01 +0800651
Andy Greena41314f2011-05-23 10:00:03 +0100652 /*
653 * if we went beyond the allowed time, kill the
654 * connection
655 */
656
657 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800658 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100659 libwebsocket_close_and_free_session(context,
660 wsi, LWS_CLOSE_STATUS_NOSTATUS);
661 }
662}
663
Andy Green9f990342011-02-12 11:57:45 +0000664/**
665 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000666 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000667 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800668 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000669 *
670 * This function closes any active connections and then frees the
671 * context. After calling this, any further use of the context is
672 * undefined.
673 */
674
675int
Peter Hinz56885f32011-03-02 22:03:47 +0000676libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000677 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000678{
Andy Greena1ce6be2013-01-18 11:43:21 +0800679 struct libwebsocket *wsi;
Andy Green6ee372f2012-04-09 15:09:01 +0800680 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 +
681 MAX_BROADCAST_PAYLOAD + LWS_SEND_BUFFER_POST_PADDING];
Andy Greenb45993c2010-12-18 15:13:50 +0000682 int n;
Andy Green0d338332011-02-12 11:57:43 +0000683 int m;
Andy Greena71eafc2011-02-14 17:59:43 +0000684 struct timeval tv;
Andy Green2366b1c2011-03-06 13:15:31 +0000685 int more = 1;
Andy Green98a717c2011-03-06 13:14:15 +0000686 struct lws_tokens eff_buf;
Andy Green03674a62013-01-16 11:47:40 +0800687#ifndef LWS_NO_CLIENT
Andy Green76f61e72013-01-16 11:53:05 +0800688 extern int lws_client_socket_service(struct libwebsocket_context *context, struct libwebsocket *wsi, struct pollfd *pollfd);
Andy Green03674a62013-01-16 11:47:40 +0800689#endif
Andy Greena1ce6be2013-01-18 11:43:21 +0800690#ifndef LWS_NO_SERVER
691 extern int lws_server_socket_service(struct libwebsocket_context *context, struct libwebsocket *wsi, struct pollfd *pollfd);
692#endif
Andy Greena71eafc2011-02-14 17:59:43 +0000693 /*
694 * you can call us with pollfd = NULL to just allow the once-per-second
695 * global timeout checks; if less than a second since the last check
696 * it returns immediately then.
697 */
698
699 gettimeofday(&tv, NULL);
700
Peter Hinz56885f32011-03-02 22:03:47 +0000701 if (context->last_timeout_check_s != tv.tv_sec) {
702 context->last_timeout_check_s = tv.tv_sec;
Andy Greena71eafc2011-02-14 17:59:43 +0000703
704 /* global timeout check once per second */
705
Peter Hinz56885f32011-03-02 22:03:47 +0000706 for (n = 0; n < context->fds_count; n++) {
Andy Greendfb23042013-01-17 12:26:48 +0800707 struct libwebsocket *new_wsi = context->lws_lookup[context->fds[n].fd];
708 if (!new_wsi)
709 continue;
710 libwebsocket_service_timeout_check(context,
711 new_wsi, tv.tv_sec);
Andy Greena71eafc2011-02-14 17:59:43 +0000712 }
713 }
714
715 /* just here for timeout management? */
716
717 if (pollfd == NULL)
718 return 0;
719
720 /* no, here to service a socket descriptor */
721
Andy Green65b0e912013-01-16 07:59:47 +0800722 /*
723 * deal with listen service piggybacking
724 * every listen_service_modulo services of other fds, we
725 * sneak one in to service the listen socket if there's anything waiting
726 *
727 * To handle connection storms, as found in ab, if we previously saw a
728 * pending connection here, it causes us to check again next time.
729 */
730
731 if (context->listen_service_fd && pollfd->fd != context->listen_service_fd) {
732 context->listen_service_count++;
733 if (context->listen_service_extraseen ||
734 context->listen_service_count == context->listen_service_modulo) {
735 context->listen_service_count = 0;
736 m = 1;
737 if (context->listen_service_extraseen > 5)
738 m = 2;
739 while (m--) {
740 /* even with extpoll, we prepared this internal fds for listen */
741 n = poll(&context->fds[0], 1, 0);
742 if (n > 0) { /* there's a connection waiting for us */
743 libwebsocket_service_fd(context, &context->fds[0]);
744 context->listen_service_extraseen++;
745 } else {
746 if (context->listen_service_extraseen)
747 context->listen_service_extraseen--;
748 break;
749 }
750 }
751 }
752
753 }
754
755 /* okay, what we came here to do... */
756
Andy Greendfb23042013-01-17 12:26:48 +0800757 wsi = context->lws_lookup[pollfd->fd];
Andy Greend280b6e2013-01-15 13:40:23 +0800758 if (wsi == NULL) {
Andy Greendfb23042013-01-17 12:26:48 +0800759 if (pollfd->fd > 11)
760 lwsl_err("unexpected NULL wsi fd=%d fds_count=%d\n", pollfd->fd, context->fds_count);
Andy Greenfa3f4052012-10-07 20:40:35 +0800761 return 0;
Andy Greend280b6e2013-01-15 13:40:23 +0800762 }
Andy Green8f037e42010-12-19 22:13:26 +0000763
Andy Green0d338332011-02-12 11:57:43 +0000764 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +0800765
Andy Greena1ce6be2013-01-18 11:43:21 +0800766#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +0800767 case LWS_CONNMODE_HTTP_SERVING:
Andy Green0d338332011-02-12 11:57:43 +0000768 case LWS_CONNMODE_SERVER_LISTENER:
Andy Green0d338332011-02-12 11:57:43 +0000769 case LWS_CONNMODE_BROADCAST_PROXY_LISTENER:
Andy Green0d338332011-02-12 11:57:43 +0000770 case LWS_CONNMODE_BROADCAST_PROXY:
Andy Greena1ce6be2013-01-18 11:43:21 +0800771 return lws_server_socket_service(context, wsi, pollfd);
772#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000773
Andy Green0d338332011-02-12 11:57:43 +0000774 case LWS_CONNMODE_WS_SERVING:
775 case LWS_CONNMODE_WS_CLIENT:
776
777 /* handle session socket closed */
778
779 if (pollfd->revents & (POLLERR | POLLHUP)) {
780
Andy Green43db0452013-01-10 19:50:35 +0800781 lwsl_debug("Session Socket %p (fd=%d) dead\n",
Andy Green0d338332011-02-12 11:57:43 +0000782 (void *)wsi, pollfd->fd);
783
Peter Hinz56885f32011-03-02 22:03:47 +0000784 libwebsocket_close_and_free_session(context, wsi,
Andy Green687b0182011-02-26 11:04:01 +0000785 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800786 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000787 }
788
Andy Green0d338332011-02-12 11:57:43 +0000789 /* the guy requested a callback when it was OK to write */
790
Andy Greenda527df2011-03-07 07:08:12 +0000791 if ((pollfd->revents & POLLOUT) &&
792 wsi->state == WSI_STATE_ESTABLISHED)
793 if (lws_handle_POLLOUT_event(context, wsi,
794 pollfd) < 0) {
795 libwebsocket_close_and_free_session(
796 context, wsi, LWS_CLOSE_STATUS_NORMAL);
Andy Green040d2ef2013-01-16 13:40:43 +0800797 return 0;
Andy Green3b84c002011-03-06 13:14:42 +0000798 }
Andy Green0d338332011-02-12 11:57:43 +0000799
Andy Green0d338332011-02-12 11:57:43 +0000800
801 /* any incoming data ready? */
802
803 if (!(pollfd->revents & POLLIN))
804 break;
805
Andy Greenb45993c2010-12-18 15:13:50 +0000806#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +0800807read_pending:
Andy Green0d338332011-02-12 11:57:43 +0000808 if (wsi->ssl)
Andy Green98a717c2011-03-06 13:14:15 +0000809 eff_buf.token_len = SSL_read(wsi->ssl, buf, sizeof buf);
Andy Greenb45993c2010-12-18 15:13:50 +0000810 else
811#endif
Andy Green98a717c2011-03-06 13:14:15 +0000812 eff_buf.token_len =
Andy Green72c34322011-04-16 10:46:21 +0100813 recv(pollfd->fd, buf, sizeof buf, 0);
Andy Greenb45993c2010-12-18 15:13:50 +0000814
Andy Green98a717c2011-03-06 13:14:15 +0000815 if (eff_buf.token_len < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800816 lwsl_debug("Socket read returned %d\n",
Andy Green98a717c2011-03-06 13:14:15 +0000817 eff_buf.token_len);
Alon Levydc93b7f2012-10-19 11:21:57 +0200818 if (errno != EINTR && errno != EAGAIN)
Andy Green6ee372f2012-04-09 15:09:01 +0800819 libwebsocket_close_and_free_session(context,
820 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800821 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000822 }
Andy Green98a717c2011-03-06 13:14:15 +0000823 if (!eff_buf.token_len) {
Peter Hinz56885f32011-03-02 22:03:47 +0000824 libwebsocket_close_and_free_session(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +0800825 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenfa3f4052012-10-07 20:40:35 +0800826 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000827 }
828
Andy Green98a717c2011-03-06 13:14:15 +0000829 /*
830 * give any active extensions a chance to munge the buffer
831 * before parse. We pass in a pointer to an lws_tokens struct
832 * prepared with the default buffer and content length that's in
833 * there. Rather than rewrite the default buffer, extensions
834 * that expect to grow the buffer can adapt .token to
835 * point to their own per-connection buffer in the extension
836 * user allocation. By default with no extensions or no
837 * extension callback handling, just the normal input buffer is
838 * used then so it is efficient.
839 */
Andy Greenb45993c2010-12-18 15:13:50 +0000840
Andy Green98a717c2011-03-06 13:14:15 +0000841 eff_buf.token = (char *)buf;
Andy Greenb45993c2010-12-18 15:13:50 +0000842
Andy Green98a717c2011-03-06 13:14:15 +0000843 more = 1;
844 while (more) {
Andy Green0d338332011-02-12 11:57:43 +0000845
Andy Green98a717c2011-03-06 13:14:15 +0000846 more = 0;
847
848 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +0000849 m = wsi->active_extensions[n]->callback(context,
850 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +0000851 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +0000852 wsi->active_extensions_user[n],
853 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +0000854 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800855 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +0800856 "Extension reports fatal error\n");
857 libwebsocket_close_and_free_session(
858 context, wsi,
859 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800860 return 0;
Andy Green98a717c2011-03-06 13:14:15 +0000861 }
862 if (m)
863 more = 1;
864 }
865
866 /* service incoming data */
867
868 if (eff_buf.token_len) {
869 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +0800870 (unsigned char *)eff_buf.token,
871 eff_buf.token_len);
Andy Green98a717c2011-03-06 13:14:15 +0000872 if (n < 0)
873 /* we closed wsi */
Andy Green040d2ef2013-01-16 13:40:43 +0800874 return 0;
Andy Green98a717c2011-03-06 13:14:15 +0000875 }
876
877 eff_buf.token = NULL;
878 eff_buf.token_len = 0;
879 }
David Galeano7ffbe1b2013-01-10 10:35:32 +0800880
881#ifdef LWS_OPENSSL_SUPPORT
882 if (wsi->ssl && SSL_pending(wsi->ssl))
883 goto read_pending;
884#endif
Andy Green98a717c2011-03-06 13:14:15 +0000885 break;
Andy Green76f61e72013-01-16 11:53:05 +0800886
887 default:
Andy Green03674a62013-01-16 11:47:40 +0800888#ifdef LWS_NO_CLIENT
889 break;
890#else
Andy Green76f61e72013-01-16 11:53:05 +0800891 return lws_client_socket_service(context, wsi, pollfd);
Andy Green03674a62013-01-16 11:47:40 +0800892#endif
Andy Greenb45993c2010-12-18 15:13:50 +0000893 }
894
895 return 0;
896}
897
Andy Green0d338332011-02-12 11:57:43 +0000898
Andy Green6964bb52011-01-23 16:50:33 +0000899/**
900 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +0000901 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +0000902 *
903 * This function closes any active connections and then frees the
904 * context. After calling this, any further use of the context is
905 * undefined.
906 */
907void
Peter Hinz56885f32011-03-02 22:03:47 +0000908libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +0000909{
Andy Green0d338332011-02-12 11:57:43 +0000910 int n;
911 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100912 struct libwebsocket_extension *ext;
Andy Green6964bb52011-01-23 16:50:33 +0000913
Andy Greendfb23042013-01-17 12:26:48 +0800914 for (n = 0; n < context->fds_count; n++) {
915 struct libwebsocket *wsi = context->lws_lookup[context->fds[n].fd];
916 libwebsocket_close_and_free_session(context,
917 wsi, LWS_CLOSE_STATUS_GOINGAWAY);
918 }
Andy Green6964bb52011-01-23 16:50:33 +0000919
Andy Greena41314f2011-05-23 10:00:03 +0100920 /*
921 * give all extensions a chance to clean up any per-context
922 * allocations they might have made
923 */
924
925 ext = context->extensions;
926 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
927 if (context->listen_port)
928 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +0800929 while (ext && ext->callback) {
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800930 ext->callback(context, ext, NULL, (enum libwebsocket_extension_callback_reasons)m, NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +0100931 ext++;
932 }
933
Peter Hinz56885f32011-03-02 22:03:47 +0000934#ifdef WIN32
935#else
936 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +0000937#endif
938
Peter Hinz56885f32011-03-02 22:03:47 +0000939#ifdef LWS_OPENSSL_SUPPORT
940 if (context->ssl_ctx)
941 SSL_CTX_free(context->ssl_ctx);
942 if (context->ssl_client_ctx)
943 SSL_CTX_free(context->ssl_client_ctx);
944#endif
945
946 free(context);
947
948#ifdef WIN32
949 WSACleanup();
950#endif
Andy Green6964bb52011-01-23 16:50:33 +0000951}
952
Alon Levy0291eb32012-10-19 11:21:56 +0200953LWS_EXTERN void *
954libwebsocket_context_user(struct libwebsocket_context *context)
955{
956 return context->user_space;
957}
958
Andy Green6964bb52011-01-23 16:50:33 +0000959/**
960 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +0000961 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +0000962 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
963 * service otherwise block and service immediately, returning
964 * after the timeout if nothing needed service.
965 *
966 * This function deals with any pending websocket traffic, for three
967 * kinds of event. It handles these events on both server and client
968 * types of connection the same.
969 *
970 * 1) Accept new connections to our context's server
971 *
972 * 2) Perform pending broadcast writes initiated from other forked
973 * processes (effectively serializing asynchronous broadcasts)
974 *
975 * 3) Call the receive callback for incoming frame data received by
976 * server or client connections.
977 *
978 * You need to call this service function periodically to all the above
979 * functions to happen; if your application is single-threaded you can
980 * just call it in your main event loop.
981 *
982 * Alternatively you can fork a new process that asynchronously handles
983 * calling this service in a loop. In that case you are happy if this
984 * call blocks your thread until it needs to take care of something and
985 * would call it with a large nonzero timeout. Your loop then takes no
986 * CPU while there is nothing happening.
987 *
988 * If you are calling it in a single-threaded app, you don't want it to
989 * wait around blocking other things in your loop from happening, so you
990 * would call it with a timeout_ms of 0, so it returns immediately if
991 * nothing is pending, or as soon as it services whatever was pending.
992 */
993
Andy Greenb45993c2010-12-18 15:13:50 +0000994
Andy Greene92cd172011-01-19 13:11:55 +0000995int
Peter Hinz56885f32011-03-02 22:03:47 +0000996libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +0000997{
998 int n;
Andy Greene92cd172011-01-19 13:11:55 +0000999
1000 /* stay dead once we are dead */
1001
Peter Hinz56885f32011-03-02 22:03:47 +00001002 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001003 return 1;
1004
Andy Green0d338332011-02-12 11:57:43 +00001005 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001006
Peter Hinz56885f32011-03-02 22:03:47 +00001007 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green3221f922011-02-12 13:14:11 +00001008 if (n == 0) /* poll timeout */
1009 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001010
Andy Greendfb23042013-01-17 12:26:48 +08001011 if (n < 0)
Andy Green3928f612012-07-20 12:58:38 +08001012 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001013
Andy Greendfb23042013-01-17 12:26:48 +08001014 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001015
Peter Hinz56885f32011-03-02 22:03:47 +00001016 for (n = 0; n < context->fds_count; n++)
1017 if (context->fds[n].revents)
Andy Green3928f612012-07-20 12:58:38 +08001018 if (libwebsocket_service_fd(context,
1019 &context->fds[n]) < 0)
1020 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001021 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001022}
1023
Andy Greena41314f2011-05-23 10:00:03 +01001024int
1025lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001026 struct libwebsocket *wsi,
1027 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001028 void *v, size_t len)
1029{
1030 int n;
1031 int handled = 0;
1032
1033 /* maybe an extension will take care of it for us */
1034
1035 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1036 if (!wsi->active_extensions[n]->callback)
1037 continue;
1038
1039 handled |= wsi->active_extensions[n]->callback(context,
1040 wsi->active_extensions[n], wsi,
1041 r, wsi->active_extensions_user[n], v, len);
1042 }
1043
1044 return handled;
1045}
1046
1047
1048void *
1049lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001050 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001051{
1052 int n = 0;
1053
Andy Green68b45042011-05-25 21:41:57 +01001054 if (wsi == NULL)
1055 return NULL;
1056
Andy Greena41314f2011-05-23 10:00:03 +01001057 while (n < wsi->count_active_extensions) {
1058 if (wsi->active_extensions[n] != ext) {
1059 n++;
1060 continue;
1061 }
1062 return wsi->active_extensions_user[n];
1063 }
1064
1065 return NULL;
1066}
1067
Andy Green90c7cbc2011-01-27 06:26:52 +00001068/**
1069 * libwebsocket_callback_on_writable() - Request a callback when this socket
1070 * becomes able to be written to without
1071 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001072 *
Peter Hinz56885f32011-03-02 22:03:47 +00001073 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001074 * @wsi: Websocket connection instance to get callback for
1075 */
1076
1077int
Peter Hinz56885f32011-03-02 22:03:47 +00001078libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001079 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001080{
Andy Green90c7cbc2011-01-27 06:26:52 +00001081 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001082 int handled = 0;
1083
1084 /* maybe an extension will take care of it for us */
1085
1086 for (n = 0; n < wsi->count_active_extensions; n++) {
1087 if (!wsi->active_extensions[n]->callback)
1088 continue;
1089
1090 handled |= wsi->active_extensions[n]->callback(context,
1091 wsi->active_extensions[n], wsi,
1092 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1093 wsi->active_extensions_user[n], NULL, 0);
1094 }
1095
1096 if (handled)
1097 return 1;
Andy Green90c7cbc2011-01-27 06:26:52 +00001098
Andy Greendfb23042013-01-17 12:26:48 +08001099 if (wsi->position_in_fds_table < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001100 lwsl_err("libwebsocket_callback_on_writable: "
Andy Green6ee372f2012-04-09 15:09:01 +08001101 "failed to find socket %d\n", wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001102 return -1;
1103 }
1104
1105 context->fds[wsi->position_in_fds_table].events |= POLLOUT;
Andy Greena41314f2011-05-23 10:00:03 +01001106
Andy Green3221f922011-02-12 13:14:11 +00001107 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00001108 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001109 LWS_CALLBACK_SET_MODE_POLL_FD,
1110 (void *)(long)wsi->sock, NULL, POLLOUT);
1111
Andy Green90c7cbc2011-01-27 06:26:52 +00001112 return 1;
1113}
1114
1115/**
1116 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1117 * all connections using the given protocol when it
1118 * becomes possible to write to each socket without
1119 * blocking in turn.
1120 *
1121 * @protocol: Protocol whose connections will get callbacks
1122 */
1123
1124int
1125libwebsocket_callback_on_writable_all_protocol(
1126 const struct libwebsocket_protocols *protocol)
1127{
Peter Hinz56885f32011-03-02 22:03:47 +00001128 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001129 int n;
Andy Green0d338332011-02-12 11:57:43 +00001130 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001131
Andy Greendfb23042013-01-17 12:26:48 +08001132 for (n = 0; n < context->fds_count; n++) {
1133 wsi = context->lws_lookup[context->fds[n].fd];
1134 if (!wsi)
1135 continue;
1136 if (wsi->protocol == protocol)
1137 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001138 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001139
1140 return 0;
1141}
1142
Andy Greenbe93fef2011-02-14 20:25:43 +00001143/**
1144 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1145 *
1146 * You will not need this unless you are doing something special
1147 *
1148 * @wsi: Websocket connection instance
1149 * @reason: timeout reason
1150 * @secs: how many seconds
1151 */
1152
1153void
1154libwebsocket_set_timeout(struct libwebsocket *wsi,
1155 enum pending_timeout reason, int secs)
1156{
1157 struct timeval tv;
1158
1159 gettimeofday(&tv, NULL);
1160
1161 wsi->pending_timeout_limit = tv.tv_sec + secs;
1162 wsi->pending_timeout = reason;
1163}
1164
Andy Greena6cbece2011-01-27 20:06:03 +00001165
1166/**
1167 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1168 *
1169 * You will not need this unless you are doing something special
1170 *
1171 * @wsi: Websocket connection instance
1172 */
1173
1174int
1175libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1176{
1177 return wsi->sock;
1178}
1179
Andy Greena1ce6be2013-01-18 11:43:21 +08001180#ifdef LWS_NO_SERVER
1181int
1182_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1183{
1184 return 0;
1185}
1186#else
Andy Green706961d2013-01-17 16:50:35 +08001187int
1188_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1189{
1190 struct libwebsocket_context *context = wsi->protocol->owning_server;
1191 int n;
1192
1193 if (!(wsi->rxflow_change_to & 2))
1194 return 0;
1195
1196 wsi->rxflow_change_to &= ~2;
1197
1198 lwsl_info("rxflow: wsi %p change_to %d\n", wsi, wsi->rxflow_change_to);
1199
1200 /* if we're letting it come again, did we interrupt anything? */
1201 if ((wsi->rxflow_change_to & 1) && wsi->rxflow_buffer) {
1202 n = libwebsocket_interpret_incoming_packet(wsi, NULL, 0);
1203 if (n < 0) {
1204 libwebsocket_close_and_free_session(context, wsi, LWS_CLOSE_STATUS_NOSTATUS);
1205 return -1;
1206 }
1207 if (n)
1208 /* oh he stuck again, do nothing */
1209 return 0;
1210 }
1211
1212 if (wsi->rxflow_change_to & 1)
1213 context->fds[wsi->position_in_fds_table].events |= POLLIN;
1214 else
1215 context->fds[wsi->position_in_fds_table].events &= ~POLLIN;
1216
1217 if (wsi->rxflow_change_to & 1)
1218 /* external POLL support via protocol 0 */
1219 context->protocols[0].callback(context, wsi,
1220 LWS_CALLBACK_SET_MODE_POLL_FD,
1221 (void *)(long)wsi->sock, NULL, POLLIN);
1222 else
1223 /* external POLL support via protocol 0 */
1224 context->protocols[0].callback(context, wsi,
1225 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
1226 (void *)(long)wsi->sock, NULL, POLLIN);
1227
1228 return 1;
1229}
Andy Greena1ce6be2013-01-18 11:43:21 +08001230#endif
Andy Green706961d2013-01-17 16:50:35 +08001231
Andy Green90c7cbc2011-01-27 06:26:52 +00001232/**
1233 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1234 * receieved packets.
1235 *
1236 * If the output side of a server process becomes choked, this allows flow
1237 * control for the input side.
1238 *
1239 * @wsi: Websocket connection instance to get callback for
1240 * @enable: 0 = disable read servicing for this connection, 1 = enable
1241 */
1242
1243int
1244libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1245{
Andy Green706961d2013-01-17 16:50:35 +08001246 wsi->rxflow_change_to = 2 | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001247
Andy Green706961d2013-01-17 16:50:35 +08001248 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001249}
1250
Andy Green706961d2013-01-17 16:50:35 +08001251
Andy Green2ac5a6f2011-01-28 10:00:18 +00001252/**
1253 * libwebsocket_canonical_hostname() - returns this host's hostname
1254 *
1255 * This is typically used by client code to fill in the host parameter
1256 * when making a client connection. You can only call it after the context
1257 * has been created.
1258 *
Peter Hinz56885f32011-03-02 22:03:47 +00001259 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001260 */
1261
1262
1263extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001264libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001265{
Peter Hinz56885f32011-03-02 22:03:47 +00001266 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001267}
1268
1269
Andy Green90c7cbc2011-01-27 06:26:52 +00001270static void sigpipe_handler(int x)
1271{
1272}
1273
Andy Green6901cb32011-02-21 08:06:47 +00001274#ifdef LWS_OPENSSL_SUPPORT
1275static int
1276OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1277{
1278
1279 SSL *ssl;
1280 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001281 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001282
1283 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1284 SSL_get_ex_data_X509_STORE_CTX_idx());
1285
1286 /*
Andy Green2e24da02011-03-05 16:12:04 +00001287 * !!! nasty openssl requires the index to come as a library-scope
1288 * static
Andy Green6901cb32011-02-21 08:06:47 +00001289 */
Andy Green2e24da02011-03-05 16:12:04 +00001290 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001291
Peter Hinz56885f32011-03-02 22:03:47 +00001292 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001293 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1294 x509_ctx, ssl, preverify_ok);
1295
1296 /* convert return code from 0 = OK to 1 = OK */
1297
1298 if (!n)
1299 n = 1;
1300 else
1301 n = 0;
1302
1303 return n;
1304}
1305#endif
1306
Andy Green706961d2013-01-17 16:50:35 +08001307int user_callback_handle_rxflow(callback_function callback_function,
1308 struct libwebsocket_context * context,
1309 struct libwebsocket *wsi,
1310 enum libwebsocket_callback_reasons reason, void *user,
1311 void *in, size_t len)
1312{
1313 int n;
1314
1315 n = callback_function(context, wsi, reason, user, in, len);
1316 if (n < 0)
1317 return n;
1318
1319 _libwebsocket_rx_flow_control(wsi);
1320
1321 return 0;
1322}
1323
Andy Greenb45993c2010-12-18 15:13:50 +00001324
Andy Greenab990e42010-10-31 12:42:52 +00001325/**
Andy Green4739e5c2011-01-22 12:51:57 +00001326 * libwebsocket_create_context() - Create the websocket handler
1327 * @port: Port to listen on... you can use 0 to suppress listening on
Andy Green6964bb52011-01-23 16:50:33 +00001328 * any port, that's what you want if you are not running a
1329 * websocket server at all but just using it as a client
Peter Hinz56885f32011-03-02 22:03:47 +00001330 * @interf: NULL to bind the listen socket to all interfaces, or the
Andy Green32375b72011-02-19 08:32:53 +00001331 * interface name, eg, "eth2"
Andy Green4f3943a2010-11-12 10:44:16 +00001332 * @protocols: Array of structures listing supported protocols and a protocol-
Andy Green8f037e42010-12-19 22:13:26 +00001333 * specific callback for each one. The list is ended with an
1334 * entry that has a NULL callback pointer.
Andy Green6964bb52011-01-23 16:50:33 +00001335 * It's not const because we write the owning_server member
Andy Greenc5114822011-03-06 10:29:35 +00001336 * @extensions: NULL or array of libwebsocket_extension structs listing the
Andy Green6ee372f2012-04-09 15:09:01 +08001337 * extensions this context supports
Andy Green3faa9c72010-11-08 17:03:03 +00001338 * @ssl_cert_filepath: If libwebsockets was compiled to use ssl, and you want
Andy Green8f037e42010-12-19 22:13:26 +00001339 * to listen using SSL, set to the filepath to fetch the
1340 * server cert from, otherwise NULL for unencrypted
Andy Green3faa9c72010-11-08 17:03:03 +00001341 * @ssl_private_key_filepath: filepath to private key if wanting SSL mode,
Andy Green8f037e42010-12-19 22:13:26 +00001342 * else ignored
David Galeano2f82be82013-01-09 16:25:54 +08001343 * @ssl_ca_filepath: CA certificate filepath or NULL
Andy Green3faa9c72010-11-08 17:03:03 +00001344 * @gid: group id to change to after setting listen socket, or -1.
1345 * @uid: user id to change to after setting listen socket, or -1.
Andy Greenbfb051f2011-02-09 08:49:14 +00001346 * @options: 0, or LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK
Andy Green15e31f32012-10-19 18:36:28 +08001347 * @user: optional user pointer that can be recovered via the context
1348 * pointer using libwebsocket_context_user
Andy Green05464c62010-11-12 10:44:18 +00001349 *
Andy Green8f037e42010-12-19 22:13:26 +00001350 * This function creates the listening socket and takes care
1351 * of all initialization in one step.
1352 *
Andy Greene92cd172011-01-19 13:11:55 +00001353 * After initialization, it returns a struct libwebsocket_context * that
1354 * represents this server. After calling, user code needs to take care
1355 * of calling libwebsocket_service() with the context pointer to get the
1356 * server's sockets serviced. This can be done in the same process context
1357 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001358 *
Andy Green8f037e42010-12-19 22:13:26 +00001359 * The protocol callback functions are called for a handful of events
1360 * including http requests coming in, websocket connections becoming
1361 * established, and data arriving; it's also called periodically to allow
1362 * async transmission.
1363 *
1364 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1365 * at that time websocket protocol has not been negotiated. Other
1366 * protocols after the first one never see any HTTP callack activity.
1367 *
1368 * The server created is a simple http server by default; part of the
1369 * websocket standard is upgrading this http connection to a websocket one.
1370 *
1371 * This allows the same server to provide files like scripts and favicon /
1372 * images or whatever over http and dynamic data over websockets all in
1373 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001374 */
Andy Green4ea60062010-10-30 12:15:07 +01001375
Andy Greene92cd172011-01-19 13:11:55 +00001376struct libwebsocket_context *
Peter Hinz56885f32011-03-02 22:03:47 +00001377libwebsocket_create_context(int port, const char *interf,
Andy Greenb45993c2010-12-18 15:13:50 +00001378 struct libwebsocket_protocols *protocols,
Andy Greend6e09112011-03-05 16:12:15 +00001379 struct libwebsocket_extension *extensions,
Andy Green8f037e42010-12-19 22:13:26 +00001380 const char *ssl_cert_filepath,
1381 const char *ssl_private_key_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08001382 const char *ssl_ca_filepath,
Alon Levy0291eb32012-10-19 11:21:56 +02001383 int gid, int uid, unsigned int options,
David Galeano2f82be82013-01-09 16:25:54 +08001384 void *user)
Andy Greenff95d7a2010-10-28 22:36:01 +01001385{
1386 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001387 int m;
Andy Green251f6fa2010-11-03 11:13:06 +00001388 int fd;
Andy Greenff95d7a2010-10-28 22:36:01 +01001389 struct sockaddr_in serv_addr, cli_addr;
Andy Green251f6fa2010-11-03 11:13:06 +00001390 int opt = 1;
Peter Hinz56885f32011-03-02 22:03:47 +00001391 struct libwebsocket_context *context = NULL;
Andy Greenb45993c2010-12-18 15:13:50 +00001392 unsigned int slen;
Andy Green9659f372011-01-27 22:01:43 +00001393 char *p;
Andy Green0d338332011-02-12 11:57:43 +00001394 struct libwebsocket *wsi;
Andy Greenff95d7a2010-10-28 22:36:01 +01001395
Andy Green3faa9c72010-11-08 17:03:03 +00001396#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001397 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001398 char ssl_err_buf[512];
Andy Green3faa9c72010-11-08 17:03:03 +00001399#endif
1400
Andy Green43db0452013-01-10 19:50:35 +08001401 lwsl_info("Initial logging level %d\n", log_level);
Andy Greenc0d6b632013-01-12 23:42:17 +08001402 lwsl_info(" LWS_MAX_HEADER_NAME_LENGTH: %u\n", LWS_MAX_HEADER_NAME_LENGTH);
1403 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
1404 lwsl_info(" LWS_INITIAL_HDR_ALLOC: %u\n", LWS_INITIAL_HDR_ALLOC);
1405 lwsl_info(" LWS_ADDITIONAL_HDR_ALLOC: %u\n", LWS_ADDITIONAL_HDR_ALLOC);
1406 lwsl_info(" MAX_USER_RX_BUFFER: %u\n", MAX_USER_RX_BUFFER);
1407 lwsl_info(" MAX_BROADCAST_PAYLOAD: %u\n", MAX_BROADCAST_PAYLOAD);
1408 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
1409 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n", LWS_MAX_EXTENSIONS_ACTIVE);
1410 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1411 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
1412 lwsl_info(" CIPHERS_LIST_STRING: '%s'\n", CIPHERS_LIST_STRING);
1413 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1414 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001415
Peter Hinz56885f32011-03-02 22:03:47 +00001416#ifdef _WIN32
1417 {
1418 WORD wVersionRequested;
1419 WSADATA wsaData;
1420 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001421 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001422
1423 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1424 wVersionRequested = MAKEWORD(2, 2);
1425
1426 err = WSAStartup(wVersionRequested, &wsaData);
1427 if (err != 0) {
1428 /* Tell the user that we could not find a usable */
1429 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001430 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001431 return NULL;
1432 }
David Galeano7b11fec2011-10-04 19:55:18 +08001433
Andy Green6ee372f2012-04-09 15:09:01 +08001434 /* default to a poll() made out of select() */
1435 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001436
Andy Green6ee372f2012-04-09 15:09:01 +08001437 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001438 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001439 if (wsdll)
1440 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Peter Hinz56885f32011-03-02 22:03:47 +00001441 }
1442#endif
1443
1444
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001445 context = (struct libwebsocket_context *) malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001446 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001447 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001448 return NULL;
1449 }
Peter Hinz56885f32011-03-02 22:03:47 +00001450 context->protocols = protocols;
1451 context->listen_port = port;
1452 context->http_proxy_port = 0;
1453 context->http_proxy_address[0] = '\0';
1454 context->options = options;
Andy Greendfb23042013-01-17 12:26:48 +08001455 /* to reduce this allocation, */
1456 context->max_fds = getdtablesize();
1457 lwsl_info(" max fd tracked: %u\n", context->max_fds);
1458
1459 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) * context->max_fds);
1460 if (context->fds == NULL) {
1461 lwsl_err("Unable to allocate fds array for %d connections\n", context->max_fds);
1462 free(context);
1463 return NULL;
1464 }
1465 context->lws_lookup = (struct libwebsocket **)malloc(sizeof(struct libwebsocke *) * context->max_fds);
1466 if (context->lws_lookup == NULL) {
1467 lwsl_err("Unable to allocate lws_lookup array for %d connections\n", context->max_fds);
1468 free(context->fds);
1469 free(context);
1470 return NULL;
1471 }
Peter Hinz56885f32011-03-02 22:03:47 +00001472 context->fds_count = 0;
Andy Greend6e09112011-03-05 16:12:15 +00001473 context->extensions = extensions;
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08001474 context->last_timeout_check_s = 0;
Andy Greendfb23042013-01-17 12:26:48 +08001475 context->user_space = user;
Andy Green9659f372011-01-27 22:01:43 +00001476
Peter Hinz56885f32011-03-02 22:03:47 +00001477#ifdef WIN32
1478 context->fd_random = 0;
1479#else
1480 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
1481 if (context->fd_random < 0) {
Andy Greendfb23042013-01-17 12:26:48 +08001482 free(context);
Andy Green43db0452013-01-10 19:50:35 +08001483 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001484 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Andy Green44eee682011-02-10 09:32:24 +00001485 return NULL;
1486 }
Peter Hinz56885f32011-03-02 22:03:47 +00001487#endif
Andy Green44eee682011-02-10 09:32:24 +00001488
Peter Hinz56885f32011-03-02 22:03:47 +00001489#ifdef LWS_OPENSSL_SUPPORT
1490 context->use_ssl = 0;
1491 context->ssl_ctx = NULL;
1492 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00001493 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001494#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00001495
Andy Greena1ce6be2013-01-18 11:43:21 +08001496 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08001497
Andy Greena1ce6be2013-01-18 11:43:21 +08001498#ifndef LWS_NO_SERVER
1499 if (!(options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
1500 struct sockaddr sa;
1501 char hostname[1024] = "";
Andy Green788c4a82012-10-22 12:29:57 +01001502
1503 /* find canonical hostname */
1504
1505 hostname[(sizeof hostname) - 1] = '\0';
1506 memset(&sa, 0, sizeof(sa));
1507 sa.sa_family = AF_INET;
1508 sa.sa_data[(sizeof sa.sa_data) - 1] = '\0';
1509 gethostname(hostname, (sizeof hostname) - 1);
1510
1511 n = 0;
1512
David Galeanoed3c8402013-01-10 10:45:24 +08001513 if (strlen(hostname) < sizeof(sa.sa_data) - 1) {
Andy Green788c4a82012-10-22 12:29:57 +01001514 strcpy(sa.sa_data, hostname);
Andy Green43db0452013-01-10 19:50:35 +08001515 // lwsl_debug("my host name is %s\n", sa.sa_data);
Andy Green788c4a82012-10-22 12:29:57 +01001516 n = getnameinfo(&sa, sizeof(sa), hostname,
1517 (sizeof hostname) - 1, NULL, 0, 0);
1518 }
1519
1520 if (!n) {
1521 strncpy(context->canonical_hostname, hostname,
1522 sizeof context->canonical_hostname - 1);
1523 context->canonical_hostname[
1524 sizeof context->canonical_hostname - 1] = '\0';
1525 } else
1526 strncpy(context->canonical_hostname, hostname,
1527 sizeof context->canonical_hostname - 1);
1528
Andy Greena1ce6be2013-01-18 11:43:21 +08001529 lwsl_info(" canonical_hostname = %s\n", context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08001530 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001531#endif
Andy Greena69f0512012-05-03 12:32:38 +08001532
Andy Green9659f372011-01-27 22:01:43 +00001533 /* split the proxy ads:port if given */
1534
1535 p = getenv("http_proxy");
1536 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00001537 strncpy(context->http_proxy_address, p,
Andy Green6ee372f2012-04-09 15:09:01 +08001538 sizeof context->http_proxy_address - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00001539 context->http_proxy_address[
1540 sizeof context->http_proxy_address - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00001541
Peter Hinz56885f32011-03-02 22:03:47 +00001542 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00001543 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001544 lwsl_err("http_proxy needs to be ads:port\n");
Andy Green9659f372011-01-27 22:01:43 +00001545 return NULL;
1546 }
1547 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00001548 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00001549
Andy Greena1ce6be2013-01-18 11:43:21 +08001550 lwsl_info(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001551 context->http_proxy_address,
1552 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00001553 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001554
Andy Greena1ce6be2013-01-18 11:43:21 +08001555#ifndef LWS_NO_SERVER
Andy Green90c7cbc2011-01-27 06:26:52 +00001556 if (port) {
1557
Andy Green3faa9c72010-11-08 17:03:03 +00001558#ifdef LWS_OPENSSL_SUPPORT
Peter Hinz56885f32011-03-02 22:03:47 +00001559 context->use_ssl = ssl_cert_filepath != NULL &&
Andy Green90c7cbc2011-01-27 06:26:52 +00001560 ssl_private_key_filepath != NULL;
Peter Hinz56885f32011-03-02 22:03:47 +00001561 if (context->use_ssl)
Andy Green43db0452013-01-10 19:50:35 +08001562 lwsl_info(" Compiled with SSL support, using it\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001563 else
Andy Green43db0452013-01-10 19:50:35 +08001564 lwsl_info(" Compiled with SSL support, not using it\n");
Andy Green3faa9c72010-11-08 17:03:03 +00001565
Andy Green90c7cbc2011-01-27 06:26:52 +00001566#else
1567 if (ssl_cert_filepath != NULL &&
1568 ssl_private_key_filepath != NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001569 lwsl_info(" Not compiled for OpenSSl support!\n");
Andy Greene92cd172011-01-19 13:11:55 +00001570 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +00001571 }
Andy Green43db0452013-01-10 19:50:35 +08001572 lwsl_info(" Compiled without SSL support, "
Andy Green90c7cbc2011-01-27 06:26:52 +00001573 "serving unencrypted\n");
1574#endif
1575 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001576#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001577
1578 /* ignore SIGPIPE */
Peter Hinz56885f32011-03-02 22:03:47 +00001579#ifdef WIN32
1580#else
Andy Green90c7cbc2011-01-27 06:26:52 +00001581 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00001582#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001583
1584
1585#ifdef LWS_OPENSSL_SUPPORT
1586
1587 /* basic openssl init */
1588
1589 SSL_library_init();
1590
1591 OpenSSL_add_all_algorithms();
1592 SSL_load_error_strings();
1593
Andy Green2e24da02011-03-05 16:12:04 +00001594 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00001595 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
1596
Andy Green90c7cbc2011-01-27 06:26:52 +00001597 /*
1598 * Firefox insists on SSLv23 not SSLv3
1599 * Konq disables SSLv2 by default now, SSLv23 works
1600 */
1601
1602 method = (SSL_METHOD *)SSLv23_server_method();
1603 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001604 lwsl_err("problem creating ssl method: %s\n",
Andy Green90c7cbc2011-01-27 06:26:52 +00001605 ERR_error_string(ERR_get_error(), ssl_err_buf));
1606 return NULL;
1607 }
Peter Hinz56885f32011-03-02 22:03:47 +00001608 context->ssl_ctx = SSL_CTX_new(method); /* create context */
1609 if (!context->ssl_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001610 lwsl_err("problem creating ssl context: %s\n",
Andy Green90c7cbc2011-01-27 06:26:52 +00001611 ERR_error_string(ERR_get_error(), ssl_err_buf));
1612 return NULL;
1613 }
1614
David Galeanocc148e42013-01-10 10:18:59 +08001615#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001616 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001617#endif
David Galeano77a677c2013-01-10 10:14:12 +08001618 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
David Galeanof177f2a2013-01-10 10:15:19 +08001619 SSL_CTX_set_cipher_list(context->ssl_ctx, CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001620
Andy Greena1ce6be2013-01-18 11:43:21 +08001621#ifndef LWS_NO_CLIENT
1622
Andy Green90c7cbc2011-01-27 06:26:52 +00001623 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08001624
1625 if (port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00001626 method = (SSL_METHOD *)SSLv23_client_method();
1627 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001628 lwsl_err("problem creating ssl method: %s\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001629 ERR_error_string(ERR_get_error(), ssl_err_buf));
1630 return NULL;
1631 }
1632 /* create context */
1633 context->ssl_client_ctx = SSL_CTX_new(method);
1634 if (!context->ssl_client_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001635 lwsl_err("problem creating ssl context: %s\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001636 ERR_error_string(ERR_get_error(), ssl_err_buf));
1637 return NULL;
1638 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001639
David Galeanocc148e42013-01-10 10:18:59 +08001640#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001641 SSL_CTX_set_options(context->ssl_client_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001642#endif
David Galeano77a677c2013-01-10 10:14:12 +08001643 SSL_CTX_set_options(context->ssl_client_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
David Galeanof177f2a2013-01-10 10:15:19 +08001644 SSL_CTX_set_cipher_list(context->ssl_client_ctx, CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001645
Peter Hinz56885f32011-03-02 22:03:47 +00001646 /* openssl init for cert verification (for client sockets) */
David Galeano2f82be82013-01-09 16:25:54 +08001647 if (!ssl_ca_filepath) {
1648 if (!SSL_CTX_load_verify_locations(
1649 context->ssl_client_ctx, NULL,
1650 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08001651 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08001652 "Unable to load SSL Client certs from %s "
1653 "(set by --with-client-cert-dir= in configure) -- "
1654 " client ssl isn't going to work",
1655 LWS_OPENSSL_CLIENT_CERTS);
1656 } else
1657 if (!SSL_CTX_load_verify_locations(
1658 context->ssl_client_ctx, ssl_ca_filepath,
1659 NULL))
Andy Green43db0452013-01-10 19:50:35 +08001660 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08001661 "Unable to load SSL Client certs "
1662 "file from %s -- client ssl isn't "
1663 "going to work", ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00001664
1665 /*
1666 * callback allowing user code to load extra verification certs
1667 * helping the client to verify server identity
1668 */
1669
1670 context->protocols[0].callback(context, NULL,
1671 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
1672 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00001673 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001674#endif
Andy Green6ee372f2012-04-09 15:09:01 +08001675
Andy Greenc6bf2c22011-02-20 11:10:47 +00001676 /* as a server, are we requiring clients to identify themselves? */
1677
1678 if (options & LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
1679
1680 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08001681
Peter Hinz56885f32011-03-02 22:03:47 +00001682 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00001683 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1684 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001685
1686 /*
1687 * give user code a chance to load certs into the server
1688 * allowing it to verify incoming client certs
1689 */
1690
Peter Hinz56885f32011-03-02 22:03:47 +00001691 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00001692 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00001693 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001694 }
1695
Peter Hinz56885f32011-03-02 22:03:47 +00001696 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001697
1698 /* openssl init for server sockets */
1699
Andy Green3faa9c72010-11-08 17:03:03 +00001700 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08001701 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
1702 ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00001703 if (n != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001704 lwsl_err("problem getting cert '%s': %s\n",
Andy Green3faa9c72010-11-08 17:03:03 +00001705 ssl_cert_filepath,
1706 ERR_error_string(ERR_get_error(), ssl_err_buf));
Andy Greene92cd172011-01-19 13:11:55 +00001707 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +00001708 }
1709 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00001710 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
1711 ssl_private_key_filepath, SSL_FILETYPE_PEM) != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001712 lwsl_err("ssl problem getting key '%s': %s\n",
Andy Green018d8eb2010-11-08 21:04:23 +00001713 ssl_private_key_filepath,
1714 ERR_error_string(ERR_get_error(), ssl_err_buf));
Andy Greene92cd172011-01-19 13:11:55 +00001715 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +00001716 }
1717 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00001718 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08001719 lwsl_err("Private SSL key doesn't match cert\n");
Andy Greene92cd172011-01-19 13:11:55 +00001720 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +00001721 }
1722
1723 /* SSL is happy and has a cert it's content with */
1724 }
1725#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001726
Andy Greendf736162011-01-18 15:39:02 +00001727 /* selftest */
1728
1729 if (lws_b64_selftest())
Andy Greene92cd172011-01-19 13:11:55 +00001730 return NULL;
Andy Greendf736162011-01-18 15:39:02 +00001731
Andy Greena1ce6be2013-01-18 11:43:21 +08001732#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00001733 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00001734
Andy Green4739e5c2011-01-22 12:51:57 +00001735 if (port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08001736 extern int interface_to_sa(const char *ifname, struct sockaddr_in *addr, size_t addrlen);
1737 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00001738
Andy Green4739e5c2011-01-22 12:51:57 +00001739 sockfd = socket(AF_INET, SOCK_STREAM, 0);
1740 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08001741 lwsl_err("ERROR opening socket\n");
Andy Green4739e5c2011-01-22 12:51:57 +00001742 return NULL;
1743 }
Andy Green775c0dd2010-10-29 14:15:22 +01001744
Andy Green4739e5c2011-01-22 12:51:57 +00001745 /* allow us to restart even if old sockets in TIME_WAIT */
Andy Green6ee372f2012-04-09 15:09:01 +08001746 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
1747 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00001748
1749 /* Disable Nagle */
1750 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08001751 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
1752 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00001753
Andy Green4739e5c2011-01-22 12:51:57 +00001754 bzero((char *) &serv_addr, sizeof(serv_addr));
1755 serv_addr.sin_family = AF_INET;
Peter Hinz56885f32011-03-02 22:03:47 +00001756 if (interf == NULL)
Andy Green32375b72011-02-19 08:32:53 +00001757 serv_addr.sin_addr.s_addr = INADDR_ANY;
1758 else
Peter Hinz56885f32011-03-02 22:03:47 +00001759 interface_to_sa(interf, &serv_addr,
Andy Green32375b72011-02-19 08:32:53 +00001760 sizeof(serv_addr));
Andy Green4739e5c2011-01-22 12:51:57 +00001761 serv_addr.sin_port = htons(port);
1762
1763 n = bind(sockfd, (struct sockaddr *) &serv_addr,
1764 sizeof(serv_addr));
1765 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001766 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green8f037e42010-12-19 22:13:26 +00001767 port, n, errno);
Andy Green41c58032013-01-12 13:21:08 +08001768 close(sockfd);
Andy Green4739e5c2011-01-22 12:51:57 +00001769 return NULL;
1770 }
Andy Green0d338332011-02-12 11:57:43 +00001771
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001772 wsi = (struct libwebsocket *)malloc(sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08001773 if (wsi == NULL) {
1774 lwsl_err("Out of mem\n");
1775 close(sockfd);
1776 return NULL;
1777 }
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001778 memset(wsi, 0, sizeof (struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00001779 wsi->sock = sockfd;
Andy Greend6e09112011-03-05 16:12:15 +00001780 wsi->count_active_extensions = 0;
Andy Green0d338332011-02-12 11:57:43 +00001781 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08001782
1783 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001784
Andy Green65b0e912013-01-16 07:59:47 +08001785 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
1786 context->listen_service_count = 0;
1787 context->listen_service_fd = sockfd;
1788
Andy Greena824d182013-01-15 20:52:29 +08001789 listen(sockfd, LWS_SOMAXCONN);
Andy Green43db0452013-01-10 19:50:35 +08001790 lwsl_info(" Listening on port %d\n", port);
Andy Green8f037e42010-12-19 22:13:26 +00001791 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001792#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001793
Andy Green6ee372f2012-04-09 15:09:01 +08001794 /*
1795 * drop any root privs for this process
1796 * to listen on port < 1023 we would have needed root, but now we are
1797 * listening, we don't want the power for anything else
1798 */
Peter Hinz56885f32011-03-02 22:03:47 +00001799#ifdef WIN32
1800#else
Andy Green3faa9c72010-11-08 17:03:03 +00001801 if (gid != -1)
1802 if (setgid(gid))
Andy Green43db0452013-01-10 19:50:35 +08001803 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green3faa9c72010-11-08 17:03:03 +00001804 if (uid != -1)
1805 if (setuid(uid))
Andy Green43db0452013-01-10 19:50:35 +08001806 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00001807#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001808
1809 /* set up our internal broadcast trigger sockets per-protocol */
1810
Peter Hinz56885f32011-03-02 22:03:47 +00001811 for (context->count_protocols = 0;
1812 protocols[context->count_protocols].callback;
1813 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01001814
Andy Green43db0452013-01-10 19:50:35 +08001815 lwsl_parser(" Protocol: %s\n",
1816 protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01001817
Peter Hinz56885f32011-03-02 22:03:47 +00001818 protocols[context->count_protocols].owning_server = context;
1819 protocols[context->count_protocols].protocol_index =
1820 context->count_protocols;
Andy Greenb45993c2010-12-18 15:13:50 +00001821
1822 fd = socket(AF_INET, SOCK_STREAM, 0);
1823 if (fd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08001824 lwsl_err("ERROR opening socket\n");
Andy Greene92cd172011-01-19 13:11:55 +00001825 return NULL;
Andy Greenb45993c2010-12-18 15:13:50 +00001826 }
Andy Green8f037e42010-12-19 22:13:26 +00001827
Andy Greenb45993c2010-12-18 15:13:50 +00001828 /* allow us to restart even if old sockets in TIME_WAIT */
Andy Green6ee372f2012-04-09 15:09:01 +08001829 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&opt,
1830 sizeof(opt));
Andy Greenb45993c2010-12-18 15:13:50 +00001831
1832 bzero((char *) &serv_addr, sizeof(serv_addr));
1833 serv_addr.sin_family = AF_INET;
1834 serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
1835 serv_addr.sin_port = 0; /* pick the port for us */
1836
1837 n = bind(fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
1838 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001839 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Greenb45993c2010-12-18 15:13:50 +00001840 port, n, errno);
Andy Greene92cd172011-01-19 13:11:55 +00001841 return NULL;
Andy Greenb45993c2010-12-18 15:13:50 +00001842 }
1843
1844 slen = sizeof cli_addr;
1845 n = getsockname(fd, (struct sockaddr *)&cli_addr, &slen);
1846 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001847 lwsl_err("getsockname failed\n");
Andy Greene92cd172011-01-19 13:11:55 +00001848 return NULL;
Andy Greenb45993c2010-12-18 15:13:50 +00001849 }
Peter Hinz56885f32011-03-02 22:03:47 +00001850 protocols[context->count_protocols].broadcast_socket_port =
Andy Greenb45993c2010-12-18 15:13:50 +00001851 ntohs(cli_addr.sin_port);
1852 listen(fd, 5);
1853
Andy Green43db0452013-01-10 19:50:35 +08001854 lwsl_debug(" Protocol %s broadcast socket %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001855 protocols[context->count_protocols].name,
Andy Greenb45993c2010-12-18 15:13:50 +00001856 ntohs(cli_addr.sin_port));
1857
Andy Green0d338332011-02-12 11:57:43 +00001858 /* dummy wsi per broadcast proxy socket */
1859
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001860 wsi = (struct libwebsocket *)malloc(sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08001861 if (wsi == NULL) {
1862 lwsl_err("Out of mem\n");
1863 close(fd);
1864 return NULL;
1865 }
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001866 memset(wsi, 0, sizeof (struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00001867 wsi->sock = fd;
1868 wsi->mode = LWS_CONNMODE_BROADCAST_PROXY_LISTENER;
Andy Greend6e09112011-03-05 16:12:15 +00001869 wsi->count_active_extensions = 0;
Andy Green0d338332011-02-12 11:57:43 +00001870 /* note which protocol we are proxying */
Peter Hinz56885f32011-03-02 22:03:47 +00001871 wsi->protocol_index_for_broadcast_proxy =
1872 context->count_protocols;
Andy Green0d338332011-02-12 11:57:43 +00001873
Andy Greendfb23042013-01-17 12:26:48 +08001874 insert_wsi_socket_into_fds(context, wsi);
Andy Greenb45993c2010-12-18 15:13:50 +00001875 }
1876
Andy Greena41314f2011-05-23 10:00:03 +01001877 /*
1878 * give all extensions a chance to create any per-context
1879 * allocations they need
1880 */
1881
1882 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
1883 if (port)
1884 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andrew Chambersd5512172012-05-20 08:17:09 +08001885
1886 if (extensions) {
1887 while (extensions->callback) {
Andy Green43db0452013-01-10 19:50:35 +08001888 lwsl_ext(" Extension: %s\n", extensions->name);
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001889 extensions->callback(context, extensions, NULL,
1890 (enum libwebsocket_extension_callback_reasons)m,
1891 NULL, NULL, 0);
Andrew Chambersd5512172012-05-20 08:17:09 +08001892 extensions++;
1893 }
Andy Greena41314f2011-05-23 10:00:03 +01001894 }
1895
Peter Hinz56885f32011-03-02 22:03:47 +00001896 return context;
Andy Greene92cd172011-01-19 13:11:55 +00001897}
Andy Greenb45993c2010-12-18 15:13:50 +00001898
Andy Green4739e5c2011-01-22 12:51:57 +00001899
Andy Greened11a022011-01-20 10:23:50 +00001900#ifndef LWS_NO_FORK
1901
Andy Greene92cd172011-01-19 13:11:55 +00001902/**
1903 * libwebsockets_fork_service_loop() - Optional helper function forks off
1904 * a process for the websocket server loop.
Andy Green6964bb52011-01-23 16:50:33 +00001905 * You don't have to use this but if not, you
1906 * have to make sure you are calling
1907 * libwebsocket_service periodically to service
1908 * the websocket traffic
Peter Hinz56885f32011-03-02 22:03:47 +00001909 * @context: server context returned by creation function
Andy Greene92cd172011-01-19 13:11:55 +00001910 */
Andy Greenb45993c2010-12-18 15:13:50 +00001911
Andy Greene92cd172011-01-19 13:11:55 +00001912int
Peter Hinz56885f32011-03-02 22:03:47 +00001913libwebsockets_fork_service_loop(struct libwebsocket_context *context)
Andy Greene92cd172011-01-19 13:11:55 +00001914{
Andy Greene92cd172011-01-19 13:11:55 +00001915 int fd;
1916 struct sockaddr_in cli_addr;
1917 int n;
Andy Green3221f922011-02-12 13:14:11 +00001918 int p;
Andy Greenb45993c2010-12-18 15:13:50 +00001919
Andy Greened11a022011-01-20 10:23:50 +00001920 n = fork();
1921 if (n < 0)
1922 return n;
1923
1924 if (!n) {
1925
1926 /* main process context */
1927
Andy Green3221f922011-02-12 13:14:11 +00001928 /*
1929 * set up the proxy sockets to allow broadcast from
1930 * service process context
1931 */
1932
Peter Hinz56885f32011-03-02 22:03:47 +00001933 for (p = 0; p < context->count_protocols; p++) {
Andy Greened11a022011-01-20 10:23:50 +00001934 fd = socket(AF_INET, SOCK_STREAM, 0);
1935 if (fd < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001936 lwsl_err("Unable to create socket\n");
Andy Greened11a022011-01-20 10:23:50 +00001937 return -1;
1938 }
1939 cli_addr.sin_family = AF_INET;
1940 cli_addr.sin_port = htons(
Peter Hinz56885f32011-03-02 22:03:47 +00001941 context->protocols[p].broadcast_socket_port);
Andy Greened11a022011-01-20 10:23:50 +00001942 cli_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
1943 n = connect(fd, (struct sockaddr *)&cli_addr,
1944 sizeof cli_addr);
1945 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001946 lwsl_err("Unable to connect to "
Andy Greened11a022011-01-20 10:23:50 +00001947 "broadcast socket %d, %s\n",
Andy Green3221f922011-02-12 13:14:11 +00001948 n, strerror(errno));
Andy Greened11a022011-01-20 10:23:50 +00001949 return -1;
1950 }
1951
Peter Hinz56885f32011-03-02 22:03:47 +00001952 context->protocols[p].broadcast_socket_user_fd = fd;
Andy Greened11a022011-01-20 10:23:50 +00001953 }
1954
Andy Greene92cd172011-01-19 13:11:55 +00001955 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +00001956 }
1957
Artem Baguinski91531662011-12-14 22:14:03 +01001958#ifdef HAVE_SYS_PRCTL_H
Andy Greenb45993c2010-12-18 15:13:50 +00001959 /* we want a SIGHUP when our parent goes down */
1960 prctl(PR_SET_PDEATHSIG, SIGHUP);
Artem Baguinski91531662011-12-14 22:14:03 +01001961#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001962
1963 /* in this forked process, sit and service websocket connections */
Andy Green8f037e42010-12-19 22:13:26 +00001964
Artem Baguinski91531662011-12-14 22:14:03 +01001965 while (1) {
Peter Hinz56885f32011-03-02 22:03:47 +00001966 if (libwebsocket_service(context, 1000))
Andy Green3928f612012-07-20 12:58:38 +08001967 break;
Andy Green5e8967a2012-10-17 20:10:44 +08001968//#ifndef HAVE_SYS_PRCTL_H
Artem Baguinski91531662011-12-14 22:14:03 +01001969/*
1970 * on systems without prctl() (i.e. anything but linux) we can notice that our
1971 * parent is dead if getppid() returns 1. FIXME apparently this is not true for
1972 * solaris, could remember ppid right after fork and wait for it to change.
1973 */
1974
1975 if (getppid() == 1)
1976 break;
Andy Green5e8967a2012-10-17 20:10:44 +08001977//#endif
Artem Baguinski91531662011-12-14 22:14:03 +01001978 }
1979
Andy Green8f037e42010-12-19 22:13:26 +00001980
Andy Green3928f612012-07-20 12:58:38 +08001981 return 1;
Andy Greenff95d7a2010-10-28 22:36:01 +01001982}
1983
Andy Greened11a022011-01-20 10:23:50 +00001984#endif
1985
Andy Greenb45993c2010-12-18 15:13:50 +00001986/**
1987 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00001988 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00001989 * @wsi: pointer to struct websocket you want to know the protocol of
1990 *
Andy Green8f037e42010-12-19 22:13:26 +00001991 *
1992 * This is useful to get the protocol to broadcast back to from inside
Andy Greenb45993c2010-12-18 15:13:50 +00001993 * the callback.
1994 */
Andy Greenab990e42010-10-31 12:42:52 +00001995
Andy Greenb45993c2010-12-18 15:13:50 +00001996const struct libwebsocket_protocols *
1997libwebsockets_get_protocol(struct libwebsocket *wsi)
1998{
1999 return wsi->protocol;
2000}
2001
2002/**
Andy Greene92cd172011-01-19 13:11:55 +00002003 * libwebsockets_broadcast() - Sends a buffer to the callback for all active
Andy Green8f037e42010-12-19 22:13:26 +00002004 * connections of the given protocol.
Andy Greenb45993c2010-12-18 15:13:50 +00002005 * @protocol: pointer to the protocol you will broadcast to all members of
2006 * @buf: buffer containing the data to be broadcase. NOTE: this has to be
Andy Green8f037e42010-12-19 22:13:26 +00002007 * allocated with LWS_SEND_BUFFER_PRE_PADDING valid bytes before
2008 * the pointer and LWS_SEND_BUFFER_POST_PADDING afterwards in the
2009 * case you are calling this function from callback context.
Andy Greenb45993c2010-12-18 15:13:50 +00002010 * @len: length of payload data in buf, starting from buf.
Andy Green8f037e42010-12-19 22:13:26 +00002011 *
2012 * This function allows bulk sending of a packet to every connection using
Andy Greenb45993c2010-12-18 15:13:50 +00002013 * the given protocol. It does not send the data directly; instead it calls
2014 * the callback with a reason type of LWS_CALLBACK_BROADCAST. If the callback
2015 * wants to actually send the data for that connection, the callback itself
2016 * should call libwebsocket_write().
2017 *
2018 * libwebsockets_broadcast() can be called from another fork context without
2019 * having to take any care about data visibility between the processes, it'll
2020 * "just work".
2021 */
2022
2023
2024int
Andy Green8f037e42010-12-19 22:13:26 +00002025libwebsockets_broadcast(const struct libwebsocket_protocols *protocol,
Andy Greenb45993c2010-12-18 15:13:50 +00002026 unsigned char *buf, size_t len)
2027{
Peter Hinz56885f32011-03-02 22:03:47 +00002028 struct libwebsocket_context *context = protocol->owning_server;
Andy Greenb45993c2010-12-18 15:13:50 +00002029 int n;
Andy Green6ee372f2012-04-09 15:09:01 +08002030 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +00002031
2032 if (!protocol->broadcast_socket_user_fd) {
2033 /*
Andy Greene92cd172011-01-19 13:11:55 +00002034 * We are either running unforked / flat, or we are being
2035 * called from poll thread context
Andy Greenb45993c2010-12-18 15:13:50 +00002036 * eg, from a callback. In that case don't use sockets for
2037 * broadcast IPC (since we can't open a socket connection to
2038 * a socket listening on our own thread) but directly do the
2039 * send action.
2040 *
2041 * Locking is not needed because we are by definition being
2042 * called in the poll thread context and are serialized.
2043 */
2044
Andy Greendfb23042013-01-17 12:26:48 +08002045 for (n = 0; n < context->fds_count; n++) {
Andy Greenb45993c2010-12-18 15:13:50 +00002046
Andy Greendfb23042013-01-17 12:26:48 +08002047 wsi = context->lws_lookup[context->fds[n].fd];
2048 if (!wsi)
2049 continue;
Andy Greenb45993c2010-12-18 15:13:50 +00002050
Andy Greendfb23042013-01-17 12:26:48 +08002051 if (wsi->mode != LWS_CONNMODE_WS_SERVING)
2052 continue;
Andy Greenb45993c2010-12-18 15:13:50 +00002053
Andy Greendfb23042013-01-17 12:26:48 +08002054 /*
2055 * never broadcast to non-established connections
2056 */
2057 if (wsi->state != WSI_STATE_ESTABLISHED)
2058 continue;
Andy Greenb45993c2010-12-18 15:13:50 +00002059
Andy Greendfb23042013-01-17 12:26:48 +08002060 /* only broadcast to guys using
2061 * requested protocol
2062 */
2063 if (wsi->protocol != protocol)
2064 continue;
Andy Green0d338332011-02-12 11:57:43 +00002065
Andy Green706961d2013-01-17 16:50:35 +08002066 user_callback_handle_rxflow(wsi->protocol->callback,
2067 context, wsi,
Andy Greendfb23042013-01-17 12:26:48 +08002068 LWS_CALLBACK_BROADCAST,
2069 wsi->user_space,
2070 buf, len);
Andy Greenb45993c2010-12-18 15:13:50 +00002071 }
2072
2073 return 0;
2074 }
2075
Andy Green0ca6a172010-12-19 20:50:01 +00002076 /*
2077 * We're being called from a different process context than the server
2078 * loop. Instead of broadcasting directly, we send our
2079 * payload on a socket to do the IPC; the server process will serialize
2080 * the broadcast action in its main poll() loop.
2081 *
2082 * There's one broadcast socket listening for each protocol supported
2083 * set up when the websocket server initializes
2084 */
2085
Andy Green6964bb52011-01-23 16:50:33 +00002086 n = send(protocol->broadcast_socket_user_fd, buf, len, MSG_NOSIGNAL);
Andy Greenb45993c2010-12-18 15:13:50 +00002087
2088 return n;
2089}
Andy Green82c3d542011-03-07 21:16:31 +00002090
2091int
2092libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2093{
2094 return wsi->final;
2095}
Alex Bligh49146db2011-11-07 17:19:25 +08002096
David Galeanoe2cf9922013-01-09 18:06:55 +08002097unsigned char
2098libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2099{
2100 return wsi->rsv;
2101}
2102
Alex Bligh49146db2011-11-07 17:19:25 +08002103void *
2104libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2105{
2106 /* allocate the per-connection user memory (if any) */
2107
2108 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2109 wsi->user_space = malloc(
2110 wsi->protocol->per_session_data_size);
2111 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002112 lwsl_err("Out of memory for conn user space\n");
Alex Bligh49146db2011-11-07 17:19:25 +08002113 return NULL;
2114 }
Andy Green6ee372f2012-04-09 15:09:01 +08002115 memset(wsi->user_space, 0,
2116 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002117 }
2118 return wsi->user_space;
2119}
Andy Green43db0452013-01-10 19:50:35 +08002120
Andy Greenacbaee62013-01-18 22:00:22 +08002121/**
2122 * lws_confirm_legit_wsi: returns nonzero if the wsi looks bad
2123 *
2124 * @wsi: struct libwebsocket to assess
2125 *
2126 * Performs consistecy checks on what the wsi claims and what the
2127 * polling arrays hold. This'll catch a closed wsi still in use.
2128 * Don't try to use on the listen (nonconnection) wsi as it will
2129 * fail it. Otherwise 0 return == wsi seems consistent.
2130 */
2131
2132int lws_confirm_legit_wsi(struct libwebsocket *wsi)
2133{
2134 struct libwebsocket_context *context;
2135
2136 if (!(wsi && wsi->protocol && wsi->protocol->owning_server))
2137 return 1;
2138
2139 context = wsi->protocol->owning_server;
2140
2141 if (!context)
2142 return 2;
2143
2144 if (!wsi->position_in_fds_table)
2145 return 3; /* position in fds table looks bad */
2146 if (context->fds[wsi->position_in_fds_table].fd != wsi->sock)
2147 return 4; /* pollfd entry does not wait on our socket descriptor */
2148 if (context->lws_lookup[wsi->sock] != wsi)
2149 return 5; /* lookup table does not agree with wsi */
2150
2151 return 0;
2152}
Andy Greende8f27a2013-01-12 09:17:42 +08002153
Andy Green0b319092013-01-19 11:17:56 +08002154static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002155{
Andy Green0b319092013-01-19 11:17:56 +08002156 char buf[300];
2157 struct timeval tv;
2158 int pos = 0;
2159 int n;
2160
2161 gettimeofday(&tv, NULL);
2162
2163 buf[0] = '\0';
2164 for (n = 0; n < LLL_COUNT; n++)
2165 if (level == (1 << n)) {
2166 pos = sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
2167 (int)(tv.tv_usec / 100), log_level_names[n]);
2168 break;
2169 }
2170
2171 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002172}
2173
Andy Green43db0452013-01-10 19:50:35 +08002174void _lws_log(int filter, const char *format, ...)
2175{
Andy Greende8f27a2013-01-12 09:17:42 +08002176 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002177 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002178
2179 if (!(log_level & filter))
2180 return;
2181
Andy Green43db0452013-01-10 19:50:35 +08002182 va_start(ap, format);
Andy Green0b319092013-01-19 11:17:56 +08002183 vsnprintf(buf, (sizeof buf), format, ap);
Andy Greende8f27a2013-01-12 09:17:42 +08002184 buf[(sizeof buf) - 1] = '\0';
2185 va_end(ap);
2186
Andy Green0b319092013-01-19 11:17:56 +08002187 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002188}
2189
2190/**
2191 * lws_set_log_level() - Set the logging bitfield
2192 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002193 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2194 * function to perform log string emission instead of
2195 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002196 *
Andy Greende8f27a2013-01-12 09:17:42 +08002197 * log level defaults to "err" and "warn" contexts enabled only and
2198 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002199 */
2200
Andy Greende8f27a2013-01-12 09:17:42 +08002201void lws_set_log_level(int level, void (*log_emit_function)(const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002202{
2203 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002204 if (log_emit_function)
2205 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002206}