blob: 728d5e96f6a1422e58d0805a1e2e7d24459d0c2e [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 Green3182ece2013-01-20 17:08:31 +080051
Andy Green7c19c342013-01-19 12:18:07 +080052static int log_level = LLL_ERR | LLL_WARN | LLL_NOTICE;
Andy Green058ba812013-01-19 11:32:18 +080053static void lwsl_emit_stderr(int level, const char *line);
54static void (*lwsl_emit)(int level, const char *line) = lwsl_emit_stderr;
55
Andy Green43db0452013-01-10 19:50:35 +080056static const char *log_level_names[] = {
57 "ERR",
58 "WARN",
Andy Green7c19c342013-01-19 12:18:07 +080059 "NOTICE",
Andy Green43db0452013-01-10 19:50:35 +080060 "INFO",
61 "DEBUG",
62 "PARSER",
63 "HEADER",
64 "EXTENSION",
65 "CLIENT",
66};
67
Andy Green0d338332011-02-12 11:57:43 +000068int
Andy Greendfb23042013-01-17 12:26:48 +080069insert_wsi_socket_into_fds(struct libwebsocket_context *context, struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +000070{
Andy Greendfb23042013-01-17 12:26:48 +080071 if (context->fds_count >= context->max_fds) {
72 lwsl_err("Reached limit of fds tracking (%d)\n", context->max_fds);
Andy Green0d338332011-02-12 11:57:43 +000073 return 1;
74 }
75
Andy Greendfb23042013-01-17 12:26:48 +080076 if (wsi->sock > context->max_fds) {
77 lwsl_err("Socket fd %d is beyond what we can index (%d)\n", wsi->sock, context->max_fds);
78 return 1;
79 }
80
81 assert(wsi);
82 assert(wsi->sock);
83
84 lwsl_info("insert_wsi_socket_into_fds: wsi=%p, sock=%d, fds pos=%d\n", wsi, wsi->sock, context->fds_count);
85
86 context->lws_lookup[wsi->sock] = wsi;
87 wsi->position_in_fds_table = context->fds_count;
88 context->fds[context->fds_count].fd = wsi->sock;
89 context->fds[context->fds_count].events = POLLIN;
90 context->fds[context->fds_count++].revents = 0;
91
92 /* external POLL support via protocol 0 */
93 context->protocols[0].callback(context, wsi,
94 LWS_CALLBACK_ADD_POLL_FD,
95 (void *)(long)wsi->sock, NULL, POLLIN);
Andy Green0d338332011-02-12 11:57:43 +000096
97 return 0;
98}
99
Andy Greendfb23042013-01-17 12:26:48 +0800100static int
101remove_wsi_socket_from_fds(struct libwebsocket_context *context, struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000102{
Andy Greendfb23042013-01-17 12:26:48 +0800103 int m;
Andy Green0d338332011-02-12 11:57:43 +0000104
Andy Greendfb23042013-01-17 12:26:48 +0800105 if (!--context->fds_count)
106 goto do_ext;
Andy Green0d338332011-02-12 11:57:43 +0000107
Andy Greendfb23042013-01-17 12:26:48 +0800108 if (wsi->sock > context->max_fds) {
109 lwsl_err("Socket fd %d is beyond what we can index (%d)\n", wsi->sock, context->max_fds);
110 return 1;
111 }
Andy Green0d338332011-02-12 11:57:43 +0000112
Andy Greendfb23042013-01-17 12:26:48 +0800113 lwsl_info("remove_wsi_socket_from_fds: wsi=%p, sock=%d, fds pos=%d\n", wsi, wsi->sock, wsi->position_in_fds_table);
114
115 m = wsi->position_in_fds_table; /* replace the contents for this */
116
117 /* have the last guy take up the vacant slot */
118 context->fds[m] = context->fds[context->fds_count]; /* vacant fds slot filled with end one */
119 /* end guy's fds_lookup entry remains unchanged (still same fd pointing to same wsi) */
120 /* end guy's "position in fds table" changed */
121 context->lws_lookup[context->fds[context->fds_count].fd]->position_in_fds_table = m;
122 /* deletion guy's lws_lookup entry needs nuking */
123 context->lws_lookup[wsi->sock] = NULL; /* no WSI for the socket of the wsi being removed*/
124 wsi->position_in_fds_table = -1; /* removed wsi has no position any more */
125
126do_ext:
127 /* remove also from external POLL support via protocol 0 */
128 if (wsi->sock)
129 context->protocols[0].callback(context, wsi,
130 LWS_CALLBACK_DEL_POLL_FD, (void *)(long)wsi->sock, NULL, 0);
131
132 return 0;
Andy Green0d338332011-02-12 11:57:43 +0000133}
134
Andy Green32375b72011-02-19 08:32:53 +0000135
Andy Green8f037e42010-12-19 22:13:26 +0000136void
Peter Hinz56885f32011-03-02 22:03:47 +0000137libwebsocket_close_and_free_session(struct libwebsocket_context *context,
Andy Green687b0182011-02-26 11:04:01 +0000138 struct libwebsocket *wsi, enum lws_close_status reason)
Andy Green251f6fa2010-11-03 11:13:06 +0000139{
Andy Greenb45993c2010-12-18 15:13:50 +0000140 int n;
Andy Green62c54d22011-02-14 09:14:25 +0000141 int old_state;
Andy Green5e1fa172011-02-10 09:07:05 +0000142 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 +
143 LWS_SEND_BUFFER_POST_PADDING];
Andy Green3182ece2013-01-20 17:08:31 +0800144#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000145 int ret;
146 int m;
147 struct lws_tokens eff_buf;
Andy Greena41314f2011-05-23 10:00:03 +0100148 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +0800149#endif
Andy Greenb45993c2010-12-18 15:13:50 +0000150
Andy Green4b6fbe12011-02-14 08:03:48 +0000151 if (!wsi)
Andy Greenb45993c2010-12-18 15:13:50 +0000152 return;
153
Andy Green62c54d22011-02-14 09:14:25 +0000154 old_state = wsi->state;
Andy Green251f6fa2010-11-03 11:13:06 +0000155
Andy Green62c54d22011-02-14 09:14:25 +0000156 if (old_state == WSI_STATE_DEAD_SOCKET)
Andy Green5e1fa172011-02-10 09:07:05 +0000157 return;
158
Andy Green623a98d2013-01-21 11:04:23 +0800159 wsi->u.ws.close_reason = reason;
Andy Greenda527df2011-03-07 07:08:12 +0000160
Andy Green3182ece2013-01-20 17:08:31 +0800161#ifndef LWS_NO_EXTENSIONS
Andy Greenda527df2011-03-07 07:08:12 +0000162 /*
Andy Green68b45042011-05-25 21:41:57 +0100163 * are his extensions okay with him closing? Eg he might be a mux
164 * parent and just his ch1 aspect is closing?
165 */
166
Andy Green68b45042011-05-25 21:41:57 +0100167 for (n = 0; n < wsi->count_active_extensions; n++) {
168 if (!wsi->active_extensions[n]->callback)
169 continue;
170
171 m = wsi->active_extensions[n]->callback(context,
172 wsi->active_extensions[n], wsi,
173 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
174 wsi->active_extensions_user[n], NULL, 0);
175
176 /*
177 * if somebody vetoed actually closing him at this time....
178 * up to the extension to track the attempted close, let's
179 * just bail
180 */
181
182 if (m) {
Andy Green43db0452013-01-10 19:50:35 +0800183 lwsl_ext("extension vetoed close\n");
Andy Green68b45042011-05-25 21:41:57 +0100184 return;
185 }
186 }
187
Andy Green68b45042011-05-25 21:41:57 +0100188 /*
Andy Greenc44159f2011-03-07 07:08:18 +0000189 * flush any tx pending from extensions, since we may send close packet
190 * if there are problems with send, just nuke the connection
191 */
192
193 ret = 1;
194 while (ret == 1) {
195
196 /* default to nobody has more to spill */
197
198 ret = 0;
199 eff_buf.token = NULL;
200 eff_buf.token_len = 0;
201
202 /* show every extension the new incoming data */
203
204 for (n = 0; n < wsi->count_active_extensions; n++) {
205 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000206 wsi->protocol->owning_server,
207 wsi->active_extensions[n], wsi,
Andy Greenc44159f2011-03-07 07:08:18 +0000208 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
209 wsi->active_extensions_user[n], &eff_buf, 0);
210 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800211 lwsl_ext("Extension reports fatal error\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000212 goto just_kill_connection;
213 }
214 if (m)
215 /*
216 * at least one extension told us he has more
217 * to spill, so we will go around again after
218 */
219 ret = 1;
220 }
221
222 /* assuming they left us something to send, send it */
223
224 if (eff_buf.token_len)
225 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
Andy Green0303db42013-01-17 14:46:43 +0800226 eff_buf.token_len)) {
227 lwsl_debug("close: sending final extension spill had problems\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000228 goto just_kill_connection;
Andy Green0303db42013-01-17 14:46:43 +0800229 }
Andy Greenc44159f2011-03-07 07:08:18 +0000230 }
Andy Green3182ece2013-01-20 17:08:31 +0800231#endif
Andy Greenc44159f2011-03-07 07:08:18 +0000232
233 /*
Andy Greenda527df2011-03-07 07:08:12 +0000234 * signal we are closing, libsocket_write will
235 * add any necessary version-specific stuff. If the write fails,
236 * no worries we are closing anyway. If we didn't initiate this
237 * close, then our state has been changed to
238 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this.
239 *
240 * Likewise if it's a second call to close this connection after we
241 * sent the close indication to the peer already, we are in state
242 * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time.
243 */
244
245 if (old_state == WSI_STATE_ESTABLISHED &&
246 reason != LWS_CLOSE_STATUS_NOSTATUS) {
Andy Green66a16f32011-05-24 22:07:45 +0100247
Andy Green43db0452013-01-10 19:50:35 +0800248 lwsl_debug("sending close indication...\n");
Andy Green66a16f32011-05-24 22:07:45 +0100249
Andy Greenda527df2011-03-07 07:08:12 +0000250 n = libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING],
251 0, LWS_WRITE_CLOSE);
252 if (!n) {
253 /*
254 * we have sent a nice protocol level indication we
255 * now wish to close, we should not send anything more
256 */
257
258 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
259
Andy Green0303db42013-01-17 14:46:43 +0800260 /* and we should wait for a reply for a bit out of politeness */
Andy Greenda527df2011-03-07 07:08:12 +0000261
262 libwebsocket_set_timeout(wsi,
Andy Green0303db42013-01-17 14:46:43 +0800263 PENDING_TIMEOUT_CLOSE_ACK, 1);
Andy Greenda527df2011-03-07 07:08:12 +0000264
Andy Green43db0452013-01-10 19:50:35 +0800265 lwsl_debug("sent close indication, awaiting ack\n");
Andy Greenda527df2011-03-07 07:08:12 +0000266
267 return;
268 }
269
Andy Green0303db42013-01-17 14:46:43 +0800270 lwsl_info("close: sending the close packet failed, hanging up\n");
271
Andy Greenda527df2011-03-07 07:08:12 +0000272 /* else, the send failed and we should just hang up */
273 }
274
Andy Green3182ece2013-01-20 17:08:31 +0800275#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000276just_kill_connection:
Andy Green3182ece2013-01-20 17:08:31 +0800277#endif
Andy Green66a16f32011-05-24 22:07:45 +0100278
Andy Green43db0452013-01-10 19:50:35 +0800279 lwsl_debug("libwebsocket_close_and_free_session: just_kill_connection\n");
Andy Green66a16f32011-05-24 22:07:45 +0100280
Andy Greenda527df2011-03-07 07:08:12 +0000281 /*
282 * we won't be servicing or receiving anything further from this guy
Andy Greendfb23042013-01-17 12:26:48 +0800283 * delete socket from the internal poll list if still present
Andy Greenda527df2011-03-07 07:08:12 +0000284 */
Andy Green4b6fbe12011-02-14 08:03:48 +0000285
Andy Greendfb23042013-01-17 12:26:48 +0800286 remove_wsi_socket_from_fds(context, wsi);
Andy Green4b6fbe12011-02-14 08:03:48 +0000287
Andy Green251f6fa2010-11-03 11:13:06 +0000288 wsi->state = WSI_STATE_DEAD_SOCKET;
289
Andy Green4b6fbe12011-02-14 08:03:48 +0000290 /* tell the user it's all over for this guy */
291
Andy Greend4302732011-02-28 07:45:29 +0000292 if (wsi->protocol && wsi->protocol->callback &&
Andy Green6ee372f2012-04-09 15:09:01 +0800293 ((old_state == WSI_STATE_ESTABLISHED) ||
294 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
295 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
Andy Green43db0452013-01-10 19:50:35 +0800296 lwsl_debug("calling back CLOSED\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000297 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000298 wsi->user_space, NULL, 0);
Andy Greencc012472011-11-07 19:53:23 +0800299 } else
Andy Green43db0452013-01-10 19:50:35 +0800300 lwsl_debug("not calling back closed, old_state=%d\n", old_state);
Andy Green251f6fa2010-11-03 11:13:06 +0000301
Andy Green3182ece2013-01-20 17:08:31 +0800302#ifndef LWS_NO_EXTENSIONS
Andy Greenef660a92011-03-06 10:29:38 +0000303 /* deallocate any active extension contexts */
304
305 for (n = 0; n < wsi->count_active_extensions; n++) {
306 if (!wsi->active_extensions[n]->callback)
307 continue;
308
Andy Green46c2ea02011-03-22 09:04:01 +0000309 wsi->active_extensions[n]->callback(context,
310 wsi->active_extensions[n], wsi,
311 LWS_EXT_CALLBACK_DESTROY,
312 wsi->active_extensions_user[n], NULL, 0);
Andy Greenef660a92011-03-06 10:29:38 +0000313
314 free(wsi->active_extensions_user[n]);
315 }
316
Andy Greena41314f2011-05-23 10:00:03 +0100317 /*
318 * inform all extensions in case they tracked this guy out of band
319 * even though not active on him specifically
320 */
321
322 ext = context->extensions;
323 while (ext && ext->callback) {
324 ext->callback(context, ext, wsi,
325 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
326 NULL, NULL, 0);
327 ext++;
328 }
Andy Green3182ece2013-01-20 17:08:31 +0800329#endif
Andy Greena41314f2011-05-23 10:00:03 +0100330
Andy Greenef660a92011-03-06 10:29:38 +0000331 /* free up his parsing allocations */
Andy Green4b6fbe12011-02-14 08:03:48 +0000332
Andy Green251f6fa2010-11-03 11:13:06 +0000333 for (n = 0; n < WSI_TOKEN_COUNT; n++)
334 if (wsi->utf8_token[n].token)
335 free(wsi->utf8_token[n].token);
Andy Greena1ce6be2013-01-18 11:43:21 +0800336#ifndef LWS_NO_CLIENT
Andy Greena41314f2011-05-23 10:00:03 +0100337 if (wsi->c_address)
338 free(wsi->c_address);
Andy Greena1ce6be2013-01-18 11:43:21 +0800339#endif
Andy Green623a98d2013-01-21 11:04:23 +0800340 if (wsi->u.ws.rxflow_buffer)
341 free(wsi->u.ws.rxflow_buffer);
Andy Green706961d2013-01-17 16:50:35 +0800342
Andy Green43db0452013-01-10 19:50:35 +0800343/* lwsl_info("closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000344
Andy Green3faa9c72010-11-08 17:03:03 +0000345#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000346 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000347 n = SSL_get_fd(wsi->ssl);
348 SSL_shutdown(wsi->ssl);
Andy Green3fc2c652013-01-14 15:35:02 +0800349 compatible_close(n);
Andy Green3faa9c72010-11-08 17:03:03 +0000350 SSL_free(wsi->ssl);
351 } else {
352#endif
Andy Green0303db42013-01-17 14:46:43 +0800353 if (wsi->sock) {
354 n = shutdown(wsi->sock, SHUT_RDWR);
355 if (n)
356 lwsl_debug("closing: shutdown returned %d\n", errno);
Andy Green3fc2c652013-01-14 15:35:02 +0800357
Andy Green0303db42013-01-17 14:46:43 +0800358 n = compatible_close(wsi->sock);
359 if (n)
360 lwsl_debug("closing: close returned %d\n", errno);
361 }
Andy Green3faa9c72010-11-08 17:03:03 +0000362#ifdef LWS_OPENSSL_SUPPORT
363 }
364#endif
David Brooks2c60d952012-04-20 12:19:01 +0800365 if (wsi->protocol && wsi->protocol->per_session_data_size && wsi->user_space) /* user code may own */
Andy Green4f3943a2010-11-12 10:44:16 +0000366 free(wsi->user_space);
367
Andy Green251f6fa2010-11-03 11:13:06 +0000368 free(wsi);
369}
370
Andy Green07034092011-02-13 08:37:12 +0000371/**
Andy Greenf7ee5492011-02-13 09:04:21 +0000372 * libwebsockets_hangup_on_client() - Server calls to terminate client
Andy Green6ee372f2012-04-09 15:09:01 +0800373 * connection
Peter Hinz56885f32011-03-02 22:03:47 +0000374 * @context: libwebsockets context
Andy Greenf7ee5492011-02-13 09:04:21 +0000375 * @fd: Connection socket descriptor
376 */
377
378void
Peter Hinz56885f32011-03-02 22:03:47 +0000379libwebsockets_hangup_on_client(struct libwebsocket_context *context, int fd)
Andy Greenf7ee5492011-02-13 09:04:21 +0000380{
Andy Greendfb23042013-01-17 12:26:48 +0800381 struct libwebsocket *wsi = context->lws_lookup[fd];
Andy Greenf7ee5492011-02-13 09:04:21 +0000382
Andy Greendfb23042013-01-17 12:26:48 +0800383 if (wsi) {
384 libwebsocket_close_and_free_session(context,
385 wsi, LWS_CLOSE_STATUS_NOSTATUS);
386 } else
387 close(fd);
Andy Greenf7ee5492011-02-13 09:04:21 +0000388}
389
390
391/**
Andy Green07034092011-02-13 08:37:12 +0000392 * libwebsockets_get_peer_addresses() - Get client address information
393 * @fd: Connection socket descriptor
394 * @name: Buffer to take client address name
395 * @name_len: Length of client address name buffer
396 * @rip: Buffer to take client address IP qotted quad
397 * @rip_len: Length of client address IP buffer
398 *
399 * This function fills in @name and @rip with the name and IP of
Andy Green6ee372f2012-04-09 15:09:01 +0800400 * the client connected with socket descriptor @fd. Names may be
401 * truncated if there is not enough room. If either cannot be
402 * determined, they will be returned as valid zero-length strings.
Andy Green07034092011-02-13 08:37:12 +0000403 */
404
405void
406libwebsockets_get_peer_addresses(int fd, char *name, int name_len,
407 char *rip, int rip_len)
408{
409 unsigned int len;
410 struct sockaddr_in sin;
411 struct hostent *host;
412 struct hostent *host1;
413 char ip[128];
Andy Greenf92def72011-03-09 15:02:20 +0000414 unsigned char *p;
Andy Green07034092011-02-13 08:37:12 +0000415 int n;
David Galeanocb193682013-01-09 15:29:00 +0800416#ifdef AF_LOCAL
417 struct sockaddr_un *un;
418#endif
Andy Green07034092011-02-13 08:37:12 +0000419
420 rip[0] = '\0';
421 name[0] = '\0';
422
423 len = sizeof sin;
424 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
425 perror("getpeername");
426 return;
427 }
Andy Green6ee372f2012-04-09 15:09:01 +0800428
Andy Green07034092011-02-13 08:37:12 +0000429 host = gethostbyaddr((char *) &sin.sin_addr, sizeof sin.sin_addr,
430 AF_INET);
431 if (host == NULL) {
432 perror("gethostbyaddr");
433 return;
434 }
435
436 strncpy(name, host->h_name, name_len);
437 name[name_len - 1] = '\0';
438
439 host1 = gethostbyname(host->h_name);
440 if (host1 == NULL)
441 return;
Andy Greenf92def72011-03-09 15:02:20 +0000442 p = (unsigned char *)host1;
Andy Green07034092011-02-13 08:37:12 +0000443 n = 0;
444 while (p != NULL) {
Andy Greenf92def72011-03-09 15:02:20 +0000445 p = (unsigned char *)host1->h_addr_list[n++];
Andy Green07034092011-02-13 08:37:12 +0000446 if (p == NULL)
447 continue;
Peter Hinzbb45a902011-03-10 18:14:01 +0000448 if ((host1->h_addrtype != AF_INET)
449#ifdef AF_LOCAL
450 && (host1->h_addrtype != AF_LOCAL)
451#endif
452 )
Andy Green07034092011-02-13 08:37:12 +0000453 continue;
454
Andy Green7627af52011-03-09 15:13:52 +0000455 if (host1->h_addrtype == AF_INET)
456 sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
Peter Hinzbb45a902011-03-10 18:14:01 +0000457#ifdef AF_LOCAL
Andy Green7627af52011-03-09 15:13:52 +0000458 else {
459 un = (struct sockaddr_un *)p;
Andy Green6ee372f2012-04-09 15:09:01 +0800460 strncpy(ip, un->sun_path, sizeof(ip) - 1);
Andy Green7627af52011-03-09 15:13:52 +0000461 ip[sizeof(ip) - 1] = '\0';
462 }
Peter Hinzbb45a902011-03-10 18:14:01 +0000463#endif
Andy Green07034092011-02-13 08:37:12 +0000464 p = NULL;
465 strncpy(rip, ip, rip_len);
466 rip[rip_len - 1] = '\0';
467 }
468}
Andy Green9f990342011-02-12 11:57:45 +0000469
Peter Hinz56885f32011-03-02 22:03:47 +0000470int libwebsockets_get_random(struct libwebsocket_context *context,
471 void *buf, int len)
472{
473 int n;
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800474 char *p = (char *)buf;
Peter Hinz56885f32011-03-02 22:03:47 +0000475
476#ifdef WIN32
477 for (n = 0; n < len; n++)
478 p[n] = (unsigned char)rand();
479#else
480 n = read(context->fd_random, p, len);
481#endif
482
483 return n;
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{
Andy Green3b84c002011-03-06 13:14:42 +0000509 int n;
Andy Green3182ece2013-01-20 17:08:31 +0800510#ifndef LWS_NO_EXTENSIONS
511 struct lws_tokens eff_buf;
Andy Green3b84c002011-03-06 13:14:42 +0000512 int ret;
513 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100514 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000515
Andy Greena41314f2011-05-23 10:00:03 +0100516 for (n = 0; n < wsi->count_active_extensions; n++) {
517 if (!wsi->active_extensions[n]->callback)
518 continue;
519
520 m = wsi->active_extensions[n]->callback(context,
521 wsi->active_extensions[n], wsi,
522 LWS_EXT_CALLBACK_IS_WRITEABLE,
523 wsi->active_extensions_user[n], NULL, 0);
524 if (m > handled)
525 handled = m;
526 }
527
528 if (handled == 1)
529 goto notify_action;
530
531 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000532 goto user_service;
533
534 /*
535 * check in on the active extensions, see if they
536 * had pending stuff to spill... they need to get the
537 * first look-in otherwise sequence will be disordered
538 *
539 * NULL, zero-length eff_buf means just spill pending
540 */
541
542 ret = 1;
543 while (ret == 1) {
544
545 /* default to nobody has more to spill */
546
547 ret = 0;
548 eff_buf.token = NULL;
549 eff_buf.token_len = 0;
550
551 /* give every extension a chance to spill */
552
553 for (n = 0; n < wsi->count_active_extensions; n++) {
554 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000555 wsi->protocol->owning_server,
556 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000557 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
558 wsi->active_extensions_user[n], &eff_buf, 0);
559 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800560 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000561 return -1;
562 }
563 if (m)
564 /*
565 * at least one extension told us he has more
566 * to spill, so we will go around again after
567 */
568 ret = 1;
569 }
570
571 /* assuming they gave us something to send, send it */
572
573 if (eff_buf.token_len) {
574 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
575 eff_buf.token_len))
576 return -1;
577 } else
578 continue;
579
580 /* no extension has more to spill */
581
582 if (!ret)
583 continue;
584
585 /*
586 * There's more to spill from an extension, but we just sent
587 * something... did that leave the pipe choked?
588 */
589
590 if (!lws_send_pipe_choked(wsi))
591 /* no we could add more */
592 continue;
593
Andy Green43db0452013-01-10 19:50:35 +0800594 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000595
596 /*
597 * Yes, he's choked. Leave the POLLOUT masked on so we will
598 * come back here when he is unchoked. Don't call the user
599 * callback to enforce ordering of spilling, he'll get called
600 * when we come back here and there's nothing more to spill.
601 */
602
603 return 0;
604 }
605
606 wsi->extension_data_pending = 0;
607
608user_service:
Andy Green3182ece2013-01-20 17:08:31 +0800609#endif
Andy Green3b84c002011-03-06 13:14:42 +0000610 /* one shot */
611
Andy Greena41314f2011-05-23 10:00:03 +0100612 if (pollfd) {
613 pollfd->events &= ~POLLOUT;
Andy Green3b84c002011-03-06 13:14:42 +0000614
Andy Greena41314f2011-05-23 10:00:03 +0100615 /* external POLL support via protocol 0 */
616 context->protocols[0].callback(context, wsi,
617 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
618 (void *)(long)wsi->sock, NULL, POLLOUT);
619 }
Andy Green3182ece2013-01-20 17:08:31 +0800620#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100621notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800622#endif
Andy Green3b84c002011-03-06 13:14:42 +0000623
Andy Green9e4c2b62011-03-07 20:47:39 +0000624 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
625 n = LWS_CALLBACK_CLIENT_WRITEABLE;
626 else
627 n = LWS_CALLBACK_SERVER_WRITEABLE;
628
Andy Green706961d2013-01-17 16:50:35 +0800629 user_callback_handle_rxflow(wsi->protocol->callback, context,
630 wsi, (enum libwebsocket_callback_reasons) n, wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000631
632 return 0;
633}
634
635
636
Andy Greena41314f2011-05-23 10:00:03 +0100637void
638libwebsocket_service_timeout_check(struct libwebsocket_context *context,
639 struct libwebsocket *wsi, unsigned int sec)
640{
Andy Green3182ece2013-01-20 17:08:31 +0800641#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100642 int n;
643
644 /*
645 * if extensions want in on it (eg, we are a mux parent)
646 * give them a chance to service child timeouts
647 */
648
649 for (n = 0; n < wsi->count_active_extensions; n++)
650 wsi->active_extensions[n]->callback(
651 context, wsi->active_extensions[n],
652 wsi, LWS_EXT_CALLBACK_1HZ,
653 wsi->active_extensions_user[n], NULL, sec);
654
Andy Green3182ece2013-01-20 17:08:31 +0800655#endif
Andy Greena41314f2011-05-23 10:00:03 +0100656 if (!wsi->pending_timeout)
657 return;
Andy Green6ee372f2012-04-09 15:09:01 +0800658
Andy Greena41314f2011-05-23 10:00:03 +0100659 /*
660 * if we went beyond the allowed time, kill the
661 * connection
662 */
663
664 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800665 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100666 libwebsocket_close_and_free_session(context,
667 wsi, LWS_CLOSE_STATUS_NOSTATUS);
668 }
669}
670
Andy Green9f990342011-02-12 11:57:45 +0000671/**
672 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000673 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000674 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800675 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000676 *
Andy Green75006172013-01-22 12:32:11 +0800677 * This function takes a pollfd that has POLLIN or POLLOUT activity and
678 * services it according to the state of the associated struct libwebsocket.
679 *
680 * The one call deals with all "service" that might happen on a socket
681 * including listen accepts, http files as well as websocket protocol.
Andy Green9f990342011-02-12 11:57:45 +0000682 */
683
684int
Peter Hinz56885f32011-03-02 22:03:47 +0000685libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000686 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000687{
Andy Greena1ce6be2013-01-18 11:43:21 +0800688 struct libwebsocket *wsi;
Andy Green6ee372f2012-04-09 15:09:01 +0800689 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 +
690 MAX_BROADCAST_PAYLOAD + LWS_SEND_BUFFER_POST_PADDING];
Andy Greenb45993c2010-12-18 15:13:50 +0000691 int n;
Andy Green0d338332011-02-12 11:57:43 +0000692 int m;
Andy Greena71eafc2011-02-14 17:59:43 +0000693 struct timeval tv;
Andy Green3182ece2013-01-20 17:08:31 +0800694#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000695 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800696#endif
Andy Green98a717c2011-03-06 13:14:15 +0000697 struct lws_tokens eff_buf;
Andy Green03674a62013-01-16 11:47:40 +0800698#ifndef LWS_NO_CLIENT
Andy Green76f61e72013-01-16 11:53:05 +0800699 extern int lws_client_socket_service(struct libwebsocket_context *context, struct libwebsocket *wsi, struct pollfd *pollfd);
Andy Green03674a62013-01-16 11:47:40 +0800700#endif
Andy Greena1ce6be2013-01-18 11:43:21 +0800701#ifndef LWS_NO_SERVER
702 extern int lws_server_socket_service(struct libwebsocket_context *context, struct libwebsocket *wsi, struct pollfd *pollfd);
703#endif
Andy Greena71eafc2011-02-14 17:59:43 +0000704 /*
705 * you can call us with pollfd = NULL to just allow the once-per-second
706 * global timeout checks; if less than a second since the last check
707 * it returns immediately then.
708 */
709
710 gettimeofday(&tv, NULL);
711
Peter Hinz56885f32011-03-02 22:03:47 +0000712 if (context->last_timeout_check_s != tv.tv_sec) {
713 context->last_timeout_check_s = tv.tv_sec;
Andy Greena71eafc2011-02-14 17:59:43 +0000714
Andy Green24cba922013-01-19 13:56:10 +0800715 /* if our parent went down, don't linger around */
716 if (context->started_with_parent && kill(context->started_with_parent, 0) < 0)
717 kill(getpid(), SIGTERM);
718
Andy Greena71eafc2011-02-14 17:59:43 +0000719 /* global timeout check once per second */
720
Peter Hinz56885f32011-03-02 22:03:47 +0000721 for (n = 0; n < context->fds_count; n++) {
Andy Greendfb23042013-01-17 12:26:48 +0800722 struct libwebsocket *new_wsi = context->lws_lookup[context->fds[n].fd];
723 if (!new_wsi)
724 continue;
725 libwebsocket_service_timeout_check(context,
726 new_wsi, tv.tv_sec);
Andy Greena71eafc2011-02-14 17:59:43 +0000727 }
728 }
729
730 /* just here for timeout management? */
731
732 if (pollfd == NULL)
733 return 0;
734
735 /* no, here to service a socket descriptor */
736
Andy Green65b0e912013-01-16 07:59:47 +0800737 /*
738 * deal with listen service piggybacking
739 * every listen_service_modulo services of other fds, we
740 * sneak one in to service the listen socket if there's anything waiting
741 *
742 * To handle connection storms, as found in ab, if we previously saw a
743 * pending connection here, it causes us to check again next time.
744 */
745
746 if (context->listen_service_fd && pollfd->fd != context->listen_service_fd) {
747 context->listen_service_count++;
748 if (context->listen_service_extraseen ||
749 context->listen_service_count == context->listen_service_modulo) {
750 context->listen_service_count = 0;
751 m = 1;
752 if (context->listen_service_extraseen > 5)
753 m = 2;
754 while (m--) {
755 /* even with extpoll, we prepared this internal fds for listen */
756 n = poll(&context->fds[0], 1, 0);
757 if (n > 0) { /* there's a connection waiting for us */
758 libwebsocket_service_fd(context, &context->fds[0]);
759 context->listen_service_extraseen++;
760 } else {
761 if (context->listen_service_extraseen)
762 context->listen_service_extraseen--;
763 break;
764 }
765 }
766 }
767
768 }
769
770 /* okay, what we came here to do... */
771
Andy Greendfb23042013-01-17 12:26:48 +0800772 wsi = context->lws_lookup[pollfd->fd];
Andy Greend280b6e2013-01-15 13:40:23 +0800773 if (wsi == NULL) {
Andy Greendfb23042013-01-17 12:26:48 +0800774 if (pollfd->fd > 11)
775 lwsl_err("unexpected NULL wsi fd=%d fds_count=%d\n", pollfd->fd, context->fds_count);
Andy Greenfa3f4052012-10-07 20:40:35 +0800776 return 0;
Andy Greend280b6e2013-01-15 13:40:23 +0800777 }
Andy Green8f037e42010-12-19 22:13:26 +0000778
Andy Green0d338332011-02-12 11:57:43 +0000779 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +0800780
Andy Greena1ce6be2013-01-18 11:43:21 +0800781#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +0800782 case LWS_CONNMODE_HTTP_SERVING:
Andy Green0d338332011-02-12 11:57:43 +0000783 case LWS_CONNMODE_SERVER_LISTENER:
Andy Green0d338332011-02-12 11:57:43 +0000784 case LWS_CONNMODE_BROADCAST_PROXY_LISTENER:
Andy Green0d338332011-02-12 11:57:43 +0000785 case LWS_CONNMODE_BROADCAST_PROXY:
Andy Greene2160712013-01-28 12:19:10 +0800786 case LWS_CONNMODE_SSL_ACK_PENDING:
787
788lwsl_debug("*\n");
Andy Greena1ce6be2013-01-18 11:43:21 +0800789 return lws_server_socket_service(context, wsi, pollfd);
790#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000791
Andy Green0d338332011-02-12 11:57:43 +0000792 case LWS_CONNMODE_WS_SERVING:
793 case LWS_CONNMODE_WS_CLIENT:
794
795 /* handle session socket closed */
796
797 if (pollfd->revents & (POLLERR | POLLHUP)) {
798
Andy Green43db0452013-01-10 19:50:35 +0800799 lwsl_debug("Session Socket %p (fd=%d) dead\n",
Andy Green0d338332011-02-12 11:57:43 +0000800 (void *)wsi, pollfd->fd);
801
Peter Hinz56885f32011-03-02 22:03:47 +0000802 libwebsocket_close_and_free_session(context, wsi,
Andy Green687b0182011-02-26 11:04:01 +0000803 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800804 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000805 }
806
Andy Green0d338332011-02-12 11:57:43 +0000807 /* the guy requested a callback when it was OK to write */
808
Andy Greenda527df2011-03-07 07:08:12 +0000809 if ((pollfd->revents & POLLOUT) &&
810 wsi->state == WSI_STATE_ESTABLISHED)
811 if (lws_handle_POLLOUT_event(context, wsi,
812 pollfd) < 0) {
813 libwebsocket_close_and_free_session(
814 context, wsi, LWS_CLOSE_STATUS_NORMAL);
Andy Green040d2ef2013-01-16 13:40:43 +0800815 return 0;
Andy Green3b84c002011-03-06 13:14:42 +0000816 }
Andy Green0d338332011-02-12 11:57:43 +0000817
Andy Green0d338332011-02-12 11:57:43 +0000818
819 /* any incoming data ready? */
820
821 if (!(pollfd->revents & POLLIN))
822 break;
823
Andy Greenb45993c2010-12-18 15:13:50 +0000824#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +0800825read_pending:
Andy Green0d338332011-02-12 11:57:43 +0000826 if (wsi->ssl)
Andy Green98a717c2011-03-06 13:14:15 +0000827 eff_buf.token_len = SSL_read(wsi->ssl, buf, sizeof buf);
Andy Greenb45993c2010-12-18 15:13:50 +0000828 else
829#endif
Andy Green98a717c2011-03-06 13:14:15 +0000830 eff_buf.token_len =
Andy Green72c34322011-04-16 10:46:21 +0100831 recv(pollfd->fd, buf, sizeof buf, 0);
Andy Greenb45993c2010-12-18 15:13:50 +0000832
Andy Green98a717c2011-03-06 13:14:15 +0000833 if (eff_buf.token_len < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800834 lwsl_debug("Socket read returned %d\n",
Andy Green98a717c2011-03-06 13:14:15 +0000835 eff_buf.token_len);
Alon Levydc93b7f2012-10-19 11:21:57 +0200836 if (errno != EINTR && errno != EAGAIN)
Andy Green6ee372f2012-04-09 15:09:01 +0800837 libwebsocket_close_and_free_session(context,
838 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800839 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000840 }
Andy Green98a717c2011-03-06 13:14:15 +0000841 if (!eff_buf.token_len) {
Peter Hinz56885f32011-03-02 22:03:47 +0000842 libwebsocket_close_and_free_session(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +0800843 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenfa3f4052012-10-07 20:40:35 +0800844 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000845 }
846
Andy Green98a717c2011-03-06 13:14:15 +0000847 /*
848 * give any active extensions a chance to munge the buffer
849 * before parse. We pass in a pointer to an lws_tokens struct
850 * prepared with the default buffer and content length that's in
851 * there. Rather than rewrite the default buffer, extensions
852 * that expect to grow the buffer can adapt .token to
853 * point to their own per-connection buffer in the extension
854 * user allocation. By default with no extensions or no
855 * extension callback handling, just the normal input buffer is
856 * used then so it is efficient.
857 */
Andy Greenb45993c2010-12-18 15:13:50 +0000858
Andy Green98a717c2011-03-06 13:14:15 +0000859 eff_buf.token = (char *)buf;
Andy Green3182ece2013-01-20 17:08:31 +0800860#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +0000861 more = 1;
862 while (more) {
Andy Green0d338332011-02-12 11:57:43 +0000863
Andy Green98a717c2011-03-06 13:14:15 +0000864 more = 0;
865
866 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +0000867 m = wsi->active_extensions[n]->callback(context,
868 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +0000869 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +0000870 wsi->active_extensions_user[n],
871 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +0000872 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800873 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +0800874 "Extension reports fatal error\n");
875 libwebsocket_close_and_free_session(
876 context, wsi,
877 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800878 return 0;
Andy Green98a717c2011-03-06 13:14:15 +0000879 }
880 if (m)
881 more = 1;
882 }
Andy Green3182ece2013-01-20 17:08:31 +0800883#endif
Andy Green98a717c2011-03-06 13:14:15 +0000884 /* service incoming data */
885
886 if (eff_buf.token_len) {
887 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +0800888 (unsigned char *)eff_buf.token,
889 eff_buf.token_len);
Andy Green98a717c2011-03-06 13:14:15 +0000890 if (n < 0)
891 /* we closed wsi */
Andy Green040d2ef2013-01-16 13:40:43 +0800892 return 0;
Andy Green98a717c2011-03-06 13:14:15 +0000893 }
Andy Green3182ece2013-01-20 17:08:31 +0800894#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +0000895 eff_buf.token = NULL;
896 eff_buf.token_len = 0;
897 }
Andy Green3182ece2013-01-20 17:08:31 +0800898#endif
David Galeano7ffbe1b2013-01-10 10:35:32 +0800899
900#ifdef LWS_OPENSSL_SUPPORT
901 if (wsi->ssl && SSL_pending(wsi->ssl))
902 goto read_pending;
903#endif
Andy Green98a717c2011-03-06 13:14:15 +0000904 break;
Andy Green76f61e72013-01-16 11:53:05 +0800905
906 default:
Andy Green03674a62013-01-16 11:47:40 +0800907#ifdef LWS_NO_CLIENT
908 break;
909#else
Andy Green76f61e72013-01-16 11:53:05 +0800910 return lws_client_socket_service(context, wsi, pollfd);
Andy Green03674a62013-01-16 11:47:40 +0800911#endif
Andy Greenb45993c2010-12-18 15:13:50 +0000912 }
913
914 return 0;
915}
916
Andy Green0d338332011-02-12 11:57:43 +0000917
Andy Green6964bb52011-01-23 16:50:33 +0000918/**
919 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +0000920 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +0000921 *
922 * This function closes any active connections and then frees the
923 * context. After calling this, any further use of the context is
924 * undefined.
925 */
926void
Peter Hinz56885f32011-03-02 22:03:47 +0000927libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +0000928{
Andy Green3182ece2013-01-20 17:08:31 +0800929#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +0000930 int n;
931 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100932 struct libwebsocket_extension *ext;
Andy Green6964bb52011-01-23 16:50:33 +0000933
Andy Greendfb23042013-01-17 12:26:48 +0800934 for (n = 0; n < context->fds_count; n++) {
935 struct libwebsocket *wsi = context->lws_lookup[context->fds[n].fd];
936 libwebsocket_close_and_free_session(context,
937 wsi, LWS_CLOSE_STATUS_GOINGAWAY);
938 }
Andy Green6964bb52011-01-23 16:50:33 +0000939
Andy Greena41314f2011-05-23 10:00:03 +0100940 /*
941 * give all extensions a chance to clean up any per-context
942 * allocations they might have made
943 */
944
945 ext = context->extensions;
946 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
947 if (context->listen_port)
948 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +0800949 while (ext && ext->callback) {
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800950 ext->callback(context, ext, NULL, (enum libwebsocket_extension_callback_reasons)m, NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +0100951 ext++;
952 }
Andy Green3182ece2013-01-20 17:08:31 +0800953#endif
Andy Greena41314f2011-05-23 10:00:03 +0100954
Peter Hinz56885f32011-03-02 22:03:47 +0000955#ifdef WIN32
956#else
957 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +0000958#endif
959
Peter Hinz56885f32011-03-02 22:03:47 +0000960#ifdef LWS_OPENSSL_SUPPORT
961 if (context->ssl_ctx)
962 SSL_CTX_free(context->ssl_ctx);
963 if (context->ssl_client_ctx)
964 SSL_CTX_free(context->ssl_client_ctx);
965#endif
966
967 free(context);
968
969#ifdef WIN32
970 WSACleanup();
971#endif
Andy Green6964bb52011-01-23 16:50:33 +0000972}
973
Andy Greend88146d2013-01-22 12:40:35 +0800974/**
975 * libwebsocket_context_user() - get the user data associated with the whole context
976 * @context: Websocket context
977 *
978 * This returns the optional user allocation that can be attached to
979 * the context the sockets live in at context_create time. It's a way
980 * to let all sockets serviced in the same context share data without
981 * using globals statics in the user code.
982 */
983
984
Alon Levy0291eb32012-10-19 11:21:56 +0200985LWS_EXTERN void *
986libwebsocket_context_user(struct libwebsocket_context *context)
987{
988 return context->user_space;
989}
990
Andy Green6964bb52011-01-23 16:50:33 +0000991/**
992 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +0000993 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +0000994 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
995 * service otherwise block and service immediately, returning
996 * after the timeout if nothing needed service.
997 *
998 * This function deals with any pending websocket traffic, for three
999 * kinds of event. It handles these events on both server and client
1000 * types of connection the same.
1001 *
1002 * 1) Accept new connections to our context's server
1003 *
1004 * 2) Perform pending broadcast writes initiated from other forked
1005 * processes (effectively serializing asynchronous broadcasts)
1006 *
1007 * 3) Call the receive callback for incoming frame data received by
1008 * server or client connections.
1009 *
1010 * You need to call this service function periodically to all the above
1011 * functions to happen; if your application is single-threaded you can
1012 * just call it in your main event loop.
1013 *
1014 * Alternatively you can fork a new process that asynchronously handles
1015 * calling this service in a loop. In that case you are happy if this
1016 * call blocks your thread until it needs to take care of something and
1017 * would call it with a large nonzero timeout. Your loop then takes no
1018 * CPU while there is nothing happening.
1019 *
1020 * If you are calling it in a single-threaded app, you don't want it to
1021 * wait around blocking other things in your loop from happening, so you
1022 * would call it with a timeout_ms of 0, so it returns immediately if
1023 * nothing is pending, or as soon as it services whatever was pending.
1024 */
1025
Andy Greenb45993c2010-12-18 15:13:50 +00001026
Andy Greene92cd172011-01-19 13:11:55 +00001027int
Peter Hinz56885f32011-03-02 22:03:47 +00001028libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001029{
1030 int n;
Andy Greene92cd172011-01-19 13:11:55 +00001031
1032 /* stay dead once we are dead */
1033
Peter Hinz56885f32011-03-02 22:03:47 +00001034 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001035 return 1;
1036
Andy Green0d338332011-02-12 11:57:43 +00001037 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001038
Peter Hinz56885f32011-03-02 22:03:47 +00001039 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green3221f922011-02-12 13:14:11 +00001040 if (n == 0) /* poll timeout */
1041 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001042
Andy Greendfb23042013-01-17 12:26:48 +08001043 if (n < 0)
Andy Green3928f612012-07-20 12:58:38 +08001044 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001045
Andy Greendfb23042013-01-17 12:26:48 +08001046 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001047
Peter Hinz56885f32011-03-02 22:03:47 +00001048 for (n = 0; n < context->fds_count; n++)
1049 if (context->fds[n].revents)
Andy Green3928f612012-07-20 12:58:38 +08001050 if (libwebsocket_service_fd(context,
1051 &context->fds[n]) < 0)
1052 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001053 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001054}
1055
Andy Green3182ece2013-01-20 17:08:31 +08001056#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001057int
1058lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001059 struct libwebsocket *wsi,
1060 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001061 void *v, size_t len)
1062{
1063 int n;
1064 int handled = 0;
1065
1066 /* maybe an extension will take care of it for us */
1067
1068 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1069 if (!wsi->active_extensions[n]->callback)
1070 continue;
1071
1072 handled |= wsi->active_extensions[n]->callback(context,
1073 wsi->active_extensions[n], wsi,
1074 r, wsi->active_extensions_user[n], v, len);
1075 }
1076
1077 return handled;
1078}
1079
1080
1081void *
1082lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001083 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001084{
1085 int n = 0;
1086
Andy Green68b45042011-05-25 21:41:57 +01001087 if (wsi == NULL)
1088 return NULL;
1089
Andy Greena41314f2011-05-23 10:00:03 +01001090 while (n < wsi->count_active_extensions) {
1091 if (wsi->active_extensions[n] != ext) {
1092 n++;
1093 continue;
1094 }
1095 return wsi->active_extensions_user[n];
1096 }
1097
1098 return NULL;
1099}
Andy Green3182ece2013-01-20 17:08:31 +08001100#endif
Andy Greena41314f2011-05-23 10:00:03 +01001101
Andy Green90c7cbc2011-01-27 06:26:52 +00001102/**
1103 * libwebsocket_callback_on_writable() - Request a callback when this socket
1104 * becomes able to be written to without
1105 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001106 *
Peter Hinz56885f32011-03-02 22:03:47 +00001107 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001108 * @wsi: Websocket connection instance to get callback for
1109 */
1110
1111int
Peter Hinz56885f32011-03-02 22:03:47 +00001112libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001113 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001114{
Andy Green3182ece2013-01-20 17:08:31 +08001115#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001116 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001117 int handled = 0;
1118
1119 /* maybe an extension will take care of it for us */
1120
1121 for (n = 0; n < wsi->count_active_extensions; n++) {
1122 if (!wsi->active_extensions[n]->callback)
1123 continue;
1124
1125 handled |= wsi->active_extensions[n]->callback(context,
1126 wsi->active_extensions[n], wsi,
1127 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1128 wsi->active_extensions_user[n], NULL, 0);
1129 }
1130
1131 if (handled)
1132 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001133#endif
Andy Greendfb23042013-01-17 12:26:48 +08001134 if (wsi->position_in_fds_table < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001135 lwsl_err("libwebsocket_callback_on_writable: "
Andy Green6ee372f2012-04-09 15:09:01 +08001136 "failed to find socket %d\n", wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001137 return -1;
1138 }
1139
1140 context->fds[wsi->position_in_fds_table].events |= POLLOUT;
Andy Greena41314f2011-05-23 10:00:03 +01001141
Andy Green3221f922011-02-12 13:14:11 +00001142 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00001143 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001144 LWS_CALLBACK_SET_MODE_POLL_FD,
1145 (void *)(long)wsi->sock, NULL, POLLOUT);
1146
Andy Green90c7cbc2011-01-27 06:26:52 +00001147 return 1;
1148}
1149
1150/**
1151 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1152 * all connections using the given protocol when it
1153 * becomes possible to write to each socket without
1154 * blocking in turn.
1155 *
1156 * @protocol: Protocol whose connections will get callbacks
1157 */
1158
1159int
1160libwebsocket_callback_on_writable_all_protocol(
1161 const struct libwebsocket_protocols *protocol)
1162{
Peter Hinz56885f32011-03-02 22:03:47 +00001163 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001164 int n;
Andy Green0d338332011-02-12 11:57:43 +00001165 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001166
Andy Greendfb23042013-01-17 12:26:48 +08001167 for (n = 0; n < context->fds_count; n++) {
1168 wsi = context->lws_lookup[context->fds[n].fd];
1169 if (!wsi)
1170 continue;
1171 if (wsi->protocol == protocol)
1172 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001173 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001174
1175 return 0;
1176}
1177
Andy Greenbe93fef2011-02-14 20:25:43 +00001178/**
1179 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1180 *
1181 * You will not need this unless you are doing something special
1182 *
1183 * @wsi: Websocket connection instance
1184 * @reason: timeout reason
1185 * @secs: how many seconds
1186 */
1187
1188void
1189libwebsocket_set_timeout(struct libwebsocket *wsi,
1190 enum pending_timeout reason, int secs)
1191{
1192 struct timeval tv;
1193
1194 gettimeofday(&tv, NULL);
1195
1196 wsi->pending_timeout_limit = tv.tv_sec + secs;
1197 wsi->pending_timeout = reason;
1198}
1199
Andy Greena6cbece2011-01-27 20:06:03 +00001200
1201/**
1202 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1203 *
1204 * You will not need this unless you are doing something special
1205 *
1206 * @wsi: Websocket connection instance
1207 */
1208
1209int
1210libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1211{
1212 return wsi->sock;
1213}
1214
Andy Greena1ce6be2013-01-18 11:43:21 +08001215#ifdef LWS_NO_SERVER
1216int
1217_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1218{
1219 return 0;
1220}
1221#else
Andy Green706961d2013-01-17 16:50:35 +08001222int
1223_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1224{
1225 struct libwebsocket_context *context = wsi->protocol->owning_server;
1226 int n;
1227
Andy Green623a98d2013-01-21 11:04:23 +08001228 if (!(wsi->u.ws.rxflow_change_to & 2))
Andy Green706961d2013-01-17 16:50:35 +08001229 return 0;
1230
Andy Green623a98d2013-01-21 11:04:23 +08001231 wsi->u.ws.rxflow_change_to &= ~2;
Andy Green706961d2013-01-17 16:50:35 +08001232
Andy Green623a98d2013-01-21 11:04:23 +08001233 lwsl_info("rxflow: wsi %p change_to %d\n", wsi, wsi->u.ws.rxflow_change_to);
Andy Green706961d2013-01-17 16:50:35 +08001234
1235 /* if we're letting it come again, did we interrupt anything? */
Andy Green623a98d2013-01-21 11:04:23 +08001236 if ((wsi->u.ws.rxflow_change_to & 1) && wsi->u.ws.rxflow_buffer) {
Andy Green706961d2013-01-17 16:50:35 +08001237 n = libwebsocket_interpret_incoming_packet(wsi, NULL, 0);
1238 if (n < 0) {
1239 libwebsocket_close_and_free_session(context, wsi, LWS_CLOSE_STATUS_NOSTATUS);
1240 return -1;
1241 }
1242 if (n)
1243 /* oh he stuck again, do nothing */
1244 return 0;
1245 }
1246
Andy Green623a98d2013-01-21 11:04:23 +08001247 if (wsi->u.ws.rxflow_change_to & 1)
Andy Green706961d2013-01-17 16:50:35 +08001248 context->fds[wsi->position_in_fds_table].events |= POLLIN;
1249 else
1250 context->fds[wsi->position_in_fds_table].events &= ~POLLIN;
1251
Andy Green623a98d2013-01-21 11:04:23 +08001252 if (wsi->u.ws.rxflow_change_to & 1)
Andy Green706961d2013-01-17 16:50:35 +08001253 /* external POLL support via protocol 0 */
1254 context->protocols[0].callback(context, wsi,
1255 LWS_CALLBACK_SET_MODE_POLL_FD,
1256 (void *)(long)wsi->sock, NULL, POLLIN);
1257 else
1258 /* external POLL support via protocol 0 */
1259 context->protocols[0].callback(context, wsi,
1260 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
1261 (void *)(long)wsi->sock, NULL, POLLIN);
1262
1263 return 1;
1264}
Andy Greena1ce6be2013-01-18 11:43:21 +08001265#endif
Andy Green706961d2013-01-17 16:50:35 +08001266
Andy Green90c7cbc2011-01-27 06:26:52 +00001267/**
1268 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1269 * receieved packets.
1270 *
1271 * If the output side of a server process becomes choked, this allows flow
1272 * control for the input side.
1273 *
1274 * @wsi: Websocket connection instance to get callback for
1275 * @enable: 0 = disable read servicing for this connection, 1 = enable
1276 */
1277
1278int
1279libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1280{
Andy Green623a98d2013-01-21 11:04:23 +08001281 wsi->u.ws.rxflow_change_to = 2 | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001282
Andy Green706961d2013-01-17 16:50:35 +08001283 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001284}
1285
Andy Green706961d2013-01-17 16:50:35 +08001286
Andy Green2ac5a6f2011-01-28 10:00:18 +00001287/**
1288 * libwebsocket_canonical_hostname() - returns this host's hostname
1289 *
1290 * This is typically used by client code to fill in the host parameter
1291 * when making a client connection. You can only call it after the context
1292 * has been created.
1293 *
Peter Hinz56885f32011-03-02 22:03:47 +00001294 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001295 */
1296
1297
1298extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001299libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001300{
Peter Hinz56885f32011-03-02 22:03:47 +00001301 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001302}
1303
1304
Andy Green90c7cbc2011-01-27 06:26:52 +00001305static void sigpipe_handler(int x)
1306{
1307}
1308
Andy Green6901cb32011-02-21 08:06:47 +00001309#ifdef LWS_OPENSSL_SUPPORT
1310static int
1311OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1312{
1313
1314 SSL *ssl;
1315 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001316 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001317
1318 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1319 SSL_get_ex_data_X509_STORE_CTX_idx());
1320
1321 /*
Andy Green2e24da02011-03-05 16:12:04 +00001322 * !!! nasty openssl requires the index to come as a library-scope
1323 * static
Andy Green6901cb32011-02-21 08:06:47 +00001324 */
Andy Green2e24da02011-03-05 16:12:04 +00001325 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001326
Peter Hinz56885f32011-03-02 22:03:47 +00001327 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001328 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1329 x509_ctx, ssl, preverify_ok);
1330
1331 /* convert return code from 0 = OK to 1 = OK */
1332
1333 if (!n)
1334 n = 1;
1335 else
1336 n = 0;
1337
1338 return n;
1339}
1340#endif
1341
Andy Green706961d2013-01-17 16:50:35 +08001342int user_callback_handle_rxflow(callback_function callback_function,
1343 struct libwebsocket_context * context,
1344 struct libwebsocket *wsi,
1345 enum libwebsocket_callback_reasons reason, void *user,
1346 void *in, size_t len)
1347{
1348 int n;
1349
1350 n = callback_function(context, wsi, reason, user, in, len);
1351 if (n < 0)
1352 return n;
1353
1354 _libwebsocket_rx_flow_control(wsi);
1355
1356 return 0;
1357}
1358
Andy Greenb45993c2010-12-18 15:13:50 +00001359
Andy Greenab990e42010-10-31 12:42:52 +00001360/**
Andy Green4739e5c2011-01-22 12:51:57 +00001361 * libwebsocket_create_context() - Create the websocket handler
1362 * @port: Port to listen on... you can use 0 to suppress listening on
Andy Green6964bb52011-01-23 16:50:33 +00001363 * any port, that's what you want if you are not running a
1364 * websocket server at all but just using it as a client
Peter Hinz56885f32011-03-02 22:03:47 +00001365 * @interf: NULL to bind the listen socket to all interfaces, or the
Andy Green32375b72011-02-19 08:32:53 +00001366 * interface name, eg, "eth2"
Andy Green4f3943a2010-11-12 10:44:16 +00001367 * @protocols: Array of structures listing supported protocols and a protocol-
Andy Green8f037e42010-12-19 22:13:26 +00001368 * specific callback for each one. The list is ended with an
1369 * entry that has a NULL callback pointer.
Andy Green6964bb52011-01-23 16:50:33 +00001370 * It's not const because we write the owning_server member
Andy Greenc5114822011-03-06 10:29:35 +00001371 * @extensions: NULL or array of libwebsocket_extension structs listing the
Andy Green3182ece2013-01-20 17:08:31 +08001372 * extensions this context supports. If you configured with
1373 * --without-extensions, you should give NULL here.
Andy Green3faa9c72010-11-08 17:03:03 +00001374 * @ssl_cert_filepath: If libwebsockets was compiled to use ssl, and you want
Andy Green8f037e42010-12-19 22:13:26 +00001375 * to listen using SSL, set to the filepath to fetch the
1376 * server cert from, otherwise NULL for unencrypted
Andy Green3faa9c72010-11-08 17:03:03 +00001377 * @ssl_private_key_filepath: filepath to private key if wanting SSL mode,
Andy Green8f037e42010-12-19 22:13:26 +00001378 * else ignored
David Galeano2f82be82013-01-09 16:25:54 +08001379 * @ssl_ca_filepath: CA certificate filepath or NULL
Andy Green3faa9c72010-11-08 17:03:03 +00001380 * @gid: group id to change to after setting listen socket, or -1.
1381 * @uid: user id to change to after setting listen socket, or -1.
Andy Greenbfb051f2011-02-09 08:49:14 +00001382 * @options: 0, or LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK
Andy Green15e31f32012-10-19 18:36:28 +08001383 * @user: optional user pointer that can be recovered via the context
1384 * pointer using libwebsocket_context_user
Andy Green05464c62010-11-12 10:44:18 +00001385 *
Andy Green8f037e42010-12-19 22:13:26 +00001386 * This function creates the listening socket and takes care
1387 * of all initialization in one step.
1388 *
Andy Greene92cd172011-01-19 13:11:55 +00001389 * After initialization, it returns a struct libwebsocket_context * that
1390 * represents this server. After calling, user code needs to take care
1391 * of calling libwebsocket_service() with the context pointer to get the
1392 * server's sockets serviced. This can be done in the same process context
1393 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001394 *
Andy Green8f037e42010-12-19 22:13:26 +00001395 * The protocol callback functions are called for a handful of events
1396 * including http requests coming in, websocket connections becoming
1397 * established, and data arriving; it's also called periodically to allow
1398 * async transmission.
1399 *
1400 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1401 * at that time websocket protocol has not been negotiated. Other
1402 * protocols after the first one never see any HTTP callack activity.
1403 *
1404 * The server created is a simple http server by default; part of the
1405 * websocket standard is upgrading this http connection to a websocket one.
1406 *
1407 * This allows the same server to provide files like scripts and favicon /
1408 * images or whatever over http and dynamic data over websockets all in
1409 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001410 */
Andy Green4ea60062010-10-30 12:15:07 +01001411
Andy Greene92cd172011-01-19 13:11:55 +00001412struct libwebsocket_context *
Peter Hinz56885f32011-03-02 22:03:47 +00001413libwebsocket_create_context(int port, const char *interf,
Andy Greenb45993c2010-12-18 15:13:50 +00001414 struct libwebsocket_protocols *protocols,
Andy Greend6e09112011-03-05 16:12:15 +00001415 struct libwebsocket_extension *extensions,
Andy Green8f037e42010-12-19 22:13:26 +00001416 const char *ssl_cert_filepath,
1417 const char *ssl_private_key_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08001418 const char *ssl_ca_filepath,
Alon Levy0291eb32012-10-19 11:21:56 +02001419 int gid, int uid, unsigned int options,
David Galeano2f82be82013-01-09 16:25:54 +08001420 void *user)
Andy Greenff95d7a2010-10-28 22:36:01 +01001421{
1422 int n;
Andy Greenf5bc1302013-01-21 09:09:52 +08001423 struct sockaddr_in serv_addr;
Andy Green251f6fa2010-11-03 11:13:06 +00001424 int opt = 1;
Peter Hinz56885f32011-03-02 22:03:47 +00001425 struct libwebsocket_context *context = NULL;
Andy Greenf5bc1302013-01-21 09:09:52 +08001426#ifndef LWS_NO_FORK
Andy Greenb45993c2010-12-18 15:13:50 +00001427 unsigned int slen;
Andy Greenf5bc1302013-01-21 09:09:52 +08001428 struct sockaddr_in cli_addr;
1429 int fd;
1430#endif
Andy Green9659f372011-01-27 22:01:43 +00001431 char *p;
Andy Green0d338332011-02-12 11:57:43 +00001432 struct libwebsocket *wsi;
Andy Green3182ece2013-01-20 17:08:31 +08001433#ifndef LWS_NO_EXTENSIONS
1434 int m;
1435#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001436
Andy Green3faa9c72010-11-08 17:03:03 +00001437#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001438 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001439 char ssl_err_buf[512];
Andy Green3faa9c72010-11-08 17:03:03 +00001440#endif
1441
Andy Greenb3a614a2013-01-19 13:08:17 +08001442 lwsl_notice("Initial logging level %d\n", log_level);
Andy Greenc0d6b632013-01-12 23:42:17 +08001443 lwsl_info(" LWS_MAX_HEADER_NAME_LENGTH: %u\n", LWS_MAX_HEADER_NAME_LENGTH);
1444 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
1445 lwsl_info(" LWS_INITIAL_HDR_ALLOC: %u\n", LWS_INITIAL_HDR_ALLOC);
1446 lwsl_info(" LWS_ADDITIONAL_HDR_ALLOC: %u\n", LWS_ADDITIONAL_HDR_ALLOC);
1447 lwsl_info(" MAX_USER_RX_BUFFER: %u\n", MAX_USER_RX_BUFFER);
1448 lwsl_info(" MAX_BROADCAST_PAYLOAD: %u\n", MAX_BROADCAST_PAYLOAD);
1449 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001450#ifndef LWS_NO_EXTENSIONS
Andy Greenc0d6b632013-01-12 23:42:17 +08001451 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n", LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001452#else
1453 lwsl_notice(" Configured without extension support\n");
1454#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001455 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1456 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
1457 lwsl_info(" CIPHERS_LIST_STRING: '%s'\n", CIPHERS_LIST_STRING);
1458 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1459 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001460
Peter Hinz56885f32011-03-02 22:03:47 +00001461#ifdef _WIN32
1462 {
1463 WORD wVersionRequested;
1464 WSADATA wsaData;
1465 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001466 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001467
1468 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1469 wVersionRequested = MAKEWORD(2, 2);
1470
1471 err = WSAStartup(wVersionRequested, &wsaData);
1472 if (err != 0) {
1473 /* Tell the user that we could not find a usable */
1474 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001475 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001476 return NULL;
1477 }
David Galeano7b11fec2011-10-04 19:55:18 +08001478
Andy Green6ee372f2012-04-09 15:09:01 +08001479 /* default to a poll() made out of select() */
1480 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001481
Andy Green6ee372f2012-04-09 15:09:01 +08001482 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001483 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001484 if (wsdll)
1485 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Peter Hinz56885f32011-03-02 22:03:47 +00001486 }
1487#endif
1488
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001489 context = (struct libwebsocket_context *) malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001490 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001491 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001492 return NULL;
1493 }
Andy Green35f332b2013-01-21 13:06:38 +08001494#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001495 extern int pid_daemon;
1496 context->started_with_parent = pid_daemon;
1497 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1498#endif
1499
Peter Hinz56885f32011-03-02 22:03:47 +00001500 context->protocols = protocols;
1501 context->listen_port = port;
1502 context->http_proxy_port = 0;
1503 context->http_proxy_address[0] = '\0';
1504 context->options = options;
Andy Greendfb23042013-01-17 12:26:48 +08001505 /* to reduce this allocation, */
1506 context->max_fds = getdtablesize();
Andy Greenb3a614a2013-01-19 13:08:17 +08001507 lwsl_notice(" max fd tracked: %u\n", context->max_fds);
Andy Greena17c6922013-01-20 20:21:54 +08001508 lwsl_notice(" static allocation: %u bytes\n",
1509 (sizeof(struct pollfd) * context->max_fds) +
1510 (sizeof(struct libwebsocket *) * context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08001511
1512 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) * context->max_fds);
1513 if (context->fds == NULL) {
1514 lwsl_err("Unable to allocate fds array for %d connections\n", context->max_fds);
1515 free(context);
1516 return NULL;
1517 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08001518 context->lws_lookup = (struct libwebsocket **)malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001519 if (context->lws_lookup == NULL) {
1520 lwsl_err("Unable to allocate lws_lookup array for %d connections\n", context->max_fds);
1521 free(context->fds);
1522 free(context);
1523 return NULL;
1524 }
Andy Greena17c6922013-01-20 20:21:54 +08001525
Peter Hinz56885f32011-03-02 22:03:47 +00001526 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001527#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00001528 context->extensions = extensions;
Andy Green3182ece2013-01-20 17:08:31 +08001529#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08001530 context->last_timeout_check_s = 0;
Andy Greendfb23042013-01-17 12:26:48 +08001531 context->user_space = user;
Andy Green9659f372011-01-27 22:01:43 +00001532
Peter Hinz56885f32011-03-02 22:03:47 +00001533#ifdef WIN32
1534 context->fd_random = 0;
1535#else
1536 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
1537 if (context->fd_random < 0) {
Andy Greendfb23042013-01-17 12:26:48 +08001538 free(context);
Andy Green43db0452013-01-10 19:50:35 +08001539 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001540 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Andy Green44eee682011-02-10 09:32:24 +00001541 return NULL;
1542 }
Peter Hinz56885f32011-03-02 22:03:47 +00001543#endif
Andy Green44eee682011-02-10 09:32:24 +00001544
Peter Hinz56885f32011-03-02 22:03:47 +00001545#ifdef LWS_OPENSSL_SUPPORT
1546 context->use_ssl = 0;
1547 context->ssl_ctx = NULL;
1548 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00001549 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001550#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00001551
Andy Greena1ce6be2013-01-18 11:43:21 +08001552 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08001553
Andy Greena1ce6be2013-01-18 11:43:21 +08001554#ifndef LWS_NO_SERVER
1555 if (!(options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
1556 struct sockaddr sa;
1557 char hostname[1024] = "";
Andy Green788c4a82012-10-22 12:29:57 +01001558
1559 /* find canonical hostname */
1560
1561 hostname[(sizeof hostname) - 1] = '\0';
1562 memset(&sa, 0, sizeof(sa));
1563 sa.sa_family = AF_INET;
1564 sa.sa_data[(sizeof sa.sa_data) - 1] = '\0';
1565 gethostname(hostname, (sizeof hostname) - 1);
1566
1567 n = 0;
1568
David Galeanoed3c8402013-01-10 10:45:24 +08001569 if (strlen(hostname) < sizeof(sa.sa_data) - 1) {
Andy Green788c4a82012-10-22 12:29:57 +01001570 strcpy(sa.sa_data, hostname);
Andy Green43db0452013-01-10 19:50:35 +08001571 // lwsl_debug("my host name is %s\n", sa.sa_data);
Andy Green788c4a82012-10-22 12:29:57 +01001572 n = getnameinfo(&sa, sizeof(sa), hostname,
1573 (sizeof hostname) - 1, NULL, 0, 0);
1574 }
1575
1576 if (!n) {
1577 strncpy(context->canonical_hostname, hostname,
1578 sizeof context->canonical_hostname - 1);
1579 context->canonical_hostname[
1580 sizeof context->canonical_hostname - 1] = '\0';
1581 } else
1582 strncpy(context->canonical_hostname, hostname,
1583 sizeof context->canonical_hostname - 1);
1584
Andy Greenb3a614a2013-01-19 13:08:17 +08001585 lwsl_notice(" canonical_hostname = %s\n", context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08001586 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001587#endif
Andy Greena69f0512012-05-03 12:32:38 +08001588
Andy Green9659f372011-01-27 22:01:43 +00001589 /* split the proxy ads:port if given */
1590
1591 p = getenv("http_proxy");
1592 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00001593 strncpy(context->http_proxy_address, p,
Andy Green6ee372f2012-04-09 15:09:01 +08001594 sizeof context->http_proxy_address - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00001595 context->http_proxy_address[
1596 sizeof context->http_proxy_address - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00001597
Peter Hinz56885f32011-03-02 22:03:47 +00001598 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00001599 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001600 lwsl_err("http_proxy needs to be ads:port\n");
Andy Green9659f372011-01-27 22:01:43 +00001601 return NULL;
1602 }
1603 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00001604 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00001605
Andy Greenb3a614a2013-01-19 13:08:17 +08001606 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001607 context->http_proxy_address,
1608 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00001609 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001610
Andy Greena1ce6be2013-01-18 11:43:21 +08001611#ifndef LWS_NO_SERVER
Andy Green90c7cbc2011-01-27 06:26:52 +00001612 if (port) {
1613
Andy Green3faa9c72010-11-08 17:03:03 +00001614#ifdef LWS_OPENSSL_SUPPORT
Peter Hinz56885f32011-03-02 22:03:47 +00001615 context->use_ssl = ssl_cert_filepath != NULL &&
Andy Green90c7cbc2011-01-27 06:26:52 +00001616 ssl_private_key_filepath != NULL;
Peter Hinz56885f32011-03-02 22:03:47 +00001617 if (context->use_ssl)
Andy Greenb3a614a2013-01-19 13:08:17 +08001618 lwsl_notice(" Compiled with SSL support, using it\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001619 else
Andy Greenb3a614a2013-01-19 13:08:17 +08001620 lwsl_notice(" Compiled with SSL support, not using it\n");
Andy Green3faa9c72010-11-08 17:03:03 +00001621
Andy Green90c7cbc2011-01-27 06:26:52 +00001622#else
1623 if (ssl_cert_filepath != NULL &&
1624 ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08001625 lwsl_notice(" Not compiled for OpenSSl support!\n");
Andy Greene92cd172011-01-19 13:11:55 +00001626 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +00001627 }
Andy Greenb3a614a2013-01-19 13:08:17 +08001628 lwsl_notice(" Compiled without SSL support, "
Andy Green90c7cbc2011-01-27 06:26:52 +00001629 "serving unencrypted\n");
1630#endif
Andy Greena17c6922013-01-20 20:21:54 +08001631
1632 lwsl_notice(" per-connection allocation: %u + headers\n", sizeof(struct libwebsocket));
Andy Green90c7cbc2011-01-27 06:26:52 +00001633 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001634#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001635
1636 /* ignore SIGPIPE */
Peter Hinz56885f32011-03-02 22:03:47 +00001637#ifdef WIN32
1638#else
Andy Green90c7cbc2011-01-27 06:26:52 +00001639 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00001640#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001641
1642
1643#ifdef LWS_OPENSSL_SUPPORT
1644
1645 /* basic openssl init */
1646
1647 SSL_library_init();
1648
1649 OpenSSL_add_all_algorithms();
1650 SSL_load_error_strings();
1651
Andy Green2e24da02011-03-05 16:12:04 +00001652 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00001653 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
1654
Andy Green90c7cbc2011-01-27 06:26:52 +00001655 /*
1656 * Firefox insists on SSLv23 not SSLv3
1657 * Konq disables SSLv2 by default now, SSLv23 works
1658 */
1659
1660 method = (SSL_METHOD *)SSLv23_server_method();
1661 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001662 lwsl_err("problem creating ssl method: %s\n",
Andy Green90c7cbc2011-01-27 06:26:52 +00001663 ERR_error_string(ERR_get_error(), ssl_err_buf));
1664 return NULL;
1665 }
Peter Hinz56885f32011-03-02 22:03:47 +00001666 context->ssl_ctx = SSL_CTX_new(method); /* create context */
1667 if (!context->ssl_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001668 lwsl_err("problem creating ssl context: %s\n",
Andy Green90c7cbc2011-01-27 06:26:52 +00001669 ERR_error_string(ERR_get_error(), ssl_err_buf));
1670 return NULL;
1671 }
1672
David Galeanocc148e42013-01-10 10:18:59 +08001673#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001674 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001675#endif
David Galeano77a677c2013-01-10 10:14:12 +08001676 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
David Galeanof177f2a2013-01-10 10:15:19 +08001677 SSL_CTX_set_cipher_list(context->ssl_ctx, CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001678
Andy Greena1ce6be2013-01-18 11:43:21 +08001679#ifndef LWS_NO_CLIENT
1680
Andy Green90c7cbc2011-01-27 06:26:52 +00001681 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08001682
1683 if (port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00001684 method = (SSL_METHOD *)SSLv23_client_method();
1685 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001686 lwsl_err("problem creating ssl method: %s\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001687 ERR_error_string(ERR_get_error(), ssl_err_buf));
1688 return NULL;
1689 }
1690 /* create context */
1691 context->ssl_client_ctx = SSL_CTX_new(method);
1692 if (!context->ssl_client_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001693 lwsl_err("problem creating ssl context: %s\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001694 ERR_error_string(ERR_get_error(), ssl_err_buf));
1695 return NULL;
1696 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001697
David Galeanocc148e42013-01-10 10:18:59 +08001698#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001699 SSL_CTX_set_options(context->ssl_client_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001700#endif
David Galeano77a677c2013-01-10 10:14:12 +08001701 SSL_CTX_set_options(context->ssl_client_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
David Galeanof177f2a2013-01-10 10:15:19 +08001702 SSL_CTX_set_cipher_list(context->ssl_client_ctx, CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001703
Peter Hinz56885f32011-03-02 22:03:47 +00001704 /* openssl init for cert verification (for client sockets) */
David Galeano2f82be82013-01-09 16:25:54 +08001705 if (!ssl_ca_filepath) {
1706 if (!SSL_CTX_load_verify_locations(
1707 context->ssl_client_ctx, NULL,
1708 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08001709 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08001710 "Unable to load SSL Client certs from %s "
1711 "(set by --with-client-cert-dir= in configure) -- "
1712 " client ssl isn't going to work",
1713 LWS_OPENSSL_CLIENT_CERTS);
1714 } else
1715 if (!SSL_CTX_load_verify_locations(
1716 context->ssl_client_ctx, ssl_ca_filepath,
1717 NULL))
Andy Green43db0452013-01-10 19:50:35 +08001718 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08001719 "Unable to load SSL Client certs "
1720 "file from %s -- client ssl isn't "
1721 "going to work", ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00001722
1723 /*
1724 * callback allowing user code to load extra verification certs
1725 * helping the client to verify server identity
1726 */
1727
1728 context->protocols[0].callback(context, NULL,
1729 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
1730 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00001731 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001732#endif
Andy Green6ee372f2012-04-09 15:09:01 +08001733
Andy Greenc6bf2c22011-02-20 11:10:47 +00001734 /* as a server, are we requiring clients to identify themselves? */
1735
1736 if (options & LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
1737
1738 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08001739
Peter Hinz56885f32011-03-02 22:03:47 +00001740 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00001741 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1742 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001743
1744 /*
1745 * give user code a chance to load certs into the server
1746 * allowing it to verify incoming client certs
1747 */
1748
Peter Hinz56885f32011-03-02 22:03:47 +00001749 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00001750 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00001751 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001752 }
1753
Peter Hinz56885f32011-03-02 22:03:47 +00001754 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001755
1756 /* openssl init for server sockets */
1757
Andy Green3faa9c72010-11-08 17:03:03 +00001758 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08001759 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
1760 ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00001761 if (n != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001762 lwsl_err("problem getting cert '%s': %s\n",
Andy Green3faa9c72010-11-08 17:03:03 +00001763 ssl_cert_filepath,
1764 ERR_error_string(ERR_get_error(), ssl_err_buf));
Andy Greene92cd172011-01-19 13:11:55 +00001765 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +00001766 }
1767 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00001768 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
1769 ssl_private_key_filepath, SSL_FILETYPE_PEM) != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001770 lwsl_err("ssl problem getting key '%s': %s\n",
Andy Green018d8eb2010-11-08 21:04:23 +00001771 ssl_private_key_filepath,
1772 ERR_error_string(ERR_get_error(), ssl_err_buf));
Andy Greene92cd172011-01-19 13:11:55 +00001773 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +00001774 }
1775 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00001776 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08001777 lwsl_err("Private SSL key doesn't match cert\n");
Andy Greene92cd172011-01-19 13:11:55 +00001778 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +00001779 }
1780
1781 /* SSL is happy and has a cert it's content with */
1782 }
1783#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001784
Andy Greendf736162011-01-18 15:39:02 +00001785 /* selftest */
1786
1787 if (lws_b64_selftest())
Andy Greene92cd172011-01-19 13:11:55 +00001788 return NULL;
Andy Greendf736162011-01-18 15:39:02 +00001789
Andy Greena1ce6be2013-01-18 11:43:21 +08001790#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00001791 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00001792
Andy Green4739e5c2011-01-22 12:51:57 +00001793 if (port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08001794 extern int interface_to_sa(const char *ifname, struct sockaddr_in *addr, size_t addrlen);
1795 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00001796
Andy Green4739e5c2011-01-22 12:51:57 +00001797 sockfd = socket(AF_INET, SOCK_STREAM, 0);
1798 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08001799 lwsl_err("ERROR opening socket\n");
Andy Green4739e5c2011-01-22 12:51:57 +00001800 return NULL;
1801 }
Andy Green775c0dd2010-10-29 14:15:22 +01001802
Andy Green4739e5c2011-01-22 12:51:57 +00001803 /* allow us to restart even if old sockets in TIME_WAIT */
Andy Green6ee372f2012-04-09 15:09:01 +08001804 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
1805 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00001806
1807 /* Disable Nagle */
1808 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08001809 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
1810 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00001811
Andy Greene2160712013-01-28 12:19:10 +08001812 fcntl(sockfd, F_SETFL, O_NONBLOCK);
1813
Andy Green4739e5c2011-01-22 12:51:57 +00001814 bzero((char *) &serv_addr, sizeof(serv_addr));
1815 serv_addr.sin_family = AF_INET;
Peter Hinz56885f32011-03-02 22:03:47 +00001816 if (interf == NULL)
Andy Green32375b72011-02-19 08:32:53 +00001817 serv_addr.sin_addr.s_addr = INADDR_ANY;
1818 else
Peter Hinz56885f32011-03-02 22:03:47 +00001819 interface_to_sa(interf, &serv_addr,
Andy Green32375b72011-02-19 08:32:53 +00001820 sizeof(serv_addr));
Andy Green4739e5c2011-01-22 12:51:57 +00001821 serv_addr.sin_port = htons(port);
1822
1823 n = bind(sockfd, (struct sockaddr *) &serv_addr,
1824 sizeof(serv_addr));
1825 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001826 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green8f037e42010-12-19 22:13:26 +00001827 port, n, errno);
Andy Green41c58032013-01-12 13:21:08 +08001828 close(sockfd);
Andy Green4739e5c2011-01-22 12:51:57 +00001829 return NULL;
1830 }
Andy Green0d338332011-02-12 11:57:43 +00001831
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001832 wsi = (struct libwebsocket *)malloc(sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08001833 if (wsi == NULL) {
1834 lwsl_err("Out of mem\n");
1835 close(sockfd);
1836 return NULL;
1837 }
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001838 memset(wsi, 0, sizeof (struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00001839 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08001840#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00001841 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001842#endif
Andy Green0d338332011-02-12 11:57:43 +00001843 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08001844
1845 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001846
Andy Green65b0e912013-01-16 07:59:47 +08001847 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
1848 context->listen_service_count = 0;
1849 context->listen_service_fd = sockfd;
1850
Andy Greena824d182013-01-15 20:52:29 +08001851 listen(sockfd, LWS_SOMAXCONN);
Andy Greenb3a614a2013-01-19 13:08:17 +08001852 lwsl_notice(" Listening on port %d\n", port);
Andy Green8f037e42010-12-19 22:13:26 +00001853 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001854#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001855
Andy Green6ee372f2012-04-09 15:09:01 +08001856 /*
1857 * drop any root privs for this process
1858 * to listen on port < 1023 we would have needed root, but now we are
1859 * listening, we don't want the power for anything else
1860 */
Peter Hinz56885f32011-03-02 22:03:47 +00001861#ifdef WIN32
1862#else
Andy Green3faa9c72010-11-08 17:03:03 +00001863 if (gid != -1)
1864 if (setgid(gid))
Andy Green43db0452013-01-10 19:50:35 +08001865 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green3faa9c72010-11-08 17:03:03 +00001866 if (uid != -1)
1867 if (setuid(uid))
Andy Green43db0452013-01-10 19:50:35 +08001868 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00001869#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001870
1871 /* set up our internal broadcast trigger sockets per-protocol */
1872
Peter Hinz56885f32011-03-02 22:03:47 +00001873 for (context->count_protocols = 0;
1874 protocols[context->count_protocols].callback;
1875 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01001876
Andy Green43db0452013-01-10 19:50:35 +08001877 lwsl_parser(" Protocol: %s\n",
1878 protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01001879
Peter Hinz56885f32011-03-02 22:03:47 +00001880 protocols[context->count_protocols].owning_server = context;
1881 protocols[context->count_protocols].protocol_index =
1882 context->count_protocols;
Andy Greenb45993c2010-12-18 15:13:50 +00001883
Andy Greenf5bc1302013-01-21 09:09:52 +08001884#ifndef LWS_NO_FORK
Andy Greenb45993c2010-12-18 15:13:50 +00001885 fd = socket(AF_INET, SOCK_STREAM, 0);
1886 if (fd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08001887 lwsl_err("ERROR opening socket\n");
Andy Greene92cd172011-01-19 13:11:55 +00001888 return NULL;
Andy Greenb45993c2010-12-18 15:13:50 +00001889 }
Andy Green8f037e42010-12-19 22:13:26 +00001890
Andy Greenb45993c2010-12-18 15:13:50 +00001891 /* allow us to restart even if old sockets in TIME_WAIT */
Andy Green6ee372f2012-04-09 15:09:01 +08001892 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&opt,
1893 sizeof(opt));
Andy Greenb45993c2010-12-18 15:13:50 +00001894
1895 bzero((char *) &serv_addr, sizeof(serv_addr));
1896 serv_addr.sin_family = AF_INET;
1897 serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
1898 serv_addr.sin_port = 0; /* pick the port for us */
1899
1900 n = bind(fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
1901 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001902 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Greenb45993c2010-12-18 15:13:50 +00001903 port, n, errno);
Andy Greene92cd172011-01-19 13:11:55 +00001904 return NULL;
Andy Greenb45993c2010-12-18 15:13:50 +00001905 }
1906
1907 slen = sizeof cli_addr;
1908 n = getsockname(fd, (struct sockaddr *)&cli_addr, &slen);
1909 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001910 lwsl_err("getsockname failed\n");
Andy Greene92cd172011-01-19 13:11:55 +00001911 return NULL;
Andy Greenb45993c2010-12-18 15:13:50 +00001912 }
Peter Hinz56885f32011-03-02 22:03:47 +00001913 protocols[context->count_protocols].broadcast_socket_port =
Andy Greenb45993c2010-12-18 15:13:50 +00001914 ntohs(cli_addr.sin_port);
1915 listen(fd, 5);
1916
Andy Green43db0452013-01-10 19:50:35 +08001917 lwsl_debug(" Protocol %s broadcast socket %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001918 protocols[context->count_protocols].name,
Andy Greenb45993c2010-12-18 15:13:50 +00001919 ntohs(cli_addr.sin_port));
1920
Andy Green0d338332011-02-12 11:57:43 +00001921 /* dummy wsi per broadcast proxy socket */
1922
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001923 wsi = (struct libwebsocket *)malloc(sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08001924 if (wsi == NULL) {
1925 lwsl_err("Out of mem\n");
1926 close(fd);
1927 return NULL;
1928 }
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001929 memset(wsi, 0, sizeof (struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00001930 wsi->sock = fd;
1931 wsi->mode = LWS_CONNMODE_BROADCAST_PROXY_LISTENER;
Andy Green3182ece2013-01-20 17:08:31 +08001932#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00001933 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001934#endif
Andy Green0d338332011-02-12 11:57:43 +00001935 /* note which protocol we are proxying */
Peter Hinz56885f32011-03-02 22:03:47 +00001936 wsi->protocol_index_for_broadcast_proxy =
1937 context->count_protocols;
Andy Green0d338332011-02-12 11:57:43 +00001938
Andy Greendfb23042013-01-17 12:26:48 +08001939 insert_wsi_socket_into_fds(context, wsi);
Andy Greenf5bc1302013-01-21 09:09:52 +08001940#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001941 }
Andy Greenf5bc1302013-01-21 09:09:52 +08001942
Andy Green3182ece2013-01-20 17:08:31 +08001943#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001944 /*
1945 * give all extensions a chance to create any per-context
1946 * allocations they need
1947 */
1948
1949 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
1950 if (port)
1951 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andrew Chambersd5512172012-05-20 08:17:09 +08001952
1953 if (extensions) {
1954 while (extensions->callback) {
Andy Green43db0452013-01-10 19:50:35 +08001955 lwsl_ext(" Extension: %s\n", extensions->name);
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001956 extensions->callback(context, extensions, NULL,
1957 (enum libwebsocket_extension_callback_reasons)m,
1958 NULL, NULL, 0);
Andrew Chambersd5512172012-05-20 08:17:09 +08001959 extensions++;
1960 }
Andy Greena41314f2011-05-23 10:00:03 +01001961 }
Andy Green3182ece2013-01-20 17:08:31 +08001962#endif
Peter Hinz56885f32011-03-02 22:03:47 +00001963 return context;
Andy Greene92cd172011-01-19 13:11:55 +00001964}
Andy Greenb45993c2010-12-18 15:13:50 +00001965
Andy Green4739e5c2011-01-22 12:51:57 +00001966
Andy Greened11a022011-01-20 10:23:50 +00001967#ifndef LWS_NO_FORK
1968
Andy Greene92cd172011-01-19 13:11:55 +00001969/**
1970 * libwebsockets_fork_service_loop() - Optional helper function forks off
1971 * a process for the websocket server loop.
Andy Green6964bb52011-01-23 16:50:33 +00001972 * You don't have to use this but if not, you
1973 * have to make sure you are calling
1974 * libwebsocket_service periodically to service
1975 * the websocket traffic
Peter Hinz56885f32011-03-02 22:03:47 +00001976 * @context: server context returned by creation function
Andy Greene92cd172011-01-19 13:11:55 +00001977 */
Andy Greenb45993c2010-12-18 15:13:50 +00001978
Andy Greene92cd172011-01-19 13:11:55 +00001979int
Peter Hinz56885f32011-03-02 22:03:47 +00001980libwebsockets_fork_service_loop(struct libwebsocket_context *context)
Andy Greene92cd172011-01-19 13:11:55 +00001981{
Andy Greene92cd172011-01-19 13:11:55 +00001982 int n;
Andy Greenb45993c2010-12-18 15:13:50 +00001983
Andy Greened11a022011-01-20 10:23:50 +00001984 n = fork();
1985 if (n < 0)
1986 return n;
1987
Andy Greenfd6764a2013-01-19 11:11:42 +08001988 if (n) {
Andy Greened11a022011-01-20 10:23:50 +00001989
1990 /* main process context */
1991
Andy Greene92cd172011-01-19 13:11:55 +00001992 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +00001993 }
1994
Artem Baguinski91531662011-12-14 22:14:03 +01001995#ifdef HAVE_SYS_PRCTL_H
Andy Greenb45993c2010-12-18 15:13:50 +00001996 /* we want a SIGHUP when our parent goes down */
Andy Greenfd6764a2013-01-19 11:11:42 +08001997 signal(SIGHUP, SIG_DFL);
Andy Greenb45993c2010-12-18 15:13:50 +00001998 prctl(PR_SET_PDEATHSIG, SIGHUP);
Artem Baguinski91531662011-12-14 22:14:03 +01001999#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002000
2001 /* in this forked process, sit and service websocket connections */
Andy Green8f037e42010-12-19 22:13:26 +00002002
Artem Baguinski91531662011-12-14 22:14:03 +01002003 while (1) {
Peter Hinz56885f32011-03-02 22:03:47 +00002004 if (libwebsocket_service(context, 1000))
Andy Green3928f612012-07-20 12:58:38 +08002005 break;
Andy Green5e8967a2012-10-17 20:10:44 +08002006//#ifndef HAVE_SYS_PRCTL_H
Artem Baguinski91531662011-12-14 22:14:03 +01002007/*
2008 * on systems without prctl() (i.e. anything but linux) we can notice that our
2009 * parent is dead if getppid() returns 1. FIXME apparently this is not true for
2010 * solaris, could remember ppid right after fork and wait for it to change.
2011 */
2012
Andy Green24cba922013-01-19 13:56:10 +08002013 /* if our parent went down, don't linger around */
2014 if (context->started_with_parent && kill(context->started_with_parent, 0) < 0)
2015 kill(getpid(), SIGTERM);
2016
2017 if (getppid() == 1)
2018 break;
Andy Green5e8967a2012-10-17 20:10:44 +08002019//#endif
Andy Green24cba922013-01-19 13:56:10 +08002020 }
Artem Baguinski91531662011-12-14 22:14:03 +01002021
Andy Green8f037e42010-12-19 22:13:26 +00002022
Andy Green3928f612012-07-20 12:58:38 +08002023 return 1;
Andy Greenff95d7a2010-10-28 22:36:01 +01002024}
2025
Andy Greened11a022011-01-20 10:23:50 +00002026#endif
2027
Andy Greenb45993c2010-12-18 15:13:50 +00002028/**
2029 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002030 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002031 * @wsi: pointer to struct websocket you want to know the protocol of
2032 *
Andy Green8f037e42010-12-19 22:13:26 +00002033 *
2034 * This is useful to get the protocol to broadcast back to from inside
Andy Greenb45993c2010-12-18 15:13:50 +00002035 * the callback.
2036 */
Andy Greenab990e42010-10-31 12:42:52 +00002037
Andy Greenb45993c2010-12-18 15:13:50 +00002038const struct libwebsocket_protocols *
2039libwebsockets_get_protocol(struct libwebsocket *wsi)
2040{
2041 return wsi->protocol;
2042}
2043
2044/**
Andy Green52f28ce2013-01-25 17:34:15 +08002045 * libwebsockets_broadcast() - Sends a buffer to tx callback for all connections of given protocol from single thread
2046 *
Andy Greenb45993c2010-12-18 15:13:50 +00002047 * @protocol: pointer to the protocol you will broadcast to all members of
2048 * @buf: buffer containing the data to be broadcase. NOTE: this has to be
Andy Green8f037e42010-12-19 22:13:26 +00002049 * allocated with LWS_SEND_BUFFER_PRE_PADDING valid bytes before
2050 * the pointer and LWS_SEND_BUFFER_POST_PADDING afterwards in the
2051 * case you are calling this function from callback context.
Andy Greenb45993c2010-12-18 15:13:50 +00002052 * @len: length of payload data in buf, starting from buf.
Andy Green8f037e42010-12-19 22:13:26 +00002053 *
2054 * This function allows bulk sending of a packet to every connection using
Andy Greenb45993c2010-12-18 15:13:50 +00002055 * the given protocol. It does not send the data directly; instead it calls
2056 * the callback with a reason type of LWS_CALLBACK_BROADCAST. If the callback
2057 * wants to actually send the data for that connection, the callback itself
2058 * should call libwebsocket_write().
2059 *
Andy Green52f28ce2013-01-25 17:34:15 +08002060 * This version only works from the same thread / process context as the service
2061 * loop. Use libwesockets_broadcast_foreign(...) to do the same job from a different
2062 * thread in a safe way.
Andy Greenb45993c2010-12-18 15:13:50 +00002063 */
2064
2065
2066int
Andy Green8f037e42010-12-19 22:13:26 +00002067libwebsockets_broadcast(const struct libwebsocket_protocols *protocol,
Andy Greenb45993c2010-12-18 15:13:50 +00002068 unsigned char *buf, size_t len)
2069{
Peter Hinz56885f32011-03-02 22:03:47 +00002070 struct libwebsocket_context *context = protocol->owning_server;
Andy Greenb45993c2010-12-18 15:13:50 +00002071 int n;
Andy Green6ee372f2012-04-09 15:09:01 +08002072 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +00002073
Andy Greenf5bc1302013-01-21 09:09:52 +08002074 if (!context)
2075 return 1;
2076
Andy Green52f28ce2013-01-25 17:34:15 +08002077 /*
2078 * We are either running unforked / flat, or we are being
2079 * called from poll thread context
2080 * eg, from a callback. In that case don't use sockets for
2081 * broadcast IPC (since we can't open a socket connection to
2082 * a socket listening on our own thread) but directly do the
2083 * send action.
2084 *
2085 * Locking is not needed because we are by definition being
2086 * called in the poll thread context and are serialized.
2087 */
2088
2089 for (n = 0; n < context->fds_count; n++) {
2090
2091 wsi = context->lws_lookup[context->fds[n].fd];
2092 if (!wsi)
2093 continue;
2094
2095 if (wsi->mode != LWS_CONNMODE_WS_SERVING)
2096 continue;
2097
Andy Greenb45993c2010-12-18 15:13:50 +00002098 /*
Andy Green52f28ce2013-01-25 17:34:15 +08002099 * never broadcast to non-established connections
Andy Greenb45993c2010-12-18 15:13:50 +00002100 */
Andy Green52f28ce2013-01-25 17:34:15 +08002101 if (wsi->state != WSI_STATE_ESTABLISHED)
2102 continue;
Andy Greenb45993c2010-12-18 15:13:50 +00002103
Andy Green52f28ce2013-01-25 17:34:15 +08002104 /* only broadcast to guys using
2105 * requested protocol
2106 */
2107 if (wsi->protocol != protocol)
2108 continue;
Andy Greenb45993c2010-12-18 15:13:50 +00002109
Andy Green52f28ce2013-01-25 17:34:15 +08002110 user_callback_handle_rxflow(wsi->protocol->callback,
2111 context, wsi,
2112 LWS_CALLBACK_BROADCAST,
2113 wsi->user_space,
2114 buf, len);
Andy Greenb45993c2010-12-18 15:13:50 +00002115 }
2116
Andy Green52f28ce2013-01-25 17:34:15 +08002117 return 0;
2118}
2119
2120
2121#ifndef LWS_NO_FORK
2122/**
2123 * libwebsockets_broadcast_foreign() - Sends a buffer to the callback for all active
2124 * connections of the given protocol.
2125 * @protocol: pointer to the protocol you will broadcast to all members of
2126 * @buf: buffer containing the data to be broadcase. NOTE: this has to be
2127 * allocated with LWS_SEND_BUFFER_PRE_PADDING valid bytes before
2128 * the pointer and LWS_SEND_BUFFER_POST_PADDING afterwards in the
2129 * case you are calling this function from callback context.
2130 * @len: length of payload data in buf, starting from buf.
2131 *
2132 * This function allows bulk sending of a packet to every connection using
2133 * the given protocol. It does not send the data directly; instead it calls
2134 * the callback with a reason type of LWS_CALLBACK_BROADCAST. If the callback
2135 * wants to actually send the data for that connection, the callback itself
2136 * should call libwebsocket_write().
2137 *
2138 * This ..._foreign() version is designed to be randomly called from other thread or
2139 * process contexts than the main libwebsocket service one. A private socket is used
2140 * to serialize accesses here with the main service loop.
2141 */
2142
2143int
2144libwebsockets_broadcast_foreign(struct libwebsocket_protocols *protocol,
2145 unsigned char *buf, size_t len)
2146{
2147 struct libwebsocket_context *context = protocol->owning_server;
2148 int n;
2149 int fd;
2150 struct sockaddr_in cli_addr;
2151
2152 if (!context)
2153 return 1;
2154
Andy Green0ca6a172010-12-19 20:50:01 +00002155 /*
2156 * We're being called from a different process context than the server
2157 * loop. Instead of broadcasting directly, we send our
2158 * payload on a socket to do the IPC; the server process will serialize
2159 * the broadcast action in its main poll() loop.
2160 *
2161 * There's one broadcast socket listening for each protocol supported
2162 * set up when the websocket server initializes
2163 */
2164
Andy Green52f28ce2013-01-25 17:34:15 +08002165
2166 /*
2167 * autoconnect to this protocol's broadcast proxy socket for this
2168 * thread if needed
2169 */
2170
2171 if (protocol->broadcast_socket_user_fd <= 0) {
2172 fd = socket(AF_INET, SOCK_STREAM, 0);
2173 if (fd < 0) {
2174 lwsl_err("Unable to create socket\n");
2175 return -1;
2176 }
2177 cli_addr.sin_family = AF_INET;
2178 cli_addr.sin_port = htons(protocol->broadcast_socket_port);
2179 cli_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
2180 n = connect(fd, (struct sockaddr *)&cli_addr, sizeof cli_addr);
2181 if (n < 0) {
2182 lwsl_err("Unable to connect to "
2183 "broadcast socket %d, %s\n",
2184 n, strerror(errno));
2185 return -1;
2186 }
2187
2188 protocol->broadcast_socket_user_fd = fd;
2189 }
2190
Andy Green6964bb52011-01-23 16:50:33 +00002191 n = send(protocol->broadcast_socket_user_fd, buf, len, MSG_NOSIGNAL);
Andy Greenb45993c2010-12-18 15:13:50 +00002192
2193 return n;
2194}
Andy Green52f28ce2013-01-25 17:34:15 +08002195#endif
Andy Green82c3d542011-03-07 21:16:31 +00002196
2197int
2198libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2199{
Andy Green623a98d2013-01-21 11:04:23 +08002200 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002201}
Alex Bligh49146db2011-11-07 17:19:25 +08002202
David Galeanoe2cf9922013-01-09 18:06:55 +08002203unsigned char
2204libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2205{
Andy Green623a98d2013-01-21 11:04:23 +08002206 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002207}
2208
Alex Bligh49146db2011-11-07 17:19:25 +08002209void *
2210libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2211{
2212 /* allocate the per-connection user memory (if any) */
2213
2214 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2215 wsi->user_space = malloc(
2216 wsi->protocol->per_session_data_size);
2217 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002218 lwsl_err("Out of memory for conn user space\n");
Alex Bligh49146db2011-11-07 17:19:25 +08002219 return NULL;
2220 }
Andy Green6ee372f2012-04-09 15:09:01 +08002221 memset(wsi->user_space, 0,
2222 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002223 }
2224 return wsi->user_space;
2225}
Andy Green43db0452013-01-10 19:50:35 +08002226
Andy Greenacbaee62013-01-18 22:00:22 +08002227/**
2228 * lws_confirm_legit_wsi: returns nonzero if the wsi looks bad
2229 *
2230 * @wsi: struct libwebsocket to assess
2231 *
2232 * Performs consistecy checks on what the wsi claims and what the
2233 * polling arrays hold. This'll catch a closed wsi still in use.
2234 * Don't try to use on the listen (nonconnection) wsi as it will
2235 * fail it. Otherwise 0 return == wsi seems consistent.
2236 */
2237
2238int lws_confirm_legit_wsi(struct libwebsocket *wsi)
2239{
2240 struct libwebsocket_context *context;
2241
2242 if (!(wsi && wsi->protocol && wsi->protocol->owning_server))
2243 return 1;
2244
2245 context = wsi->protocol->owning_server;
2246
2247 if (!context)
2248 return 2;
2249
2250 if (!wsi->position_in_fds_table)
2251 return 3; /* position in fds table looks bad */
2252 if (context->fds[wsi->position_in_fds_table].fd != wsi->sock)
2253 return 4; /* pollfd entry does not wait on our socket descriptor */
2254 if (context->lws_lookup[wsi->sock] != wsi)
2255 return 5; /* lookup table does not agree with wsi */
2256
2257 return 0;
2258}
Andy Greende8f27a2013-01-12 09:17:42 +08002259
Andy Green0b319092013-01-19 11:17:56 +08002260static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002261{
Andy Green0b319092013-01-19 11:17:56 +08002262 char buf[300];
2263 struct timeval tv;
Andy Green0b319092013-01-19 11:17:56 +08002264 int n;
2265
2266 gettimeofday(&tv, NULL);
2267
2268 buf[0] = '\0';
2269 for (n = 0; n < LLL_COUNT; n++)
2270 if (level == (1 << n)) {
Andy Green058ba812013-01-19 11:32:18 +08002271 sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
Andy Green0b319092013-01-19 11:17:56 +08002272 (int)(tv.tv_usec / 100), log_level_names[n]);
2273 break;
2274 }
2275
2276 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002277}
2278
Andy Greenc11db202013-01-19 11:12:16 +08002279void lwsl_emit_syslog(int level, const char *line)
2280{
2281 int syslog_level = LOG_DEBUG;
2282
2283 switch (level) {
2284 case LLL_ERR:
2285 syslog_level = LOG_ERR;
2286 break;
2287 case LLL_WARN:
2288 syslog_level = LOG_WARNING;
2289 break;
2290 case LLL_NOTICE:
2291 syslog_level = LOG_NOTICE;
2292 break;
2293 case LLL_INFO:
2294 syslog_level = LOG_INFO;
2295 break;
2296 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002297 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002298}
2299
Andy Green43db0452013-01-10 19:50:35 +08002300void _lws_log(int filter, const char *format, ...)
2301{
Andy Greende8f27a2013-01-12 09:17:42 +08002302 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002303 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002304
2305 if (!(log_level & filter))
2306 return;
2307
Andy Green43db0452013-01-10 19:50:35 +08002308 va_start(ap, format);
Andy Green0b319092013-01-19 11:17:56 +08002309 vsnprintf(buf, (sizeof buf), format, ap);
Andy Greende8f27a2013-01-12 09:17:42 +08002310 buf[(sizeof buf) - 1] = '\0';
2311 va_end(ap);
2312
Andy Green0b319092013-01-19 11:17:56 +08002313 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002314}
2315
2316/**
2317 * lws_set_log_level() - Set the logging bitfield
2318 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002319 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2320 * function to perform log string emission instead of
2321 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002322 *
Andy Greende8f27a2013-01-12 09:17:42 +08002323 * log level defaults to "err" and "warn" contexts enabled only and
2324 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002325 */
2326
Andy Green058ba812013-01-19 11:32:18 +08002327void lws_set_log_level(int level, void (*log_emit_function)(int level, const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002328{
2329 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002330 if (log_emit_function)
2331 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002332}