blob: 6ae44c4c6ea8b9758e912d0c8ffffa922b44d209 [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 Greenc11db202013-01-19 11:12:16 +080023#include <syslog.h>
Andy Greenff95d7a2010-10-28 22:36:01 +010024
Peter Hinz56885f32011-03-02 22:03:47 +000025#ifdef WIN32
David Galeanocb193682013-01-09 15:29:00 +080026#include <tchar.h>
27#include <io.h>
Peter Hinz56885f32011-03-02 22:03:47 +000028#else
Davidc4ef7b12013-01-12 20:39:47 +080029#ifdef LWS_BUILTIN_GETIFADDRS
30#include <getifaddrs.h>
31#else
Peter Hinz56885f32011-03-02 22:03:47 +000032#include <ifaddrs.h>
Davidc4ef7b12013-01-12 20:39:47 +080033#endif
Andy Green7627af52011-03-09 15:13:52 +000034#include <sys/un.h>
Andy Greena69f0512012-05-03 12:32:38 +080035#include <sys/socket.h>
36#include <netdb.h>
Peter Hinz56885f32011-03-02 22:03:47 +000037#endif
Andy Green2e24da02011-03-05 16:12:04 +000038
39#ifdef LWS_OPENSSL_SUPPORT
40int openssl_websocket_private_data_index;
41#endif
42
Andy Greenaa6fc442012-04-12 13:26:49 +080043#ifdef __MINGW32__
44#include "../win32port/win32helpers/websock-w32.c"
45#else
46#ifdef __MINGW64__
47#include "../win32port/win32helpers/websock-w32.c"
48#endif
49#endif
50
Andy Green7c19c342013-01-19 12:18:07 +080051static int log_level = LLL_ERR | LLL_WARN | LLL_NOTICE;
Andy Green058ba812013-01-19 11:32:18 +080052static void lwsl_emit_stderr(int level, const char *line);
53static void (*lwsl_emit)(int level, const char *line) = lwsl_emit_stderr;
54
Andy Green43db0452013-01-10 19:50:35 +080055static const char *log_level_names[] = {
56 "ERR",
57 "WARN",
Andy Green7c19c342013-01-19 12:18:07 +080058 "NOTICE",
Andy Green43db0452013-01-10 19:50:35 +080059 "INFO",
60 "DEBUG",
61 "PARSER",
62 "HEADER",
63 "EXTENSION",
64 "CLIENT",
65};
66
Andy Green0d338332011-02-12 11:57:43 +000067int
Andy Greendfb23042013-01-17 12:26:48 +080068insert_wsi_socket_into_fds(struct libwebsocket_context *context, struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +000069{
Andy Greendfb23042013-01-17 12:26:48 +080070 if (context->fds_count >= context->max_fds) {
71 lwsl_err("Reached limit of fds tracking (%d)\n", context->max_fds);
Andy Green0d338332011-02-12 11:57:43 +000072 return 1;
73 }
74
Andy Greendfb23042013-01-17 12:26:48 +080075 if (wsi->sock > context->max_fds) {
76 lwsl_err("Socket fd %d is beyond what we can index (%d)\n", wsi->sock, context->max_fds);
77 return 1;
78 }
79
80 assert(wsi);
81 assert(wsi->sock);
82
83 lwsl_info("insert_wsi_socket_into_fds: wsi=%p, sock=%d, fds pos=%d\n", wsi, wsi->sock, context->fds_count);
84
85 context->lws_lookup[wsi->sock] = wsi;
86 wsi->position_in_fds_table = context->fds_count;
87 context->fds[context->fds_count].fd = wsi->sock;
88 context->fds[context->fds_count].events = POLLIN;
89 context->fds[context->fds_count++].revents = 0;
90
91 /* external POLL support via protocol 0 */
92 context->protocols[0].callback(context, wsi,
93 LWS_CALLBACK_ADD_POLL_FD,
94 (void *)(long)wsi->sock, NULL, POLLIN);
Andy Green0d338332011-02-12 11:57:43 +000095
96 return 0;
97}
98
Andy Greendfb23042013-01-17 12:26:48 +080099static int
100remove_wsi_socket_from_fds(struct libwebsocket_context *context, struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000101{
Andy Greendfb23042013-01-17 12:26:48 +0800102 int m;
Andy Green0d338332011-02-12 11:57:43 +0000103
Andy Greendfb23042013-01-17 12:26:48 +0800104 if (!--context->fds_count)
105 goto do_ext;
Andy Green0d338332011-02-12 11:57:43 +0000106
Andy Greendfb23042013-01-17 12:26:48 +0800107 if (wsi->sock > context->max_fds) {
108 lwsl_err("Socket fd %d is beyond what we can index (%d)\n", wsi->sock, context->max_fds);
109 return 1;
110 }
Andy Green0d338332011-02-12 11:57:43 +0000111
Andy Greendfb23042013-01-17 12:26:48 +0800112 lwsl_info("remove_wsi_socket_from_fds: wsi=%p, sock=%d, fds pos=%d\n", wsi, wsi->sock, wsi->position_in_fds_table);
113
114 m = wsi->position_in_fds_table; /* replace the contents for this */
115
116 /* have the last guy take up the vacant slot */
117 context->fds[m] = context->fds[context->fds_count]; /* vacant fds slot filled with end one */
118 /* end guy's fds_lookup entry remains unchanged (still same fd pointing to same wsi) */
119 /* end guy's "position in fds table" changed */
120 context->lws_lookup[context->fds[context->fds_count].fd]->position_in_fds_table = m;
121 /* deletion guy's lws_lookup entry needs nuking */
122 context->lws_lookup[wsi->sock] = NULL; /* no WSI for the socket of the wsi being removed*/
123 wsi->position_in_fds_table = -1; /* removed wsi has no position any more */
124
125do_ext:
126 /* remove also from external POLL support via protocol 0 */
127 if (wsi->sock)
128 context->protocols[0].callback(context, wsi,
129 LWS_CALLBACK_DEL_POLL_FD, (void *)(long)wsi->sock, NULL, 0);
130
131 return 0;
Andy Green0d338332011-02-12 11:57:43 +0000132}
133
Andy Green32375b72011-02-19 08:32:53 +0000134
Andy Green8f037e42010-12-19 22:13:26 +0000135void
Peter Hinz56885f32011-03-02 22:03:47 +0000136libwebsocket_close_and_free_session(struct libwebsocket_context *context,
Andy Green687b0182011-02-26 11:04:01 +0000137 struct libwebsocket *wsi, enum lws_close_status reason)
Andy Green251f6fa2010-11-03 11:13:06 +0000138{
Andy Greenb45993c2010-12-18 15:13:50 +0000139 int n;
Andy Green62c54d22011-02-14 09:14:25 +0000140 int old_state;
Andy Green5e1fa172011-02-10 09:07:05 +0000141 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 +
142 LWS_SEND_BUFFER_POST_PADDING];
Andy Greenc44159f2011-03-07 07:08:18 +0000143 int ret;
144 int m;
145 struct lws_tokens eff_buf;
Andy Greena41314f2011-05-23 10:00:03 +0100146 struct libwebsocket_extension *ext;
Andy Greenb45993c2010-12-18 15:13:50 +0000147
Andy Green4b6fbe12011-02-14 08:03:48 +0000148 if (!wsi)
Andy Greenb45993c2010-12-18 15:13:50 +0000149 return;
150
Andy Green62c54d22011-02-14 09:14:25 +0000151 old_state = wsi->state;
Andy Green251f6fa2010-11-03 11:13:06 +0000152
Andy Green62c54d22011-02-14 09:14:25 +0000153 if (old_state == WSI_STATE_DEAD_SOCKET)
Andy Green5e1fa172011-02-10 09:07:05 +0000154 return;
155
Andy Greenda527df2011-03-07 07:08:12 +0000156 wsi->close_reason = reason;
157
158 /*
Andy Green68b45042011-05-25 21:41:57 +0100159 * are his extensions okay with him closing? Eg he might be a mux
160 * parent and just his ch1 aspect is closing?
161 */
162
163
164 for (n = 0; n < wsi->count_active_extensions; n++) {
165 if (!wsi->active_extensions[n]->callback)
166 continue;
167
168 m = wsi->active_extensions[n]->callback(context,
169 wsi->active_extensions[n], wsi,
170 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
171 wsi->active_extensions_user[n], NULL, 0);
172
173 /*
174 * if somebody vetoed actually closing him at this time....
175 * up to the extension to track the attempted close, let's
176 * just bail
177 */
178
179 if (m) {
Andy Green43db0452013-01-10 19:50:35 +0800180 lwsl_ext("extension vetoed close\n");
Andy Green68b45042011-05-25 21:41:57 +0100181 return;
182 }
183 }
184
185
186
187 /*
Andy Greenc44159f2011-03-07 07:08:18 +0000188 * flush any tx pending from extensions, since we may send close packet
189 * if there are problems with send, just nuke the connection
190 */
191
192 ret = 1;
193 while (ret == 1) {
194
195 /* default to nobody has more to spill */
196
197 ret = 0;
198 eff_buf.token = NULL;
199 eff_buf.token_len = 0;
200
201 /* show every extension the new incoming data */
202
203 for (n = 0; n < wsi->count_active_extensions; n++) {
204 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000205 wsi->protocol->owning_server,
206 wsi->active_extensions[n], wsi,
Andy Greenc44159f2011-03-07 07:08:18 +0000207 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
208 wsi->active_extensions_user[n], &eff_buf, 0);
209 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800210 lwsl_ext("Extension reports fatal error\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000211 goto just_kill_connection;
212 }
213 if (m)
214 /*
215 * at least one extension told us he has more
216 * to spill, so we will go around again after
217 */
218 ret = 1;
219 }
220
221 /* assuming they left us something to send, send it */
222
223 if (eff_buf.token_len)
224 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
Andy Green0303db42013-01-17 14:46:43 +0800225 eff_buf.token_len)) {
226 lwsl_debug("close: sending final extension spill had problems\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000227 goto just_kill_connection;
Andy Green0303db42013-01-17 14:46:43 +0800228 }
Andy Greenc44159f2011-03-07 07:08:18 +0000229 }
230
231 /*
Andy Greenda527df2011-03-07 07:08:12 +0000232 * signal we are closing, libsocket_write will
233 * add any necessary version-specific stuff. If the write fails,
234 * no worries we are closing anyway. If we didn't initiate this
235 * close, then our state has been changed to
236 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this.
237 *
238 * Likewise if it's a second call to close this connection after we
239 * sent the close indication to the peer already, we are in state
240 * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time.
241 */
242
243 if (old_state == WSI_STATE_ESTABLISHED &&
244 reason != LWS_CLOSE_STATUS_NOSTATUS) {
Andy Green66a16f32011-05-24 22:07:45 +0100245
Andy Green43db0452013-01-10 19:50:35 +0800246 lwsl_debug("sending close indication...\n");
Andy Green66a16f32011-05-24 22:07:45 +0100247
Andy Greenda527df2011-03-07 07:08:12 +0000248 n = libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING],
249 0, LWS_WRITE_CLOSE);
250 if (!n) {
251 /*
252 * we have sent a nice protocol level indication we
253 * now wish to close, we should not send anything more
254 */
255
256 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
257
Andy Green0303db42013-01-17 14:46:43 +0800258 /* and we should wait for a reply for a bit out of politeness */
Andy Greenda527df2011-03-07 07:08:12 +0000259
260 libwebsocket_set_timeout(wsi,
Andy Green0303db42013-01-17 14:46:43 +0800261 PENDING_TIMEOUT_CLOSE_ACK, 1);
Andy Greenda527df2011-03-07 07:08:12 +0000262
Andy Green43db0452013-01-10 19:50:35 +0800263 lwsl_debug("sent close indication, awaiting ack\n");
Andy Greenda527df2011-03-07 07:08:12 +0000264
265 return;
266 }
267
Andy Green0303db42013-01-17 14:46:43 +0800268 lwsl_info("close: sending the close packet failed, hanging up\n");
269
Andy Greenda527df2011-03-07 07:08:12 +0000270 /* else, the send failed and we should just hang up */
271 }
272
Andy Greenc44159f2011-03-07 07:08:18 +0000273just_kill_connection:
Andy Green66a16f32011-05-24 22:07:45 +0100274
Andy Green43db0452013-01-10 19:50:35 +0800275 lwsl_debug("libwebsocket_close_and_free_session: just_kill_connection\n");
Andy Green66a16f32011-05-24 22:07:45 +0100276
Andy Greenda527df2011-03-07 07:08:12 +0000277 /*
278 * we won't be servicing or receiving anything further from this guy
Andy Greendfb23042013-01-17 12:26:48 +0800279 * delete socket from the internal poll list if still present
Andy Greenda527df2011-03-07 07:08:12 +0000280 */
Andy Green4b6fbe12011-02-14 08:03:48 +0000281
Andy Greendfb23042013-01-17 12:26:48 +0800282 remove_wsi_socket_from_fds(context, wsi);
Andy Green4b6fbe12011-02-14 08:03:48 +0000283
Andy Green251f6fa2010-11-03 11:13:06 +0000284 wsi->state = WSI_STATE_DEAD_SOCKET;
285
Andy Green4b6fbe12011-02-14 08:03:48 +0000286 /* tell the user it's all over for this guy */
287
Andy Greend4302732011-02-28 07:45:29 +0000288 if (wsi->protocol && wsi->protocol->callback &&
Andy Green6ee372f2012-04-09 15:09:01 +0800289 ((old_state == WSI_STATE_ESTABLISHED) ||
290 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
291 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
Andy Green43db0452013-01-10 19:50:35 +0800292 lwsl_debug("calling back CLOSED\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000293 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000294 wsi->user_space, NULL, 0);
Andy Greencc012472011-11-07 19:53:23 +0800295 } else
Andy Green43db0452013-01-10 19:50:35 +0800296 lwsl_debug("not calling back closed, old_state=%d\n", old_state);
Andy Green251f6fa2010-11-03 11:13:06 +0000297
Andy Greenef660a92011-03-06 10:29:38 +0000298 /* deallocate any active extension contexts */
299
300 for (n = 0; n < wsi->count_active_extensions; n++) {
301 if (!wsi->active_extensions[n]->callback)
302 continue;
303
Andy Green46c2ea02011-03-22 09:04:01 +0000304 wsi->active_extensions[n]->callback(context,
305 wsi->active_extensions[n], wsi,
306 LWS_EXT_CALLBACK_DESTROY,
307 wsi->active_extensions_user[n], NULL, 0);
Andy Greenef660a92011-03-06 10:29:38 +0000308
309 free(wsi->active_extensions_user[n]);
310 }
311
Andy Greena41314f2011-05-23 10:00:03 +0100312 /*
313 * inform all extensions in case they tracked this guy out of band
314 * even though not active on him specifically
315 */
316
317 ext = context->extensions;
318 while (ext && ext->callback) {
319 ext->callback(context, ext, wsi,
320 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
321 NULL, NULL, 0);
322 ext++;
323 }
324
Andy Greenef660a92011-03-06 10:29:38 +0000325 /* free up his parsing allocations */
Andy Green4b6fbe12011-02-14 08:03:48 +0000326
Andy Green251f6fa2010-11-03 11:13:06 +0000327 for (n = 0; n < WSI_TOKEN_COUNT; n++)
328 if (wsi->utf8_token[n].token)
329 free(wsi->utf8_token[n].token);
Andy Greena1ce6be2013-01-18 11:43:21 +0800330#ifndef LWS_NO_CLIENT
Andy Greena41314f2011-05-23 10:00:03 +0100331 if (wsi->c_address)
332 free(wsi->c_address);
Andy Greena1ce6be2013-01-18 11:43:21 +0800333#endif
Andy Green706961d2013-01-17 16:50:35 +0800334 if (wsi->rxflow_buffer)
335 free(wsi->rxflow_buffer);
336
Andy Green43db0452013-01-10 19:50:35 +0800337/* lwsl_info("closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000338
Andy Green3faa9c72010-11-08 17:03:03 +0000339#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000340 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000341 n = SSL_get_fd(wsi->ssl);
342 SSL_shutdown(wsi->ssl);
Andy Green3fc2c652013-01-14 15:35:02 +0800343 compatible_close(n);
Andy Green3faa9c72010-11-08 17:03:03 +0000344 SSL_free(wsi->ssl);
345 } else {
346#endif
Andy Green0303db42013-01-17 14:46:43 +0800347 if (wsi->sock) {
348 n = shutdown(wsi->sock, SHUT_RDWR);
349 if (n)
350 lwsl_debug("closing: shutdown returned %d\n", errno);
Andy Green3fc2c652013-01-14 15:35:02 +0800351
Andy Green0303db42013-01-17 14:46:43 +0800352 n = compatible_close(wsi->sock);
353 if (n)
354 lwsl_debug("closing: close returned %d\n", errno);
355 }
Andy Green3faa9c72010-11-08 17:03:03 +0000356#ifdef LWS_OPENSSL_SUPPORT
357 }
358#endif
David Brooks2c60d952012-04-20 12:19:01 +0800359 if (wsi->protocol && wsi->protocol->per_session_data_size && wsi->user_space) /* user code may own */
Andy Green4f3943a2010-11-12 10:44:16 +0000360 free(wsi->user_space);
361
Andy Green251f6fa2010-11-03 11:13:06 +0000362 free(wsi);
363}
364
Andy Green07034092011-02-13 08:37:12 +0000365/**
Andy Greenf7ee5492011-02-13 09:04:21 +0000366 * libwebsockets_hangup_on_client() - Server calls to terminate client
Andy Green6ee372f2012-04-09 15:09:01 +0800367 * connection
Peter Hinz56885f32011-03-02 22:03:47 +0000368 * @context: libwebsockets context
Andy Greenf7ee5492011-02-13 09:04:21 +0000369 * @fd: Connection socket descriptor
370 */
371
372void
Peter Hinz56885f32011-03-02 22:03:47 +0000373libwebsockets_hangup_on_client(struct libwebsocket_context *context, int fd)
Andy Greenf7ee5492011-02-13 09:04:21 +0000374{
Andy Greendfb23042013-01-17 12:26:48 +0800375 struct libwebsocket *wsi = context->lws_lookup[fd];
Andy Greenf7ee5492011-02-13 09:04:21 +0000376
Andy Greendfb23042013-01-17 12:26:48 +0800377 if (wsi) {
378 libwebsocket_close_and_free_session(context,
379 wsi, LWS_CLOSE_STATUS_NOSTATUS);
380 } else
381 close(fd);
Andy Greenf7ee5492011-02-13 09:04:21 +0000382}
383
384
385/**
Andy Green07034092011-02-13 08:37:12 +0000386 * libwebsockets_get_peer_addresses() - Get client address information
387 * @fd: Connection socket descriptor
388 * @name: Buffer to take client address name
389 * @name_len: Length of client address name buffer
390 * @rip: Buffer to take client address IP qotted quad
391 * @rip_len: Length of client address IP buffer
392 *
393 * This function fills in @name and @rip with the name and IP of
Andy Green6ee372f2012-04-09 15:09:01 +0800394 * the client connected with socket descriptor @fd. Names may be
395 * truncated if there is not enough room. If either cannot be
396 * determined, they will be returned as valid zero-length strings.
Andy Green07034092011-02-13 08:37:12 +0000397 */
398
399void
400libwebsockets_get_peer_addresses(int fd, char *name, int name_len,
401 char *rip, int rip_len)
402{
403 unsigned int len;
404 struct sockaddr_in sin;
405 struct hostent *host;
406 struct hostent *host1;
407 char ip[128];
Andy Greenf92def72011-03-09 15:02:20 +0000408 unsigned char *p;
Andy Green07034092011-02-13 08:37:12 +0000409 int n;
David Galeanocb193682013-01-09 15:29:00 +0800410#ifdef AF_LOCAL
411 struct sockaddr_un *un;
412#endif
Andy Green07034092011-02-13 08:37:12 +0000413
414 rip[0] = '\0';
415 name[0] = '\0';
416
417 len = sizeof sin;
418 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
419 perror("getpeername");
420 return;
421 }
Andy Green6ee372f2012-04-09 15:09:01 +0800422
Andy Green07034092011-02-13 08:37:12 +0000423 host = gethostbyaddr((char *) &sin.sin_addr, sizeof sin.sin_addr,
424 AF_INET);
425 if (host == NULL) {
426 perror("gethostbyaddr");
427 return;
428 }
429
430 strncpy(name, host->h_name, name_len);
431 name[name_len - 1] = '\0';
432
433 host1 = gethostbyname(host->h_name);
434 if (host1 == NULL)
435 return;
Andy Greenf92def72011-03-09 15:02:20 +0000436 p = (unsigned char *)host1;
Andy Green07034092011-02-13 08:37:12 +0000437 n = 0;
438 while (p != NULL) {
Andy Greenf92def72011-03-09 15:02:20 +0000439 p = (unsigned char *)host1->h_addr_list[n++];
Andy Green07034092011-02-13 08:37:12 +0000440 if (p == NULL)
441 continue;
Peter Hinzbb45a902011-03-10 18:14:01 +0000442 if ((host1->h_addrtype != AF_INET)
443#ifdef AF_LOCAL
444 && (host1->h_addrtype != AF_LOCAL)
445#endif
446 )
Andy Green07034092011-02-13 08:37:12 +0000447 continue;
448
Andy Green7627af52011-03-09 15:13:52 +0000449 if (host1->h_addrtype == AF_INET)
450 sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
Peter Hinzbb45a902011-03-10 18:14:01 +0000451#ifdef AF_LOCAL
Andy Green7627af52011-03-09 15:13:52 +0000452 else {
453 un = (struct sockaddr_un *)p;
Andy Green6ee372f2012-04-09 15:09:01 +0800454 strncpy(ip, un->sun_path, sizeof(ip) - 1);
Andy Green7627af52011-03-09 15:13:52 +0000455 ip[sizeof(ip) - 1] = '\0';
456 }
Peter Hinzbb45a902011-03-10 18:14:01 +0000457#endif
Andy Green07034092011-02-13 08:37:12 +0000458 p = NULL;
459 strncpy(rip, ip, rip_len);
460 rip[rip_len - 1] = '\0';
461 }
462}
Andy Green9f990342011-02-12 11:57:45 +0000463
Peter Hinz56885f32011-03-02 22:03:47 +0000464int libwebsockets_get_random(struct libwebsocket_context *context,
465 void *buf, int len)
466{
467 int n;
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800468 char *p = (char *)buf;
Peter Hinz56885f32011-03-02 22:03:47 +0000469
470#ifdef WIN32
471 for (n = 0; n < len; n++)
472 p[n] = (unsigned char)rand();
473#else
474 n = read(context->fd_random, p, len);
475#endif
476
477 return n;
478}
479
Andy Green2836c642011-03-07 20:47:41 +0000480unsigned char *
481libwebsockets_SHA1(const unsigned char *d, size_t n, unsigned char *md)
482{
483 return SHA1(d, n, md);
484}
485
Andy Green95a7b5d2011-03-06 10:29:39 +0000486int lws_send_pipe_choked(struct libwebsocket *wsi)
487{
488 struct pollfd fds;
489
490 fds.fd = wsi->sock;
491 fds.events = POLLOUT;
492 fds.revents = 0;
493
494 if (poll(&fds, 1, 0) != 1)
495 return 1;
496
497 if ((fds.revents & POLLOUT) == 0)
498 return 1;
499
500 /* okay to send another packet without blocking */
501
502 return 0;
503}
504
Andy Greena41314f2011-05-23 10:00:03 +0100505int
Andy Green3b84c002011-03-06 13:14:42 +0000506lws_handle_POLLOUT_event(struct libwebsocket_context *context,
507 struct libwebsocket *wsi, struct pollfd *pollfd)
508{
509 struct lws_tokens eff_buf;
510 int n;
511 int ret;
512 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100513 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000514
Andy Greena41314f2011-05-23 10:00:03 +0100515 for (n = 0; n < wsi->count_active_extensions; n++) {
516 if (!wsi->active_extensions[n]->callback)
517 continue;
518
519 m = wsi->active_extensions[n]->callback(context,
520 wsi->active_extensions[n], wsi,
521 LWS_EXT_CALLBACK_IS_WRITEABLE,
522 wsi->active_extensions_user[n], NULL, 0);
523 if (m > handled)
524 handled = m;
525 }
526
527 if (handled == 1)
528 goto notify_action;
529
530 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000531 goto user_service;
532
533 /*
534 * check in on the active extensions, see if they
535 * had pending stuff to spill... they need to get the
536 * first look-in otherwise sequence will be disordered
537 *
538 * NULL, zero-length eff_buf means just spill pending
539 */
540
541 ret = 1;
542 while (ret == 1) {
543
544 /* default to nobody has more to spill */
545
546 ret = 0;
547 eff_buf.token = NULL;
548 eff_buf.token_len = 0;
549
550 /* give every extension a chance to spill */
551
552 for (n = 0; n < wsi->count_active_extensions; n++) {
553 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000554 wsi->protocol->owning_server,
555 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000556 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
557 wsi->active_extensions_user[n], &eff_buf, 0);
558 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800559 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000560 return -1;
561 }
562 if (m)
563 /*
564 * at least one extension told us he has more
565 * to spill, so we will go around again after
566 */
567 ret = 1;
568 }
569
570 /* assuming they gave us something to send, send it */
571
572 if (eff_buf.token_len) {
573 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
574 eff_buf.token_len))
575 return -1;
576 } else
577 continue;
578
579 /* no extension has more to spill */
580
581 if (!ret)
582 continue;
583
584 /*
585 * There's more to spill from an extension, but we just sent
586 * something... did that leave the pipe choked?
587 */
588
589 if (!lws_send_pipe_choked(wsi))
590 /* no we could add more */
591 continue;
592
Andy Green43db0452013-01-10 19:50:35 +0800593 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000594
595 /*
596 * Yes, he's choked. Leave the POLLOUT masked on so we will
597 * come back here when he is unchoked. Don't call the user
598 * callback to enforce ordering of spilling, he'll get called
599 * when we come back here and there's nothing more to spill.
600 */
601
602 return 0;
603 }
604
605 wsi->extension_data_pending = 0;
606
607user_service:
608 /* one shot */
609
Andy Greena41314f2011-05-23 10:00:03 +0100610 if (pollfd) {
611 pollfd->events &= ~POLLOUT;
Andy Green3b84c002011-03-06 13:14:42 +0000612
Andy Greena41314f2011-05-23 10:00:03 +0100613 /* external POLL support via protocol 0 */
614 context->protocols[0].callback(context, wsi,
615 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
616 (void *)(long)wsi->sock, NULL, POLLOUT);
617 }
618
619notify_action:
Andy Green3b84c002011-03-06 13:14:42 +0000620
Andy Green9e4c2b62011-03-07 20:47:39 +0000621 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
622 n = LWS_CALLBACK_CLIENT_WRITEABLE;
623 else
624 n = LWS_CALLBACK_SERVER_WRITEABLE;
625
Andy Green706961d2013-01-17 16:50:35 +0800626 user_callback_handle_rxflow(wsi->protocol->callback, context,
627 wsi, (enum libwebsocket_callback_reasons) n, wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000628
629 return 0;
630}
631
632
633
Andy Greena41314f2011-05-23 10:00:03 +0100634void
635libwebsocket_service_timeout_check(struct libwebsocket_context *context,
636 struct libwebsocket *wsi, unsigned int sec)
637{
638 int n;
639
640 /*
641 * if extensions want in on it (eg, we are a mux parent)
642 * give them a chance to service child timeouts
643 */
644
645 for (n = 0; n < wsi->count_active_extensions; n++)
646 wsi->active_extensions[n]->callback(
647 context, wsi->active_extensions[n],
648 wsi, LWS_EXT_CALLBACK_1HZ,
649 wsi->active_extensions_user[n], NULL, sec);
650
651 if (!wsi->pending_timeout)
652 return;
Andy Green6ee372f2012-04-09 15:09:01 +0800653
Andy Greena41314f2011-05-23 10:00:03 +0100654 /*
655 * if we went beyond the allowed time, kill the
656 * connection
657 */
658
659 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800660 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100661 libwebsocket_close_and_free_session(context,
662 wsi, LWS_CLOSE_STATUS_NOSTATUS);
663 }
664}
665
Andy Green9f990342011-02-12 11:57:45 +0000666/**
667 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000668 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000669 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800670 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000671 *
672 * This function closes any active connections and then frees the
673 * context. After calling this, any further use of the context is
674 * undefined.
675 */
676
677int
Peter Hinz56885f32011-03-02 22:03:47 +0000678libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000679 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000680{
Andy Greena1ce6be2013-01-18 11:43:21 +0800681 struct libwebsocket *wsi;
Andy Green6ee372f2012-04-09 15:09:01 +0800682 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 +
683 MAX_BROADCAST_PAYLOAD + LWS_SEND_BUFFER_POST_PADDING];
Andy Greenb45993c2010-12-18 15:13:50 +0000684 int n;
Andy Green0d338332011-02-12 11:57:43 +0000685 int m;
Andy Greena71eafc2011-02-14 17:59:43 +0000686 struct timeval tv;
Andy Green2366b1c2011-03-06 13:15:31 +0000687 int more = 1;
Andy Green98a717c2011-03-06 13:14:15 +0000688 struct lws_tokens eff_buf;
Andy Green03674a62013-01-16 11:47:40 +0800689#ifndef LWS_NO_CLIENT
Andy Green76f61e72013-01-16 11:53:05 +0800690 extern int lws_client_socket_service(struct libwebsocket_context *context, struct libwebsocket *wsi, struct pollfd *pollfd);
Andy Green03674a62013-01-16 11:47:40 +0800691#endif
Andy Greena1ce6be2013-01-18 11:43:21 +0800692#ifndef LWS_NO_SERVER
693 extern int lws_server_socket_service(struct libwebsocket_context *context, struct libwebsocket *wsi, struct pollfd *pollfd);
694#endif
Andy Greena71eafc2011-02-14 17:59:43 +0000695 /*
696 * you can call us with pollfd = NULL to just allow the once-per-second
697 * global timeout checks; if less than a second since the last check
698 * it returns immediately then.
699 */
700
701 gettimeofday(&tv, NULL);
702
Peter Hinz56885f32011-03-02 22:03:47 +0000703 if (context->last_timeout_check_s != tv.tv_sec) {
704 context->last_timeout_check_s = tv.tv_sec;
Andy Greena71eafc2011-02-14 17:59:43 +0000705
Andy Green24cba922013-01-19 13:56:10 +0800706 /* if our parent went down, don't linger around */
707 if (context->started_with_parent && kill(context->started_with_parent, 0) < 0)
708 kill(getpid(), SIGTERM);
709
Andy Greena71eafc2011-02-14 17:59:43 +0000710 /* global timeout check once per second */
711
Peter Hinz56885f32011-03-02 22:03:47 +0000712 for (n = 0; n < context->fds_count; n++) {
Andy Greendfb23042013-01-17 12:26:48 +0800713 struct libwebsocket *new_wsi = context->lws_lookup[context->fds[n].fd];
714 if (!new_wsi)
715 continue;
716 libwebsocket_service_timeout_check(context,
717 new_wsi, tv.tv_sec);
Andy Greena71eafc2011-02-14 17:59:43 +0000718 }
719 }
720
721 /* just here for timeout management? */
722
723 if (pollfd == NULL)
724 return 0;
725
726 /* no, here to service a socket descriptor */
727
Andy Green65b0e912013-01-16 07:59:47 +0800728 /*
729 * deal with listen service piggybacking
730 * every listen_service_modulo services of other fds, we
731 * sneak one in to service the listen socket if there's anything waiting
732 *
733 * To handle connection storms, as found in ab, if we previously saw a
734 * pending connection here, it causes us to check again next time.
735 */
736
737 if (context->listen_service_fd && pollfd->fd != context->listen_service_fd) {
738 context->listen_service_count++;
739 if (context->listen_service_extraseen ||
740 context->listen_service_count == context->listen_service_modulo) {
741 context->listen_service_count = 0;
742 m = 1;
743 if (context->listen_service_extraseen > 5)
744 m = 2;
745 while (m--) {
746 /* even with extpoll, we prepared this internal fds for listen */
747 n = poll(&context->fds[0], 1, 0);
748 if (n > 0) { /* there's a connection waiting for us */
749 libwebsocket_service_fd(context, &context->fds[0]);
750 context->listen_service_extraseen++;
751 } else {
752 if (context->listen_service_extraseen)
753 context->listen_service_extraseen--;
754 break;
755 }
756 }
757 }
758
759 }
760
761 /* okay, what we came here to do... */
762
Andy Greendfb23042013-01-17 12:26:48 +0800763 wsi = context->lws_lookup[pollfd->fd];
Andy Greend280b6e2013-01-15 13:40:23 +0800764 if (wsi == NULL) {
Andy Greendfb23042013-01-17 12:26:48 +0800765 if (pollfd->fd > 11)
766 lwsl_err("unexpected NULL wsi fd=%d fds_count=%d\n", pollfd->fd, context->fds_count);
Andy Greenfa3f4052012-10-07 20:40:35 +0800767 return 0;
Andy Greend280b6e2013-01-15 13:40:23 +0800768 }
Andy Green8f037e42010-12-19 22:13:26 +0000769
Andy Green0d338332011-02-12 11:57:43 +0000770 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +0800771
Andy Greena1ce6be2013-01-18 11:43:21 +0800772#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +0800773 case LWS_CONNMODE_HTTP_SERVING:
Andy Green0d338332011-02-12 11:57:43 +0000774 case LWS_CONNMODE_SERVER_LISTENER:
Andy Green0d338332011-02-12 11:57:43 +0000775 case LWS_CONNMODE_BROADCAST_PROXY_LISTENER:
Andy Green0d338332011-02-12 11:57:43 +0000776 case LWS_CONNMODE_BROADCAST_PROXY:
Andy Greena1ce6be2013-01-18 11:43:21 +0800777 return lws_server_socket_service(context, wsi, pollfd);
778#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000779
Andy Green0d338332011-02-12 11:57:43 +0000780 case LWS_CONNMODE_WS_SERVING:
781 case LWS_CONNMODE_WS_CLIENT:
782
783 /* handle session socket closed */
784
785 if (pollfd->revents & (POLLERR | POLLHUP)) {
786
Andy Green43db0452013-01-10 19:50:35 +0800787 lwsl_debug("Session Socket %p (fd=%d) dead\n",
Andy Green0d338332011-02-12 11:57:43 +0000788 (void *)wsi, pollfd->fd);
789
Peter Hinz56885f32011-03-02 22:03:47 +0000790 libwebsocket_close_and_free_session(context, wsi,
Andy Green687b0182011-02-26 11:04:01 +0000791 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800792 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000793 }
794
Andy Green0d338332011-02-12 11:57:43 +0000795 /* the guy requested a callback when it was OK to write */
796
Andy Greenda527df2011-03-07 07:08:12 +0000797 if ((pollfd->revents & POLLOUT) &&
798 wsi->state == WSI_STATE_ESTABLISHED)
799 if (lws_handle_POLLOUT_event(context, wsi,
800 pollfd) < 0) {
801 libwebsocket_close_and_free_session(
802 context, wsi, LWS_CLOSE_STATUS_NORMAL);
Andy Green040d2ef2013-01-16 13:40:43 +0800803 return 0;
Andy Green3b84c002011-03-06 13:14:42 +0000804 }
Andy Green0d338332011-02-12 11:57:43 +0000805
Andy Green0d338332011-02-12 11:57:43 +0000806
807 /* any incoming data ready? */
808
809 if (!(pollfd->revents & POLLIN))
810 break;
811
Andy Greenb45993c2010-12-18 15:13:50 +0000812#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +0800813read_pending:
Andy Green0d338332011-02-12 11:57:43 +0000814 if (wsi->ssl)
Andy Green98a717c2011-03-06 13:14:15 +0000815 eff_buf.token_len = SSL_read(wsi->ssl, buf, sizeof buf);
Andy Greenb45993c2010-12-18 15:13:50 +0000816 else
817#endif
Andy Green98a717c2011-03-06 13:14:15 +0000818 eff_buf.token_len =
Andy Green72c34322011-04-16 10:46:21 +0100819 recv(pollfd->fd, buf, sizeof buf, 0);
Andy Greenb45993c2010-12-18 15:13:50 +0000820
Andy Green98a717c2011-03-06 13:14:15 +0000821 if (eff_buf.token_len < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800822 lwsl_debug("Socket read returned %d\n",
Andy Green98a717c2011-03-06 13:14:15 +0000823 eff_buf.token_len);
Alon Levydc93b7f2012-10-19 11:21:57 +0200824 if (errno != EINTR && errno != EAGAIN)
Andy Green6ee372f2012-04-09 15:09:01 +0800825 libwebsocket_close_and_free_session(context,
826 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800827 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000828 }
Andy Green98a717c2011-03-06 13:14:15 +0000829 if (!eff_buf.token_len) {
Peter Hinz56885f32011-03-02 22:03:47 +0000830 libwebsocket_close_and_free_session(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +0800831 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenfa3f4052012-10-07 20:40:35 +0800832 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000833 }
834
Andy Green98a717c2011-03-06 13:14:15 +0000835 /*
836 * give any active extensions a chance to munge the buffer
837 * before parse. We pass in a pointer to an lws_tokens struct
838 * prepared with the default buffer and content length that's in
839 * there. Rather than rewrite the default buffer, extensions
840 * that expect to grow the buffer can adapt .token to
841 * point to their own per-connection buffer in the extension
842 * user allocation. By default with no extensions or no
843 * extension callback handling, just the normal input buffer is
844 * used then so it is efficient.
845 */
Andy Greenb45993c2010-12-18 15:13:50 +0000846
Andy Green98a717c2011-03-06 13:14:15 +0000847 eff_buf.token = (char *)buf;
Andy Greenb45993c2010-12-18 15:13:50 +0000848
Andy Green98a717c2011-03-06 13:14:15 +0000849 more = 1;
850 while (more) {
Andy Green0d338332011-02-12 11:57:43 +0000851
Andy Green98a717c2011-03-06 13:14:15 +0000852 more = 0;
853
854 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +0000855 m = wsi->active_extensions[n]->callback(context,
856 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +0000857 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +0000858 wsi->active_extensions_user[n],
859 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +0000860 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800861 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +0800862 "Extension reports fatal error\n");
863 libwebsocket_close_and_free_session(
864 context, wsi,
865 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800866 return 0;
Andy Green98a717c2011-03-06 13:14:15 +0000867 }
868 if (m)
869 more = 1;
870 }
871
872 /* service incoming data */
873
874 if (eff_buf.token_len) {
875 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +0800876 (unsigned char *)eff_buf.token,
877 eff_buf.token_len);
Andy Green98a717c2011-03-06 13:14:15 +0000878 if (n < 0)
879 /* we closed wsi */
Andy Green040d2ef2013-01-16 13:40:43 +0800880 return 0;
Andy Green98a717c2011-03-06 13:14:15 +0000881 }
882
883 eff_buf.token = NULL;
884 eff_buf.token_len = 0;
885 }
David Galeano7ffbe1b2013-01-10 10:35:32 +0800886
887#ifdef LWS_OPENSSL_SUPPORT
888 if (wsi->ssl && SSL_pending(wsi->ssl))
889 goto read_pending;
890#endif
Andy Green98a717c2011-03-06 13:14:15 +0000891 break;
Andy Green76f61e72013-01-16 11:53:05 +0800892
893 default:
Andy Green03674a62013-01-16 11:47:40 +0800894#ifdef LWS_NO_CLIENT
895 break;
896#else
Andy Green76f61e72013-01-16 11:53:05 +0800897 return lws_client_socket_service(context, wsi, pollfd);
Andy Green03674a62013-01-16 11:47:40 +0800898#endif
Andy Greenb45993c2010-12-18 15:13:50 +0000899 }
900
901 return 0;
902}
903
Andy Green0d338332011-02-12 11:57:43 +0000904
Andy Green6964bb52011-01-23 16:50:33 +0000905/**
906 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +0000907 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +0000908 *
909 * This function closes any active connections and then frees the
910 * context. After calling this, any further use of the context is
911 * undefined.
912 */
913void
Peter Hinz56885f32011-03-02 22:03:47 +0000914libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +0000915{
Andy Green0d338332011-02-12 11:57:43 +0000916 int n;
917 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100918 struct libwebsocket_extension *ext;
Andy Green6964bb52011-01-23 16:50:33 +0000919
Andy Greendfb23042013-01-17 12:26:48 +0800920 for (n = 0; n < context->fds_count; n++) {
921 struct libwebsocket *wsi = context->lws_lookup[context->fds[n].fd];
922 libwebsocket_close_and_free_session(context,
923 wsi, LWS_CLOSE_STATUS_GOINGAWAY);
924 }
Andy Green6964bb52011-01-23 16:50:33 +0000925
Andy Greena41314f2011-05-23 10:00:03 +0100926 /*
927 * give all extensions a chance to clean up any per-context
928 * allocations they might have made
929 */
930
931 ext = context->extensions;
932 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
933 if (context->listen_port)
934 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +0800935 while (ext && ext->callback) {
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800936 ext->callback(context, ext, NULL, (enum libwebsocket_extension_callback_reasons)m, NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +0100937 ext++;
938 }
939
Peter Hinz56885f32011-03-02 22:03:47 +0000940#ifdef WIN32
941#else
942 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +0000943#endif
944
Peter Hinz56885f32011-03-02 22:03:47 +0000945#ifdef LWS_OPENSSL_SUPPORT
946 if (context->ssl_ctx)
947 SSL_CTX_free(context->ssl_ctx);
948 if (context->ssl_client_ctx)
949 SSL_CTX_free(context->ssl_client_ctx);
950#endif
951
952 free(context);
953
954#ifdef WIN32
955 WSACleanup();
956#endif
Andy Green6964bb52011-01-23 16:50:33 +0000957}
958
Alon Levy0291eb32012-10-19 11:21:56 +0200959LWS_EXTERN void *
960libwebsocket_context_user(struct libwebsocket_context *context)
961{
962 return context->user_space;
963}
964
Andy Green6964bb52011-01-23 16:50:33 +0000965/**
966 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +0000967 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +0000968 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
969 * service otherwise block and service immediately, returning
970 * after the timeout if nothing needed service.
971 *
972 * This function deals with any pending websocket traffic, for three
973 * kinds of event. It handles these events on both server and client
974 * types of connection the same.
975 *
976 * 1) Accept new connections to our context's server
977 *
978 * 2) Perform pending broadcast writes initiated from other forked
979 * processes (effectively serializing asynchronous broadcasts)
980 *
981 * 3) Call the receive callback for incoming frame data received by
982 * server or client connections.
983 *
984 * You need to call this service function periodically to all the above
985 * functions to happen; if your application is single-threaded you can
986 * just call it in your main event loop.
987 *
988 * Alternatively you can fork a new process that asynchronously handles
989 * calling this service in a loop. In that case you are happy if this
990 * call blocks your thread until it needs to take care of something and
991 * would call it with a large nonzero timeout. Your loop then takes no
992 * CPU while there is nothing happening.
993 *
994 * If you are calling it in a single-threaded app, you don't want it to
995 * wait around blocking other things in your loop from happening, so you
996 * would call it with a timeout_ms of 0, so it returns immediately if
997 * nothing is pending, or as soon as it services whatever was pending.
998 */
999
Andy Greenb45993c2010-12-18 15:13:50 +00001000
Andy Greene92cd172011-01-19 13:11:55 +00001001int
Peter Hinz56885f32011-03-02 22:03:47 +00001002libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001003{
1004 int n;
Andy Greene92cd172011-01-19 13:11:55 +00001005
1006 /* stay dead once we are dead */
1007
Peter Hinz56885f32011-03-02 22:03:47 +00001008 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001009 return 1;
1010
Andy Green0d338332011-02-12 11:57:43 +00001011 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001012
Peter Hinz56885f32011-03-02 22:03:47 +00001013 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green3221f922011-02-12 13:14:11 +00001014 if (n == 0) /* poll timeout */
1015 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001016
Andy Greendfb23042013-01-17 12:26:48 +08001017 if (n < 0)
Andy Green3928f612012-07-20 12:58:38 +08001018 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001019
Andy Greendfb23042013-01-17 12:26:48 +08001020 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001021
Peter Hinz56885f32011-03-02 22:03:47 +00001022 for (n = 0; n < context->fds_count; n++)
1023 if (context->fds[n].revents)
Andy Green3928f612012-07-20 12:58:38 +08001024 if (libwebsocket_service_fd(context,
1025 &context->fds[n]) < 0)
1026 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001027 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001028}
1029
Andy Greena41314f2011-05-23 10:00:03 +01001030int
1031lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001032 struct libwebsocket *wsi,
1033 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001034 void *v, size_t len)
1035{
1036 int n;
1037 int handled = 0;
1038
1039 /* maybe an extension will take care of it for us */
1040
1041 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1042 if (!wsi->active_extensions[n]->callback)
1043 continue;
1044
1045 handled |= wsi->active_extensions[n]->callback(context,
1046 wsi->active_extensions[n], wsi,
1047 r, wsi->active_extensions_user[n], v, len);
1048 }
1049
1050 return handled;
1051}
1052
1053
1054void *
1055lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001056 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001057{
1058 int n = 0;
1059
Andy Green68b45042011-05-25 21:41:57 +01001060 if (wsi == NULL)
1061 return NULL;
1062
Andy Greena41314f2011-05-23 10:00:03 +01001063 while (n < wsi->count_active_extensions) {
1064 if (wsi->active_extensions[n] != ext) {
1065 n++;
1066 continue;
1067 }
1068 return wsi->active_extensions_user[n];
1069 }
1070
1071 return NULL;
1072}
1073
Andy Green90c7cbc2011-01-27 06:26:52 +00001074/**
1075 * libwebsocket_callback_on_writable() - Request a callback when this socket
1076 * becomes able to be written to without
1077 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001078 *
Peter Hinz56885f32011-03-02 22:03:47 +00001079 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001080 * @wsi: Websocket connection instance to get callback for
1081 */
1082
1083int
Peter Hinz56885f32011-03-02 22:03:47 +00001084libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001085 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001086{
Andy Green90c7cbc2011-01-27 06:26:52 +00001087 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001088 int handled = 0;
1089
1090 /* maybe an extension will take care of it for us */
1091
1092 for (n = 0; n < wsi->count_active_extensions; n++) {
1093 if (!wsi->active_extensions[n]->callback)
1094 continue;
1095
1096 handled |= wsi->active_extensions[n]->callback(context,
1097 wsi->active_extensions[n], wsi,
1098 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1099 wsi->active_extensions_user[n], NULL, 0);
1100 }
1101
1102 if (handled)
1103 return 1;
Andy Green90c7cbc2011-01-27 06:26:52 +00001104
Andy Greendfb23042013-01-17 12:26:48 +08001105 if (wsi->position_in_fds_table < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001106 lwsl_err("libwebsocket_callback_on_writable: "
Andy Green6ee372f2012-04-09 15:09:01 +08001107 "failed to find socket %d\n", wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001108 return -1;
1109 }
1110
1111 context->fds[wsi->position_in_fds_table].events |= POLLOUT;
Andy Greena41314f2011-05-23 10:00:03 +01001112
Andy Green3221f922011-02-12 13:14:11 +00001113 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00001114 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001115 LWS_CALLBACK_SET_MODE_POLL_FD,
1116 (void *)(long)wsi->sock, NULL, POLLOUT);
1117
Andy Green90c7cbc2011-01-27 06:26:52 +00001118 return 1;
1119}
1120
1121/**
1122 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1123 * all connections using the given protocol when it
1124 * becomes possible to write to each socket without
1125 * blocking in turn.
1126 *
1127 * @protocol: Protocol whose connections will get callbacks
1128 */
1129
1130int
1131libwebsocket_callback_on_writable_all_protocol(
1132 const struct libwebsocket_protocols *protocol)
1133{
Peter Hinz56885f32011-03-02 22:03:47 +00001134 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001135 int n;
Andy Green0d338332011-02-12 11:57:43 +00001136 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001137
Andy Greendfb23042013-01-17 12:26:48 +08001138 for (n = 0; n < context->fds_count; n++) {
1139 wsi = context->lws_lookup[context->fds[n].fd];
1140 if (!wsi)
1141 continue;
1142 if (wsi->protocol == protocol)
1143 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001144 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001145
1146 return 0;
1147}
1148
Andy Greenbe93fef2011-02-14 20:25:43 +00001149/**
1150 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1151 *
1152 * You will not need this unless you are doing something special
1153 *
1154 * @wsi: Websocket connection instance
1155 * @reason: timeout reason
1156 * @secs: how many seconds
1157 */
1158
1159void
1160libwebsocket_set_timeout(struct libwebsocket *wsi,
1161 enum pending_timeout reason, int secs)
1162{
1163 struct timeval tv;
1164
1165 gettimeofday(&tv, NULL);
1166
1167 wsi->pending_timeout_limit = tv.tv_sec + secs;
1168 wsi->pending_timeout = reason;
1169}
1170
Andy Greena6cbece2011-01-27 20:06:03 +00001171
1172/**
1173 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1174 *
1175 * You will not need this unless you are doing something special
1176 *
1177 * @wsi: Websocket connection instance
1178 */
1179
1180int
1181libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1182{
1183 return wsi->sock;
1184}
1185
Andy Greena1ce6be2013-01-18 11:43:21 +08001186#ifdef LWS_NO_SERVER
1187int
1188_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1189{
1190 return 0;
1191}
1192#else
Andy Green706961d2013-01-17 16:50:35 +08001193int
1194_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1195{
1196 struct libwebsocket_context *context = wsi->protocol->owning_server;
1197 int n;
1198
1199 if (!(wsi->rxflow_change_to & 2))
1200 return 0;
1201
1202 wsi->rxflow_change_to &= ~2;
1203
1204 lwsl_info("rxflow: wsi %p change_to %d\n", wsi, wsi->rxflow_change_to);
1205
1206 /* if we're letting it come again, did we interrupt anything? */
1207 if ((wsi->rxflow_change_to & 1) && wsi->rxflow_buffer) {
1208 n = libwebsocket_interpret_incoming_packet(wsi, NULL, 0);
1209 if (n < 0) {
1210 libwebsocket_close_and_free_session(context, wsi, LWS_CLOSE_STATUS_NOSTATUS);
1211 return -1;
1212 }
1213 if (n)
1214 /* oh he stuck again, do nothing */
1215 return 0;
1216 }
1217
1218 if (wsi->rxflow_change_to & 1)
1219 context->fds[wsi->position_in_fds_table].events |= POLLIN;
1220 else
1221 context->fds[wsi->position_in_fds_table].events &= ~POLLIN;
1222
1223 if (wsi->rxflow_change_to & 1)
1224 /* external POLL support via protocol 0 */
1225 context->protocols[0].callback(context, wsi,
1226 LWS_CALLBACK_SET_MODE_POLL_FD,
1227 (void *)(long)wsi->sock, NULL, POLLIN);
1228 else
1229 /* external POLL support via protocol 0 */
1230 context->protocols[0].callback(context, wsi,
1231 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
1232 (void *)(long)wsi->sock, NULL, POLLIN);
1233
1234 return 1;
1235}
Andy Greena1ce6be2013-01-18 11:43:21 +08001236#endif
Andy Green706961d2013-01-17 16:50:35 +08001237
Andy Green90c7cbc2011-01-27 06:26:52 +00001238/**
1239 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1240 * receieved packets.
1241 *
1242 * If the output side of a server process becomes choked, this allows flow
1243 * control for the input side.
1244 *
1245 * @wsi: Websocket connection instance to get callback for
1246 * @enable: 0 = disable read servicing for this connection, 1 = enable
1247 */
1248
1249int
1250libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1251{
Andy Green706961d2013-01-17 16:50:35 +08001252 wsi->rxflow_change_to = 2 | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001253
Andy Green706961d2013-01-17 16:50:35 +08001254 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001255}
1256
Andy Green706961d2013-01-17 16:50:35 +08001257
Andy Green2ac5a6f2011-01-28 10:00:18 +00001258/**
1259 * libwebsocket_canonical_hostname() - returns this host's hostname
1260 *
1261 * This is typically used by client code to fill in the host parameter
1262 * when making a client connection. You can only call it after the context
1263 * has been created.
1264 *
Peter Hinz56885f32011-03-02 22:03:47 +00001265 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001266 */
1267
1268
1269extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001270libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001271{
Peter Hinz56885f32011-03-02 22:03:47 +00001272 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001273}
1274
1275
Andy Green90c7cbc2011-01-27 06:26:52 +00001276static void sigpipe_handler(int x)
1277{
1278}
1279
Andy Green6901cb32011-02-21 08:06:47 +00001280#ifdef LWS_OPENSSL_SUPPORT
1281static int
1282OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1283{
1284
1285 SSL *ssl;
1286 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001287 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001288
1289 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1290 SSL_get_ex_data_X509_STORE_CTX_idx());
1291
1292 /*
Andy Green2e24da02011-03-05 16:12:04 +00001293 * !!! nasty openssl requires the index to come as a library-scope
1294 * static
Andy Green6901cb32011-02-21 08:06:47 +00001295 */
Andy Green2e24da02011-03-05 16:12:04 +00001296 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001297
Peter Hinz56885f32011-03-02 22:03:47 +00001298 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001299 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1300 x509_ctx, ssl, preverify_ok);
1301
1302 /* convert return code from 0 = OK to 1 = OK */
1303
1304 if (!n)
1305 n = 1;
1306 else
1307 n = 0;
1308
1309 return n;
1310}
1311#endif
1312
Andy Green706961d2013-01-17 16:50:35 +08001313int user_callback_handle_rxflow(callback_function callback_function,
1314 struct libwebsocket_context * context,
1315 struct libwebsocket *wsi,
1316 enum libwebsocket_callback_reasons reason, void *user,
1317 void *in, size_t len)
1318{
1319 int n;
1320
1321 n = callback_function(context, wsi, reason, user, in, len);
1322 if (n < 0)
1323 return n;
1324
1325 _libwebsocket_rx_flow_control(wsi);
1326
1327 return 0;
1328}
1329
Andy Greenb45993c2010-12-18 15:13:50 +00001330
Andy Greenab990e42010-10-31 12:42:52 +00001331/**
Andy Green4739e5c2011-01-22 12:51:57 +00001332 * libwebsocket_create_context() - Create the websocket handler
1333 * @port: Port to listen on... you can use 0 to suppress listening on
Andy Green6964bb52011-01-23 16:50:33 +00001334 * any port, that's what you want if you are not running a
1335 * websocket server at all but just using it as a client
Peter Hinz56885f32011-03-02 22:03:47 +00001336 * @interf: NULL to bind the listen socket to all interfaces, or the
Andy Green32375b72011-02-19 08:32:53 +00001337 * interface name, eg, "eth2"
Andy Green4f3943a2010-11-12 10:44:16 +00001338 * @protocols: Array of structures listing supported protocols and a protocol-
Andy Green8f037e42010-12-19 22:13:26 +00001339 * specific callback for each one. The list is ended with an
1340 * entry that has a NULL callback pointer.
Andy Green6964bb52011-01-23 16:50:33 +00001341 * It's not const because we write the owning_server member
Andy Greenc5114822011-03-06 10:29:35 +00001342 * @extensions: NULL or array of libwebsocket_extension structs listing the
Andy Green6ee372f2012-04-09 15:09:01 +08001343 * extensions this context supports
Andy Green3faa9c72010-11-08 17:03:03 +00001344 * @ssl_cert_filepath: If libwebsockets was compiled to use ssl, and you want
Andy Green8f037e42010-12-19 22:13:26 +00001345 * to listen using SSL, set to the filepath to fetch the
1346 * server cert from, otherwise NULL for unencrypted
Andy Green3faa9c72010-11-08 17:03:03 +00001347 * @ssl_private_key_filepath: filepath to private key if wanting SSL mode,
Andy Green8f037e42010-12-19 22:13:26 +00001348 * else ignored
David Galeano2f82be82013-01-09 16:25:54 +08001349 * @ssl_ca_filepath: CA certificate filepath or NULL
Andy Green3faa9c72010-11-08 17:03:03 +00001350 * @gid: group id to change to after setting listen socket, or -1.
1351 * @uid: user id to change to after setting listen socket, or -1.
Andy Greenbfb051f2011-02-09 08:49:14 +00001352 * @options: 0, or LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK
Andy Green15e31f32012-10-19 18:36:28 +08001353 * @user: optional user pointer that can be recovered via the context
1354 * pointer using libwebsocket_context_user
Andy Green05464c62010-11-12 10:44:18 +00001355 *
Andy Green8f037e42010-12-19 22:13:26 +00001356 * This function creates the listening socket and takes care
1357 * of all initialization in one step.
1358 *
Andy Greene92cd172011-01-19 13:11:55 +00001359 * After initialization, it returns a struct libwebsocket_context * that
1360 * represents this server. After calling, user code needs to take care
1361 * of calling libwebsocket_service() with the context pointer to get the
1362 * server's sockets serviced. This can be done in the same process context
1363 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001364 *
Andy Green8f037e42010-12-19 22:13:26 +00001365 * The protocol callback functions are called for a handful of events
1366 * including http requests coming in, websocket connections becoming
1367 * established, and data arriving; it's also called periodically to allow
1368 * async transmission.
1369 *
1370 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1371 * at that time websocket protocol has not been negotiated. Other
1372 * protocols after the first one never see any HTTP callack activity.
1373 *
1374 * The server created is a simple http server by default; part of the
1375 * websocket standard is upgrading this http connection to a websocket one.
1376 *
1377 * This allows the same server to provide files like scripts and favicon /
1378 * images or whatever over http and dynamic data over websockets all in
1379 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001380 */
Andy Green4ea60062010-10-30 12:15:07 +01001381
Andy Greene92cd172011-01-19 13:11:55 +00001382struct libwebsocket_context *
Peter Hinz56885f32011-03-02 22:03:47 +00001383libwebsocket_create_context(int port, const char *interf,
Andy Greenb45993c2010-12-18 15:13:50 +00001384 struct libwebsocket_protocols *protocols,
Andy Greend6e09112011-03-05 16:12:15 +00001385 struct libwebsocket_extension *extensions,
Andy Green8f037e42010-12-19 22:13:26 +00001386 const char *ssl_cert_filepath,
1387 const char *ssl_private_key_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08001388 const char *ssl_ca_filepath,
Alon Levy0291eb32012-10-19 11:21:56 +02001389 int gid, int uid, unsigned int options,
David Galeano2f82be82013-01-09 16:25:54 +08001390 void *user)
Andy Greenff95d7a2010-10-28 22:36:01 +01001391{
1392 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001393 int m;
Andy Green251f6fa2010-11-03 11:13:06 +00001394 int fd;
Andy Greenff95d7a2010-10-28 22:36:01 +01001395 struct sockaddr_in serv_addr, cli_addr;
Andy Green251f6fa2010-11-03 11:13:06 +00001396 int opt = 1;
Peter Hinz56885f32011-03-02 22:03:47 +00001397 struct libwebsocket_context *context = NULL;
Andy Greenb45993c2010-12-18 15:13:50 +00001398 unsigned int slen;
Andy Green9659f372011-01-27 22:01:43 +00001399 char *p;
Andy Green0d338332011-02-12 11:57:43 +00001400 struct libwebsocket *wsi;
Andy Greenff95d7a2010-10-28 22:36:01 +01001401
Andy Green3faa9c72010-11-08 17:03:03 +00001402#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001403 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001404 char ssl_err_buf[512];
Andy Green3faa9c72010-11-08 17:03:03 +00001405#endif
1406
Andy Greenb3a614a2013-01-19 13:08:17 +08001407 lwsl_notice("Initial logging level %d\n", log_level);
Andy Greenc0d6b632013-01-12 23:42:17 +08001408 lwsl_info(" LWS_MAX_HEADER_NAME_LENGTH: %u\n", LWS_MAX_HEADER_NAME_LENGTH);
1409 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
1410 lwsl_info(" LWS_INITIAL_HDR_ALLOC: %u\n", LWS_INITIAL_HDR_ALLOC);
1411 lwsl_info(" LWS_ADDITIONAL_HDR_ALLOC: %u\n", LWS_ADDITIONAL_HDR_ALLOC);
1412 lwsl_info(" MAX_USER_RX_BUFFER: %u\n", MAX_USER_RX_BUFFER);
1413 lwsl_info(" MAX_BROADCAST_PAYLOAD: %u\n", MAX_BROADCAST_PAYLOAD);
1414 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
1415 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n", LWS_MAX_EXTENSIONS_ACTIVE);
1416 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1417 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
1418 lwsl_info(" CIPHERS_LIST_STRING: '%s'\n", CIPHERS_LIST_STRING);
1419 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1420 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001421
Peter Hinz56885f32011-03-02 22:03:47 +00001422#ifdef _WIN32
1423 {
1424 WORD wVersionRequested;
1425 WSADATA wsaData;
1426 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001427 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001428
1429 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1430 wVersionRequested = MAKEWORD(2, 2);
1431
1432 err = WSAStartup(wVersionRequested, &wsaData);
1433 if (err != 0) {
1434 /* Tell the user that we could not find a usable */
1435 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001436 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001437 return NULL;
1438 }
David Galeano7b11fec2011-10-04 19:55:18 +08001439
Andy Green6ee372f2012-04-09 15:09:01 +08001440 /* default to a poll() made out of select() */
1441 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001442
Andy Green6ee372f2012-04-09 15:09:01 +08001443 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001444 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001445 if (wsdll)
1446 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Peter Hinz56885f32011-03-02 22:03:47 +00001447 }
1448#endif
1449
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001450 context = (struct libwebsocket_context *) malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001451 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001452 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001453 return NULL;
1454 }
Andy Green24cba922013-01-19 13:56:10 +08001455#ifndef NO_DAEMONIZE
1456 extern int pid_daemon;
1457 context->started_with_parent = pid_daemon;
1458 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1459#endif
1460
Peter Hinz56885f32011-03-02 22:03:47 +00001461 context->protocols = protocols;
1462 context->listen_port = port;
1463 context->http_proxy_port = 0;
1464 context->http_proxy_address[0] = '\0';
1465 context->options = options;
Andy Greendfb23042013-01-17 12:26:48 +08001466 /* to reduce this allocation, */
1467 context->max_fds = getdtablesize();
Andy Greenb3a614a2013-01-19 13:08:17 +08001468 lwsl_notice(" max fd tracked: %u\n", context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001469
1470 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) * context->max_fds);
1471 if (context->fds == NULL) {
1472 lwsl_err("Unable to allocate fds array for %d connections\n", context->max_fds);
1473 free(context);
1474 return NULL;
1475 }
1476 context->lws_lookup = (struct libwebsocket **)malloc(sizeof(struct libwebsocke *) * context->max_fds);
1477 if (context->lws_lookup == NULL) {
1478 lwsl_err("Unable to allocate lws_lookup array for %d connections\n", context->max_fds);
1479 free(context->fds);
1480 free(context);
1481 return NULL;
1482 }
Peter Hinz56885f32011-03-02 22:03:47 +00001483 context->fds_count = 0;
Andy Greend6e09112011-03-05 16:12:15 +00001484 context->extensions = extensions;
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08001485 context->last_timeout_check_s = 0;
Andy Greendfb23042013-01-17 12:26:48 +08001486 context->user_space = user;
Andy Green9659f372011-01-27 22:01:43 +00001487
Peter Hinz56885f32011-03-02 22:03:47 +00001488#ifdef WIN32
1489 context->fd_random = 0;
1490#else
1491 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
1492 if (context->fd_random < 0) {
Andy Greendfb23042013-01-17 12:26:48 +08001493 free(context);
Andy Green43db0452013-01-10 19:50:35 +08001494 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001495 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Andy Green44eee682011-02-10 09:32:24 +00001496 return NULL;
1497 }
Peter Hinz56885f32011-03-02 22:03:47 +00001498#endif
Andy Green44eee682011-02-10 09:32:24 +00001499
Peter Hinz56885f32011-03-02 22:03:47 +00001500#ifdef LWS_OPENSSL_SUPPORT
1501 context->use_ssl = 0;
1502 context->ssl_ctx = NULL;
1503 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00001504 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001505#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00001506
Andy Greena1ce6be2013-01-18 11:43:21 +08001507 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08001508
Andy Greena1ce6be2013-01-18 11:43:21 +08001509#ifndef LWS_NO_SERVER
1510 if (!(options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
1511 struct sockaddr sa;
1512 char hostname[1024] = "";
Andy Green788c4a82012-10-22 12:29:57 +01001513
1514 /* find canonical hostname */
1515
1516 hostname[(sizeof hostname) - 1] = '\0';
1517 memset(&sa, 0, sizeof(sa));
1518 sa.sa_family = AF_INET;
1519 sa.sa_data[(sizeof sa.sa_data) - 1] = '\0';
1520 gethostname(hostname, (sizeof hostname) - 1);
1521
1522 n = 0;
1523
David Galeanoed3c8402013-01-10 10:45:24 +08001524 if (strlen(hostname) < sizeof(sa.sa_data) - 1) {
Andy Green788c4a82012-10-22 12:29:57 +01001525 strcpy(sa.sa_data, hostname);
Andy Green43db0452013-01-10 19:50:35 +08001526 // lwsl_debug("my host name is %s\n", sa.sa_data);
Andy Green788c4a82012-10-22 12:29:57 +01001527 n = getnameinfo(&sa, sizeof(sa), hostname,
1528 (sizeof hostname) - 1, NULL, 0, 0);
1529 }
1530
1531 if (!n) {
1532 strncpy(context->canonical_hostname, hostname,
1533 sizeof context->canonical_hostname - 1);
1534 context->canonical_hostname[
1535 sizeof context->canonical_hostname - 1] = '\0';
1536 } else
1537 strncpy(context->canonical_hostname, hostname,
1538 sizeof context->canonical_hostname - 1);
1539
Andy Greenb3a614a2013-01-19 13:08:17 +08001540 lwsl_notice(" canonical_hostname = %s\n", context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08001541 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001542#endif
Andy Greena69f0512012-05-03 12:32:38 +08001543
Andy Green9659f372011-01-27 22:01:43 +00001544 /* split the proxy ads:port if given */
1545
1546 p = getenv("http_proxy");
1547 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00001548 strncpy(context->http_proxy_address, p,
Andy Green6ee372f2012-04-09 15:09:01 +08001549 sizeof context->http_proxy_address - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00001550 context->http_proxy_address[
1551 sizeof context->http_proxy_address - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00001552
Peter Hinz56885f32011-03-02 22:03:47 +00001553 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00001554 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001555 lwsl_err("http_proxy needs to be ads:port\n");
Andy Green9659f372011-01-27 22:01:43 +00001556 return NULL;
1557 }
1558 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00001559 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00001560
Andy Greenb3a614a2013-01-19 13:08:17 +08001561 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001562 context->http_proxy_address,
1563 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00001564 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001565
Andy Greena1ce6be2013-01-18 11:43:21 +08001566#ifndef LWS_NO_SERVER
Andy Green90c7cbc2011-01-27 06:26:52 +00001567 if (port) {
1568
Andy Green3faa9c72010-11-08 17:03:03 +00001569#ifdef LWS_OPENSSL_SUPPORT
Peter Hinz56885f32011-03-02 22:03:47 +00001570 context->use_ssl = ssl_cert_filepath != NULL &&
Andy Green90c7cbc2011-01-27 06:26:52 +00001571 ssl_private_key_filepath != NULL;
Peter Hinz56885f32011-03-02 22:03:47 +00001572 if (context->use_ssl)
Andy Greenb3a614a2013-01-19 13:08:17 +08001573 lwsl_notice(" Compiled with SSL support, using it\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001574 else
Andy Greenb3a614a2013-01-19 13:08:17 +08001575 lwsl_notice(" Compiled with SSL support, not using it\n");
Andy Green3faa9c72010-11-08 17:03:03 +00001576
Andy Green90c7cbc2011-01-27 06:26:52 +00001577#else
1578 if (ssl_cert_filepath != NULL &&
1579 ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08001580 lwsl_notice(" Not compiled for OpenSSl support!\n");
Andy Greene92cd172011-01-19 13:11:55 +00001581 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +00001582 }
Andy Greenb3a614a2013-01-19 13:08:17 +08001583 lwsl_notice(" Compiled without SSL support, "
Andy Green90c7cbc2011-01-27 06:26:52 +00001584 "serving unencrypted\n");
1585#endif
1586 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001587#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001588
1589 /* ignore SIGPIPE */
Peter Hinz56885f32011-03-02 22:03:47 +00001590#ifdef WIN32
1591#else
Andy Green90c7cbc2011-01-27 06:26:52 +00001592 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00001593#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001594
1595
1596#ifdef LWS_OPENSSL_SUPPORT
1597
1598 /* basic openssl init */
1599
1600 SSL_library_init();
1601
1602 OpenSSL_add_all_algorithms();
1603 SSL_load_error_strings();
1604
Andy Green2e24da02011-03-05 16:12:04 +00001605 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00001606 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
1607
Andy Green90c7cbc2011-01-27 06:26:52 +00001608 /*
1609 * Firefox insists on SSLv23 not SSLv3
1610 * Konq disables SSLv2 by default now, SSLv23 works
1611 */
1612
1613 method = (SSL_METHOD *)SSLv23_server_method();
1614 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001615 lwsl_err("problem creating ssl method: %s\n",
Andy Green90c7cbc2011-01-27 06:26:52 +00001616 ERR_error_string(ERR_get_error(), ssl_err_buf));
1617 return NULL;
1618 }
Peter Hinz56885f32011-03-02 22:03:47 +00001619 context->ssl_ctx = SSL_CTX_new(method); /* create context */
1620 if (!context->ssl_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001621 lwsl_err("problem creating ssl context: %s\n",
Andy Green90c7cbc2011-01-27 06:26:52 +00001622 ERR_error_string(ERR_get_error(), ssl_err_buf));
1623 return NULL;
1624 }
1625
David Galeanocc148e42013-01-10 10:18:59 +08001626#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001627 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001628#endif
David Galeano77a677c2013-01-10 10:14:12 +08001629 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
David Galeanof177f2a2013-01-10 10:15:19 +08001630 SSL_CTX_set_cipher_list(context->ssl_ctx, CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001631
Andy Greena1ce6be2013-01-18 11:43:21 +08001632#ifndef LWS_NO_CLIENT
1633
Andy Green90c7cbc2011-01-27 06:26:52 +00001634 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08001635
1636 if (port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00001637 method = (SSL_METHOD *)SSLv23_client_method();
1638 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001639 lwsl_err("problem creating ssl method: %s\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001640 ERR_error_string(ERR_get_error(), ssl_err_buf));
1641 return NULL;
1642 }
1643 /* create context */
1644 context->ssl_client_ctx = SSL_CTX_new(method);
1645 if (!context->ssl_client_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001646 lwsl_err("problem creating ssl context: %s\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001647 ERR_error_string(ERR_get_error(), ssl_err_buf));
1648 return NULL;
1649 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001650
David Galeanocc148e42013-01-10 10:18:59 +08001651#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001652 SSL_CTX_set_options(context->ssl_client_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001653#endif
David Galeano77a677c2013-01-10 10:14:12 +08001654 SSL_CTX_set_options(context->ssl_client_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
David Galeanof177f2a2013-01-10 10:15:19 +08001655 SSL_CTX_set_cipher_list(context->ssl_client_ctx, CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001656
Peter Hinz56885f32011-03-02 22:03:47 +00001657 /* openssl init for cert verification (for client sockets) */
David Galeano2f82be82013-01-09 16:25:54 +08001658 if (!ssl_ca_filepath) {
1659 if (!SSL_CTX_load_verify_locations(
1660 context->ssl_client_ctx, NULL,
1661 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08001662 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08001663 "Unable to load SSL Client certs from %s "
1664 "(set by --with-client-cert-dir= in configure) -- "
1665 " client ssl isn't going to work",
1666 LWS_OPENSSL_CLIENT_CERTS);
1667 } else
1668 if (!SSL_CTX_load_verify_locations(
1669 context->ssl_client_ctx, ssl_ca_filepath,
1670 NULL))
Andy Green43db0452013-01-10 19:50:35 +08001671 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08001672 "Unable to load SSL Client certs "
1673 "file from %s -- client ssl isn't "
1674 "going to work", ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00001675
1676 /*
1677 * callback allowing user code to load extra verification certs
1678 * helping the client to verify server identity
1679 */
1680
1681 context->protocols[0].callback(context, NULL,
1682 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
1683 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00001684 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001685#endif
Andy Green6ee372f2012-04-09 15:09:01 +08001686
Andy Greenc6bf2c22011-02-20 11:10:47 +00001687 /* as a server, are we requiring clients to identify themselves? */
1688
1689 if (options & LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
1690
1691 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08001692
Peter Hinz56885f32011-03-02 22:03:47 +00001693 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00001694 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1695 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001696
1697 /*
1698 * give user code a chance to load certs into the server
1699 * allowing it to verify incoming client certs
1700 */
1701
Peter Hinz56885f32011-03-02 22:03:47 +00001702 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00001703 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00001704 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001705 }
1706
Peter Hinz56885f32011-03-02 22:03:47 +00001707 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001708
1709 /* openssl init for server sockets */
1710
Andy Green3faa9c72010-11-08 17:03:03 +00001711 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08001712 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
1713 ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00001714 if (n != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001715 lwsl_err("problem getting cert '%s': %s\n",
Andy Green3faa9c72010-11-08 17:03:03 +00001716 ssl_cert_filepath,
1717 ERR_error_string(ERR_get_error(), ssl_err_buf));
Andy Greene92cd172011-01-19 13:11:55 +00001718 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +00001719 }
1720 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00001721 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
1722 ssl_private_key_filepath, SSL_FILETYPE_PEM) != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001723 lwsl_err("ssl problem getting key '%s': %s\n",
Andy Green018d8eb2010-11-08 21:04:23 +00001724 ssl_private_key_filepath,
1725 ERR_error_string(ERR_get_error(), ssl_err_buf));
Andy Greene92cd172011-01-19 13:11:55 +00001726 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +00001727 }
1728 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00001729 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08001730 lwsl_err("Private SSL key doesn't match cert\n");
Andy Greene92cd172011-01-19 13:11:55 +00001731 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +00001732 }
1733
1734 /* SSL is happy and has a cert it's content with */
1735 }
1736#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001737
Andy Greendf736162011-01-18 15:39:02 +00001738 /* selftest */
1739
1740 if (lws_b64_selftest())
Andy Greene92cd172011-01-19 13:11:55 +00001741 return NULL;
Andy Greendf736162011-01-18 15:39:02 +00001742
Andy Greena1ce6be2013-01-18 11:43:21 +08001743#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00001744 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00001745
Andy Green4739e5c2011-01-22 12:51:57 +00001746 if (port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08001747 extern int interface_to_sa(const char *ifname, struct sockaddr_in *addr, size_t addrlen);
1748 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00001749
Andy Green4739e5c2011-01-22 12:51:57 +00001750 sockfd = socket(AF_INET, SOCK_STREAM, 0);
1751 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08001752 lwsl_err("ERROR opening socket\n");
Andy Green4739e5c2011-01-22 12:51:57 +00001753 return NULL;
1754 }
Andy Green775c0dd2010-10-29 14:15:22 +01001755
Andy Green4739e5c2011-01-22 12:51:57 +00001756 /* allow us to restart even if old sockets in TIME_WAIT */
Andy Green6ee372f2012-04-09 15:09:01 +08001757 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
1758 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00001759
1760 /* Disable Nagle */
1761 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08001762 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
1763 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00001764
Andy Green4739e5c2011-01-22 12:51:57 +00001765 bzero((char *) &serv_addr, sizeof(serv_addr));
1766 serv_addr.sin_family = AF_INET;
Peter Hinz56885f32011-03-02 22:03:47 +00001767 if (interf == NULL)
Andy Green32375b72011-02-19 08:32:53 +00001768 serv_addr.sin_addr.s_addr = INADDR_ANY;
1769 else
Peter Hinz56885f32011-03-02 22:03:47 +00001770 interface_to_sa(interf, &serv_addr,
Andy Green32375b72011-02-19 08:32:53 +00001771 sizeof(serv_addr));
Andy Green4739e5c2011-01-22 12:51:57 +00001772 serv_addr.sin_port = htons(port);
1773
1774 n = bind(sockfd, (struct sockaddr *) &serv_addr,
1775 sizeof(serv_addr));
1776 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001777 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green8f037e42010-12-19 22:13:26 +00001778 port, n, errno);
Andy Green41c58032013-01-12 13:21:08 +08001779 close(sockfd);
Andy Green4739e5c2011-01-22 12:51:57 +00001780 return NULL;
1781 }
Andy Green0d338332011-02-12 11:57:43 +00001782
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001783 wsi = (struct libwebsocket *)malloc(sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08001784 if (wsi == NULL) {
1785 lwsl_err("Out of mem\n");
1786 close(sockfd);
1787 return NULL;
1788 }
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001789 memset(wsi, 0, sizeof (struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00001790 wsi->sock = sockfd;
Andy Greend6e09112011-03-05 16:12:15 +00001791 wsi->count_active_extensions = 0;
Andy Green0d338332011-02-12 11:57:43 +00001792 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08001793
1794 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001795
Andy Green65b0e912013-01-16 07:59:47 +08001796 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
1797 context->listen_service_count = 0;
1798 context->listen_service_fd = sockfd;
1799
Andy Greena824d182013-01-15 20:52:29 +08001800 listen(sockfd, LWS_SOMAXCONN);
Andy Greenb3a614a2013-01-19 13:08:17 +08001801 lwsl_notice(" Listening on port %d\n", port);
Andy Green8f037e42010-12-19 22:13:26 +00001802 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001803#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001804
Andy Green6ee372f2012-04-09 15:09:01 +08001805 /*
1806 * drop any root privs for this process
1807 * to listen on port < 1023 we would have needed root, but now we are
1808 * listening, we don't want the power for anything else
1809 */
Peter Hinz56885f32011-03-02 22:03:47 +00001810#ifdef WIN32
1811#else
Andy Green3faa9c72010-11-08 17:03:03 +00001812 if (gid != -1)
1813 if (setgid(gid))
Andy Green43db0452013-01-10 19:50:35 +08001814 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green3faa9c72010-11-08 17:03:03 +00001815 if (uid != -1)
1816 if (setuid(uid))
Andy Green43db0452013-01-10 19:50:35 +08001817 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00001818#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001819
1820 /* set up our internal broadcast trigger sockets per-protocol */
1821
Peter Hinz56885f32011-03-02 22:03:47 +00001822 for (context->count_protocols = 0;
1823 protocols[context->count_protocols].callback;
1824 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01001825
Andy Green43db0452013-01-10 19:50:35 +08001826 lwsl_parser(" Protocol: %s\n",
1827 protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01001828
Peter Hinz56885f32011-03-02 22:03:47 +00001829 protocols[context->count_protocols].owning_server = context;
1830 protocols[context->count_protocols].protocol_index =
1831 context->count_protocols;
Andy Greenb45993c2010-12-18 15:13:50 +00001832
1833 fd = socket(AF_INET, SOCK_STREAM, 0);
1834 if (fd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08001835 lwsl_err("ERROR opening socket\n");
Andy Greene92cd172011-01-19 13:11:55 +00001836 return NULL;
Andy Greenb45993c2010-12-18 15:13:50 +00001837 }
Andy Green8f037e42010-12-19 22:13:26 +00001838
Andy Greenb45993c2010-12-18 15:13:50 +00001839 /* allow us to restart even if old sockets in TIME_WAIT */
Andy Green6ee372f2012-04-09 15:09:01 +08001840 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&opt,
1841 sizeof(opt));
Andy Greenb45993c2010-12-18 15:13:50 +00001842
1843 bzero((char *) &serv_addr, sizeof(serv_addr));
1844 serv_addr.sin_family = AF_INET;
1845 serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
1846 serv_addr.sin_port = 0; /* pick the port for us */
1847
1848 n = bind(fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
1849 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001850 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Greenb45993c2010-12-18 15:13:50 +00001851 port, n, errno);
Andy Greene92cd172011-01-19 13:11:55 +00001852 return NULL;
Andy Greenb45993c2010-12-18 15:13:50 +00001853 }
1854
1855 slen = sizeof cli_addr;
1856 n = getsockname(fd, (struct sockaddr *)&cli_addr, &slen);
1857 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001858 lwsl_err("getsockname failed\n");
Andy Greene92cd172011-01-19 13:11:55 +00001859 return NULL;
Andy Greenb45993c2010-12-18 15:13:50 +00001860 }
Peter Hinz56885f32011-03-02 22:03:47 +00001861 protocols[context->count_protocols].broadcast_socket_port =
Andy Greenb45993c2010-12-18 15:13:50 +00001862 ntohs(cli_addr.sin_port);
1863 listen(fd, 5);
1864
Andy Green43db0452013-01-10 19:50:35 +08001865 lwsl_debug(" Protocol %s broadcast socket %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001866 protocols[context->count_protocols].name,
Andy Greenb45993c2010-12-18 15:13:50 +00001867 ntohs(cli_addr.sin_port));
1868
Andy Green0d338332011-02-12 11:57:43 +00001869 /* dummy wsi per broadcast proxy socket */
1870
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001871 wsi = (struct libwebsocket *)malloc(sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08001872 if (wsi == NULL) {
1873 lwsl_err("Out of mem\n");
1874 close(fd);
1875 return NULL;
1876 }
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001877 memset(wsi, 0, sizeof (struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00001878 wsi->sock = fd;
1879 wsi->mode = LWS_CONNMODE_BROADCAST_PROXY_LISTENER;
Andy Greend6e09112011-03-05 16:12:15 +00001880 wsi->count_active_extensions = 0;
Andy Green0d338332011-02-12 11:57:43 +00001881 /* note which protocol we are proxying */
Peter Hinz56885f32011-03-02 22:03:47 +00001882 wsi->protocol_index_for_broadcast_proxy =
1883 context->count_protocols;
Andy Green0d338332011-02-12 11:57:43 +00001884
Andy Greendfb23042013-01-17 12:26:48 +08001885 insert_wsi_socket_into_fds(context, wsi);
Andy Greenb45993c2010-12-18 15:13:50 +00001886 }
1887
Andy Greena41314f2011-05-23 10:00:03 +01001888 /*
1889 * give all extensions a chance to create any per-context
1890 * allocations they need
1891 */
1892
1893 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
1894 if (port)
1895 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andrew Chambersd5512172012-05-20 08:17:09 +08001896
1897 if (extensions) {
1898 while (extensions->callback) {
Andy Green43db0452013-01-10 19:50:35 +08001899 lwsl_ext(" Extension: %s\n", extensions->name);
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001900 extensions->callback(context, extensions, NULL,
1901 (enum libwebsocket_extension_callback_reasons)m,
1902 NULL, NULL, 0);
Andrew Chambersd5512172012-05-20 08:17:09 +08001903 extensions++;
1904 }
Andy Greena41314f2011-05-23 10:00:03 +01001905 }
1906
Peter Hinz56885f32011-03-02 22:03:47 +00001907 return context;
Andy Greene92cd172011-01-19 13:11:55 +00001908}
Andy Greenb45993c2010-12-18 15:13:50 +00001909
Andy Green4739e5c2011-01-22 12:51:57 +00001910
Andy Greened11a022011-01-20 10:23:50 +00001911#ifndef LWS_NO_FORK
1912
Andy Greene92cd172011-01-19 13:11:55 +00001913/**
1914 * libwebsockets_fork_service_loop() - Optional helper function forks off
1915 * a process for the websocket server loop.
Andy Green6964bb52011-01-23 16:50:33 +00001916 * You don't have to use this but if not, you
1917 * have to make sure you are calling
1918 * libwebsocket_service periodically to service
1919 * the websocket traffic
Peter Hinz56885f32011-03-02 22:03:47 +00001920 * @context: server context returned by creation function
Andy Greene92cd172011-01-19 13:11:55 +00001921 */
Andy Greenb45993c2010-12-18 15:13:50 +00001922
Andy Greene92cd172011-01-19 13:11:55 +00001923int
Peter Hinz56885f32011-03-02 22:03:47 +00001924libwebsockets_fork_service_loop(struct libwebsocket_context *context)
Andy Greene92cd172011-01-19 13:11:55 +00001925{
Andy Greene92cd172011-01-19 13:11:55 +00001926 int fd;
1927 struct sockaddr_in cli_addr;
1928 int n;
Andy Green3221f922011-02-12 13:14:11 +00001929 int p;
Andy Greenb45993c2010-12-18 15:13:50 +00001930
Andy Greened11a022011-01-20 10:23:50 +00001931 n = fork();
1932 if (n < 0)
1933 return n;
1934
Andy Greenfd6764a2013-01-19 11:11:42 +08001935 if (n) {
Andy Greened11a022011-01-20 10:23:50 +00001936
1937 /* main process context */
1938
Andy Green3221f922011-02-12 13:14:11 +00001939 /*
1940 * set up the proxy sockets to allow broadcast from
1941 * service process context
1942 */
1943
Peter Hinz56885f32011-03-02 22:03:47 +00001944 for (p = 0; p < context->count_protocols; p++) {
Andy Greened11a022011-01-20 10:23:50 +00001945 fd = socket(AF_INET, SOCK_STREAM, 0);
1946 if (fd < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001947 lwsl_err("Unable to create socket\n");
Andy Greened11a022011-01-20 10:23:50 +00001948 return -1;
1949 }
1950 cli_addr.sin_family = AF_INET;
1951 cli_addr.sin_port = htons(
Peter Hinz56885f32011-03-02 22:03:47 +00001952 context->protocols[p].broadcast_socket_port);
Andy Greened11a022011-01-20 10:23:50 +00001953 cli_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
1954 n = connect(fd, (struct sockaddr *)&cli_addr,
1955 sizeof cli_addr);
1956 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001957 lwsl_err("Unable to connect to "
Andy Greened11a022011-01-20 10:23:50 +00001958 "broadcast socket %d, %s\n",
Andy Green3221f922011-02-12 13:14:11 +00001959 n, strerror(errno));
Andy Greened11a022011-01-20 10:23:50 +00001960 return -1;
1961 }
1962
Peter Hinz56885f32011-03-02 22:03:47 +00001963 context->protocols[p].broadcast_socket_user_fd = fd;
Andy Greened11a022011-01-20 10:23:50 +00001964 }
1965
Andy Greene92cd172011-01-19 13:11:55 +00001966 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +00001967 }
1968
Artem Baguinski91531662011-12-14 22:14:03 +01001969#ifdef HAVE_SYS_PRCTL_H
Andy Greenb45993c2010-12-18 15:13:50 +00001970 /* we want a SIGHUP when our parent goes down */
Andy Greenfd6764a2013-01-19 11:11:42 +08001971 signal(SIGHUP, SIG_DFL);
Andy Greenb45993c2010-12-18 15:13:50 +00001972 prctl(PR_SET_PDEATHSIG, SIGHUP);
Artem Baguinski91531662011-12-14 22:14:03 +01001973#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001974
1975 /* in this forked process, sit and service websocket connections */
Andy Green8f037e42010-12-19 22:13:26 +00001976
Artem Baguinski91531662011-12-14 22:14:03 +01001977 while (1) {
Peter Hinz56885f32011-03-02 22:03:47 +00001978 if (libwebsocket_service(context, 1000))
Andy Green3928f612012-07-20 12:58:38 +08001979 break;
Andy Green5e8967a2012-10-17 20:10:44 +08001980//#ifndef HAVE_SYS_PRCTL_H
Artem Baguinski91531662011-12-14 22:14:03 +01001981/*
1982 * on systems without prctl() (i.e. anything but linux) we can notice that our
1983 * parent is dead if getppid() returns 1. FIXME apparently this is not true for
1984 * solaris, could remember ppid right after fork and wait for it to change.
1985 */
1986
Andy Green24cba922013-01-19 13:56:10 +08001987 /* if our parent went down, don't linger around */
1988 if (context->started_with_parent && kill(context->started_with_parent, 0) < 0)
1989 kill(getpid(), SIGTERM);
1990
1991 if (getppid() == 1)
1992 break;
Andy Green5e8967a2012-10-17 20:10:44 +08001993//#endif
Andy Green24cba922013-01-19 13:56:10 +08001994 }
Artem Baguinski91531662011-12-14 22:14:03 +01001995
Andy Green8f037e42010-12-19 22:13:26 +00001996
Andy Green3928f612012-07-20 12:58:38 +08001997 return 1;
Andy Greenff95d7a2010-10-28 22:36:01 +01001998}
1999
Andy Greened11a022011-01-20 10:23:50 +00002000#endif
2001
Andy Greenb45993c2010-12-18 15:13:50 +00002002/**
2003 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002004 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002005 * @wsi: pointer to struct websocket you want to know the protocol of
2006 *
Andy Green8f037e42010-12-19 22:13:26 +00002007 *
2008 * This is useful to get the protocol to broadcast back to from inside
Andy Greenb45993c2010-12-18 15:13:50 +00002009 * the callback.
2010 */
Andy Greenab990e42010-10-31 12:42:52 +00002011
Andy Greenb45993c2010-12-18 15:13:50 +00002012const struct libwebsocket_protocols *
2013libwebsockets_get_protocol(struct libwebsocket *wsi)
2014{
2015 return wsi->protocol;
2016}
2017
2018/**
Andy Greene92cd172011-01-19 13:11:55 +00002019 * libwebsockets_broadcast() - Sends a buffer to the callback for all active
Andy Green8f037e42010-12-19 22:13:26 +00002020 * connections of the given protocol.
Andy Greenb45993c2010-12-18 15:13:50 +00002021 * @protocol: pointer to the protocol you will broadcast to all members of
2022 * @buf: buffer containing the data to be broadcase. NOTE: this has to be
Andy Green8f037e42010-12-19 22:13:26 +00002023 * allocated with LWS_SEND_BUFFER_PRE_PADDING valid bytes before
2024 * the pointer and LWS_SEND_BUFFER_POST_PADDING afterwards in the
2025 * case you are calling this function from callback context.
Andy Greenb45993c2010-12-18 15:13:50 +00002026 * @len: length of payload data in buf, starting from buf.
Andy Green8f037e42010-12-19 22:13:26 +00002027 *
2028 * This function allows bulk sending of a packet to every connection using
Andy Greenb45993c2010-12-18 15:13:50 +00002029 * the given protocol. It does not send the data directly; instead it calls
2030 * the callback with a reason type of LWS_CALLBACK_BROADCAST. If the callback
2031 * wants to actually send the data for that connection, the callback itself
2032 * should call libwebsocket_write().
2033 *
2034 * libwebsockets_broadcast() can be called from another fork context without
2035 * having to take any care about data visibility between the processes, it'll
2036 * "just work".
2037 */
2038
2039
2040int
Andy Green8f037e42010-12-19 22:13:26 +00002041libwebsockets_broadcast(const struct libwebsocket_protocols *protocol,
Andy Greenb45993c2010-12-18 15:13:50 +00002042 unsigned char *buf, size_t len)
2043{
Peter Hinz56885f32011-03-02 22:03:47 +00002044 struct libwebsocket_context *context = protocol->owning_server;
Andy Greenb45993c2010-12-18 15:13:50 +00002045 int n;
Andy Green6ee372f2012-04-09 15:09:01 +08002046 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +00002047
2048 if (!protocol->broadcast_socket_user_fd) {
2049 /*
Andy Greene92cd172011-01-19 13:11:55 +00002050 * We are either running unforked / flat, or we are being
2051 * called from poll thread context
Andy Greenb45993c2010-12-18 15:13:50 +00002052 * eg, from a callback. In that case don't use sockets for
2053 * broadcast IPC (since we can't open a socket connection to
2054 * a socket listening on our own thread) but directly do the
2055 * send action.
2056 *
2057 * Locking is not needed because we are by definition being
2058 * called in the poll thread context and are serialized.
2059 */
2060
Andy Greendfb23042013-01-17 12:26:48 +08002061 for (n = 0; n < context->fds_count; n++) {
Andy Greenb45993c2010-12-18 15:13:50 +00002062
Andy Greendfb23042013-01-17 12:26:48 +08002063 wsi = context->lws_lookup[context->fds[n].fd];
2064 if (!wsi)
2065 continue;
Andy Greenb45993c2010-12-18 15:13:50 +00002066
Andy Greendfb23042013-01-17 12:26:48 +08002067 if (wsi->mode != LWS_CONNMODE_WS_SERVING)
2068 continue;
Andy Greenb45993c2010-12-18 15:13:50 +00002069
Andy Greendfb23042013-01-17 12:26:48 +08002070 /*
2071 * never broadcast to non-established connections
2072 */
2073 if (wsi->state != WSI_STATE_ESTABLISHED)
2074 continue;
Andy Greenb45993c2010-12-18 15:13:50 +00002075
Andy Greendfb23042013-01-17 12:26:48 +08002076 /* only broadcast to guys using
2077 * requested protocol
2078 */
2079 if (wsi->protocol != protocol)
2080 continue;
Andy Green0d338332011-02-12 11:57:43 +00002081
Andy Green706961d2013-01-17 16:50:35 +08002082 user_callback_handle_rxflow(wsi->protocol->callback,
2083 context, wsi,
Andy Greendfb23042013-01-17 12:26:48 +08002084 LWS_CALLBACK_BROADCAST,
2085 wsi->user_space,
2086 buf, len);
Andy Greenb45993c2010-12-18 15:13:50 +00002087 }
2088
2089 return 0;
2090 }
2091
Andy Green0ca6a172010-12-19 20:50:01 +00002092 /*
2093 * We're being called from a different process context than the server
2094 * loop. Instead of broadcasting directly, we send our
2095 * payload on a socket to do the IPC; the server process will serialize
2096 * the broadcast action in its main poll() loop.
2097 *
2098 * There's one broadcast socket listening for each protocol supported
2099 * set up when the websocket server initializes
2100 */
2101
Andy Green6964bb52011-01-23 16:50:33 +00002102 n = send(protocol->broadcast_socket_user_fd, buf, len, MSG_NOSIGNAL);
Andy Greenb45993c2010-12-18 15:13:50 +00002103
2104 return n;
2105}
Andy Green82c3d542011-03-07 21:16:31 +00002106
2107int
2108libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2109{
2110 return wsi->final;
2111}
Alex Bligh49146db2011-11-07 17:19:25 +08002112
David Galeanoe2cf9922013-01-09 18:06:55 +08002113unsigned char
2114libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2115{
2116 return wsi->rsv;
2117}
2118
Alex Bligh49146db2011-11-07 17:19:25 +08002119void *
2120libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2121{
2122 /* allocate the per-connection user memory (if any) */
2123
2124 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2125 wsi->user_space = malloc(
2126 wsi->protocol->per_session_data_size);
2127 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002128 lwsl_err("Out of memory for conn user space\n");
Alex Bligh49146db2011-11-07 17:19:25 +08002129 return NULL;
2130 }
Andy Green6ee372f2012-04-09 15:09:01 +08002131 memset(wsi->user_space, 0,
2132 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002133 }
2134 return wsi->user_space;
2135}
Andy Green43db0452013-01-10 19:50:35 +08002136
Andy Greenacbaee62013-01-18 22:00:22 +08002137/**
2138 * lws_confirm_legit_wsi: returns nonzero if the wsi looks bad
2139 *
2140 * @wsi: struct libwebsocket to assess
2141 *
2142 * Performs consistecy checks on what the wsi claims and what the
2143 * polling arrays hold. This'll catch a closed wsi still in use.
2144 * Don't try to use on the listen (nonconnection) wsi as it will
2145 * fail it. Otherwise 0 return == wsi seems consistent.
2146 */
2147
2148int lws_confirm_legit_wsi(struct libwebsocket *wsi)
2149{
2150 struct libwebsocket_context *context;
2151
2152 if (!(wsi && wsi->protocol && wsi->protocol->owning_server))
2153 return 1;
2154
2155 context = wsi->protocol->owning_server;
2156
2157 if (!context)
2158 return 2;
2159
2160 if (!wsi->position_in_fds_table)
2161 return 3; /* position in fds table looks bad */
2162 if (context->fds[wsi->position_in_fds_table].fd != wsi->sock)
2163 return 4; /* pollfd entry does not wait on our socket descriptor */
2164 if (context->lws_lookup[wsi->sock] != wsi)
2165 return 5; /* lookup table does not agree with wsi */
2166
2167 return 0;
2168}
Andy Greende8f27a2013-01-12 09:17:42 +08002169
Andy Green0b319092013-01-19 11:17:56 +08002170static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002171{
Andy Green0b319092013-01-19 11:17:56 +08002172 char buf[300];
2173 struct timeval tv;
Andy Green0b319092013-01-19 11:17:56 +08002174 int n;
2175
2176 gettimeofday(&tv, NULL);
2177
2178 buf[0] = '\0';
2179 for (n = 0; n < LLL_COUNT; n++)
2180 if (level == (1 << n)) {
Andy Green058ba812013-01-19 11:32:18 +08002181 sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
Andy Green0b319092013-01-19 11:17:56 +08002182 (int)(tv.tv_usec / 100), log_level_names[n]);
2183 break;
2184 }
2185
2186 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002187}
2188
Andy Greenc11db202013-01-19 11:12:16 +08002189void lwsl_emit_syslog(int level, const char *line)
2190{
2191 int syslog_level = LOG_DEBUG;
2192
2193 switch (level) {
2194 case LLL_ERR:
2195 syslog_level = LOG_ERR;
2196 break;
2197 case LLL_WARN:
2198 syslog_level = LOG_WARNING;
2199 break;
2200 case LLL_NOTICE:
2201 syslog_level = LOG_NOTICE;
2202 break;
2203 case LLL_INFO:
2204 syslog_level = LOG_INFO;
2205 break;
2206 }
2207 syslog(syslog_level, line);
2208}
2209
Andy Green43db0452013-01-10 19:50:35 +08002210void _lws_log(int filter, const char *format, ...)
2211{
Andy Greende8f27a2013-01-12 09:17:42 +08002212 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002213 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002214
2215 if (!(log_level & filter))
2216 return;
2217
Andy Green43db0452013-01-10 19:50:35 +08002218 va_start(ap, format);
Andy Green0b319092013-01-19 11:17:56 +08002219 vsnprintf(buf, (sizeof buf), format, ap);
Andy Greende8f27a2013-01-12 09:17:42 +08002220 buf[(sizeof buf) - 1] = '\0';
2221 va_end(ap);
2222
Andy Green0b319092013-01-19 11:17:56 +08002223 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002224}
2225
2226/**
2227 * lws_set_log_level() - Set the logging bitfield
2228 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002229 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2230 * function to perform log string emission instead of
2231 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002232 *
Andy Greende8f27a2013-01-12 09:17:42 +08002233 * log level defaults to "err" and "warn" contexts enabled only and
2234 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002235 */
2236
Andy Green058ba812013-01-19 11:32:18 +08002237void lws_set_log_level(int level, void (*log_emit_function)(int level, const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002238{
2239 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002240 if (log_emit_function)
2241 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002242}