blob: e6c87fd18de8908f0d3589ef9c045b0ac77e519f [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 Green6f520a52013-01-29 17:57:39 +0800510
Andy Green3182ece2013-01-20 17:08:31 +0800511#ifndef LWS_NO_EXTENSIONS
512 struct lws_tokens eff_buf;
Andy Green3b84c002011-03-06 13:14:42 +0000513 int ret;
514 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100515 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000516
Andy Greena41314f2011-05-23 10:00:03 +0100517 for (n = 0; n < wsi->count_active_extensions; n++) {
518 if (!wsi->active_extensions[n]->callback)
519 continue;
520
521 m = wsi->active_extensions[n]->callback(context,
522 wsi->active_extensions[n], wsi,
523 LWS_EXT_CALLBACK_IS_WRITEABLE,
524 wsi->active_extensions_user[n], NULL, 0);
525 if (m > handled)
526 handled = m;
527 }
528
529 if (handled == 1)
530 goto notify_action;
531
532 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000533 goto user_service;
534
535 /*
536 * check in on the active extensions, see if they
537 * had pending stuff to spill... they need to get the
538 * first look-in otherwise sequence will be disordered
539 *
540 * NULL, zero-length eff_buf means just spill pending
541 */
542
543 ret = 1;
544 while (ret == 1) {
545
546 /* default to nobody has more to spill */
547
548 ret = 0;
549 eff_buf.token = NULL;
550 eff_buf.token_len = 0;
551
552 /* give every extension a chance to spill */
553
554 for (n = 0; n < wsi->count_active_extensions; n++) {
555 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000556 wsi->protocol->owning_server,
557 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000558 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
559 wsi->active_extensions_user[n], &eff_buf, 0);
560 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800561 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000562 return -1;
563 }
564 if (m)
565 /*
566 * at least one extension told us he has more
567 * to spill, so we will go around again after
568 */
569 ret = 1;
570 }
571
572 /* assuming they gave us something to send, send it */
573
574 if (eff_buf.token_len) {
575 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
576 eff_buf.token_len))
577 return -1;
578 } else
579 continue;
580
581 /* no extension has more to spill */
582
583 if (!ret)
584 continue;
585
586 /*
587 * There's more to spill from an extension, but we just sent
588 * something... did that leave the pipe choked?
589 */
590
591 if (!lws_send_pipe_choked(wsi))
592 /* no we could add more */
593 continue;
594
Andy Green43db0452013-01-10 19:50:35 +0800595 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000596
597 /*
598 * Yes, he's choked. Leave the POLLOUT masked on so we will
599 * come back here when he is unchoked. Don't call the user
600 * callback to enforce ordering of spilling, he'll get called
601 * when we come back here and there's nothing more to spill.
602 */
603
604 return 0;
605 }
606
607 wsi->extension_data_pending = 0;
608
609user_service:
Andy Green3182ece2013-01-20 17:08:31 +0800610#endif
Andy Green3b84c002011-03-06 13:14:42 +0000611 /* one shot */
612
Andy Greena41314f2011-05-23 10:00:03 +0100613 if (pollfd) {
614 pollfd->events &= ~POLLOUT;
Andy Green3b84c002011-03-06 13:14:42 +0000615
Andy Greena41314f2011-05-23 10:00:03 +0100616 /* external POLL support via protocol 0 */
617 context->protocols[0].callback(context, wsi,
618 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
619 (void *)(long)wsi->sock, NULL, POLLOUT);
620 }
Andy Green3182ece2013-01-20 17:08:31 +0800621#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100622notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800623#endif
Andy Green3b84c002011-03-06 13:14:42 +0000624
Andy Green9e4c2b62011-03-07 20:47:39 +0000625 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
626 n = LWS_CALLBACK_CLIENT_WRITEABLE;
627 else
628 n = LWS_CALLBACK_SERVER_WRITEABLE;
629
Andy Green706961d2013-01-17 16:50:35 +0800630 user_callback_handle_rxflow(wsi->protocol->callback, context,
631 wsi, (enum libwebsocket_callback_reasons) n, wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000632
633 return 0;
634}
635
636
637
Andy Greena41314f2011-05-23 10:00:03 +0100638void
639libwebsocket_service_timeout_check(struct libwebsocket_context *context,
640 struct libwebsocket *wsi, unsigned int sec)
641{
Andy Green3182ece2013-01-20 17:08:31 +0800642#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100643 int n;
644
645 /*
646 * if extensions want in on it (eg, we are a mux parent)
647 * give them a chance to service child timeouts
648 */
649
650 for (n = 0; n < wsi->count_active_extensions; n++)
651 wsi->active_extensions[n]->callback(
652 context, wsi->active_extensions[n],
653 wsi, LWS_EXT_CALLBACK_1HZ,
654 wsi->active_extensions_user[n], NULL, sec);
655
Andy Green3182ece2013-01-20 17:08:31 +0800656#endif
Andy Greena41314f2011-05-23 10:00:03 +0100657 if (!wsi->pending_timeout)
658 return;
Andy Green6ee372f2012-04-09 15:09:01 +0800659
Andy Greena41314f2011-05-23 10:00:03 +0100660 /*
661 * if we went beyond the allowed time, kill the
662 * connection
663 */
664
665 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800666 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100667 libwebsocket_close_and_free_session(context,
668 wsi, LWS_CLOSE_STATUS_NOSTATUS);
669 }
670}
671
Andy Green9f990342011-02-12 11:57:45 +0000672/**
673 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000674 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000675 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800676 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000677 *
Andy Green75006172013-01-22 12:32:11 +0800678 * This function takes a pollfd that has POLLIN or POLLOUT activity and
679 * services it according to the state of the associated struct libwebsocket.
680 *
681 * The one call deals with all "service" that might happen on a socket
682 * including listen accepts, http files as well as websocket protocol.
Andy Green9f990342011-02-12 11:57:45 +0000683 */
684
685int
Peter Hinz56885f32011-03-02 22:03:47 +0000686libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000687 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000688{
Andy Greena1ce6be2013-01-18 11:43:21 +0800689 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000690 int n;
Andy Green0d338332011-02-12 11:57:43 +0000691 int m;
Andy Greena71eafc2011-02-14 17:59:43 +0000692 struct timeval tv;
Andy Green6f520a52013-01-29 17:57:39 +0800693 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 +
694 MAX_USER_RX_BUFFER + LWS_SEND_BUFFER_POST_PADDING];
695
Andy Green3182ece2013-01-20 17:08:31 +0800696#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000697 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800698#endif
Andy Green98a717c2011-03-06 13:14:15 +0000699 struct lws_tokens eff_buf;
Andy Green03674a62013-01-16 11:47:40 +0800700#ifndef LWS_NO_CLIENT
Andy Green76f61e72013-01-16 11:53:05 +0800701 extern int lws_client_socket_service(struct libwebsocket_context *context, struct libwebsocket *wsi, struct pollfd *pollfd);
Andy Green03674a62013-01-16 11:47:40 +0800702#endif
Andy Greena1ce6be2013-01-18 11:43:21 +0800703#ifndef LWS_NO_SERVER
704 extern int lws_server_socket_service(struct libwebsocket_context *context, struct libwebsocket *wsi, struct pollfd *pollfd);
705#endif
Andy Greena71eafc2011-02-14 17:59:43 +0000706 /*
707 * you can call us with pollfd = NULL to just allow the once-per-second
708 * global timeout checks; if less than a second since the last check
709 * it returns immediately then.
710 */
711
712 gettimeofday(&tv, NULL);
713
Peter Hinz56885f32011-03-02 22:03:47 +0000714 if (context->last_timeout_check_s != tv.tv_sec) {
715 context->last_timeout_check_s = tv.tv_sec;
Andy Greena71eafc2011-02-14 17:59:43 +0000716
Andy Green24cba922013-01-19 13:56:10 +0800717 /* if our parent went down, don't linger around */
718 if (context->started_with_parent && kill(context->started_with_parent, 0) < 0)
719 kill(getpid(), SIGTERM);
720
Andy Greena71eafc2011-02-14 17:59:43 +0000721 /* global timeout check once per second */
722
Peter Hinz56885f32011-03-02 22:03:47 +0000723 for (n = 0; n < context->fds_count; n++) {
Andy Greendfb23042013-01-17 12:26:48 +0800724 struct libwebsocket *new_wsi = context->lws_lookup[context->fds[n].fd];
725 if (!new_wsi)
726 continue;
727 libwebsocket_service_timeout_check(context,
728 new_wsi, tv.tv_sec);
Andy Greena71eafc2011-02-14 17:59:43 +0000729 }
730 }
731
732 /* just here for timeout management? */
733
734 if (pollfd == NULL)
735 return 0;
736
737 /* no, here to service a socket descriptor */
738
Andy Green65b0e912013-01-16 07:59:47 +0800739 /*
740 * deal with listen service piggybacking
741 * every listen_service_modulo services of other fds, we
742 * sneak one in to service the listen socket if there's anything waiting
743 *
744 * To handle connection storms, as found in ab, if we previously saw a
745 * pending connection here, it causes us to check again next time.
746 */
747
748 if (context->listen_service_fd && pollfd->fd != context->listen_service_fd) {
749 context->listen_service_count++;
750 if (context->listen_service_extraseen ||
751 context->listen_service_count == context->listen_service_modulo) {
752 context->listen_service_count = 0;
753 m = 1;
754 if (context->listen_service_extraseen > 5)
755 m = 2;
756 while (m--) {
757 /* even with extpoll, we prepared this internal fds for listen */
758 n = poll(&context->fds[0], 1, 0);
759 if (n > 0) { /* there's a connection waiting for us */
760 libwebsocket_service_fd(context, &context->fds[0]);
761 context->listen_service_extraseen++;
762 } else {
763 if (context->listen_service_extraseen)
764 context->listen_service_extraseen--;
765 break;
766 }
767 }
768 }
769
770 }
771
772 /* okay, what we came here to do... */
773
Andy Greendfb23042013-01-17 12:26:48 +0800774 wsi = context->lws_lookup[pollfd->fd];
Andy Greend280b6e2013-01-15 13:40:23 +0800775 if (wsi == NULL) {
Andy Greendfb23042013-01-17 12:26:48 +0800776 if (pollfd->fd > 11)
777 lwsl_err("unexpected NULL wsi fd=%d fds_count=%d\n", pollfd->fd, context->fds_count);
Andy Greenfa3f4052012-10-07 20:40:35 +0800778 return 0;
Andy Greend280b6e2013-01-15 13:40:23 +0800779 }
Andy Green8f037e42010-12-19 22:13:26 +0000780
Andy Green0d338332011-02-12 11:57:43 +0000781 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +0800782
Andy Greena1ce6be2013-01-18 11:43:21 +0800783#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +0800784 case LWS_CONNMODE_HTTP_SERVING:
Andy Green0d338332011-02-12 11:57:43 +0000785 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +0800786 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greena1ce6be2013-01-18 11:43:21 +0800787 return lws_server_socket_service(context, wsi, pollfd);
788#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000789
Andy Green0d338332011-02-12 11:57:43 +0000790 case LWS_CONNMODE_WS_SERVING:
791 case LWS_CONNMODE_WS_CLIENT:
792
793 /* handle session socket closed */
794
795 if (pollfd->revents & (POLLERR | POLLHUP)) {
796
Andy Green43db0452013-01-10 19:50:35 +0800797 lwsl_debug("Session Socket %p (fd=%d) dead\n",
Andy Green0d338332011-02-12 11:57:43 +0000798 (void *)wsi, pollfd->fd);
799
Peter Hinz56885f32011-03-02 22:03:47 +0000800 libwebsocket_close_and_free_session(context, wsi,
Andy Green687b0182011-02-26 11:04:01 +0000801 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800802 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000803 }
804
Andy Green0d338332011-02-12 11:57:43 +0000805 /* the guy requested a callback when it was OK to write */
806
Andy Greenda527df2011-03-07 07:08:12 +0000807 if ((pollfd->revents & POLLOUT) &&
808 wsi->state == WSI_STATE_ESTABLISHED)
809 if (lws_handle_POLLOUT_event(context, wsi,
810 pollfd) < 0) {
811 libwebsocket_close_and_free_session(
812 context, wsi, LWS_CLOSE_STATUS_NORMAL);
Andy Green040d2ef2013-01-16 13:40:43 +0800813 return 0;
Andy Green3b84c002011-03-06 13:14:42 +0000814 }
Andy Green0d338332011-02-12 11:57:43 +0000815
Andy Green0d338332011-02-12 11:57:43 +0000816
817 /* any incoming data ready? */
818
819 if (!(pollfd->revents & POLLIN))
820 break;
821
Andy Greenb45993c2010-12-18 15:13:50 +0000822#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +0800823read_pending:
Andy Green0d338332011-02-12 11:57:43 +0000824 if (wsi->ssl)
Andy Green98a717c2011-03-06 13:14:15 +0000825 eff_buf.token_len = SSL_read(wsi->ssl, buf, sizeof buf);
Andy Greenb45993c2010-12-18 15:13:50 +0000826 else
827#endif
Andy Green98a717c2011-03-06 13:14:15 +0000828 eff_buf.token_len =
Andy Green72c34322011-04-16 10:46:21 +0100829 recv(pollfd->fd, buf, sizeof buf, 0);
Andy Greenb45993c2010-12-18 15:13:50 +0000830
Andy Green98a717c2011-03-06 13:14:15 +0000831 if (eff_buf.token_len < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800832 lwsl_debug("Socket read returned %d\n",
Andy Green98a717c2011-03-06 13:14:15 +0000833 eff_buf.token_len);
Alon Levydc93b7f2012-10-19 11:21:57 +0200834 if (errno != EINTR && errno != EAGAIN)
Andy Green6ee372f2012-04-09 15:09:01 +0800835 libwebsocket_close_and_free_session(context,
836 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800837 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000838 }
Andy Green98a717c2011-03-06 13:14:15 +0000839 if (!eff_buf.token_len) {
Peter Hinz56885f32011-03-02 22:03:47 +0000840 libwebsocket_close_and_free_session(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +0800841 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenfa3f4052012-10-07 20:40:35 +0800842 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000843 }
844
Andy Green98a717c2011-03-06 13:14:15 +0000845 /*
846 * give any active extensions a chance to munge the buffer
847 * before parse. We pass in a pointer to an lws_tokens struct
848 * prepared with the default buffer and content length that's in
849 * there. Rather than rewrite the default buffer, extensions
850 * that expect to grow the buffer can adapt .token to
851 * point to their own per-connection buffer in the extension
852 * user allocation. By default with no extensions or no
853 * extension callback handling, just the normal input buffer is
854 * used then so it is efficient.
855 */
Andy Greenb45993c2010-12-18 15:13:50 +0000856
Andy Green98a717c2011-03-06 13:14:15 +0000857 eff_buf.token = (char *)buf;
Andy Green3182ece2013-01-20 17:08:31 +0800858#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +0000859 more = 1;
860 while (more) {
Andy Green0d338332011-02-12 11:57:43 +0000861
Andy Green98a717c2011-03-06 13:14:15 +0000862 more = 0;
863
864 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +0000865 m = wsi->active_extensions[n]->callback(context,
866 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +0000867 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +0000868 wsi->active_extensions_user[n],
869 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +0000870 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800871 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +0800872 "Extension reports fatal error\n");
873 libwebsocket_close_and_free_session(
874 context, wsi,
875 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800876 return 0;
Andy Green98a717c2011-03-06 13:14:15 +0000877 }
878 if (m)
879 more = 1;
880 }
Andy Green3182ece2013-01-20 17:08:31 +0800881#endif
Andy Green98a717c2011-03-06 13:14:15 +0000882 /* service incoming data */
883
884 if (eff_buf.token_len) {
885 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +0800886 (unsigned char *)eff_buf.token,
887 eff_buf.token_len);
Andy Green98a717c2011-03-06 13:14:15 +0000888 if (n < 0)
889 /* we closed wsi */
Andy Green040d2ef2013-01-16 13:40:43 +0800890 return 0;
Andy Green98a717c2011-03-06 13:14:15 +0000891 }
Andy Green3182ece2013-01-20 17:08:31 +0800892#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +0000893 eff_buf.token = NULL;
894 eff_buf.token_len = 0;
895 }
Andy Green3182ece2013-01-20 17:08:31 +0800896#endif
David Galeano7ffbe1b2013-01-10 10:35:32 +0800897
898#ifdef LWS_OPENSSL_SUPPORT
899 if (wsi->ssl && SSL_pending(wsi->ssl))
900 goto read_pending;
901#endif
Andy Green98a717c2011-03-06 13:14:15 +0000902 break;
Andy Green76f61e72013-01-16 11:53:05 +0800903
904 default:
Andy Green03674a62013-01-16 11:47:40 +0800905#ifdef LWS_NO_CLIENT
906 break;
907#else
Andy Green76f61e72013-01-16 11:53:05 +0800908 return lws_client_socket_service(context, wsi, pollfd);
Andy Green03674a62013-01-16 11:47:40 +0800909#endif
Andy Greenb45993c2010-12-18 15:13:50 +0000910 }
911
912 return 0;
913}
914
Andy Green0d338332011-02-12 11:57:43 +0000915
Andy Green6964bb52011-01-23 16:50:33 +0000916/**
917 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +0000918 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +0000919 *
920 * This function closes any active connections and then frees the
921 * context. After calling this, any further use of the context is
922 * undefined.
923 */
924void
Peter Hinz56885f32011-03-02 22:03:47 +0000925libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +0000926{
Andy Green3182ece2013-01-20 17:08:31 +0800927#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +0000928 int n;
929 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100930 struct libwebsocket_extension *ext;
Andy Green6964bb52011-01-23 16:50:33 +0000931
Andy Greendfb23042013-01-17 12:26:48 +0800932 for (n = 0; n < context->fds_count; n++) {
933 struct libwebsocket *wsi = context->lws_lookup[context->fds[n].fd];
934 libwebsocket_close_and_free_session(context,
935 wsi, LWS_CLOSE_STATUS_GOINGAWAY);
936 }
Andy Green6964bb52011-01-23 16:50:33 +0000937
Andy Greena41314f2011-05-23 10:00:03 +0100938 /*
939 * give all extensions a chance to clean up any per-context
940 * allocations they might have made
941 */
942
943 ext = context->extensions;
944 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
945 if (context->listen_port)
946 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +0800947 while (ext && ext->callback) {
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800948 ext->callback(context, ext, NULL, (enum libwebsocket_extension_callback_reasons)m, NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +0100949 ext++;
950 }
Andy Green3182ece2013-01-20 17:08:31 +0800951#endif
Andy Greena41314f2011-05-23 10:00:03 +0100952
Peter Hinz56885f32011-03-02 22:03:47 +0000953#ifdef WIN32
954#else
955 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +0000956#endif
957
Peter Hinz56885f32011-03-02 22:03:47 +0000958#ifdef LWS_OPENSSL_SUPPORT
959 if (context->ssl_ctx)
960 SSL_CTX_free(context->ssl_ctx);
961 if (context->ssl_client_ctx)
962 SSL_CTX_free(context->ssl_client_ctx);
963#endif
964
965 free(context);
966
967#ifdef WIN32
968 WSACleanup();
969#endif
Andy Green6964bb52011-01-23 16:50:33 +0000970}
971
Andy Greend88146d2013-01-22 12:40:35 +0800972/**
973 * libwebsocket_context_user() - get the user data associated with the whole context
974 * @context: Websocket context
975 *
976 * This returns the optional user allocation that can be attached to
977 * the context the sockets live in at context_create time. It's a way
978 * to let all sockets serviced in the same context share data without
979 * using globals statics in the user code.
980 */
981
982
Alon Levy0291eb32012-10-19 11:21:56 +0200983LWS_EXTERN void *
984libwebsocket_context_user(struct libwebsocket_context *context)
985{
986 return context->user_space;
987}
988
Andy Green6964bb52011-01-23 16:50:33 +0000989/**
990 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +0000991 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +0000992 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
993 * service otherwise block and service immediately, returning
994 * after the timeout if nothing needed service.
995 *
996 * This function deals with any pending websocket traffic, for three
997 * kinds of event. It handles these events on both server and client
998 * types of connection the same.
999 *
1000 * 1) Accept new connections to our context's server
1001 *
Andy Green6f520a52013-01-29 17:57:39 +08001002 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001003 * server or client connections.
1004 *
1005 * You need to call this service function periodically to all the above
1006 * functions to happen; if your application is single-threaded you can
1007 * just call it in your main event loop.
1008 *
1009 * Alternatively you can fork a new process that asynchronously handles
1010 * calling this service in a loop. In that case you are happy if this
1011 * call blocks your thread until it needs to take care of something and
1012 * would call it with a large nonzero timeout. Your loop then takes no
1013 * CPU while there is nothing happening.
1014 *
1015 * If you are calling it in a single-threaded app, you don't want it to
1016 * wait around blocking other things in your loop from happening, so you
1017 * would call it with a timeout_ms of 0, so it returns immediately if
1018 * nothing is pending, or as soon as it services whatever was pending.
1019 */
1020
Andy Greenb45993c2010-12-18 15:13:50 +00001021
Andy Greene92cd172011-01-19 13:11:55 +00001022int
Peter Hinz56885f32011-03-02 22:03:47 +00001023libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001024{
1025 int n;
Andy Greene92cd172011-01-19 13:11:55 +00001026
1027 /* stay dead once we are dead */
1028
Peter Hinz56885f32011-03-02 22:03:47 +00001029 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001030 return 1;
1031
Andy Green0d338332011-02-12 11:57:43 +00001032 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001033
Peter Hinz56885f32011-03-02 22:03:47 +00001034 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green3221f922011-02-12 13:14:11 +00001035 if (n == 0) /* poll timeout */
1036 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001037
Andy Greendfb23042013-01-17 12:26:48 +08001038 if (n < 0)
Andy Green3928f612012-07-20 12:58:38 +08001039 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001040
Andy Greendfb23042013-01-17 12:26:48 +08001041 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001042
Peter Hinz56885f32011-03-02 22:03:47 +00001043 for (n = 0; n < context->fds_count; n++)
1044 if (context->fds[n].revents)
Andy Green3928f612012-07-20 12:58:38 +08001045 if (libwebsocket_service_fd(context,
1046 &context->fds[n]) < 0)
1047 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001048 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001049}
1050
Andy Green3182ece2013-01-20 17:08:31 +08001051#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001052int
1053lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001054 struct libwebsocket *wsi,
1055 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001056 void *v, size_t len)
1057{
1058 int n;
1059 int handled = 0;
1060
1061 /* maybe an extension will take care of it for us */
1062
1063 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1064 if (!wsi->active_extensions[n]->callback)
1065 continue;
1066
1067 handled |= wsi->active_extensions[n]->callback(context,
1068 wsi->active_extensions[n], wsi,
1069 r, wsi->active_extensions_user[n], v, len);
1070 }
1071
1072 return handled;
1073}
1074
1075
1076void *
1077lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001078 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001079{
1080 int n = 0;
1081
Andy Green68b45042011-05-25 21:41:57 +01001082 if (wsi == NULL)
1083 return NULL;
1084
Andy Greena41314f2011-05-23 10:00:03 +01001085 while (n < wsi->count_active_extensions) {
1086 if (wsi->active_extensions[n] != ext) {
1087 n++;
1088 continue;
1089 }
1090 return wsi->active_extensions_user[n];
1091 }
1092
1093 return NULL;
1094}
Andy Green3182ece2013-01-20 17:08:31 +08001095#endif
Andy Greena41314f2011-05-23 10:00:03 +01001096
Andy Green90c7cbc2011-01-27 06:26:52 +00001097/**
1098 * libwebsocket_callback_on_writable() - Request a callback when this socket
1099 * becomes able to be written to without
1100 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001101 *
Peter Hinz56885f32011-03-02 22:03:47 +00001102 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001103 * @wsi: Websocket connection instance to get callback for
1104 */
1105
1106int
Peter Hinz56885f32011-03-02 22:03:47 +00001107libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001108 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001109{
Andy Green3182ece2013-01-20 17:08:31 +08001110#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001111 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001112 int handled = 0;
1113
1114 /* maybe an extension will take care of it for us */
1115
1116 for (n = 0; n < wsi->count_active_extensions; n++) {
1117 if (!wsi->active_extensions[n]->callback)
1118 continue;
1119
1120 handled |= wsi->active_extensions[n]->callback(context,
1121 wsi->active_extensions[n], wsi,
1122 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1123 wsi->active_extensions_user[n], NULL, 0);
1124 }
1125
1126 if (handled)
1127 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001128#endif
Andy Greendfb23042013-01-17 12:26:48 +08001129 if (wsi->position_in_fds_table < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001130 lwsl_err("libwebsocket_callback_on_writable: "
Andy Green6ee372f2012-04-09 15:09:01 +08001131 "failed to find socket %d\n", wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001132 return -1;
1133 }
1134
1135 context->fds[wsi->position_in_fds_table].events |= POLLOUT;
Andy Greena41314f2011-05-23 10:00:03 +01001136
Andy Green3221f922011-02-12 13:14:11 +00001137 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00001138 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001139 LWS_CALLBACK_SET_MODE_POLL_FD,
1140 (void *)(long)wsi->sock, NULL, POLLOUT);
1141
Andy Green90c7cbc2011-01-27 06:26:52 +00001142 return 1;
1143}
1144
1145/**
1146 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1147 * all connections using the given protocol when it
1148 * becomes possible to write to each socket without
1149 * blocking in turn.
1150 *
1151 * @protocol: Protocol whose connections will get callbacks
1152 */
1153
1154int
1155libwebsocket_callback_on_writable_all_protocol(
1156 const struct libwebsocket_protocols *protocol)
1157{
Peter Hinz56885f32011-03-02 22:03:47 +00001158 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001159 int n;
Andy Green0d338332011-02-12 11:57:43 +00001160 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001161
Andy Greendfb23042013-01-17 12:26:48 +08001162 for (n = 0; n < context->fds_count; n++) {
1163 wsi = context->lws_lookup[context->fds[n].fd];
1164 if (!wsi)
1165 continue;
1166 if (wsi->protocol == protocol)
1167 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001168 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001169
1170 return 0;
1171}
1172
Andy Greenbe93fef2011-02-14 20:25:43 +00001173/**
1174 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1175 *
1176 * You will not need this unless you are doing something special
1177 *
1178 * @wsi: Websocket connection instance
1179 * @reason: timeout reason
1180 * @secs: how many seconds
1181 */
1182
1183void
1184libwebsocket_set_timeout(struct libwebsocket *wsi,
1185 enum pending_timeout reason, int secs)
1186{
1187 struct timeval tv;
1188
1189 gettimeofday(&tv, NULL);
1190
1191 wsi->pending_timeout_limit = tv.tv_sec + secs;
1192 wsi->pending_timeout = reason;
1193}
1194
Andy Greena6cbece2011-01-27 20:06:03 +00001195
1196/**
1197 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1198 *
1199 * You will not need this unless you are doing something special
1200 *
1201 * @wsi: Websocket connection instance
1202 */
1203
1204int
1205libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1206{
1207 return wsi->sock;
1208}
1209
Andy Greena1ce6be2013-01-18 11:43:21 +08001210#ifdef LWS_NO_SERVER
1211int
1212_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1213{
1214 return 0;
1215}
1216#else
Andy Green706961d2013-01-17 16:50:35 +08001217int
1218_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1219{
1220 struct libwebsocket_context *context = wsi->protocol->owning_server;
1221 int n;
1222
Andy Green623a98d2013-01-21 11:04:23 +08001223 if (!(wsi->u.ws.rxflow_change_to & 2))
Andy Green706961d2013-01-17 16:50:35 +08001224 return 0;
1225
Andy Green623a98d2013-01-21 11:04:23 +08001226 wsi->u.ws.rxflow_change_to &= ~2;
Andy Green706961d2013-01-17 16:50:35 +08001227
Andy Green623a98d2013-01-21 11:04:23 +08001228 lwsl_info("rxflow: wsi %p change_to %d\n", wsi, wsi->u.ws.rxflow_change_to);
Andy Green706961d2013-01-17 16:50:35 +08001229
1230 /* if we're letting it come again, did we interrupt anything? */
Andy Green623a98d2013-01-21 11:04:23 +08001231 if ((wsi->u.ws.rxflow_change_to & 1) && wsi->u.ws.rxflow_buffer) {
Andy Green706961d2013-01-17 16:50:35 +08001232 n = libwebsocket_interpret_incoming_packet(wsi, NULL, 0);
1233 if (n < 0) {
1234 libwebsocket_close_and_free_session(context, wsi, LWS_CLOSE_STATUS_NOSTATUS);
1235 return -1;
1236 }
1237 if (n)
1238 /* oh he stuck again, do nothing */
1239 return 0;
1240 }
1241
Andy Green623a98d2013-01-21 11:04:23 +08001242 if (wsi->u.ws.rxflow_change_to & 1)
Andy Green706961d2013-01-17 16:50:35 +08001243 context->fds[wsi->position_in_fds_table].events |= POLLIN;
1244 else
1245 context->fds[wsi->position_in_fds_table].events &= ~POLLIN;
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 /* external POLL support via protocol 0 */
1249 context->protocols[0].callback(context, wsi,
1250 LWS_CALLBACK_SET_MODE_POLL_FD,
1251 (void *)(long)wsi->sock, NULL, POLLIN);
1252 else
1253 /* external POLL support via protocol 0 */
1254 context->protocols[0].callback(context, wsi,
1255 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
1256 (void *)(long)wsi->sock, NULL, POLLIN);
1257
1258 return 1;
1259}
Andy Greena1ce6be2013-01-18 11:43:21 +08001260#endif
Andy Green706961d2013-01-17 16:50:35 +08001261
Andy Green90c7cbc2011-01-27 06:26:52 +00001262/**
1263 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1264 * receieved packets.
1265 *
1266 * If the output side of a server process becomes choked, this allows flow
1267 * control for the input side.
1268 *
1269 * @wsi: Websocket connection instance to get callback for
1270 * @enable: 0 = disable read servicing for this connection, 1 = enable
1271 */
1272
1273int
1274libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1275{
Andy Green623a98d2013-01-21 11:04:23 +08001276 wsi->u.ws.rxflow_change_to = 2 | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001277
Andy Green706961d2013-01-17 16:50:35 +08001278 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001279}
1280
Andy Green706961d2013-01-17 16:50:35 +08001281
Andy Green2ac5a6f2011-01-28 10:00:18 +00001282/**
1283 * libwebsocket_canonical_hostname() - returns this host's hostname
1284 *
1285 * This is typically used by client code to fill in the host parameter
1286 * when making a client connection. You can only call it after the context
1287 * has been created.
1288 *
Peter Hinz56885f32011-03-02 22:03:47 +00001289 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001290 */
1291
1292
1293extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001294libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001295{
Peter Hinz56885f32011-03-02 22:03:47 +00001296 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001297}
1298
1299
Andy Green90c7cbc2011-01-27 06:26:52 +00001300static void sigpipe_handler(int x)
1301{
1302}
1303
Andy Green6901cb32011-02-21 08:06:47 +00001304#ifdef LWS_OPENSSL_SUPPORT
1305static int
1306OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1307{
1308
1309 SSL *ssl;
1310 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001311 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001312
1313 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1314 SSL_get_ex_data_X509_STORE_CTX_idx());
1315
1316 /*
Andy Green2e24da02011-03-05 16:12:04 +00001317 * !!! nasty openssl requires the index to come as a library-scope
1318 * static
Andy Green6901cb32011-02-21 08:06:47 +00001319 */
Andy Green2e24da02011-03-05 16:12:04 +00001320 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001321
Peter Hinz56885f32011-03-02 22:03:47 +00001322 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001323 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1324 x509_ctx, ssl, preverify_ok);
1325
1326 /* convert return code from 0 = OK to 1 = OK */
1327
1328 if (!n)
1329 n = 1;
1330 else
1331 n = 0;
1332
1333 return n;
1334}
1335#endif
1336
Andy Green706961d2013-01-17 16:50:35 +08001337int user_callback_handle_rxflow(callback_function callback_function,
1338 struct libwebsocket_context * context,
1339 struct libwebsocket *wsi,
1340 enum libwebsocket_callback_reasons reason, void *user,
1341 void *in, size_t len)
1342{
1343 int n;
1344
1345 n = callback_function(context, wsi, reason, user, in, len);
1346 if (n < 0)
1347 return n;
1348
1349 _libwebsocket_rx_flow_control(wsi);
1350
1351 return 0;
1352}
1353
Andy Greenb45993c2010-12-18 15:13:50 +00001354
Andy Greenab990e42010-10-31 12:42:52 +00001355/**
Andy Green4739e5c2011-01-22 12:51:57 +00001356 * libwebsocket_create_context() - Create the websocket handler
1357 * @port: Port to listen on... you can use 0 to suppress listening on
Andy Green6964bb52011-01-23 16:50:33 +00001358 * any port, that's what you want if you are not running a
1359 * websocket server at all but just using it as a client
Peter Hinz56885f32011-03-02 22:03:47 +00001360 * @interf: NULL to bind the listen socket to all interfaces, or the
Andy Green32375b72011-02-19 08:32:53 +00001361 * interface name, eg, "eth2"
Andy Green4f3943a2010-11-12 10:44:16 +00001362 * @protocols: Array of structures listing supported protocols and a protocol-
Andy Green8f037e42010-12-19 22:13:26 +00001363 * specific callback for each one. The list is ended with an
1364 * entry that has a NULL callback pointer.
Andy Green6964bb52011-01-23 16:50:33 +00001365 * It's not const because we write the owning_server member
Andy Greenc5114822011-03-06 10:29:35 +00001366 * @extensions: NULL or array of libwebsocket_extension structs listing the
Andy Green3182ece2013-01-20 17:08:31 +08001367 * extensions this context supports. If you configured with
1368 * --without-extensions, you should give NULL here.
Andy Green3faa9c72010-11-08 17:03:03 +00001369 * @ssl_cert_filepath: If libwebsockets was compiled to use ssl, and you want
Andy Green8f037e42010-12-19 22:13:26 +00001370 * to listen using SSL, set to the filepath to fetch the
1371 * server cert from, otherwise NULL for unencrypted
Andy Green3faa9c72010-11-08 17:03:03 +00001372 * @ssl_private_key_filepath: filepath to private key if wanting SSL mode,
Andy Green8f037e42010-12-19 22:13:26 +00001373 * else ignored
David Galeano2f82be82013-01-09 16:25:54 +08001374 * @ssl_ca_filepath: CA certificate filepath or NULL
Andy Green3faa9c72010-11-08 17:03:03 +00001375 * @gid: group id to change to after setting listen socket, or -1.
1376 * @uid: user id to change to after setting listen socket, or -1.
Andy Greenbfb051f2011-02-09 08:49:14 +00001377 * @options: 0, or LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK
Andy Green15e31f32012-10-19 18:36:28 +08001378 * @user: optional user pointer that can be recovered via the context
1379 * pointer using libwebsocket_context_user
Andy Green05464c62010-11-12 10:44:18 +00001380 *
Andy Green8f037e42010-12-19 22:13:26 +00001381 * This function creates the listening socket and takes care
1382 * of all initialization in one step.
1383 *
Andy Greene92cd172011-01-19 13:11:55 +00001384 * After initialization, it returns a struct libwebsocket_context * that
1385 * represents this server. After calling, user code needs to take care
1386 * of calling libwebsocket_service() with the context pointer to get the
1387 * server's sockets serviced. This can be done in the same process context
1388 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001389 *
Andy Green8f037e42010-12-19 22:13:26 +00001390 * The protocol callback functions are called for a handful of events
1391 * including http requests coming in, websocket connections becoming
1392 * established, and data arriving; it's also called periodically to allow
1393 * async transmission.
1394 *
1395 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1396 * at that time websocket protocol has not been negotiated. Other
1397 * protocols after the first one never see any HTTP callack activity.
1398 *
1399 * The server created is a simple http server by default; part of the
1400 * websocket standard is upgrading this http connection to a websocket one.
1401 *
1402 * This allows the same server to provide files like scripts and favicon /
1403 * images or whatever over http and dynamic data over websockets all in
1404 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001405 */
Andy Green4ea60062010-10-30 12:15:07 +01001406
Andy Greene92cd172011-01-19 13:11:55 +00001407struct libwebsocket_context *
Peter Hinz56885f32011-03-02 22:03:47 +00001408libwebsocket_create_context(int port, const char *interf,
Andy Greenb45993c2010-12-18 15:13:50 +00001409 struct libwebsocket_protocols *protocols,
Andy Greend6e09112011-03-05 16:12:15 +00001410 struct libwebsocket_extension *extensions,
Andy Green8f037e42010-12-19 22:13:26 +00001411 const char *ssl_cert_filepath,
1412 const char *ssl_private_key_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08001413 const char *ssl_ca_filepath,
Alon Levy0291eb32012-10-19 11:21:56 +02001414 int gid, int uid, unsigned int options,
David Galeano2f82be82013-01-09 16:25:54 +08001415 void *user)
Andy Greenff95d7a2010-10-28 22:36:01 +01001416{
1417 int n;
Andy Greenf5bc1302013-01-21 09:09:52 +08001418 struct sockaddr_in serv_addr;
Andy Green251f6fa2010-11-03 11:13:06 +00001419 int opt = 1;
Peter Hinz56885f32011-03-02 22:03:47 +00001420 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001421 char *p;
Andy Green0d338332011-02-12 11:57:43 +00001422 struct libwebsocket *wsi;
Andy Green3182ece2013-01-20 17:08:31 +08001423#ifndef LWS_NO_EXTENSIONS
1424 int m;
1425#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001426
Andy Green3faa9c72010-11-08 17:03:03 +00001427#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001428 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001429 char ssl_err_buf[512];
Andy Green3faa9c72010-11-08 17:03:03 +00001430#endif
1431
Andy Greenb3a614a2013-01-19 13:08:17 +08001432 lwsl_notice("Initial logging level %d\n", log_level);
Andy Greenc0d6b632013-01-12 23:42:17 +08001433 lwsl_info(" LWS_MAX_HEADER_NAME_LENGTH: %u\n", LWS_MAX_HEADER_NAME_LENGTH);
1434 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
1435 lwsl_info(" LWS_INITIAL_HDR_ALLOC: %u\n", LWS_INITIAL_HDR_ALLOC);
1436 lwsl_info(" LWS_ADDITIONAL_HDR_ALLOC: %u\n", LWS_ADDITIONAL_HDR_ALLOC);
1437 lwsl_info(" MAX_USER_RX_BUFFER: %u\n", MAX_USER_RX_BUFFER);
Andy Greenc0d6b632013-01-12 23:42:17 +08001438 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001439#ifndef LWS_NO_EXTENSIONS
Andy Greenc0d6b632013-01-12 23:42:17 +08001440 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n", LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001441#else
1442 lwsl_notice(" Configured without extension support\n");
1443#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001444 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1445 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
1446 lwsl_info(" CIPHERS_LIST_STRING: '%s'\n", CIPHERS_LIST_STRING);
1447 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1448 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001449
Peter Hinz56885f32011-03-02 22:03:47 +00001450#ifdef _WIN32
1451 {
1452 WORD wVersionRequested;
1453 WSADATA wsaData;
1454 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001455 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001456
1457 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1458 wVersionRequested = MAKEWORD(2, 2);
1459
1460 err = WSAStartup(wVersionRequested, &wsaData);
1461 if (err != 0) {
1462 /* Tell the user that we could not find a usable */
1463 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001464 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001465 return NULL;
1466 }
David Galeano7b11fec2011-10-04 19:55:18 +08001467
Andy Green6ee372f2012-04-09 15:09:01 +08001468 /* default to a poll() made out of select() */
1469 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001470
Andy Green6ee372f2012-04-09 15:09:01 +08001471 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001472 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001473 if (wsdll)
1474 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Peter Hinz56885f32011-03-02 22:03:47 +00001475 }
1476#endif
1477
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001478 context = (struct libwebsocket_context *) malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001479 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001480 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001481 return NULL;
1482 }
Andy Green35f332b2013-01-21 13:06:38 +08001483#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001484 extern int pid_daemon;
1485 context->started_with_parent = pid_daemon;
1486 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1487#endif
1488
Peter Hinz56885f32011-03-02 22:03:47 +00001489 context->protocols = protocols;
1490 context->listen_port = port;
1491 context->http_proxy_port = 0;
1492 context->http_proxy_address[0] = '\0';
1493 context->options = options;
Andy Greendfb23042013-01-17 12:26:48 +08001494 /* to reduce this allocation, */
1495 context->max_fds = getdtablesize();
Andy Greenb3a614a2013-01-19 13:08:17 +08001496 lwsl_notice(" max fd tracked: %u\n", context->max_fds);
Andy Greena17c6922013-01-20 20:21:54 +08001497 lwsl_notice(" static allocation: %u bytes\n",
1498 (sizeof(struct pollfd) * context->max_fds) +
1499 (sizeof(struct libwebsocket *) * context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08001500
1501 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) * context->max_fds);
1502 if (context->fds == NULL) {
1503 lwsl_err("Unable to allocate fds array for %d connections\n", context->max_fds);
1504 free(context);
1505 return NULL;
1506 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08001507 context->lws_lookup = (struct libwebsocket **)malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001508 if (context->lws_lookup == NULL) {
1509 lwsl_err("Unable to allocate lws_lookup array for %d connections\n", context->max_fds);
1510 free(context->fds);
1511 free(context);
1512 return NULL;
1513 }
Andy Greena17c6922013-01-20 20:21:54 +08001514
Peter Hinz56885f32011-03-02 22:03:47 +00001515 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001516#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00001517 context->extensions = extensions;
Andy Green3182ece2013-01-20 17:08:31 +08001518#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08001519 context->last_timeout_check_s = 0;
Andy Greendfb23042013-01-17 12:26:48 +08001520 context->user_space = user;
Andy Green9659f372011-01-27 22:01:43 +00001521
Peter Hinz56885f32011-03-02 22:03:47 +00001522#ifdef WIN32
1523 context->fd_random = 0;
1524#else
1525 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
1526 if (context->fd_random < 0) {
Andy Greendfb23042013-01-17 12:26:48 +08001527 free(context);
Andy Green43db0452013-01-10 19:50:35 +08001528 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001529 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Andy Green44eee682011-02-10 09:32:24 +00001530 return NULL;
1531 }
Peter Hinz56885f32011-03-02 22:03:47 +00001532#endif
Andy Green44eee682011-02-10 09:32:24 +00001533
Peter Hinz56885f32011-03-02 22:03:47 +00001534#ifdef LWS_OPENSSL_SUPPORT
1535 context->use_ssl = 0;
1536 context->ssl_ctx = NULL;
1537 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00001538 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001539#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00001540
Andy Greena1ce6be2013-01-18 11:43:21 +08001541 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08001542
Andy Greena1ce6be2013-01-18 11:43:21 +08001543#ifndef LWS_NO_SERVER
1544 if (!(options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
1545 struct sockaddr sa;
1546 char hostname[1024] = "";
Andy Green788c4a82012-10-22 12:29:57 +01001547
1548 /* find canonical hostname */
1549
1550 hostname[(sizeof hostname) - 1] = '\0';
1551 memset(&sa, 0, sizeof(sa));
1552 sa.sa_family = AF_INET;
1553 sa.sa_data[(sizeof sa.sa_data) - 1] = '\0';
1554 gethostname(hostname, (sizeof hostname) - 1);
1555
1556 n = 0;
1557
David Galeanoed3c8402013-01-10 10:45:24 +08001558 if (strlen(hostname) < sizeof(sa.sa_data) - 1) {
Andy Green788c4a82012-10-22 12:29:57 +01001559 strcpy(sa.sa_data, hostname);
Andy Green43db0452013-01-10 19:50:35 +08001560 // lwsl_debug("my host name is %s\n", sa.sa_data);
Andy Green788c4a82012-10-22 12:29:57 +01001561 n = getnameinfo(&sa, sizeof(sa), hostname,
1562 (sizeof hostname) - 1, NULL, 0, 0);
1563 }
1564
1565 if (!n) {
1566 strncpy(context->canonical_hostname, hostname,
1567 sizeof context->canonical_hostname - 1);
1568 context->canonical_hostname[
1569 sizeof context->canonical_hostname - 1] = '\0';
1570 } else
1571 strncpy(context->canonical_hostname, hostname,
1572 sizeof context->canonical_hostname - 1);
1573
Andy Greenb3a614a2013-01-19 13:08:17 +08001574 lwsl_notice(" canonical_hostname = %s\n", context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08001575 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001576#endif
Andy Greena69f0512012-05-03 12:32:38 +08001577
Andy Green9659f372011-01-27 22:01:43 +00001578 /* split the proxy ads:port if given */
1579
1580 p = getenv("http_proxy");
1581 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00001582 strncpy(context->http_proxy_address, p,
Andy Green6ee372f2012-04-09 15:09:01 +08001583 sizeof context->http_proxy_address - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00001584 context->http_proxy_address[
1585 sizeof context->http_proxy_address - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00001586
Peter Hinz56885f32011-03-02 22:03:47 +00001587 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00001588 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001589 lwsl_err("http_proxy needs to be ads:port\n");
Andy Green9659f372011-01-27 22:01:43 +00001590 return NULL;
1591 }
1592 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00001593 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00001594
Andy Greenb3a614a2013-01-19 13:08:17 +08001595 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001596 context->http_proxy_address,
1597 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00001598 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001599
Andy Greena1ce6be2013-01-18 11:43:21 +08001600#ifndef LWS_NO_SERVER
Andy Green90c7cbc2011-01-27 06:26:52 +00001601 if (port) {
1602
Andy Green3faa9c72010-11-08 17:03:03 +00001603#ifdef LWS_OPENSSL_SUPPORT
Peter Hinz56885f32011-03-02 22:03:47 +00001604 context->use_ssl = ssl_cert_filepath != NULL &&
Andy Green90c7cbc2011-01-27 06:26:52 +00001605 ssl_private_key_filepath != NULL;
Peter Hinz56885f32011-03-02 22:03:47 +00001606 if (context->use_ssl)
Andy Greenb3a614a2013-01-19 13:08:17 +08001607 lwsl_notice(" Compiled with SSL support, using it\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001608 else
Andy Greenb3a614a2013-01-19 13:08:17 +08001609 lwsl_notice(" Compiled with SSL support, not using it\n");
Andy Green3faa9c72010-11-08 17:03:03 +00001610
Andy Green90c7cbc2011-01-27 06:26:52 +00001611#else
1612 if (ssl_cert_filepath != NULL &&
1613 ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08001614 lwsl_notice(" Not compiled for OpenSSl support!\n");
Andy Greene92cd172011-01-19 13:11:55 +00001615 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +00001616 }
Andy Greenb3a614a2013-01-19 13:08:17 +08001617 lwsl_notice(" Compiled without SSL support, "
Andy Green90c7cbc2011-01-27 06:26:52 +00001618 "serving unencrypted\n");
1619#endif
Andy Greena17c6922013-01-20 20:21:54 +08001620
1621 lwsl_notice(" per-connection allocation: %u + headers\n", sizeof(struct libwebsocket));
Andy Green90c7cbc2011-01-27 06:26:52 +00001622 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001623#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001624
1625 /* ignore SIGPIPE */
Peter Hinz56885f32011-03-02 22:03:47 +00001626#ifdef WIN32
1627#else
Andy Green90c7cbc2011-01-27 06:26:52 +00001628 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00001629#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001630
1631
1632#ifdef LWS_OPENSSL_SUPPORT
1633
1634 /* basic openssl init */
1635
1636 SSL_library_init();
1637
1638 OpenSSL_add_all_algorithms();
1639 SSL_load_error_strings();
1640
Andy Green2e24da02011-03-05 16:12:04 +00001641 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00001642 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
1643
Andy Green90c7cbc2011-01-27 06:26:52 +00001644 /*
1645 * Firefox insists on SSLv23 not SSLv3
1646 * Konq disables SSLv2 by default now, SSLv23 works
1647 */
1648
1649 method = (SSL_METHOD *)SSLv23_server_method();
1650 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001651 lwsl_err("problem creating ssl method: %s\n",
Andy Green90c7cbc2011-01-27 06:26:52 +00001652 ERR_error_string(ERR_get_error(), ssl_err_buf));
1653 return NULL;
1654 }
Peter Hinz56885f32011-03-02 22:03:47 +00001655 context->ssl_ctx = SSL_CTX_new(method); /* create context */
1656 if (!context->ssl_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001657 lwsl_err("problem creating ssl context: %s\n",
Andy Green90c7cbc2011-01-27 06:26:52 +00001658 ERR_error_string(ERR_get_error(), ssl_err_buf));
1659 return NULL;
1660 }
1661
David Galeanocc148e42013-01-10 10:18:59 +08001662#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001663 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001664#endif
David Galeano77a677c2013-01-10 10:14:12 +08001665 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
David Galeanof177f2a2013-01-10 10:15:19 +08001666 SSL_CTX_set_cipher_list(context->ssl_ctx, CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001667
Andy Greena1ce6be2013-01-18 11:43:21 +08001668#ifndef LWS_NO_CLIENT
1669
Andy Green90c7cbc2011-01-27 06:26:52 +00001670 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08001671
1672 if (port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00001673 method = (SSL_METHOD *)SSLv23_client_method();
1674 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001675 lwsl_err("problem creating ssl method: %s\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001676 ERR_error_string(ERR_get_error(), ssl_err_buf));
1677 return NULL;
1678 }
1679 /* create context */
1680 context->ssl_client_ctx = SSL_CTX_new(method);
1681 if (!context->ssl_client_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001682 lwsl_err("problem creating ssl context: %s\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001683 ERR_error_string(ERR_get_error(), ssl_err_buf));
1684 return NULL;
1685 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001686
David Galeanocc148e42013-01-10 10:18:59 +08001687#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001688 SSL_CTX_set_options(context->ssl_client_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001689#endif
David Galeano77a677c2013-01-10 10:14:12 +08001690 SSL_CTX_set_options(context->ssl_client_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
David Galeanof177f2a2013-01-10 10:15:19 +08001691 SSL_CTX_set_cipher_list(context->ssl_client_ctx, CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001692
Peter Hinz56885f32011-03-02 22:03:47 +00001693 /* openssl init for cert verification (for client sockets) */
David Galeano2f82be82013-01-09 16:25:54 +08001694 if (!ssl_ca_filepath) {
1695 if (!SSL_CTX_load_verify_locations(
1696 context->ssl_client_ctx, NULL,
1697 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08001698 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08001699 "Unable to load SSL Client certs from %s "
1700 "(set by --with-client-cert-dir= in configure) -- "
1701 " client ssl isn't going to work",
1702 LWS_OPENSSL_CLIENT_CERTS);
1703 } else
1704 if (!SSL_CTX_load_verify_locations(
1705 context->ssl_client_ctx, ssl_ca_filepath,
1706 NULL))
Andy Green43db0452013-01-10 19:50:35 +08001707 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08001708 "Unable to load SSL Client certs "
1709 "file from %s -- client ssl isn't "
1710 "going to work", ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00001711
1712 /*
1713 * callback allowing user code to load extra verification certs
1714 * helping the client to verify server identity
1715 */
1716
1717 context->protocols[0].callback(context, NULL,
1718 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
1719 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00001720 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001721#endif
Andy Green6ee372f2012-04-09 15:09:01 +08001722
Andy Greenc6bf2c22011-02-20 11:10:47 +00001723 /* as a server, are we requiring clients to identify themselves? */
1724
1725 if (options & LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
1726
1727 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08001728
Peter Hinz56885f32011-03-02 22:03:47 +00001729 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00001730 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1731 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001732
1733 /*
1734 * give user code a chance to load certs into the server
1735 * allowing it to verify incoming client certs
1736 */
1737
Peter Hinz56885f32011-03-02 22:03:47 +00001738 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00001739 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00001740 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001741 }
1742
Peter Hinz56885f32011-03-02 22:03:47 +00001743 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001744
1745 /* openssl init for server sockets */
1746
Andy Green3faa9c72010-11-08 17:03:03 +00001747 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08001748 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
1749 ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00001750 if (n != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001751 lwsl_err("problem getting cert '%s': %s\n",
Andy Green3faa9c72010-11-08 17:03:03 +00001752 ssl_cert_filepath,
1753 ERR_error_string(ERR_get_error(), ssl_err_buf));
Andy Greene92cd172011-01-19 13:11:55 +00001754 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +00001755 }
1756 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00001757 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
1758 ssl_private_key_filepath, SSL_FILETYPE_PEM) != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001759 lwsl_err("ssl problem getting key '%s': %s\n",
Andy Green018d8eb2010-11-08 21:04:23 +00001760 ssl_private_key_filepath,
1761 ERR_error_string(ERR_get_error(), ssl_err_buf));
Andy Greene92cd172011-01-19 13:11:55 +00001762 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +00001763 }
1764 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00001765 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08001766 lwsl_err("Private SSL key doesn't match cert\n");
Andy Greene92cd172011-01-19 13:11:55 +00001767 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +00001768 }
1769
1770 /* SSL is happy and has a cert it's content with */
1771 }
1772#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001773
Andy Greendf736162011-01-18 15:39:02 +00001774 /* selftest */
1775
1776 if (lws_b64_selftest())
Andy Greene92cd172011-01-19 13:11:55 +00001777 return NULL;
Andy Greendf736162011-01-18 15:39:02 +00001778
Andy Greena1ce6be2013-01-18 11:43:21 +08001779#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00001780 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00001781
Andy Green4739e5c2011-01-22 12:51:57 +00001782 if (port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08001783 extern int interface_to_sa(const char *ifname, struct sockaddr_in *addr, size_t addrlen);
1784 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00001785
Andy Green4739e5c2011-01-22 12:51:57 +00001786 sockfd = socket(AF_INET, SOCK_STREAM, 0);
1787 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08001788 lwsl_err("ERROR opening socket\n");
Andy Green4739e5c2011-01-22 12:51:57 +00001789 return NULL;
1790 }
Andy Green775c0dd2010-10-29 14:15:22 +01001791
Andy Green4739e5c2011-01-22 12:51:57 +00001792 /* allow us to restart even if old sockets in TIME_WAIT */
Andy Green6ee372f2012-04-09 15:09:01 +08001793 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
1794 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00001795
1796 /* Disable Nagle */
1797 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08001798 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
1799 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00001800
Andy Greene2160712013-01-28 12:19:10 +08001801 fcntl(sockfd, F_SETFL, O_NONBLOCK);
1802
Andy Green4739e5c2011-01-22 12:51:57 +00001803 bzero((char *) &serv_addr, sizeof(serv_addr));
1804 serv_addr.sin_family = AF_INET;
Peter Hinz56885f32011-03-02 22:03:47 +00001805 if (interf == NULL)
Andy Green32375b72011-02-19 08:32:53 +00001806 serv_addr.sin_addr.s_addr = INADDR_ANY;
1807 else
Peter Hinz56885f32011-03-02 22:03:47 +00001808 interface_to_sa(interf, &serv_addr,
Andy Green32375b72011-02-19 08:32:53 +00001809 sizeof(serv_addr));
Andy Green4739e5c2011-01-22 12:51:57 +00001810 serv_addr.sin_port = htons(port);
1811
1812 n = bind(sockfd, (struct sockaddr *) &serv_addr,
1813 sizeof(serv_addr));
1814 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001815 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green8f037e42010-12-19 22:13:26 +00001816 port, n, errno);
Andy Green41c58032013-01-12 13:21:08 +08001817 close(sockfd);
Andy Green4739e5c2011-01-22 12:51:57 +00001818 return NULL;
1819 }
Andy Green0d338332011-02-12 11:57:43 +00001820
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001821 wsi = (struct libwebsocket *)malloc(sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08001822 if (wsi == NULL) {
1823 lwsl_err("Out of mem\n");
1824 close(sockfd);
1825 return NULL;
1826 }
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001827 memset(wsi, 0, sizeof (struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00001828 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08001829#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00001830 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001831#endif
Andy Green0d338332011-02-12 11:57:43 +00001832 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08001833
1834 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001835
Andy Green65b0e912013-01-16 07:59:47 +08001836 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
1837 context->listen_service_count = 0;
1838 context->listen_service_fd = sockfd;
1839
Andy Greena824d182013-01-15 20:52:29 +08001840 listen(sockfd, LWS_SOMAXCONN);
Andy Greenb3a614a2013-01-19 13:08:17 +08001841 lwsl_notice(" Listening on port %d\n", port);
Andy Green8f037e42010-12-19 22:13:26 +00001842 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001843#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001844
Andy Green6ee372f2012-04-09 15:09:01 +08001845 /*
1846 * drop any root privs for this process
1847 * to listen on port < 1023 we would have needed root, but now we are
1848 * listening, we don't want the power for anything else
1849 */
Peter Hinz56885f32011-03-02 22:03:47 +00001850#ifdef WIN32
1851#else
Andy Green3faa9c72010-11-08 17:03:03 +00001852 if (gid != -1)
1853 if (setgid(gid))
Andy Green43db0452013-01-10 19:50:35 +08001854 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green3faa9c72010-11-08 17:03:03 +00001855 if (uid != -1)
1856 if (setuid(uid))
Andy Green43db0452013-01-10 19:50:35 +08001857 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00001858#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001859
Andy Green6f520a52013-01-29 17:57:39 +08001860 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00001861
Peter Hinz56885f32011-03-02 22:03:47 +00001862 for (context->count_protocols = 0;
1863 protocols[context->count_protocols].callback;
1864 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01001865
Andy Green43db0452013-01-10 19:50:35 +08001866 lwsl_parser(" Protocol: %s\n",
1867 protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01001868
Peter Hinz56885f32011-03-02 22:03:47 +00001869 protocols[context->count_protocols].owning_server = context;
1870 protocols[context->count_protocols].protocol_index =
1871 context->count_protocols;
Andy Greenb45993c2010-12-18 15:13:50 +00001872 }
Andy Greenf5bc1302013-01-21 09:09:52 +08001873
Andy Green3182ece2013-01-20 17:08:31 +08001874#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001875 /*
1876 * give all extensions a chance to create any per-context
1877 * allocations they need
1878 */
1879
1880 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
1881 if (port)
1882 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andrew Chambersd5512172012-05-20 08:17:09 +08001883
1884 if (extensions) {
1885 while (extensions->callback) {
Andy Green43db0452013-01-10 19:50:35 +08001886 lwsl_ext(" Extension: %s\n", extensions->name);
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001887 extensions->callback(context, extensions, NULL,
1888 (enum libwebsocket_extension_callback_reasons)m,
1889 NULL, NULL, 0);
Andrew Chambersd5512172012-05-20 08:17:09 +08001890 extensions++;
1891 }
Andy Greena41314f2011-05-23 10:00:03 +01001892 }
Andy Green3182ece2013-01-20 17:08:31 +08001893#endif
Peter Hinz56885f32011-03-02 22:03:47 +00001894 return context;
Andy Greene92cd172011-01-19 13:11:55 +00001895}
Andy Greenb45993c2010-12-18 15:13:50 +00001896
Andy Greenb45993c2010-12-18 15:13:50 +00001897/**
1898 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00001899 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00001900 * @wsi: pointer to struct websocket you want to know the protocol of
1901 *
Andy Green8f037e42010-12-19 22:13:26 +00001902 *
Andy Green6f520a52013-01-29 17:57:39 +08001903 * Some apis can act on all live connections of a given protocol,
1904 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00001905 */
Andy Greenab990e42010-10-31 12:42:52 +00001906
Andy Greenb45993c2010-12-18 15:13:50 +00001907const struct libwebsocket_protocols *
1908libwebsockets_get_protocol(struct libwebsocket *wsi)
1909{
1910 return wsi->protocol;
1911}
1912
Andy Green82c3d542011-03-07 21:16:31 +00001913int
1914libwebsocket_is_final_fragment(struct libwebsocket *wsi)
1915{
Andy Green623a98d2013-01-21 11:04:23 +08001916 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00001917}
Alex Bligh49146db2011-11-07 17:19:25 +08001918
David Galeanoe2cf9922013-01-09 18:06:55 +08001919unsigned char
1920libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
1921{
Andy Green623a98d2013-01-21 11:04:23 +08001922 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08001923}
1924
Alex Bligh49146db2011-11-07 17:19:25 +08001925void *
1926libwebsocket_ensure_user_space(struct libwebsocket *wsi)
1927{
1928 /* allocate the per-connection user memory (if any) */
1929
1930 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
1931 wsi->user_space = malloc(
1932 wsi->protocol->per_session_data_size);
1933 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001934 lwsl_err("Out of memory for conn user space\n");
Alex Bligh49146db2011-11-07 17:19:25 +08001935 return NULL;
1936 }
Andy Green6ee372f2012-04-09 15:09:01 +08001937 memset(wsi->user_space, 0,
1938 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08001939 }
1940 return wsi->user_space;
1941}
Andy Green43db0452013-01-10 19:50:35 +08001942
Andy Green0b319092013-01-19 11:17:56 +08001943static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08001944{
Andy Green0b319092013-01-19 11:17:56 +08001945 char buf[300];
1946 struct timeval tv;
Andy Green0b319092013-01-19 11:17:56 +08001947 int n;
1948
1949 gettimeofday(&tv, NULL);
1950
1951 buf[0] = '\0';
1952 for (n = 0; n < LLL_COUNT; n++)
1953 if (level == (1 << n)) {
Andy Green058ba812013-01-19 11:32:18 +08001954 sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
Andy Green0b319092013-01-19 11:17:56 +08001955 (int)(tv.tv_usec / 100), log_level_names[n]);
1956 break;
1957 }
1958
1959 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08001960}
1961
Andy Greenc11db202013-01-19 11:12:16 +08001962void lwsl_emit_syslog(int level, const char *line)
1963{
1964 int syslog_level = LOG_DEBUG;
1965
1966 switch (level) {
1967 case LLL_ERR:
1968 syslog_level = LOG_ERR;
1969 break;
1970 case LLL_WARN:
1971 syslog_level = LOG_WARNING;
1972 break;
1973 case LLL_NOTICE:
1974 syslog_level = LOG_NOTICE;
1975 break;
1976 case LLL_INFO:
1977 syslog_level = LOG_INFO;
1978 break;
1979 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08001980 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08001981}
1982
Andy Green43db0452013-01-10 19:50:35 +08001983void _lws_log(int filter, const char *format, ...)
1984{
Andy Greende8f27a2013-01-12 09:17:42 +08001985 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08001986 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08001987
1988 if (!(log_level & filter))
1989 return;
1990
Andy Green43db0452013-01-10 19:50:35 +08001991 va_start(ap, format);
Andy Green0b319092013-01-19 11:17:56 +08001992 vsnprintf(buf, (sizeof buf), format, ap);
Andy Greende8f27a2013-01-12 09:17:42 +08001993 buf[(sizeof buf) - 1] = '\0';
1994 va_end(ap);
1995
Andy Green0b319092013-01-19 11:17:56 +08001996 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08001997}
1998
1999/**
2000 * lws_set_log_level() - Set the logging bitfield
2001 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002002 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2003 * function to perform log string emission instead of
2004 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002005 *
Andy Greende8f27a2013-01-12 09:17:42 +08002006 * log level defaults to "err" and "warn" contexts enabled only and
2007 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002008 */
2009
Andy Green058ba812013-01-19 11:32:18 +08002010void lws_set_log_level(int level, void (*log_emit_function)(int level, const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002011{
2012 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002013 if (log_emit_function)
2014 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002015}