blob: f5ef23be17094c25e55bf7f1495019ddf2471d4a [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",
Andy Greend636e352013-01-29 12:36:17 +080066 "LATENCY",
Andy Green43db0452013-01-10 19:50:35 +080067};
68
Andy Green0d338332011-02-12 11:57:43 +000069int
Andy Greendfb23042013-01-17 12:26:48 +080070insert_wsi_socket_into_fds(struct libwebsocket_context *context, struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +000071{
Andy Greendfb23042013-01-17 12:26:48 +080072 if (context->fds_count >= context->max_fds) {
73 lwsl_err("Reached limit of fds tracking (%d)\n", context->max_fds);
Andy Green0d338332011-02-12 11:57:43 +000074 return 1;
75 }
76
Andy Greendfb23042013-01-17 12:26:48 +080077 if (wsi->sock > context->max_fds) {
78 lwsl_err("Socket fd %d is beyond what we can index (%d)\n", wsi->sock, context->max_fds);
79 return 1;
80 }
81
82 assert(wsi);
83 assert(wsi->sock);
84
85 lwsl_info("insert_wsi_socket_into_fds: wsi=%p, sock=%d, fds pos=%d\n", wsi, wsi->sock, context->fds_count);
86
87 context->lws_lookup[wsi->sock] = wsi;
88 wsi->position_in_fds_table = context->fds_count;
89 context->fds[context->fds_count].fd = wsi->sock;
90 context->fds[context->fds_count].events = POLLIN;
91 context->fds[context->fds_count++].revents = 0;
92
93 /* external POLL support via protocol 0 */
94 context->protocols[0].callback(context, wsi,
95 LWS_CALLBACK_ADD_POLL_FD,
96 (void *)(long)wsi->sock, NULL, POLLIN);
Andy Green0d338332011-02-12 11:57:43 +000097
98 return 0;
99}
100
Andy Greendfb23042013-01-17 12:26:48 +0800101static int
102remove_wsi_socket_from_fds(struct libwebsocket_context *context, struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000103{
Andy Greendfb23042013-01-17 12:26:48 +0800104 int m;
Andy Green0d338332011-02-12 11:57:43 +0000105
Andy Greendfb23042013-01-17 12:26:48 +0800106 if (!--context->fds_count)
107 goto do_ext;
Andy Green0d338332011-02-12 11:57:43 +0000108
Andy Greendfb23042013-01-17 12:26:48 +0800109 if (wsi->sock > context->max_fds) {
110 lwsl_err("Socket fd %d is beyond what we can index (%d)\n", wsi->sock, context->max_fds);
111 return 1;
112 }
Andy Green0d338332011-02-12 11:57:43 +0000113
Andy Greendfb23042013-01-17 12:26:48 +0800114 lwsl_info("remove_wsi_socket_from_fds: wsi=%p, sock=%d, fds pos=%d\n", wsi, wsi->sock, wsi->position_in_fds_table);
115
116 m = wsi->position_in_fds_table; /* replace the contents for this */
117
118 /* have the last guy take up the vacant slot */
119 context->fds[m] = context->fds[context->fds_count]; /* vacant fds slot filled with end one */
120 /* end guy's fds_lookup entry remains unchanged (still same fd pointing to same wsi) */
121 /* end guy's "position in fds table" changed */
122 context->lws_lookup[context->fds[context->fds_count].fd]->position_in_fds_table = m;
123 /* deletion guy's lws_lookup entry needs nuking */
124 context->lws_lookup[wsi->sock] = NULL; /* no WSI for the socket of the wsi being removed*/
125 wsi->position_in_fds_table = -1; /* removed wsi has no position any more */
126
127do_ext:
128 /* remove also from external POLL support via protocol 0 */
129 if (wsi->sock)
130 context->protocols[0].callback(context, wsi,
131 LWS_CALLBACK_DEL_POLL_FD, (void *)(long)wsi->sock, NULL, 0);
132
133 return 0;
Andy Green0d338332011-02-12 11:57:43 +0000134}
135
Andy Green32375b72011-02-19 08:32:53 +0000136
Andy Green8f037e42010-12-19 22:13:26 +0000137void
Peter Hinz56885f32011-03-02 22:03:47 +0000138libwebsocket_close_and_free_session(struct libwebsocket_context *context,
Andy Green687b0182011-02-26 11:04:01 +0000139 struct libwebsocket *wsi, enum lws_close_status reason)
Andy Green251f6fa2010-11-03 11:13:06 +0000140{
Andy Greenb45993c2010-12-18 15:13:50 +0000141 int n;
Andy Green62c54d22011-02-14 09:14:25 +0000142 int old_state;
Andy Green5e1fa172011-02-10 09:07:05 +0000143 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 +
144 LWS_SEND_BUFFER_POST_PADDING];
Andy Green3182ece2013-01-20 17:08:31 +0800145#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000146 int ret;
147 int m;
148 struct lws_tokens eff_buf;
Andy Greena41314f2011-05-23 10:00:03 +0100149 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +0800150#endif
Andy Greenb45993c2010-12-18 15:13:50 +0000151
Andy Green4b6fbe12011-02-14 08:03:48 +0000152 if (!wsi)
Andy Greenb45993c2010-12-18 15:13:50 +0000153 return;
154
Andy Green62c54d22011-02-14 09:14:25 +0000155 old_state = wsi->state;
Andy Green251f6fa2010-11-03 11:13:06 +0000156
Andy Green62c54d22011-02-14 09:14:25 +0000157 if (old_state == WSI_STATE_DEAD_SOCKET)
Andy Green5e1fa172011-02-10 09:07:05 +0000158 return;
159
Andy Green623a98d2013-01-21 11:04:23 +0800160 wsi->u.ws.close_reason = reason;
Andy Greenda527df2011-03-07 07:08:12 +0000161
Andy Green3182ece2013-01-20 17:08:31 +0800162#ifndef LWS_NO_EXTENSIONS
Andy Greenda527df2011-03-07 07:08:12 +0000163 /*
Andy Green68b45042011-05-25 21:41:57 +0100164 * are his extensions okay with him closing? Eg he might be a mux
165 * parent and just his ch1 aspect is closing?
166 */
167
Andy Green68b45042011-05-25 21:41:57 +0100168 for (n = 0; n < wsi->count_active_extensions; n++) {
169 if (!wsi->active_extensions[n]->callback)
170 continue;
171
172 m = wsi->active_extensions[n]->callback(context,
173 wsi->active_extensions[n], wsi,
174 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
175 wsi->active_extensions_user[n], NULL, 0);
176
177 /*
178 * if somebody vetoed actually closing him at this time....
179 * up to the extension to track the attempted close, let's
180 * just bail
181 */
182
183 if (m) {
Andy Green43db0452013-01-10 19:50:35 +0800184 lwsl_ext("extension vetoed close\n");
Andy Green68b45042011-05-25 21:41:57 +0100185 return;
186 }
187 }
188
Andy Green68b45042011-05-25 21:41:57 +0100189 /*
Andy Greenc44159f2011-03-07 07:08:18 +0000190 * flush any tx pending from extensions, since we may send close packet
191 * if there are problems with send, just nuke the connection
192 */
193
194 ret = 1;
195 while (ret == 1) {
196
197 /* default to nobody has more to spill */
198
199 ret = 0;
200 eff_buf.token = NULL;
201 eff_buf.token_len = 0;
202
203 /* show every extension the new incoming data */
204
205 for (n = 0; n < wsi->count_active_extensions; n++) {
206 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000207 wsi->protocol->owning_server,
208 wsi->active_extensions[n], wsi,
Andy Greenc44159f2011-03-07 07:08:18 +0000209 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
210 wsi->active_extensions_user[n], &eff_buf, 0);
211 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800212 lwsl_ext("Extension reports fatal error\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000213 goto just_kill_connection;
214 }
215 if (m)
216 /*
217 * at least one extension told us he has more
218 * to spill, so we will go around again after
219 */
220 ret = 1;
221 }
222
223 /* assuming they left us something to send, send it */
224
225 if (eff_buf.token_len)
226 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
Andy Green0303db42013-01-17 14:46:43 +0800227 eff_buf.token_len)) {
228 lwsl_debug("close: sending final extension spill had problems\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000229 goto just_kill_connection;
Andy Green0303db42013-01-17 14:46:43 +0800230 }
Andy Greenc44159f2011-03-07 07:08:18 +0000231 }
Andy Green3182ece2013-01-20 17:08:31 +0800232#endif
Andy Greenc44159f2011-03-07 07:08:18 +0000233
234 /*
Andy Greenda527df2011-03-07 07:08:12 +0000235 * signal we are closing, libsocket_write will
236 * add any necessary version-specific stuff. If the write fails,
237 * no worries we are closing anyway. If we didn't initiate this
238 * close, then our state has been changed to
239 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this.
240 *
241 * Likewise if it's a second call to close this connection after we
242 * sent the close indication to the peer already, we are in state
243 * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time.
244 */
245
246 if (old_state == WSI_STATE_ESTABLISHED &&
247 reason != LWS_CLOSE_STATUS_NOSTATUS) {
Andy Green66a16f32011-05-24 22:07:45 +0100248
Andy Green43db0452013-01-10 19:50:35 +0800249 lwsl_debug("sending close indication...\n");
Andy Green66a16f32011-05-24 22:07:45 +0100250
Andy Greenda527df2011-03-07 07:08:12 +0000251 n = libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING],
252 0, LWS_WRITE_CLOSE);
253 if (!n) {
254 /*
255 * we have sent a nice protocol level indication we
256 * now wish to close, we should not send anything more
257 */
258
259 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
260
Andy Green0303db42013-01-17 14:46:43 +0800261 /* and we should wait for a reply for a bit out of politeness */
Andy Greenda527df2011-03-07 07:08:12 +0000262
263 libwebsocket_set_timeout(wsi,
Andy Green0303db42013-01-17 14:46:43 +0800264 PENDING_TIMEOUT_CLOSE_ACK, 1);
Andy Greenda527df2011-03-07 07:08:12 +0000265
Andy Green43db0452013-01-10 19:50:35 +0800266 lwsl_debug("sent close indication, awaiting ack\n");
Andy Greenda527df2011-03-07 07:08:12 +0000267
268 return;
269 }
270
Andy Green0303db42013-01-17 14:46:43 +0800271 lwsl_info("close: sending the close packet failed, hanging up\n");
272
Andy Greenda527df2011-03-07 07:08:12 +0000273 /* else, the send failed and we should just hang up */
274 }
275
Andy Green3182ece2013-01-20 17:08:31 +0800276#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000277just_kill_connection:
Andy Green3182ece2013-01-20 17:08:31 +0800278#endif
Andy Green66a16f32011-05-24 22:07:45 +0100279
Andy Green43db0452013-01-10 19:50:35 +0800280 lwsl_debug("libwebsocket_close_and_free_session: just_kill_connection\n");
Andy Green66a16f32011-05-24 22:07:45 +0100281
Andy Greenda527df2011-03-07 07:08:12 +0000282 /*
283 * we won't be servicing or receiving anything further from this guy
Andy Greendfb23042013-01-17 12:26:48 +0800284 * delete socket from the internal poll list if still present
Andy Greenda527df2011-03-07 07:08:12 +0000285 */
Andy Green4b6fbe12011-02-14 08:03:48 +0000286
Andy Greendfb23042013-01-17 12:26:48 +0800287 remove_wsi_socket_from_fds(context, wsi);
Andy Green4b6fbe12011-02-14 08:03:48 +0000288
Andy Green251f6fa2010-11-03 11:13:06 +0000289 wsi->state = WSI_STATE_DEAD_SOCKET;
290
Andy Green4b6fbe12011-02-14 08:03:48 +0000291 /* tell the user it's all over for this guy */
292
Andy Greend4302732011-02-28 07:45:29 +0000293 if (wsi->protocol && wsi->protocol->callback &&
Andy Green6ee372f2012-04-09 15:09:01 +0800294 ((old_state == WSI_STATE_ESTABLISHED) ||
295 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
296 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
Andy Green43db0452013-01-10 19:50:35 +0800297 lwsl_debug("calling back CLOSED\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000298 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000299 wsi->user_space, NULL, 0);
Andy Greencc012472011-11-07 19:53:23 +0800300 } else
Andy Green43db0452013-01-10 19:50:35 +0800301 lwsl_debug("not calling back closed, old_state=%d\n", old_state);
Andy Green251f6fa2010-11-03 11:13:06 +0000302
Andy Green3182ece2013-01-20 17:08:31 +0800303#ifndef LWS_NO_EXTENSIONS
Andy Greenef660a92011-03-06 10:29:38 +0000304 /* deallocate any active extension contexts */
305
306 for (n = 0; n < wsi->count_active_extensions; n++) {
307 if (!wsi->active_extensions[n]->callback)
308 continue;
309
Andy Green46c2ea02011-03-22 09:04:01 +0000310 wsi->active_extensions[n]->callback(context,
311 wsi->active_extensions[n], wsi,
312 LWS_EXT_CALLBACK_DESTROY,
313 wsi->active_extensions_user[n], NULL, 0);
Andy Greenef660a92011-03-06 10:29:38 +0000314
315 free(wsi->active_extensions_user[n]);
316 }
317
Andy Greena41314f2011-05-23 10:00:03 +0100318 /*
319 * inform all extensions in case they tracked this guy out of band
320 * even though not active on him specifically
321 */
322
323 ext = context->extensions;
324 while (ext && ext->callback) {
325 ext->callback(context, ext, wsi,
326 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
327 NULL, NULL, 0);
328 ext++;
329 }
Andy Green3182ece2013-01-20 17:08:31 +0800330#endif
Andy Greena41314f2011-05-23 10:00:03 +0100331
Andy Greenef660a92011-03-06 10:29:38 +0000332 /* free up his parsing allocations */
Andy Green4b6fbe12011-02-14 08:03:48 +0000333
Andy Green251f6fa2010-11-03 11:13:06 +0000334 for (n = 0; n < WSI_TOKEN_COUNT; n++)
335 if (wsi->utf8_token[n].token)
336 free(wsi->utf8_token[n].token);
Andy Greena1ce6be2013-01-18 11:43:21 +0800337#ifndef LWS_NO_CLIENT
Andy Greena41314f2011-05-23 10:00:03 +0100338 if (wsi->c_address)
339 free(wsi->c_address);
Andy Greena1ce6be2013-01-18 11:43:21 +0800340#endif
Andy Green623a98d2013-01-21 11:04:23 +0800341 if (wsi->u.ws.rxflow_buffer)
342 free(wsi->u.ws.rxflow_buffer);
Andy Green706961d2013-01-17 16:50:35 +0800343
Andy Green43db0452013-01-10 19:50:35 +0800344/* lwsl_info("closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000345
Andy Green3faa9c72010-11-08 17:03:03 +0000346#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000347 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000348 n = SSL_get_fd(wsi->ssl);
349 SSL_shutdown(wsi->ssl);
Andy Green3fc2c652013-01-14 15:35:02 +0800350 compatible_close(n);
Andy Green3faa9c72010-11-08 17:03:03 +0000351 SSL_free(wsi->ssl);
352 } else {
353#endif
Andy Green0303db42013-01-17 14:46:43 +0800354 if (wsi->sock) {
355 n = shutdown(wsi->sock, SHUT_RDWR);
356 if (n)
357 lwsl_debug("closing: shutdown returned %d\n", errno);
Andy Green3fc2c652013-01-14 15:35:02 +0800358
Andy Green0303db42013-01-17 14:46:43 +0800359 n = compatible_close(wsi->sock);
360 if (n)
361 lwsl_debug("closing: close returned %d\n", errno);
362 }
Andy Green3faa9c72010-11-08 17:03:03 +0000363#ifdef LWS_OPENSSL_SUPPORT
364 }
365#endif
David Brooks2c60d952012-04-20 12:19:01 +0800366 if (wsi->protocol && wsi->protocol->per_session_data_size && wsi->user_space) /* user code may own */
Andy Green4f3943a2010-11-12 10:44:16 +0000367 free(wsi->user_space);
368
Andy Green251f6fa2010-11-03 11:13:06 +0000369 free(wsi);
370}
371
Andy Green07034092011-02-13 08:37:12 +0000372/**
Andy Greenf7ee5492011-02-13 09:04:21 +0000373 * libwebsockets_hangup_on_client() - Server calls to terminate client
Andy Green6ee372f2012-04-09 15:09:01 +0800374 * connection
Peter Hinz56885f32011-03-02 22:03:47 +0000375 * @context: libwebsockets context
Andy Greenf7ee5492011-02-13 09:04:21 +0000376 * @fd: Connection socket descriptor
377 */
378
379void
Peter Hinz56885f32011-03-02 22:03:47 +0000380libwebsockets_hangup_on_client(struct libwebsocket_context *context, int fd)
Andy Greenf7ee5492011-02-13 09:04:21 +0000381{
Andy Greendfb23042013-01-17 12:26:48 +0800382 struct libwebsocket *wsi = context->lws_lookup[fd];
Andy Greenf7ee5492011-02-13 09:04:21 +0000383
Andy Greendfb23042013-01-17 12:26:48 +0800384 if (wsi) {
Andy Green467c7ef2013-01-30 12:28:34 +0800385 lwsl_info("closing connection at libwebsockets_hangup_on_client:\n");
Andy Greendfb23042013-01-17 12:26:48 +0800386 libwebsocket_close_and_free_session(context,
387 wsi, LWS_CLOSE_STATUS_NOSTATUS);
388 } else
389 close(fd);
Andy Greenf7ee5492011-02-13 09:04:21 +0000390}
391
392
393/**
Andy Green07034092011-02-13 08:37:12 +0000394 * libwebsockets_get_peer_addresses() - Get client address information
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800395 * @context: Libwebsockets context
396 * @wsi: Local struct libwebsocket associated with
Andy Green07034092011-02-13 08:37:12 +0000397 * @fd: Connection socket descriptor
398 * @name: Buffer to take client address name
399 * @name_len: Length of client address name buffer
400 * @rip: Buffer to take client address IP qotted quad
401 * @rip_len: Length of client address IP buffer
402 *
403 * This function fills in @name and @rip with the name and IP of
Andy Green6ee372f2012-04-09 15:09:01 +0800404 * the client connected with socket descriptor @fd. Names may be
405 * truncated if there is not enough room. If either cannot be
406 * determined, they will be returned as valid zero-length strings.
Andy Green07034092011-02-13 08:37:12 +0000407 */
408
409void
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800410libwebsockets_get_peer_addresses(struct libwebsocket_context *context,
411 struct libwebsocket *wsi, int fd, char *name, int name_len,
Andy Green07034092011-02-13 08:37:12 +0000412 char *rip, int rip_len)
413{
414 unsigned int len;
415 struct sockaddr_in sin;
416 struct hostent *host;
417 struct hostent *host1;
418 char ip[128];
Andy Greenf92def72011-03-09 15:02:20 +0000419 unsigned char *p;
Andy Green07034092011-02-13 08:37:12 +0000420 int n;
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800421 int ret = -1;
David Galeanocb193682013-01-09 15:29:00 +0800422#ifdef AF_LOCAL
423 struct sockaddr_un *un;
424#endif
Andy Green07034092011-02-13 08:37:12 +0000425
426 rip[0] = '\0';
427 name[0] = '\0';
428
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800429 lws_latency_pre(context, wsi);
430
Andy Green07034092011-02-13 08:37:12 +0000431 len = sizeof sin;
432 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
433 perror("getpeername");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800434 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000435 }
Andy Green6ee372f2012-04-09 15:09:01 +0800436
Andy Green07034092011-02-13 08:37:12 +0000437 host = gethostbyaddr((char *) &sin.sin_addr, sizeof sin.sin_addr,
438 AF_INET);
439 if (host == NULL) {
440 perror("gethostbyaddr");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800441 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000442 }
443
444 strncpy(name, host->h_name, name_len);
445 name[name_len - 1] = '\0';
446
447 host1 = gethostbyname(host->h_name);
448 if (host1 == NULL)
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800449 goto bail;
Andy Greenf92def72011-03-09 15:02:20 +0000450 p = (unsigned char *)host1;
Andy Green07034092011-02-13 08:37:12 +0000451 n = 0;
452 while (p != NULL) {
Andy Greenf92def72011-03-09 15:02:20 +0000453 p = (unsigned char *)host1->h_addr_list[n++];
Andy Green07034092011-02-13 08:37:12 +0000454 if (p == NULL)
455 continue;
Peter Hinzbb45a902011-03-10 18:14:01 +0000456 if ((host1->h_addrtype != AF_INET)
457#ifdef AF_LOCAL
458 && (host1->h_addrtype != AF_LOCAL)
459#endif
460 )
Andy Green07034092011-02-13 08:37:12 +0000461 continue;
462
Andy Green7627af52011-03-09 15:13:52 +0000463 if (host1->h_addrtype == AF_INET)
464 sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
Peter Hinzbb45a902011-03-10 18:14:01 +0000465#ifdef AF_LOCAL
Andy Green7627af52011-03-09 15:13:52 +0000466 else {
467 un = (struct sockaddr_un *)p;
Andy Green6ee372f2012-04-09 15:09:01 +0800468 strncpy(ip, un->sun_path, sizeof(ip) - 1);
Andy Green7627af52011-03-09 15:13:52 +0000469 ip[sizeof(ip) - 1] = '\0';
470 }
Peter Hinzbb45a902011-03-10 18:14:01 +0000471#endif
Andy Green07034092011-02-13 08:37:12 +0000472 p = NULL;
473 strncpy(rip, ip, rip_len);
474 rip[rip_len - 1] = '\0';
475 }
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800476
477 ret = 0;
478bail:
479 lws_latency(context, wsi, "libwebsockets_get_peer_addresses", ret, 1);
Andy Green07034092011-02-13 08:37:12 +0000480}
Andy Green9f990342011-02-12 11:57:45 +0000481
Peter Hinz56885f32011-03-02 22:03:47 +0000482int libwebsockets_get_random(struct libwebsocket_context *context,
483 void *buf, int len)
484{
485 int n;
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800486 char *p = (char *)buf;
Peter Hinz56885f32011-03-02 22:03:47 +0000487
488#ifdef WIN32
489 for (n = 0; n < len; n++)
490 p[n] = (unsigned char)rand();
491#else
492 n = read(context->fd_random, p, len);
493#endif
494
495 return n;
496}
497
Andy Green95a7b5d2011-03-06 10:29:39 +0000498int lws_send_pipe_choked(struct libwebsocket *wsi)
499{
500 struct pollfd fds;
501
502 fds.fd = wsi->sock;
503 fds.events = POLLOUT;
504 fds.revents = 0;
505
506 if (poll(&fds, 1, 0) != 1)
507 return 1;
508
509 if ((fds.revents & POLLOUT) == 0)
510 return 1;
511
512 /* okay to send another packet without blocking */
513
514 return 0;
515}
516
Andy Greena41314f2011-05-23 10:00:03 +0100517int
Andy Green3b84c002011-03-06 13:14:42 +0000518lws_handle_POLLOUT_event(struct libwebsocket_context *context,
519 struct libwebsocket *wsi, struct pollfd *pollfd)
520{
Andy Green3b84c002011-03-06 13:14:42 +0000521 int n;
Andy Green6f520a52013-01-29 17:57:39 +0800522
Andy Green3182ece2013-01-20 17:08:31 +0800523#ifndef LWS_NO_EXTENSIONS
524 struct lws_tokens eff_buf;
Andy Green3b84c002011-03-06 13:14:42 +0000525 int ret;
526 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100527 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000528
Andy Greena41314f2011-05-23 10:00:03 +0100529 for (n = 0; n < wsi->count_active_extensions; n++) {
530 if (!wsi->active_extensions[n]->callback)
531 continue;
532
533 m = wsi->active_extensions[n]->callback(context,
534 wsi->active_extensions[n], wsi,
535 LWS_EXT_CALLBACK_IS_WRITEABLE,
536 wsi->active_extensions_user[n], NULL, 0);
537 if (m > handled)
538 handled = m;
539 }
540
541 if (handled == 1)
542 goto notify_action;
543
544 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000545 goto user_service;
546
547 /*
548 * check in on the active extensions, see if they
549 * had pending stuff to spill... they need to get the
550 * first look-in otherwise sequence will be disordered
551 *
552 * NULL, zero-length eff_buf means just spill pending
553 */
554
555 ret = 1;
556 while (ret == 1) {
557
558 /* default to nobody has more to spill */
559
560 ret = 0;
561 eff_buf.token = NULL;
562 eff_buf.token_len = 0;
563
564 /* give every extension a chance to spill */
565
566 for (n = 0; n < wsi->count_active_extensions; n++) {
567 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000568 wsi->protocol->owning_server,
569 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000570 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
571 wsi->active_extensions_user[n], &eff_buf, 0);
572 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800573 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000574 return -1;
575 }
576 if (m)
577 /*
578 * at least one extension told us he has more
579 * to spill, so we will go around again after
580 */
581 ret = 1;
582 }
583
584 /* assuming they gave us something to send, send it */
585
586 if (eff_buf.token_len) {
587 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
588 eff_buf.token_len))
589 return -1;
590 } else
591 continue;
592
593 /* no extension has more to spill */
594
595 if (!ret)
596 continue;
597
598 /*
599 * There's more to spill from an extension, but we just sent
600 * something... did that leave the pipe choked?
601 */
602
603 if (!lws_send_pipe_choked(wsi))
604 /* no we could add more */
605 continue;
606
Andy Green43db0452013-01-10 19:50:35 +0800607 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000608
609 /*
610 * Yes, he's choked. Leave the POLLOUT masked on so we will
611 * come back here when he is unchoked. Don't call the user
612 * callback to enforce ordering of spilling, he'll get called
613 * when we come back here and there's nothing more to spill.
614 */
615
616 return 0;
617 }
618
619 wsi->extension_data_pending = 0;
620
621user_service:
Andy Green3182ece2013-01-20 17:08:31 +0800622#endif
Andy Green3b84c002011-03-06 13:14:42 +0000623 /* one shot */
624
Andy Greena41314f2011-05-23 10:00:03 +0100625 if (pollfd) {
626 pollfd->events &= ~POLLOUT;
Andy Green3b84c002011-03-06 13:14:42 +0000627
Andy Greena41314f2011-05-23 10:00:03 +0100628 /* external POLL support via protocol 0 */
629 context->protocols[0].callback(context, wsi,
630 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
631 (void *)(long)wsi->sock, NULL, POLLOUT);
632 }
Andy Green3182ece2013-01-20 17:08:31 +0800633#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100634notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800635#endif
Andy Green3b84c002011-03-06 13:14:42 +0000636
Andy Green9e4c2b62011-03-07 20:47:39 +0000637 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
638 n = LWS_CALLBACK_CLIENT_WRITEABLE;
639 else
640 n = LWS_CALLBACK_SERVER_WRITEABLE;
641
Andy Green706961d2013-01-17 16:50:35 +0800642 user_callback_handle_rxflow(wsi->protocol->callback, context,
643 wsi, (enum libwebsocket_callback_reasons) n, wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000644
645 return 0;
646}
647
648
649
Andy Greena41314f2011-05-23 10:00:03 +0100650void
651libwebsocket_service_timeout_check(struct libwebsocket_context *context,
652 struct libwebsocket *wsi, unsigned int sec)
653{
Andy Green3182ece2013-01-20 17:08:31 +0800654#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100655 int n;
656
657 /*
658 * if extensions want in on it (eg, we are a mux parent)
659 * give them a chance to service child timeouts
660 */
661
662 for (n = 0; n < wsi->count_active_extensions; n++)
663 wsi->active_extensions[n]->callback(
664 context, wsi->active_extensions[n],
665 wsi, LWS_EXT_CALLBACK_1HZ,
666 wsi->active_extensions_user[n], NULL, sec);
667
Andy Green3182ece2013-01-20 17:08:31 +0800668#endif
Andy Greena41314f2011-05-23 10:00:03 +0100669 if (!wsi->pending_timeout)
670 return;
Andy Green6ee372f2012-04-09 15:09:01 +0800671
Andy Greena41314f2011-05-23 10:00:03 +0100672 /*
673 * if we went beyond the allowed time, kill the
674 * connection
675 */
676
677 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800678 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100679 libwebsocket_close_and_free_session(context,
680 wsi, LWS_CLOSE_STATUS_NOSTATUS);
681 }
682}
683
Andy Green9f990342011-02-12 11:57:45 +0000684/**
685 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000686 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000687 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800688 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000689 *
Andy Green75006172013-01-22 12:32:11 +0800690 * This function takes a pollfd that has POLLIN or POLLOUT activity and
691 * services it according to the state of the associated struct libwebsocket.
692 *
693 * The one call deals with all "service" that might happen on a socket
694 * including listen accepts, http files as well as websocket protocol.
Andy Green9f990342011-02-12 11:57:45 +0000695 */
696
697int
Peter Hinz56885f32011-03-02 22:03:47 +0000698libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000699 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000700{
Andy Greena1ce6be2013-01-18 11:43:21 +0800701 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000702 int n;
Andy Green0d338332011-02-12 11:57:43 +0000703 int m;
Andy Greena71eafc2011-02-14 17:59:43 +0000704 struct timeval tv;
Andy Green6f520a52013-01-29 17:57:39 +0800705 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1 +
706 MAX_USER_RX_BUFFER + LWS_SEND_BUFFER_POST_PADDING];
Andy Green467c7ef2013-01-30 12:28:34 +0800707 char ssl_err_buf[512];
Andy Green6f520a52013-01-29 17:57:39 +0800708
Andy Green3182ece2013-01-20 17:08:31 +0800709#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000710 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800711#endif
Andy Green98a717c2011-03-06 13:14:15 +0000712 struct lws_tokens eff_buf;
Andy Green03674a62013-01-16 11:47:40 +0800713#ifndef LWS_NO_CLIENT
Andy Green76f61e72013-01-16 11:53:05 +0800714 extern int lws_client_socket_service(struct libwebsocket_context *context, struct libwebsocket *wsi, struct pollfd *pollfd);
Andy Green03674a62013-01-16 11:47:40 +0800715#endif
Andy Greena1ce6be2013-01-18 11:43:21 +0800716#ifndef LWS_NO_SERVER
717 extern int lws_server_socket_service(struct libwebsocket_context *context, struct libwebsocket *wsi, struct pollfd *pollfd);
718#endif
Andy Greena71eafc2011-02-14 17:59:43 +0000719 /*
720 * you can call us with pollfd = NULL to just allow the once-per-second
721 * global timeout checks; if less than a second since the last check
722 * it returns immediately then.
723 */
724
725 gettimeofday(&tv, NULL);
726
Peter Hinz56885f32011-03-02 22:03:47 +0000727 if (context->last_timeout_check_s != tv.tv_sec) {
728 context->last_timeout_check_s = tv.tv_sec;
Andy Greena71eafc2011-02-14 17:59:43 +0000729
Andy Green24cba922013-01-19 13:56:10 +0800730 /* if our parent went down, don't linger around */
731 if (context->started_with_parent && kill(context->started_with_parent, 0) < 0)
732 kill(getpid(), SIGTERM);
733
Andy Greena71eafc2011-02-14 17:59:43 +0000734 /* global timeout check once per second */
735
Peter Hinz56885f32011-03-02 22:03:47 +0000736 for (n = 0; n < context->fds_count; n++) {
Andy Greendfb23042013-01-17 12:26:48 +0800737 struct libwebsocket *new_wsi = context->lws_lookup[context->fds[n].fd];
738 if (!new_wsi)
739 continue;
740 libwebsocket_service_timeout_check(context,
741 new_wsi, tv.tv_sec);
Andy Greena71eafc2011-02-14 17:59:43 +0000742 }
743 }
744
745 /* just here for timeout management? */
746
747 if (pollfd == NULL)
748 return 0;
749
750 /* no, here to service a socket descriptor */
751
Andy Green65b0e912013-01-16 07:59:47 +0800752 /*
753 * deal with listen service piggybacking
754 * every listen_service_modulo services of other fds, we
755 * sneak one in to service the listen socket if there's anything waiting
756 *
757 * To handle connection storms, as found in ab, if we previously saw a
758 * pending connection here, it causes us to check again next time.
759 */
760
761 if (context->listen_service_fd && pollfd->fd != context->listen_service_fd) {
762 context->listen_service_count++;
763 if (context->listen_service_extraseen ||
764 context->listen_service_count == context->listen_service_modulo) {
765 context->listen_service_count = 0;
766 m = 1;
767 if (context->listen_service_extraseen > 5)
768 m = 2;
769 while (m--) {
770 /* even with extpoll, we prepared this internal fds for listen */
771 n = poll(&context->fds[0], 1, 0);
772 if (n > 0) { /* there's a connection waiting for us */
773 libwebsocket_service_fd(context, &context->fds[0]);
774 context->listen_service_extraseen++;
775 } else {
776 if (context->listen_service_extraseen)
777 context->listen_service_extraseen--;
778 break;
779 }
780 }
781 }
782
783 }
784
785 /* okay, what we came here to do... */
786
Andy Greendfb23042013-01-17 12:26:48 +0800787 wsi = context->lws_lookup[pollfd->fd];
Andy Greend280b6e2013-01-15 13:40:23 +0800788 if (wsi == NULL) {
Andy Greendfb23042013-01-17 12:26:48 +0800789 if (pollfd->fd > 11)
790 lwsl_err("unexpected NULL wsi fd=%d fds_count=%d\n", pollfd->fd, context->fds_count);
Andy Greenfa3f4052012-10-07 20:40:35 +0800791 return 0;
Andy Greend280b6e2013-01-15 13:40:23 +0800792 }
Andy Green8f037e42010-12-19 22:13:26 +0000793
Andy Green0d338332011-02-12 11:57:43 +0000794 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +0800795
Andy Greena1ce6be2013-01-18 11:43:21 +0800796#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +0800797 case LWS_CONNMODE_HTTP_SERVING:
Andy Green0d338332011-02-12 11:57:43 +0000798 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +0800799 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greena1ce6be2013-01-18 11:43:21 +0800800 return lws_server_socket_service(context, wsi, pollfd);
801#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000802
Andy Green0d338332011-02-12 11:57:43 +0000803 case LWS_CONNMODE_WS_SERVING:
804 case LWS_CONNMODE_WS_CLIENT:
805
806 /* handle session socket closed */
807
808 if (pollfd->revents & (POLLERR | POLLHUP)) {
809
Andy Green43db0452013-01-10 19:50:35 +0800810 lwsl_debug("Session Socket %p (fd=%d) dead\n",
Andy Green0d338332011-02-12 11:57:43 +0000811 (void *)wsi, pollfd->fd);
812
Peter Hinz56885f32011-03-02 22:03:47 +0000813 libwebsocket_close_and_free_session(context, wsi,
Andy Green687b0182011-02-26 11:04:01 +0000814 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800815 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000816 }
817
Andy Green0d338332011-02-12 11:57:43 +0000818 /* the guy requested a callback when it was OK to write */
819
Andy Greenda527df2011-03-07 07:08:12 +0000820 if ((pollfd->revents & POLLOUT) &&
821 wsi->state == WSI_STATE_ESTABLISHED)
822 if (lws_handle_POLLOUT_event(context, wsi,
823 pollfd) < 0) {
824 libwebsocket_close_and_free_session(
825 context, wsi, LWS_CLOSE_STATUS_NORMAL);
Andy Green040d2ef2013-01-16 13:40:43 +0800826 return 0;
Andy Green3b84c002011-03-06 13:14:42 +0000827 }
Andy Green0d338332011-02-12 11:57:43 +0000828
Andy Green0d338332011-02-12 11:57:43 +0000829
830 /* any incoming data ready? */
831
832 if (!(pollfd->revents & POLLIN))
833 break;
834
Andy Greenb45993c2010-12-18 15:13:50 +0000835#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +0800836read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +0800837 if (wsi->ssl) {
Andy Green98a717c2011-03-06 13:14:15 +0000838 eff_buf.token_len = SSL_read(wsi->ssl, buf, sizeof buf);
Andy Green467c7ef2013-01-30 12:28:34 +0800839 if (!eff_buf.token_len) {
840 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
841 lwsl_err("SSL_read returned 0 with reason %s\n", ERR_error_string(n, ssl_err_buf));
842 }
843 } else
Andy Greenb45993c2010-12-18 15:13:50 +0000844#endif
Andy Green98a717c2011-03-06 13:14:15 +0000845 eff_buf.token_len =
Andy Green72c34322011-04-16 10:46:21 +0100846 recv(pollfd->fd, buf, sizeof buf, 0);
Andy Greenb45993c2010-12-18 15:13:50 +0000847
Andy Green98a717c2011-03-06 13:14:15 +0000848 if (eff_buf.token_len < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800849 lwsl_debug("Socket read returned %d\n",
Andy Green98a717c2011-03-06 13:14:15 +0000850 eff_buf.token_len);
Alon Levydc93b7f2012-10-19 11:21:57 +0200851 if (errno != EINTR && errno != EAGAIN)
Andy Green6ee372f2012-04-09 15:09:01 +0800852 libwebsocket_close_and_free_session(context,
853 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800854 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000855 }
Andy Green98a717c2011-03-06 13:14:15 +0000856 if (!eff_buf.token_len) {
Andy Green467c7ef2013-01-30 12:28:34 +0800857 lwsl_info("closing connection due to zero length read\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000858 libwebsocket_close_and_free_session(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +0800859 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenfa3f4052012-10-07 20:40:35 +0800860 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000861 }
862
Andy Green98a717c2011-03-06 13:14:15 +0000863 /*
864 * give any active extensions a chance to munge the buffer
865 * before parse. We pass in a pointer to an lws_tokens struct
866 * prepared with the default buffer and content length that's in
867 * there. Rather than rewrite the default buffer, extensions
868 * that expect to grow the buffer can adapt .token to
869 * point to their own per-connection buffer in the extension
870 * user allocation. By default with no extensions or no
871 * extension callback handling, just the normal input buffer is
872 * used then so it is efficient.
873 */
Andy Greenb45993c2010-12-18 15:13:50 +0000874
Andy Green98a717c2011-03-06 13:14:15 +0000875 eff_buf.token = (char *)buf;
Andy Green3182ece2013-01-20 17:08:31 +0800876#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +0000877 more = 1;
878 while (more) {
Andy Green0d338332011-02-12 11:57:43 +0000879
Andy Green98a717c2011-03-06 13:14:15 +0000880 more = 0;
881
882 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +0000883 m = wsi->active_extensions[n]->callback(context,
884 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +0000885 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +0000886 wsi->active_extensions_user[n],
887 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +0000888 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800889 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +0800890 "Extension reports fatal error\n");
891 libwebsocket_close_and_free_session(
892 context, wsi,
893 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800894 return 0;
Andy Green98a717c2011-03-06 13:14:15 +0000895 }
896 if (m)
897 more = 1;
898 }
Andy Green3182ece2013-01-20 17:08:31 +0800899#endif
Andy Green98a717c2011-03-06 13:14:15 +0000900 /* service incoming data */
901
902 if (eff_buf.token_len) {
903 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +0800904 (unsigned char *)eff_buf.token,
905 eff_buf.token_len);
Andy Green98a717c2011-03-06 13:14:15 +0000906 if (n < 0)
907 /* we closed wsi */
Andy Green040d2ef2013-01-16 13:40:43 +0800908 return 0;
Andy Green98a717c2011-03-06 13:14:15 +0000909 }
Andy Green3182ece2013-01-20 17:08:31 +0800910#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +0000911 eff_buf.token = NULL;
912 eff_buf.token_len = 0;
913 }
Andy Green3182ece2013-01-20 17:08:31 +0800914#endif
David Galeano7ffbe1b2013-01-10 10:35:32 +0800915
916#ifdef LWS_OPENSSL_SUPPORT
917 if (wsi->ssl && SSL_pending(wsi->ssl))
918 goto read_pending;
919#endif
Andy Green98a717c2011-03-06 13:14:15 +0000920 break;
Andy Green76f61e72013-01-16 11:53:05 +0800921
922 default:
Andy Green03674a62013-01-16 11:47:40 +0800923#ifdef LWS_NO_CLIENT
924 break;
925#else
Andy Green76f61e72013-01-16 11:53:05 +0800926 return lws_client_socket_service(context, wsi, pollfd);
Andy Green03674a62013-01-16 11:47:40 +0800927#endif
Andy Greenb45993c2010-12-18 15:13:50 +0000928 }
929
930 return 0;
931}
932
Andy Green0d338332011-02-12 11:57:43 +0000933
Andy Green6964bb52011-01-23 16:50:33 +0000934/**
935 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +0000936 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +0000937 *
938 * This function closes any active connections and then frees the
939 * context. After calling this, any further use of the context is
940 * undefined.
941 */
942void
Peter Hinz56885f32011-03-02 22:03:47 +0000943libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +0000944{
Andy Green3182ece2013-01-20 17:08:31 +0800945#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +0000946 int n;
947 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100948 struct libwebsocket_extension *ext;
Andy Green6964bb52011-01-23 16:50:33 +0000949
Andy Greend636e352013-01-29 12:36:17 +0800950#ifdef LWS_LATENCY
951 if (context->worst_latency_info[0])
952 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
953#endif
954
Andy Greendfb23042013-01-17 12:26:48 +0800955 for (n = 0; n < context->fds_count; n++) {
956 struct libwebsocket *wsi = context->lws_lookup[context->fds[n].fd];
957 libwebsocket_close_and_free_session(context,
958 wsi, LWS_CLOSE_STATUS_GOINGAWAY);
959 }
Andy Green6964bb52011-01-23 16:50:33 +0000960
Andy Greena41314f2011-05-23 10:00:03 +0100961 /*
962 * give all extensions a chance to clean up any per-context
963 * allocations they might have made
964 */
965
966 ext = context->extensions;
967 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
968 if (context->listen_port)
969 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +0800970 while (ext && ext->callback) {
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800971 ext->callback(context, ext, NULL, (enum libwebsocket_extension_callback_reasons)m, NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +0100972 ext++;
973 }
Andy Green3182ece2013-01-20 17:08:31 +0800974#endif
Andy Greena41314f2011-05-23 10:00:03 +0100975
Peter Hinz56885f32011-03-02 22:03:47 +0000976#ifdef WIN32
977#else
978 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +0000979#endif
980
Peter Hinz56885f32011-03-02 22:03:47 +0000981#ifdef LWS_OPENSSL_SUPPORT
982 if (context->ssl_ctx)
983 SSL_CTX_free(context->ssl_ctx);
984 if (context->ssl_client_ctx)
985 SSL_CTX_free(context->ssl_client_ctx);
986#endif
987
988 free(context);
989
990#ifdef WIN32
991 WSACleanup();
992#endif
Andy Green6964bb52011-01-23 16:50:33 +0000993}
994
Andy Greend88146d2013-01-22 12:40:35 +0800995/**
996 * libwebsocket_context_user() - get the user data associated with the whole context
997 * @context: Websocket context
998 *
999 * This returns the optional user allocation that can be attached to
1000 * the context the sockets live in at context_create time. It's a way
1001 * to let all sockets serviced in the same context share data without
1002 * using globals statics in the user code.
1003 */
1004
1005
Alon Levy0291eb32012-10-19 11:21:56 +02001006LWS_EXTERN void *
1007libwebsocket_context_user(struct libwebsocket_context *context)
1008{
1009 return context->user_space;
1010}
1011
Andy Green6964bb52011-01-23 16:50:33 +00001012/**
1013 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001014 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001015 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1016 * service otherwise block and service immediately, returning
1017 * after the timeout if nothing needed service.
1018 *
1019 * This function deals with any pending websocket traffic, for three
1020 * kinds of event. It handles these events on both server and client
1021 * types of connection the same.
1022 *
1023 * 1) Accept new connections to our context's server
1024 *
Andy Green6f520a52013-01-29 17:57:39 +08001025 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001026 * server or client connections.
1027 *
1028 * You need to call this service function periodically to all the above
1029 * functions to happen; if your application is single-threaded you can
1030 * just call it in your main event loop.
1031 *
1032 * Alternatively you can fork a new process that asynchronously handles
1033 * calling this service in a loop. In that case you are happy if this
1034 * call blocks your thread until it needs to take care of something and
1035 * would call it with a large nonzero timeout. Your loop then takes no
1036 * CPU while there is nothing happening.
1037 *
1038 * If you are calling it in a single-threaded app, you don't want it to
1039 * wait around blocking other things in your loop from happening, so you
1040 * would call it with a timeout_ms of 0, so it returns immediately if
1041 * nothing is pending, or as soon as it services whatever was pending.
1042 */
1043
Andy Greenb45993c2010-12-18 15:13:50 +00001044
Andy Greene92cd172011-01-19 13:11:55 +00001045int
Peter Hinz56885f32011-03-02 22:03:47 +00001046libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001047{
1048 int n;
Andy Greene92cd172011-01-19 13:11:55 +00001049
1050 /* stay dead once we are dead */
1051
Peter Hinz56885f32011-03-02 22:03:47 +00001052 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001053 return 1;
1054
Andy Green0d338332011-02-12 11:57:43 +00001055 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001056
Peter Hinz56885f32011-03-02 22:03:47 +00001057 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green3221f922011-02-12 13:14:11 +00001058 if (n == 0) /* poll timeout */
1059 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001060
Andy Greendfb23042013-01-17 12:26:48 +08001061 if (n < 0)
Andy Green3928f612012-07-20 12:58:38 +08001062 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001063
Andy Greendfb23042013-01-17 12:26:48 +08001064 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001065
Peter Hinz56885f32011-03-02 22:03:47 +00001066 for (n = 0; n < context->fds_count; n++)
1067 if (context->fds[n].revents)
Andy Green3928f612012-07-20 12:58:38 +08001068 if (libwebsocket_service_fd(context,
1069 &context->fds[n]) < 0)
1070 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001071 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001072}
1073
Andy Green3182ece2013-01-20 17:08:31 +08001074#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001075int
1076lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001077 struct libwebsocket *wsi,
1078 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001079 void *v, size_t len)
1080{
1081 int n;
1082 int handled = 0;
1083
1084 /* maybe an extension will take care of it for us */
1085
1086 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1087 if (!wsi->active_extensions[n]->callback)
1088 continue;
1089
1090 handled |= wsi->active_extensions[n]->callback(context,
1091 wsi->active_extensions[n], wsi,
1092 r, wsi->active_extensions_user[n], v, len);
1093 }
1094
1095 return handled;
1096}
1097
1098
1099void *
1100lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001101 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001102{
1103 int n = 0;
1104
Andy Green68b45042011-05-25 21:41:57 +01001105 if (wsi == NULL)
1106 return NULL;
1107
Andy Greena41314f2011-05-23 10:00:03 +01001108 while (n < wsi->count_active_extensions) {
1109 if (wsi->active_extensions[n] != ext) {
1110 n++;
1111 continue;
1112 }
1113 return wsi->active_extensions_user[n];
1114 }
1115
1116 return NULL;
1117}
Andy Green3182ece2013-01-20 17:08:31 +08001118#endif
Andy Greena41314f2011-05-23 10:00:03 +01001119
Andy Green90c7cbc2011-01-27 06:26:52 +00001120/**
1121 * libwebsocket_callback_on_writable() - Request a callback when this socket
1122 * becomes able to be written to without
1123 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001124 *
Peter Hinz56885f32011-03-02 22:03:47 +00001125 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001126 * @wsi: Websocket connection instance to get callback for
1127 */
1128
1129int
Peter Hinz56885f32011-03-02 22:03:47 +00001130libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001131 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001132{
Andy Green3182ece2013-01-20 17:08:31 +08001133#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001134 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001135 int handled = 0;
1136
1137 /* maybe an extension will take care of it for us */
1138
1139 for (n = 0; n < wsi->count_active_extensions; n++) {
1140 if (!wsi->active_extensions[n]->callback)
1141 continue;
1142
1143 handled |= wsi->active_extensions[n]->callback(context,
1144 wsi->active_extensions[n], wsi,
1145 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1146 wsi->active_extensions_user[n], NULL, 0);
1147 }
1148
1149 if (handled)
1150 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001151#endif
Andy Greendfb23042013-01-17 12:26:48 +08001152 if (wsi->position_in_fds_table < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001153 lwsl_err("libwebsocket_callback_on_writable: "
Andy Green6ee372f2012-04-09 15:09:01 +08001154 "failed to find socket %d\n", wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001155 return -1;
1156 }
1157
1158 context->fds[wsi->position_in_fds_table].events |= POLLOUT;
Andy Greena41314f2011-05-23 10:00:03 +01001159
Andy Green3221f922011-02-12 13:14:11 +00001160 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00001161 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001162 LWS_CALLBACK_SET_MODE_POLL_FD,
1163 (void *)(long)wsi->sock, NULL, POLLOUT);
1164
Andy Green90c7cbc2011-01-27 06:26:52 +00001165 return 1;
1166}
1167
1168/**
1169 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1170 * all connections using the given protocol when it
1171 * becomes possible to write to each socket without
1172 * blocking in turn.
1173 *
1174 * @protocol: Protocol whose connections will get callbacks
1175 */
1176
1177int
1178libwebsocket_callback_on_writable_all_protocol(
1179 const struct libwebsocket_protocols *protocol)
1180{
Peter Hinz56885f32011-03-02 22:03:47 +00001181 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001182 int n;
Andy Green0d338332011-02-12 11:57:43 +00001183 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001184
Andy Greendfb23042013-01-17 12:26:48 +08001185 for (n = 0; n < context->fds_count; n++) {
1186 wsi = context->lws_lookup[context->fds[n].fd];
1187 if (!wsi)
1188 continue;
1189 if (wsi->protocol == protocol)
1190 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001191 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001192
1193 return 0;
1194}
1195
Andy Greenbe93fef2011-02-14 20:25:43 +00001196/**
1197 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1198 *
1199 * You will not need this unless you are doing something special
1200 *
1201 * @wsi: Websocket connection instance
1202 * @reason: timeout reason
1203 * @secs: how many seconds
1204 */
1205
1206void
1207libwebsocket_set_timeout(struct libwebsocket *wsi,
1208 enum pending_timeout reason, int secs)
1209{
1210 struct timeval tv;
1211
1212 gettimeofday(&tv, NULL);
1213
1214 wsi->pending_timeout_limit = tv.tv_sec + secs;
1215 wsi->pending_timeout = reason;
1216}
1217
Andy Greena6cbece2011-01-27 20:06:03 +00001218
1219/**
1220 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1221 *
1222 * You will not need this unless you are doing something special
1223 *
1224 * @wsi: Websocket connection instance
1225 */
1226
1227int
1228libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1229{
1230 return wsi->sock;
1231}
1232
Andy Greend636e352013-01-29 12:36:17 +08001233#ifdef LWS_LATENCY
1234void
1235lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi, const char *action, int ret, int completed)
1236{
1237 struct timeval tv;
1238 unsigned long u;
1239 char buf[256];
1240
1241 gettimeofday(&tv, NULL);
1242
1243 u = (tv.tv_sec * 1000000) + tv.tv_usec;
1244
1245 if (action) {
1246 if (completed) {
1247 if (wsi->action_start == wsi->latency_start)
1248 sprintf(buf, "Completion first try lat %luus: %p: ret %d: %s\n", u - wsi->latency_start, (void *)wsi, ret, action);
1249 else
1250 sprintf(buf, "Completion %luus: lat %luus: %p: ret %d: %s\n", u - wsi->action_start, u - wsi->latency_start, (void *)wsi, ret, action);
1251 wsi->action_start = 0;
1252 } else
1253 sprintf(buf, "lat %luus: %p: ret %d: %s\n", u - wsi->latency_start, (void *)wsi, ret, action);
1254 if (u - wsi->latency_start > context->worst_latency) {
1255 context->worst_latency = u - wsi->latency_start;
1256 strcpy(context->worst_latency_info, buf);
1257 }
1258 lwsl_latency("%s", buf);
1259 } else {
1260 wsi->latency_start = u;
1261 if (!wsi->action_start)
1262 wsi->action_start = u;
1263 }
1264}
1265#endif
1266
Andy Greena1ce6be2013-01-18 11:43:21 +08001267#ifdef LWS_NO_SERVER
1268int
1269_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1270{
1271 return 0;
1272}
1273#else
Andy Green706961d2013-01-17 16:50:35 +08001274int
1275_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1276{
1277 struct libwebsocket_context *context = wsi->protocol->owning_server;
1278 int n;
1279
Andy Green623a98d2013-01-21 11:04:23 +08001280 if (!(wsi->u.ws.rxflow_change_to & 2))
Andy Green706961d2013-01-17 16:50:35 +08001281 return 0;
1282
Andy Green623a98d2013-01-21 11:04:23 +08001283 wsi->u.ws.rxflow_change_to &= ~2;
Andy Green706961d2013-01-17 16:50:35 +08001284
Andy Green623a98d2013-01-21 11:04:23 +08001285 lwsl_info("rxflow: wsi %p change_to %d\n", wsi, wsi->u.ws.rxflow_change_to);
Andy Green706961d2013-01-17 16:50:35 +08001286
1287 /* if we're letting it come again, did we interrupt anything? */
Andy Green623a98d2013-01-21 11:04:23 +08001288 if ((wsi->u.ws.rxflow_change_to & 1) && wsi->u.ws.rxflow_buffer) {
Andy Green706961d2013-01-17 16:50:35 +08001289 n = libwebsocket_interpret_incoming_packet(wsi, NULL, 0);
1290 if (n < 0) {
Andy Green467c7ef2013-01-30 12:28:34 +08001291 lwsl_info("closing connection at libwebsocket_rx_flow_control:\n");
Andy Green706961d2013-01-17 16:50:35 +08001292 libwebsocket_close_and_free_session(context, wsi, LWS_CLOSE_STATUS_NOSTATUS);
1293 return -1;
1294 }
1295 if (n)
1296 /* oh he stuck again, do nothing */
1297 return 0;
1298 }
1299
Andy Green623a98d2013-01-21 11:04:23 +08001300 if (wsi->u.ws.rxflow_change_to & 1)
Andy Green706961d2013-01-17 16:50:35 +08001301 context->fds[wsi->position_in_fds_table].events |= POLLIN;
1302 else
1303 context->fds[wsi->position_in_fds_table].events &= ~POLLIN;
1304
Andy Green623a98d2013-01-21 11:04:23 +08001305 if (wsi->u.ws.rxflow_change_to & 1)
Andy Green706961d2013-01-17 16:50:35 +08001306 /* external POLL support via protocol 0 */
1307 context->protocols[0].callback(context, wsi,
1308 LWS_CALLBACK_SET_MODE_POLL_FD,
1309 (void *)(long)wsi->sock, NULL, POLLIN);
1310 else
1311 /* external POLL support via protocol 0 */
1312 context->protocols[0].callback(context, wsi,
1313 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
1314 (void *)(long)wsi->sock, NULL, POLLIN);
1315
1316 return 1;
1317}
Andy Greena1ce6be2013-01-18 11:43:21 +08001318#endif
Andy Green706961d2013-01-17 16:50:35 +08001319
Andy Green90c7cbc2011-01-27 06:26:52 +00001320/**
1321 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1322 * receieved packets.
1323 *
1324 * If the output side of a server process becomes choked, this allows flow
1325 * control for the input side.
1326 *
1327 * @wsi: Websocket connection instance to get callback for
1328 * @enable: 0 = disable read servicing for this connection, 1 = enable
1329 */
1330
1331int
1332libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1333{
Andy Green623a98d2013-01-21 11:04:23 +08001334 wsi->u.ws.rxflow_change_to = 2 | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001335
Andy Green706961d2013-01-17 16:50:35 +08001336 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001337}
1338
Andy Green706961d2013-01-17 16:50:35 +08001339
Andy Green2ac5a6f2011-01-28 10:00:18 +00001340/**
1341 * libwebsocket_canonical_hostname() - returns this host's hostname
1342 *
1343 * This is typically used by client code to fill in the host parameter
1344 * when making a client connection. You can only call it after the context
1345 * has been created.
1346 *
Peter Hinz56885f32011-03-02 22:03:47 +00001347 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001348 */
1349
1350
1351extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001352libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001353{
Peter Hinz56885f32011-03-02 22:03:47 +00001354 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001355}
1356
1357
Andy Green90c7cbc2011-01-27 06:26:52 +00001358static void sigpipe_handler(int x)
1359{
1360}
1361
Andy Green6901cb32011-02-21 08:06:47 +00001362#ifdef LWS_OPENSSL_SUPPORT
1363static int
1364OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1365{
1366
1367 SSL *ssl;
1368 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001369 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001370
1371 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1372 SSL_get_ex_data_X509_STORE_CTX_idx());
1373
1374 /*
Andy Green2e24da02011-03-05 16:12:04 +00001375 * !!! nasty openssl requires the index to come as a library-scope
1376 * static
Andy Green6901cb32011-02-21 08:06:47 +00001377 */
Andy Green2e24da02011-03-05 16:12:04 +00001378 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001379
Peter Hinz56885f32011-03-02 22:03:47 +00001380 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001381 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1382 x509_ctx, ssl, preverify_ok);
1383
1384 /* convert return code from 0 = OK to 1 = OK */
1385
1386 if (!n)
1387 n = 1;
1388 else
1389 n = 0;
1390
1391 return n;
1392}
1393#endif
1394
Andy Green706961d2013-01-17 16:50:35 +08001395int user_callback_handle_rxflow(callback_function callback_function,
1396 struct libwebsocket_context * context,
1397 struct libwebsocket *wsi,
1398 enum libwebsocket_callback_reasons reason, void *user,
1399 void *in, size_t len)
1400{
1401 int n;
1402
1403 n = callback_function(context, wsi, reason, user, in, len);
1404 if (n < 0)
1405 return n;
1406
1407 _libwebsocket_rx_flow_control(wsi);
1408
1409 return 0;
1410}
1411
Andy Greenb45993c2010-12-18 15:13:50 +00001412
Andy Greenab990e42010-10-31 12:42:52 +00001413/**
Andy Green4739e5c2011-01-22 12:51:57 +00001414 * libwebsocket_create_context() - Create the websocket handler
1415 * @port: Port to listen on... you can use 0 to suppress listening on
Andy Green6964bb52011-01-23 16:50:33 +00001416 * any port, that's what you want if you are not running a
1417 * websocket server at all but just using it as a client
Peter Hinz56885f32011-03-02 22:03:47 +00001418 * @interf: NULL to bind the listen socket to all interfaces, or the
Andy Green32375b72011-02-19 08:32:53 +00001419 * interface name, eg, "eth2"
Andy Green4f3943a2010-11-12 10:44:16 +00001420 * @protocols: Array of structures listing supported protocols and a protocol-
Andy Green8f037e42010-12-19 22:13:26 +00001421 * specific callback for each one. The list is ended with an
1422 * entry that has a NULL callback pointer.
Andy Green6964bb52011-01-23 16:50:33 +00001423 * It's not const because we write the owning_server member
Andy Greenc5114822011-03-06 10:29:35 +00001424 * @extensions: NULL or array of libwebsocket_extension structs listing the
Andy Green3182ece2013-01-20 17:08:31 +08001425 * extensions this context supports. If you configured with
1426 * --without-extensions, you should give NULL here.
Andy Green3faa9c72010-11-08 17:03:03 +00001427 * @ssl_cert_filepath: If libwebsockets was compiled to use ssl, and you want
Andy Green8f037e42010-12-19 22:13:26 +00001428 * to listen using SSL, set to the filepath to fetch the
1429 * server cert from, otherwise NULL for unencrypted
Andy Green3faa9c72010-11-08 17:03:03 +00001430 * @ssl_private_key_filepath: filepath to private key if wanting SSL mode,
Andy Green8f037e42010-12-19 22:13:26 +00001431 * else ignored
David Galeano2f82be82013-01-09 16:25:54 +08001432 * @ssl_ca_filepath: CA certificate filepath or NULL
Andy Green3faa9c72010-11-08 17:03:03 +00001433 * @gid: group id to change to after setting listen socket, or -1.
1434 * @uid: user id to change to after setting listen socket, or -1.
Andy Greenbfb051f2011-02-09 08:49:14 +00001435 * @options: 0, or LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK
Andy Green15e31f32012-10-19 18:36:28 +08001436 * @user: optional user pointer that can be recovered via the context
1437 * pointer using libwebsocket_context_user
Andy Green05464c62010-11-12 10:44:18 +00001438 *
Andy Green8f037e42010-12-19 22:13:26 +00001439 * This function creates the listening socket and takes care
1440 * of all initialization in one step.
1441 *
Andy Greene92cd172011-01-19 13:11:55 +00001442 * After initialization, it returns a struct libwebsocket_context * that
1443 * represents this server. After calling, user code needs to take care
1444 * of calling libwebsocket_service() with the context pointer to get the
1445 * server's sockets serviced. This can be done in the same process context
1446 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001447 *
Andy Green8f037e42010-12-19 22:13:26 +00001448 * The protocol callback functions are called for a handful of events
1449 * including http requests coming in, websocket connections becoming
1450 * established, and data arriving; it's also called periodically to allow
1451 * async transmission.
1452 *
1453 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1454 * at that time websocket protocol has not been negotiated. Other
1455 * protocols after the first one never see any HTTP callack activity.
1456 *
1457 * The server created is a simple http server by default; part of the
1458 * websocket standard is upgrading this http connection to a websocket one.
1459 *
1460 * This allows the same server to provide files like scripts and favicon /
1461 * images or whatever over http and dynamic data over websockets all in
1462 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001463 */
Andy Green4ea60062010-10-30 12:15:07 +01001464
Andy Greene92cd172011-01-19 13:11:55 +00001465struct libwebsocket_context *
Peter Hinz56885f32011-03-02 22:03:47 +00001466libwebsocket_create_context(int port, const char *interf,
Andy Greenb45993c2010-12-18 15:13:50 +00001467 struct libwebsocket_protocols *protocols,
Andy Greend6e09112011-03-05 16:12:15 +00001468 struct libwebsocket_extension *extensions,
Andy Green8f037e42010-12-19 22:13:26 +00001469 const char *ssl_cert_filepath,
1470 const char *ssl_private_key_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08001471 const char *ssl_ca_filepath,
Alon Levy0291eb32012-10-19 11:21:56 +02001472 int gid, int uid, unsigned int options,
David Galeano2f82be82013-01-09 16:25:54 +08001473 void *user)
Andy Greenff95d7a2010-10-28 22:36:01 +01001474{
1475 int n;
Andy Greenf5bc1302013-01-21 09:09:52 +08001476 struct sockaddr_in serv_addr;
Andy Green251f6fa2010-11-03 11:13:06 +00001477 int opt = 1;
Peter Hinz56885f32011-03-02 22:03:47 +00001478 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001479 char *p;
Andy Green0d338332011-02-12 11:57:43 +00001480 struct libwebsocket *wsi;
Andy Green3182ece2013-01-20 17:08:31 +08001481#ifndef LWS_NO_EXTENSIONS
1482 int m;
1483#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001484
Andy Green3faa9c72010-11-08 17:03:03 +00001485#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001486 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001487 char ssl_err_buf[512];
Andy Green3faa9c72010-11-08 17:03:03 +00001488#endif
1489
Andy Greenb3a614a2013-01-19 13:08:17 +08001490 lwsl_notice("Initial logging level %d\n", log_level);
Andy Greenc0d6b632013-01-12 23:42:17 +08001491 lwsl_info(" LWS_MAX_HEADER_NAME_LENGTH: %u\n", LWS_MAX_HEADER_NAME_LENGTH);
1492 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
1493 lwsl_info(" LWS_INITIAL_HDR_ALLOC: %u\n", LWS_INITIAL_HDR_ALLOC);
1494 lwsl_info(" LWS_ADDITIONAL_HDR_ALLOC: %u\n", LWS_ADDITIONAL_HDR_ALLOC);
1495 lwsl_info(" MAX_USER_RX_BUFFER: %u\n", MAX_USER_RX_BUFFER);
Andy Greenc0d6b632013-01-12 23:42:17 +08001496 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001497#ifndef LWS_NO_EXTENSIONS
Andy Greenc0d6b632013-01-12 23:42:17 +08001498 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n", LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001499#else
1500 lwsl_notice(" Configured without extension support\n");
1501#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001502 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1503 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
1504 lwsl_info(" CIPHERS_LIST_STRING: '%s'\n", CIPHERS_LIST_STRING);
1505 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1506 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001507
Peter Hinz56885f32011-03-02 22:03:47 +00001508#ifdef _WIN32
1509 {
1510 WORD wVersionRequested;
1511 WSADATA wsaData;
1512 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001513 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001514
1515 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1516 wVersionRequested = MAKEWORD(2, 2);
1517
1518 err = WSAStartup(wVersionRequested, &wsaData);
1519 if (err != 0) {
1520 /* Tell the user that we could not find a usable */
1521 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001522 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001523 return NULL;
1524 }
David Galeano7b11fec2011-10-04 19:55:18 +08001525
Andy Green6ee372f2012-04-09 15:09:01 +08001526 /* default to a poll() made out of select() */
1527 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001528
Andy Green6ee372f2012-04-09 15:09:01 +08001529 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001530 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001531 if (wsdll)
1532 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Peter Hinz56885f32011-03-02 22:03:47 +00001533 }
1534#endif
1535
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001536 context = (struct libwebsocket_context *) malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001537 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001538 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001539 return NULL;
1540 }
Andy Green35f332b2013-01-21 13:06:38 +08001541#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001542 extern int pid_daemon;
1543 context->started_with_parent = pid_daemon;
1544 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1545#endif
1546
Peter Hinz56885f32011-03-02 22:03:47 +00001547 context->protocols = protocols;
1548 context->listen_port = port;
1549 context->http_proxy_port = 0;
1550 context->http_proxy_address[0] = '\0';
1551 context->options = options;
Andy Greendfb23042013-01-17 12:26:48 +08001552 /* to reduce this allocation, */
1553 context->max_fds = getdtablesize();
Andy Greenb3a614a2013-01-19 13:08:17 +08001554 lwsl_notice(" max fd tracked: %u\n", context->max_fds);
Andy Greena17c6922013-01-20 20:21:54 +08001555 lwsl_notice(" static allocation: %u bytes\n",
1556 (sizeof(struct pollfd) * context->max_fds) +
1557 (sizeof(struct libwebsocket *) * context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08001558
1559 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) * context->max_fds);
1560 if (context->fds == NULL) {
1561 lwsl_err("Unable to allocate fds array for %d connections\n", context->max_fds);
1562 free(context);
1563 return NULL;
1564 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08001565 context->lws_lookup = (struct libwebsocket **)malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001566 if (context->lws_lookup == NULL) {
1567 lwsl_err("Unable to allocate lws_lookup array for %d connections\n", context->max_fds);
1568 free(context->fds);
1569 free(context);
1570 return NULL;
1571 }
Andy Greena17c6922013-01-20 20:21:54 +08001572
Peter Hinz56885f32011-03-02 22:03:47 +00001573 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001574#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00001575 context->extensions = extensions;
Andy Green3182ece2013-01-20 17:08:31 +08001576#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08001577 context->last_timeout_check_s = 0;
Andy Greendfb23042013-01-17 12:26:48 +08001578 context->user_space = user;
Andy Green9659f372011-01-27 22:01:43 +00001579
Peter Hinz56885f32011-03-02 22:03:47 +00001580#ifdef WIN32
1581 context->fd_random = 0;
1582#else
1583 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
1584 if (context->fd_random < 0) {
Andy Greendfb23042013-01-17 12:26:48 +08001585 free(context);
Andy Green43db0452013-01-10 19:50:35 +08001586 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001587 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Andy Green44eee682011-02-10 09:32:24 +00001588 return NULL;
1589 }
Peter Hinz56885f32011-03-02 22:03:47 +00001590#endif
Andy Green44eee682011-02-10 09:32:24 +00001591
Peter Hinz56885f32011-03-02 22:03:47 +00001592#ifdef LWS_OPENSSL_SUPPORT
1593 context->use_ssl = 0;
1594 context->ssl_ctx = NULL;
1595 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00001596 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001597#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00001598
Andy Greena1ce6be2013-01-18 11:43:21 +08001599 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08001600
Andy Greena1ce6be2013-01-18 11:43:21 +08001601#ifndef LWS_NO_SERVER
1602 if (!(options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
1603 struct sockaddr sa;
1604 char hostname[1024] = "";
Andy Green788c4a82012-10-22 12:29:57 +01001605
1606 /* find canonical hostname */
1607
1608 hostname[(sizeof hostname) - 1] = '\0';
1609 memset(&sa, 0, sizeof(sa));
1610 sa.sa_family = AF_INET;
1611 sa.sa_data[(sizeof sa.sa_data) - 1] = '\0';
1612 gethostname(hostname, (sizeof hostname) - 1);
1613
1614 n = 0;
1615
David Galeanoed3c8402013-01-10 10:45:24 +08001616 if (strlen(hostname) < sizeof(sa.sa_data) - 1) {
Andy Green788c4a82012-10-22 12:29:57 +01001617 strcpy(sa.sa_data, hostname);
Andy Green43db0452013-01-10 19:50:35 +08001618 // lwsl_debug("my host name is %s\n", sa.sa_data);
Andy Green788c4a82012-10-22 12:29:57 +01001619 n = getnameinfo(&sa, sizeof(sa), hostname,
1620 (sizeof hostname) - 1, NULL, 0, 0);
1621 }
1622
1623 if (!n) {
1624 strncpy(context->canonical_hostname, hostname,
1625 sizeof context->canonical_hostname - 1);
1626 context->canonical_hostname[
1627 sizeof context->canonical_hostname - 1] = '\0';
1628 } else
1629 strncpy(context->canonical_hostname, hostname,
1630 sizeof context->canonical_hostname - 1);
1631
Andy Greenb3a614a2013-01-19 13:08:17 +08001632 lwsl_notice(" canonical_hostname = %s\n", context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08001633 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001634#endif
Andy Greena69f0512012-05-03 12:32:38 +08001635
Andy Green9659f372011-01-27 22:01:43 +00001636 /* split the proxy ads:port if given */
1637
1638 p = getenv("http_proxy");
1639 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00001640 strncpy(context->http_proxy_address, p,
Andy Green6ee372f2012-04-09 15:09:01 +08001641 sizeof context->http_proxy_address - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00001642 context->http_proxy_address[
1643 sizeof context->http_proxy_address - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00001644
Peter Hinz56885f32011-03-02 22:03:47 +00001645 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00001646 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001647 lwsl_err("http_proxy needs to be ads:port\n");
Andy Green9659f372011-01-27 22:01:43 +00001648 return NULL;
1649 }
1650 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00001651 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00001652
Andy Greenb3a614a2013-01-19 13:08:17 +08001653 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001654 context->http_proxy_address,
1655 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00001656 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001657
Andy Greena1ce6be2013-01-18 11:43:21 +08001658#ifndef LWS_NO_SERVER
Andy Green90c7cbc2011-01-27 06:26:52 +00001659 if (port) {
1660
Andy Green3faa9c72010-11-08 17:03:03 +00001661#ifdef LWS_OPENSSL_SUPPORT
Peter Hinz56885f32011-03-02 22:03:47 +00001662 context->use_ssl = ssl_cert_filepath != NULL &&
Andy Green90c7cbc2011-01-27 06:26:52 +00001663 ssl_private_key_filepath != NULL;
Peter Hinz56885f32011-03-02 22:03:47 +00001664 if (context->use_ssl)
Andy Greenb3a614a2013-01-19 13:08:17 +08001665 lwsl_notice(" Compiled with SSL support, using it\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001666 else
Andy Greenb3a614a2013-01-19 13:08:17 +08001667 lwsl_notice(" Compiled with SSL support, not using it\n");
Andy Green3faa9c72010-11-08 17:03:03 +00001668
Andy Green90c7cbc2011-01-27 06:26:52 +00001669#else
1670 if (ssl_cert_filepath != NULL &&
1671 ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08001672 lwsl_notice(" Not compiled for OpenSSl support!\n");
Andy Greene92cd172011-01-19 13:11:55 +00001673 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +00001674 }
Andy Greenb3a614a2013-01-19 13:08:17 +08001675 lwsl_notice(" Compiled without SSL support, "
Andy Green90c7cbc2011-01-27 06:26:52 +00001676 "serving unencrypted\n");
1677#endif
Andy Greena17c6922013-01-20 20:21:54 +08001678
1679 lwsl_notice(" per-connection allocation: %u + headers\n", sizeof(struct libwebsocket));
Andy Green90c7cbc2011-01-27 06:26:52 +00001680 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001681#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001682
1683 /* ignore SIGPIPE */
Peter Hinz56885f32011-03-02 22:03:47 +00001684#ifdef WIN32
1685#else
Andy Green90c7cbc2011-01-27 06:26:52 +00001686 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00001687#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001688
1689
1690#ifdef LWS_OPENSSL_SUPPORT
1691
1692 /* basic openssl init */
1693
1694 SSL_library_init();
1695
1696 OpenSSL_add_all_algorithms();
1697 SSL_load_error_strings();
1698
Andy Green2e24da02011-03-05 16:12:04 +00001699 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00001700 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
1701
Andy Green90c7cbc2011-01-27 06:26:52 +00001702 /*
1703 * Firefox insists on SSLv23 not SSLv3
1704 * Konq disables SSLv2 by default now, SSLv23 works
1705 */
1706
1707 method = (SSL_METHOD *)SSLv23_server_method();
1708 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001709 lwsl_err("problem creating ssl method: %s\n",
Andy Green90c7cbc2011-01-27 06:26:52 +00001710 ERR_error_string(ERR_get_error(), ssl_err_buf));
1711 return NULL;
1712 }
Peter Hinz56885f32011-03-02 22:03:47 +00001713 context->ssl_ctx = SSL_CTX_new(method); /* create context */
1714 if (!context->ssl_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001715 lwsl_err("problem creating ssl context: %s\n",
Andy Green90c7cbc2011-01-27 06:26:52 +00001716 ERR_error_string(ERR_get_error(), ssl_err_buf));
1717 return NULL;
1718 }
1719
David Galeanocc148e42013-01-10 10:18:59 +08001720#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001721 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001722#endif
David Galeano77a677c2013-01-10 10:14:12 +08001723 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
David Galeanof177f2a2013-01-10 10:15:19 +08001724 SSL_CTX_set_cipher_list(context->ssl_ctx, CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001725
Andy Greena1ce6be2013-01-18 11:43:21 +08001726#ifndef LWS_NO_CLIENT
1727
Andy Green90c7cbc2011-01-27 06:26:52 +00001728 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08001729
1730 if (port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00001731 method = (SSL_METHOD *)SSLv23_client_method();
1732 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001733 lwsl_err("problem creating ssl method: %s\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001734 ERR_error_string(ERR_get_error(), ssl_err_buf));
1735 return NULL;
1736 }
1737 /* create context */
1738 context->ssl_client_ctx = SSL_CTX_new(method);
1739 if (!context->ssl_client_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001740 lwsl_err("problem creating ssl context: %s\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001741 ERR_error_string(ERR_get_error(), ssl_err_buf));
1742 return NULL;
1743 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001744
David Galeanocc148e42013-01-10 10:18:59 +08001745#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001746 SSL_CTX_set_options(context->ssl_client_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001747#endif
David Galeano77a677c2013-01-10 10:14:12 +08001748 SSL_CTX_set_options(context->ssl_client_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
David Galeanof177f2a2013-01-10 10:15:19 +08001749 SSL_CTX_set_cipher_list(context->ssl_client_ctx, CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001750
Peter Hinz56885f32011-03-02 22:03:47 +00001751 /* openssl init for cert verification (for client sockets) */
David Galeano2f82be82013-01-09 16:25:54 +08001752 if (!ssl_ca_filepath) {
1753 if (!SSL_CTX_load_verify_locations(
1754 context->ssl_client_ctx, NULL,
1755 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08001756 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08001757 "Unable to load SSL Client certs from %s "
1758 "(set by --with-client-cert-dir= in configure) -- "
1759 " client ssl isn't going to work",
1760 LWS_OPENSSL_CLIENT_CERTS);
1761 } else
1762 if (!SSL_CTX_load_verify_locations(
1763 context->ssl_client_ctx, ssl_ca_filepath,
1764 NULL))
Andy Green43db0452013-01-10 19:50:35 +08001765 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08001766 "Unable to load SSL Client certs "
1767 "file from %s -- client ssl isn't "
1768 "going to work", ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00001769
1770 /*
1771 * callback allowing user code to load extra verification certs
1772 * helping the client to verify server identity
1773 */
1774
1775 context->protocols[0].callback(context, NULL,
1776 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
1777 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00001778 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001779#endif
Andy Green6ee372f2012-04-09 15:09:01 +08001780
Andy Greenc6bf2c22011-02-20 11:10:47 +00001781 /* as a server, are we requiring clients to identify themselves? */
1782
1783 if (options & LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
1784
1785 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08001786
Peter Hinz56885f32011-03-02 22:03:47 +00001787 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00001788 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1789 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001790
1791 /*
1792 * give user code a chance to load certs into the server
1793 * allowing it to verify incoming client certs
1794 */
1795
Peter Hinz56885f32011-03-02 22:03:47 +00001796 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00001797 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00001798 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001799 }
1800
Peter Hinz56885f32011-03-02 22:03:47 +00001801 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001802
1803 /* openssl init for server sockets */
1804
Andy Green3faa9c72010-11-08 17:03:03 +00001805 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08001806 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
1807 ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00001808 if (n != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001809 lwsl_err("problem getting cert '%s': %s\n",
Andy Green3faa9c72010-11-08 17:03:03 +00001810 ssl_cert_filepath,
1811 ERR_error_string(ERR_get_error(), ssl_err_buf));
Andy Greene92cd172011-01-19 13:11:55 +00001812 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +00001813 }
1814 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00001815 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
1816 ssl_private_key_filepath, SSL_FILETYPE_PEM) != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001817 lwsl_err("ssl problem getting key '%s': %s\n",
Andy Green018d8eb2010-11-08 21:04:23 +00001818 ssl_private_key_filepath,
1819 ERR_error_string(ERR_get_error(), ssl_err_buf));
Andy Greene92cd172011-01-19 13:11:55 +00001820 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +00001821 }
1822 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00001823 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08001824 lwsl_err("Private SSL key doesn't match cert\n");
Andy Greene92cd172011-01-19 13:11:55 +00001825 return NULL;
Andy Green3faa9c72010-11-08 17:03:03 +00001826 }
1827
1828 /* SSL is happy and has a cert it's content with */
1829 }
1830#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001831
Andy Greendf736162011-01-18 15:39:02 +00001832 /* selftest */
1833
1834 if (lws_b64_selftest())
Andy Greene92cd172011-01-19 13:11:55 +00001835 return NULL;
Andy Greendf736162011-01-18 15:39:02 +00001836
Andy Greena1ce6be2013-01-18 11:43:21 +08001837#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00001838 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00001839
Andy Green4739e5c2011-01-22 12:51:57 +00001840 if (port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08001841 extern int interface_to_sa(const char *ifname, struct sockaddr_in *addr, size_t addrlen);
1842 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00001843
Andy Green4739e5c2011-01-22 12:51:57 +00001844 sockfd = socket(AF_INET, SOCK_STREAM, 0);
1845 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08001846 lwsl_err("ERROR opening socket\n");
Andy Green4739e5c2011-01-22 12:51:57 +00001847 return NULL;
1848 }
Andy Green775c0dd2010-10-29 14:15:22 +01001849
Andy Green4739e5c2011-01-22 12:51:57 +00001850 /* allow us to restart even if old sockets in TIME_WAIT */
Andy Green6ee372f2012-04-09 15:09:01 +08001851 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
1852 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00001853
1854 /* Disable Nagle */
1855 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08001856 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
1857 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00001858
Andy Greene2160712013-01-28 12:19:10 +08001859 fcntl(sockfd, F_SETFL, O_NONBLOCK);
1860
Andy Green4739e5c2011-01-22 12:51:57 +00001861 bzero((char *) &serv_addr, sizeof(serv_addr));
1862 serv_addr.sin_family = AF_INET;
Peter Hinz56885f32011-03-02 22:03:47 +00001863 if (interf == NULL)
Andy Green32375b72011-02-19 08:32:53 +00001864 serv_addr.sin_addr.s_addr = INADDR_ANY;
1865 else
Peter Hinz56885f32011-03-02 22:03:47 +00001866 interface_to_sa(interf, &serv_addr,
Andy Green32375b72011-02-19 08:32:53 +00001867 sizeof(serv_addr));
Andy Green4739e5c2011-01-22 12:51:57 +00001868 serv_addr.sin_port = htons(port);
1869
1870 n = bind(sockfd, (struct sockaddr *) &serv_addr,
1871 sizeof(serv_addr));
1872 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001873 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green8f037e42010-12-19 22:13:26 +00001874 port, n, errno);
Andy Green41c58032013-01-12 13:21:08 +08001875 close(sockfd);
Andy Green4739e5c2011-01-22 12:51:57 +00001876 return NULL;
1877 }
Andy Green0d338332011-02-12 11:57:43 +00001878
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001879 wsi = (struct libwebsocket *)malloc(sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08001880 if (wsi == NULL) {
1881 lwsl_err("Out of mem\n");
1882 close(sockfd);
1883 return NULL;
1884 }
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001885 memset(wsi, 0, sizeof (struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00001886 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08001887#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00001888 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001889#endif
Andy Green0d338332011-02-12 11:57:43 +00001890 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08001891
1892 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001893
Andy Green65b0e912013-01-16 07:59:47 +08001894 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
1895 context->listen_service_count = 0;
1896 context->listen_service_fd = sockfd;
1897
Andy Greena824d182013-01-15 20:52:29 +08001898 listen(sockfd, LWS_SOMAXCONN);
Andy Greenb3a614a2013-01-19 13:08:17 +08001899 lwsl_notice(" Listening on port %d\n", port);
Andy Green8f037e42010-12-19 22:13:26 +00001900 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001901#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001902
Andy Green6ee372f2012-04-09 15:09:01 +08001903 /*
1904 * drop any root privs for this process
1905 * to listen on port < 1023 we would have needed root, but now we are
1906 * listening, we don't want the power for anything else
1907 */
Peter Hinz56885f32011-03-02 22:03:47 +00001908#ifdef WIN32
1909#else
Andy Green3faa9c72010-11-08 17:03:03 +00001910 if (gid != -1)
1911 if (setgid(gid))
Andy Green43db0452013-01-10 19:50:35 +08001912 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green3faa9c72010-11-08 17:03:03 +00001913 if (uid != -1)
1914 if (setuid(uid))
Andy Green43db0452013-01-10 19:50:35 +08001915 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00001916#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001917
Andy Green6f520a52013-01-29 17:57:39 +08001918 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00001919
Peter Hinz56885f32011-03-02 22:03:47 +00001920 for (context->count_protocols = 0;
1921 protocols[context->count_protocols].callback;
1922 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01001923
Andy Green43db0452013-01-10 19:50:35 +08001924 lwsl_parser(" Protocol: %s\n",
1925 protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01001926
Peter Hinz56885f32011-03-02 22:03:47 +00001927 protocols[context->count_protocols].owning_server = context;
1928 protocols[context->count_protocols].protocol_index =
1929 context->count_protocols;
Andy Greenb45993c2010-12-18 15:13:50 +00001930 }
Andy Greenf5bc1302013-01-21 09:09:52 +08001931
Andy Green3182ece2013-01-20 17:08:31 +08001932#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001933 /*
1934 * give all extensions a chance to create any per-context
1935 * allocations they need
1936 */
1937
1938 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
1939 if (port)
1940 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andrew Chambersd5512172012-05-20 08:17:09 +08001941
1942 if (extensions) {
1943 while (extensions->callback) {
Andy Green43db0452013-01-10 19:50:35 +08001944 lwsl_ext(" Extension: %s\n", extensions->name);
Aaron Zinman4550f1d2013-01-10 12:35:18 +08001945 extensions->callback(context, extensions, NULL,
1946 (enum libwebsocket_extension_callback_reasons)m,
1947 NULL, NULL, 0);
Andrew Chambersd5512172012-05-20 08:17:09 +08001948 extensions++;
1949 }
Andy Greena41314f2011-05-23 10:00:03 +01001950 }
Andy Green3182ece2013-01-20 17:08:31 +08001951#endif
Peter Hinz56885f32011-03-02 22:03:47 +00001952 return context;
Andy Greene92cd172011-01-19 13:11:55 +00001953}
Andy Greenb45993c2010-12-18 15:13:50 +00001954
Andy Greenb45993c2010-12-18 15:13:50 +00001955/**
1956 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00001957 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00001958 * @wsi: pointer to struct websocket you want to know the protocol of
1959 *
Andy Green8f037e42010-12-19 22:13:26 +00001960 *
Andy Green6f520a52013-01-29 17:57:39 +08001961 * Some apis can act on all live connections of a given protocol,
1962 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00001963 */
Andy Greenab990e42010-10-31 12:42:52 +00001964
Andy Greenb45993c2010-12-18 15:13:50 +00001965const struct libwebsocket_protocols *
1966libwebsockets_get_protocol(struct libwebsocket *wsi)
1967{
1968 return wsi->protocol;
1969}
1970
Andy Green82c3d542011-03-07 21:16:31 +00001971int
1972libwebsocket_is_final_fragment(struct libwebsocket *wsi)
1973{
Andy Green623a98d2013-01-21 11:04:23 +08001974 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00001975}
Alex Bligh49146db2011-11-07 17:19:25 +08001976
David Galeanoe2cf9922013-01-09 18:06:55 +08001977unsigned char
1978libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
1979{
Andy Green623a98d2013-01-21 11:04:23 +08001980 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08001981}
1982
Alex Bligh49146db2011-11-07 17:19:25 +08001983void *
1984libwebsocket_ensure_user_space(struct libwebsocket *wsi)
1985{
1986 /* allocate the per-connection user memory (if any) */
1987
1988 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
1989 wsi->user_space = malloc(
1990 wsi->protocol->per_session_data_size);
1991 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001992 lwsl_err("Out of memory for conn user space\n");
Alex Bligh49146db2011-11-07 17:19:25 +08001993 return NULL;
1994 }
Andy Green6ee372f2012-04-09 15:09:01 +08001995 memset(wsi->user_space, 0,
1996 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08001997 }
1998 return wsi->user_space;
1999}
Andy Green43db0452013-01-10 19:50:35 +08002000
Andy Green0b319092013-01-19 11:17:56 +08002001static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002002{
Andy Green0b319092013-01-19 11:17:56 +08002003 char buf[300];
2004 struct timeval tv;
Andy Green0b319092013-01-19 11:17:56 +08002005 int n;
2006
2007 gettimeofday(&tv, NULL);
2008
2009 buf[0] = '\0';
2010 for (n = 0; n < LLL_COUNT; n++)
2011 if (level == (1 << n)) {
Andy Green058ba812013-01-19 11:32:18 +08002012 sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
Andy Green0b319092013-01-19 11:17:56 +08002013 (int)(tv.tv_usec / 100), log_level_names[n]);
2014 break;
2015 }
2016
2017 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002018}
2019
Andy Greenc11db202013-01-19 11:12:16 +08002020void lwsl_emit_syslog(int level, const char *line)
2021{
2022 int syslog_level = LOG_DEBUG;
2023
2024 switch (level) {
2025 case LLL_ERR:
2026 syslog_level = LOG_ERR;
2027 break;
2028 case LLL_WARN:
2029 syslog_level = LOG_WARNING;
2030 break;
2031 case LLL_NOTICE:
2032 syslog_level = LOG_NOTICE;
2033 break;
2034 case LLL_INFO:
2035 syslog_level = LOG_INFO;
2036 break;
2037 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002038 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002039}
2040
Andy Green43db0452013-01-10 19:50:35 +08002041void _lws_log(int filter, const char *format, ...)
2042{
Andy Greende8f27a2013-01-12 09:17:42 +08002043 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002044 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002045
2046 if (!(log_level & filter))
2047 return;
2048
Andy Green43db0452013-01-10 19:50:35 +08002049 va_start(ap, format);
Andy Green0b319092013-01-19 11:17:56 +08002050 vsnprintf(buf, (sizeof buf), format, ap);
Andy Greende8f27a2013-01-12 09:17:42 +08002051 buf[(sizeof buf) - 1] = '\0';
2052 va_end(ap);
2053
Andy Green0b319092013-01-19 11:17:56 +08002054 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002055}
2056
2057/**
2058 * lws_set_log_level() - Set the logging bitfield
2059 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002060 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2061 * function to perform log string emission instead of
2062 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002063 *
Andy Greende8f27a2013-01-12 09:17:42 +08002064 * log level defaults to "err" and "warn" contexts enabled only and
2065 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002066 */
2067
Andy Green058ba812013-01-19 11:32:18 +08002068void lws_set_log_level(int level, void (*log_emit_function)(int level, const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002069{
2070 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002071 if (log_emit_function)
2072 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002073}