blob: c905d832b60b1a36b7ad2323bab3a9171f082111 [file] [log] [blame]
Andy Green58eaa742011-03-07 17:54:06 +00001/*
Andy Greena0da8a82010-11-08 17:12:19 +00002 * libwebsockets - small server side websockets and web server implementation
Andy Green8f037e42010-12-19 22:13:26 +00003 *
Andy Greena0da8a82010-11-08 17:12:19 +00004 * Copyright (C) 2010 Andy Green <andy@warmcat.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301 USA
Andy Green05a0a7b2010-10-31 17:51:39 +000020 */
21
Andy Green7c212cc2010-11-08 20:20:42 +000022#include "private-libwebsockets.h"
Andy Greenff95d7a2010-10-28 22:36:01 +010023
Peter Hinz56885f32011-03-02 22:03:47 +000024#ifdef WIN32
David Galeanocb193682013-01-09 15:29:00 +080025#include <tchar.h>
26#include <io.h>
Joakim Soderberg63ff1202013-02-11 17:52:23 +010027#include <mstcpip.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
Joakim Soderberg4c531232013-02-06 15:26:58 +090034#include <syslog.h>
Andy Green7627af52011-03-09 15:13:52 +000035#include <sys/un.h>
Andy Greena69f0512012-05-03 12:32:38 +080036#include <sys/socket.h>
37#include <netdb.h>
Peter Hinz56885f32011-03-02 22:03:47 +000038#endif
Andy Green2e24da02011-03-05 16:12:04 +000039
40#ifdef LWS_OPENSSL_SUPPORT
41int openssl_websocket_private_data_index;
42#endif
43
Andy Greenaa6fc442012-04-12 13:26:49 +080044#ifdef __MINGW32__
45#include "../win32port/win32helpers/websock-w32.c"
46#else
47#ifdef __MINGW64__
48#include "../win32port/win32helpers/websock-w32.c"
49#endif
50#endif
51
Andy Green7b405452013-02-01 10:50:15 +080052#ifndef LWS_BUILD_HASH
53#define LWS_BUILD_HASH "unknown-build-hash"
54#endif
Andy Green3182ece2013-01-20 17:08:31 +080055
Andy Green7c19c342013-01-19 12:18:07 +080056static int log_level = LLL_ERR | LLL_WARN | LLL_NOTICE;
Andy Green058ba812013-01-19 11:32:18 +080057static void lwsl_emit_stderr(int level, const char *line);
58static void (*lwsl_emit)(int level, const char *line) = lwsl_emit_stderr;
59
Andy Green7b405452013-02-01 10:50:15 +080060static const char *library_version = LWS_LIBRARY_VERSION " " LWS_BUILD_HASH;
61
Andy Greenb5b23192013-02-11 17:13:32 +080062static const char * const log_level_names[] = {
Andy Green43db0452013-01-10 19:50:35 +080063 "ERR",
64 "WARN",
Andy Green7c19c342013-01-19 12:18:07 +080065 "NOTICE",
Andy Green43db0452013-01-10 19:50:35 +080066 "INFO",
67 "DEBUG",
68 "PARSER",
69 "HEADER",
70 "EXTENSION",
71 "CLIENT",
Andy Greend636e352013-01-29 12:36:17 +080072 "LATENCY",
Andy Green43db0452013-01-10 19:50:35 +080073};
74
Andy Greenb5b23192013-02-11 17:13:32 +080075#ifndef LWS_NO_CLIENT
76 extern int lws_client_socket_service(
77 struct libwebsocket_context *context,
78 struct libwebsocket *wsi, struct pollfd *pollfd);
79#endif
80#ifndef LWS_NO_SERVER
81 extern int lws_server_socket_service(
82 struct libwebsocket_context *context,
83 struct libwebsocket *wsi, struct pollfd *pollfd);
84#endif
85
Andy Green7b405452013-02-01 10:50:15 +080086/**
87 * lws_get_library_version: get version and git hash library built from
88 *
89 * returns a const char * to a string like "1.1 178d78c"
90 * representing the library version followed by the git head hash it
91 * was built from
92 */
93
94const char *
95lws_get_library_version(void)
96{
97 return library_version;
98}
99
Andy Green0d338332011-02-12 11:57:43 +0000100int
Andy Greenb5b23192013-02-11 17:13:32 +0800101insert_wsi_socket_into_fds(struct libwebsocket_context *context,
102 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000103{
Andy Greendfb23042013-01-17 12:26:48 +0800104 if (context->fds_count >= context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800105 lwsl_err("Too many fds (%d)\n", context->max_fds);
Andy Green0d338332011-02-12 11:57:43 +0000106 return 1;
107 }
108
Andy Greendfb23042013-01-17 12:26:48 +0800109 if (wsi->sock > context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800110 lwsl_err("Socket fd %d is too high (%d)\n",
111 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800112 return 1;
113 }
114
115 assert(wsi);
116 assert(wsi->sock);
117
Andy Greenb5b23192013-02-11 17:13:32 +0800118 lwsl_info("insert_wsi_socket_into_fds: wsi=%p, sock=%d, fds pos=%d\n",
119 wsi, wsi->sock, context->fds_count);
Andy Greendfb23042013-01-17 12:26:48 +0800120
121 context->lws_lookup[wsi->sock] = wsi;
122 wsi->position_in_fds_table = context->fds_count;
123 context->fds[context->fds_count].fd = wsi->sock;
124 context->fds[context->fds_count].events = POLLIN;
125 context->fds[context->fds_count++].revents = 0;
126
127 /* external POLL support via protocol 0 */
128 context->protocols[0].callback(context, wsi,
129 LWS_CALLBACK_ADD_POLL_FD,
130 (void *)(long)wsi->sock, NULL, POLLIN);
Andy Green0d338332011-02-12 11:57:43 +0000131
132 return 0;
133}
134
Andy Greendfb23042013-01-17 12:26:48 +0800135static int
Andy Greenb5b23192013-02-11 17:13:32 +0800136remove_wsi_socket_from_fds(struct libwebsocket_context *context,
137 struct libwebsocket *wsi)
Andy Green0d338332011-02-12 11:57:43 +0000138{
Andy Greendfb23042013-01-17 12:26:48 +0800139 int m;
Andy Green0d338332011-02-12 11:57:43 +0000140
Andy Greendfb23042013-01-17 12:26:48 +0800141 if (!--context->fds_count)
142 goto do_ext;
Andy Green0d338332011-02-12 11:57:43 +0000143
Andy Greendfb23042013-01-17 12:26:48 +0800144 if (wsi->sock > context->max_fds) {
Andy Greenb5b23192013-02-11 17:13:32 +0800145 lwsl_err("Socket fd %d too high (%d)\n",
146 wsi->sock, context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +0800147 return 1;
148 }
Andy Green0d338332011-02-12 11:57:43 +0000149
Andy Greenb5b23192013-02-11 17:13:32 +0800150 lwsl_info("remove_wsi_socket_from_fds: wsi=%p, sock=%d, fds pos=%d\n",
151 wsi, wsi->sock, wsi->position_in_fds_table);
Andy Greendfb23042013-01-17 12:26:48 +0800152
153 m = wsi->position_in_fds_table; /* replace the contents for this */
154
155 /* have the last guy take up the vacant slot */
Andy Greenb5b23192013-02-11 17:13:32 +0800156 context->fds[m] = context->fds[context->fds_count];
157 /*
158 * end guy's fds_lookup entry remains unchanged
159 * (still same fd pointing to same wsi)
160 */
Andy Greendfb23042013-01-17 12:26:48 +0800161 /* end guy's "position in fds table" changed */
Andy Greenb5b23192013-02-11 17:13:32 +0800162 context->lws_lookup[context->fds[context->fds_count].fd]->
163 position_in_fds_table = m;
Andy Greendfb23042013-01-17 12:26:48 +0800164 /* deletion guy's lws_lookup entry needs nuking */
Andy Greenb5b23192013-02-11 17:13:32 +0800165 context->lws_lookup[wsi->sock] = NULL;
166 /* removed wsi has no position any more */
167 wsi->position_in_fds_table = -1;
Andy Greendfb23042013-01-17 12:26:48 +0800168
169do_ext:
170 /* remove also from external POLL support via protocol 0 */
171 if (wsi->sock)
172 context->protocols[0].callback(context, wsi,
173 LWS_CALLBACK_DEL_POLL_FD, (void *)(long)wsi->sock, NULL, 0);
174
175 return 0;
Andy Green0d338332011-02-12 11:57:43 +0000176}
177
Andy Green32375b72011-02-19 08:32:53 +0000178
Andy Green8f037e42010-12-19 22:13:26 +0000179void
Peter Hinz56885f32011-03-02 22:03:47 +0000180libwebsocket_close_and_free_session(struct libwebsocket_context *context,
Andy Green687b0182011-02-26 11:04:01 +0000181 struct libwebsocket *wsi, enum lws_close_status reason)
Andy Green251f6fa2010-11-03 11:13:06 +0000182{
Andy Greenb45993c2010-12-18 15:13:50 +0000183 int n;
Andy Green62c54d22011-02-14 09:14:25 +0000184 int old_state;
Andy Green5e1fa172011-02-10 09:07:05 +0000185 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 +
186 LWS_SEND_BUFFER_POST_PADDING];
Andy Green3182ece2013-01-20 17:08:31 +0800187#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000188 int ret;
189 int m;
190 struct lws_tokens eff_buf;
Andy Greena41314f2011-05-23 10:00:03 +0100191 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +0800192#endif
Andy Greenb45993c2010-12-18 15:13:50 +0000193
Andy Green4b6fbe12011-02-14 08:03:48 +0000194 if (!wsi)
Andy Greenb45993c2010-12-18 15:13:50 +0000195 return;
196
Andy Green62c54d22011-02-14 09:14:25 +0000197 old_state = wsi->state;
Andy Green251f6fa2010-11-03 11:13:06 +0000198
Andy Green62c54d22011-02-14 09:14:25 +0000199 if (old_state == WSI_STATE_DEAD_SOCKET)
Andy Green5e1fa172011-02-10 09:07:05 +0000200 return;
201
Andy Green623a98d2013-01-21 11:04:23 +0800202 wsi->u.ws.close_reason = reason;
Andy Greenda527df2011-03-07 07:08:12 +0000203
Andy Greenb8b247d2013-01-22 07:20:08 +0800204 if (wsi->mode == LWS_CONNMODE_HTTP_SERVING && wsi->u.http.fd) {
205 close(wsi->u.http.fd);
206 wsi->u.http.fd = 0;
207 }
208
Andy Green3182ece2013-01-20 17:08:31 +0800209#ifndef LWS_NO_EXTENSIONS
Andy Greenda527df2011-03-07 07:08:12 +0000210 /*
Andy Green68b45042011-05-25 21:41:57 +0100211 * are his extensions okay with him closing? Eg he might be a mux
212 * parent and just his ch1 aspect is closing?
213 */
214
Andy Green68b45042011-05-25 21:41:57 +0100215 for (n = 0; n < wsi->count_active_extensions; n++) {
216 if (!wsi->active_extensions[n]->callback)
217 continue;
218
219 m = wsi->active_extensions[n]->callback(context,
220 wsi->active_extensions[n], wsi,
221 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
222 wsi->active_extensions_user[n], NULL, 0);
223
224 /*
225 * if somebody vetoed actually closing him at this time....
226 * up to the extension to track the attempted close, let's
227 * just bail
228 */
229
230 if (m) {
Andy Green43db0452013-01-10 19:50:35 +0800231 lwsl_ext("extension vetoed close\n");
Andy Green68b45042011-05-25 21:41:57 +0100232 return;
233 }
234 }
235
Andy Green68b45042011-05-25 21:41:57 +0100236 /*
Andy Greenc44159f2011-03-07 07:08:18 +0000237 * flush any tx pending from extensions, since we may send close packet
238 * if there are problems with send, just nuke the connection
239 */
240
241 ret = 1;
242 while (ret == 1) {
243
244 /* default to nobody has more to spill */
245
246 ret = 0;
247 eff_buf.token = NULL;
248 eff_buf.token_len = 0;
249
250 /* show every extension the new incoming data */
251
252 for (n = 0; n < wsi->count_active_extensions; n++) {
253 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000254 wsi->protocol->owning_server,
255 wsi->active_extensions[n], wsi,
Andy Greenc44159f2011-03-07 07:08:18 +0000256 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
257 wsi->active_extensions_user[n], &eff_buf, 0);
258 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800259 lwsl_ext("Extension reports fatal error\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000260 goto just_kill_connection;
261 }
262 if (m)
263 /*
264 * at least one extension told us he has more
265 * to spill, so we will go around again after
266 */
267 ret = 1;
268 }
269
270 /* assuming they left us something to send, send it */
271
272 if (eff_buf.token_len)
273 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
Andy Greenb5b23192013-02-11 17:13:32 +0800274 eff_buf.token_len)) {
275 lwsl_debug("close: ext spill failed\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000276 goto just_kill_connection;
Andy Green0303db42013-01-17 14:46:43 +0800277 }
Andy Greenc44159f2011-03-07 07:08:18 +0000278 }
Andy Green3182ece2013-01-20 17:08:31 +0800279#endif
Andy Greenc44159f2011-03-07 07:08:18 +0000280
281 /*
Andy Greenda527df2011-03-07 07:08:12 +0000282 * signal we are closing, libsocket_write will
283 * add any necessary version-specific stuff. If the write fails,
284 * no worries we are closing anyway. If we didn't initiate this
285 * close, then our state has been changed to
286 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this.
287 *
288 * Likewise if it's a second call to close this connection after we
289 * sent the close indication to the peer already, we are in state
290 * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time.
291 */
292
293 if (old_state == WSI_STATE_ESTABLISHED &&
294 reason != LWS_CLOSE_STATUS_NOSTATUS) {
Andy Green66a16f32011-05-24 22:07:45 +0100295
Andy Green43db0452013-01-10 19:50:35 +0800296 lwsl_debug("sending close indication...\n");
Andy Green66a16f32011-05-24 22:07:45 +0100297
Andy Greenfdd305a2013-02-11 14:32:48 +0800298 /* make valgrind happy */
Andy Greenb5b23192013-02-11 17:13:32 +0800299 memset(buf, 0, sizeof(buf));
300 n = libwebsocket_write(wsi,
301 &buf[LWS_SEND_BUFFER_PRE_PADDING + 2],
Andy Greenda527df2011-03-07 07:08:12 +0000302 0, LWS_WRITE_CLOSE);
303 if (!n) {
304 /*
305 * we have sent a nice protocol level indication we
306 * now wish to close, we should not send anything more
307 */
308
309 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
310
Andy Greenb5b23192013-02-11 17:13:32 +0800311 /*
312 * ...and we should wait for a reply for a bit
313 * out of politeness
314 */
Andy Greenda527df2011-03-07 07:08:12 +0000315
316 libwebsocket_set_timeout(wsi,
Andy Green0303db42013-01-17 14:46:43 +0800317 PENDING_TIMEOUT_CLOSE_ACK, 1);
Andy Greenda527df2011-03-07 07:08:12 +0000318
Andy Green43db0452013-01-10 19:50:35 +0800319 lwsl_debug("sent close indication, awaiting ack\n");
Andy Greenda527df2011-03-07 07:08:12 +0000320
321 return;
322 }
323
Andy Greenb5b23192013-02-11 17:13:32 +0800324 lwsl_info("close: sending close packet failed, hanging up\n");
Andy Green0303db42013-01-17 14:46:43 +0800325
Andy Greenda527df2011-03-07 07:08:12 +0000326 /* else, the send failed and we should just hang up */
327 }
328
Andy Green3182ece2013-01-20 17:08:31 +0800329#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000330just_kill_connection:
Andy Green3182ece2013-01-20 17:08:31 +0800331#endif
Andy Green66a16f32011-05-24 22:07:45 +0100332
Andy Greenb5b23192013-02-11 17:13:32 +0800333 lwsl_debug("close: just_kill_connection\n");
Andy Green66a16f32011-05-24 22:07:45 +0100334
Andy Greenda527df2011-03-07 07:08:12 +0000335 /*
336 * we won't be servicing or receiving anything further from this guy
Andy Greendfb23042013-01-17 12:26:48 +0800337 * delete socket from the internal poll list if still present
Andy Greenda527df2011-03-07 07:08:12 +0000338 */
Andy Green4b6fbe12011-02-14 08:03:48 +0000339
Andy Greendfb23042013-01-17 12:26:48 +0800340 remove_wsi_socket_from_fds(context, wsi);
Andy Green4b6fbe12011-02-14 08:03:48 +0000341
Andy Green251f6fa2010-11-03 11:13:06 +0000342 wsi->state = WSI_STATE_DEAD_SOCKET;
343
Andy Greenb5b23192013-02-11 17:13:32 +0800344 if ((old_state == WSI_STATE_ESTABLISHED ||
345 wsi->mode == LWS_CONNMODE_WS_SERVING ||
346 wsi->mode == LWS_CONNMODE_WS_CLIENT) && wsi->u.ws.rx_user_buffer) {
Andy Green54495112013-02-06 21:10:16 +0900347 free(wsi->u.ws.rx_user_buffer);
348 wsi->u.ws.rx_user_buffer = NULL;
349 }
350
Andy Green4b6fbe12011-02-14 08:03:48 +0000351 /* tell the user it's all over for this guy */
352
Andy Greend4302732011-02-28 07:45:29 +0000353 if (wsi->protocol && wsi->protocol->callback &&
Andy Green6ee372f2012-04-09 15:09:01 +0800354 ((old_state == WSI_STATE_ESTABLISHED) ||
355 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
356 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
Andy Green43db0452013-01-10 19:50:35 +0800357 lwsl_debug("calling back CLOSED\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000358 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000359 wsi->user_space, NULL, 0);
Andy Greencc012472011-11-07 19:53:23 +0800360 } else
Andy Greenb5b23192013-02-11 17:13:32 +0800361 lwsl_debug("not calling back closed\n");
Andy Green251f6fa2010-11-03 11:13:06 +0000362
Andy Green3182ece2013-01-20 17:08:31 +0800363#ifndef LWS_NO_EXTENSIONS
Andy Greenef660a92011-03-06 10:29:38 +0000364 /* deallocate any active extension contexts */
365
366 for (n = 0; n < wsi->count_active_extensions; n++) {
367 if (!wsi->active_extensions[n]->callback)
368 continue;
369
Andy Green46c2ea02011-03-22 09:04:01 +0000370 wsi->active_extensions[n]->callback(context,
371 wsi->active_extensions[n], wsi,
372 LWS_EXT_CALLBACK_DESTROY,
373 wsi->active_extensions_user[n], NULL, 0);
Andy Greenef660a92011-03-06 10:29:38 +0000374
375 free(wsi->active_extensions_user[n]);
376 }
377
Andy Greena41314f2011-05-23 10:00:03 +0100378 /*
379 * inform all extensions in case they tracked this guy out of band
380 * even though not active on him specifically
381 */
382
383 ext = context->extensions;
384 while (ext && ext->callback) {
385 ext->callback(context, ext, wsi,
386 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
387 NULL, NULL, 0);
388 ext++;
389 }
Andy Green3182ece2013-01-20 17:08:31 +0800390#endif
Andy Greena41314f2011-05-23 10:00:03 +0100391
Andy Green623a98d2013-01-21 11:04:23 +0800392 if (wsi->u.ws.rxflow_buffer)
393 free(wsi->u.ws.rxflow_buffer);
Andy Green706961d2013-01-17 16:50:35 +0800394
Andy Green43db0452013-01-10 19:50:35 +0800395/* lwsl_info("closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000396
Andy Green3faa9c72010-11-08 17:03:03 +0000397#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000398 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000399 n = SSL_get_fd(wsi->ssl);
400 SSL_shutdown(wsi->ssl);
Andy Green3fc2c652013-01-14 15:35:02 +0800401 compatible_close(n);
Andy Green3faa9c72010-11-08 17:03:03 +0000402 SSL_free(wsi->ssl);
403 } else {
404#endif
Andy Green0303db42013-01-17 14:46:43 +0800405 if (wsi->sock) {
406 n = shutdown(wsi->sock, SHUT_RDWR);
407 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800408 lwsl_debug("closing: shutdown returned %d\n",
409 errno);
Andy Green3fc2c652013-01-14 15:35:02 +0800410
Andy Green0303db42013-01-17 14:46:43 +0800411 n = compatible_close(wsi->sock);
412 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800413 lwsl_debug("closing: close returned %d\n",
414 errno);
Andy Green0303db42013-01-17 14:46:43 +0800415 }
Andy Green3faa9c72010-11-08 17:03:03 +0000416#ifdef LWS_OPENSSL_SUPPORT
417 }
418#endif
Andy Greenb5b23192013-02-11 17:13:32 +0800419 if (wsi->protocol && wsi->protocol->per_session_data_size &&
420 wsi->user_space) /* user code may own */
Andy Green4f3943a2010-11-12 10:44:16 +0000421 free(wsi->user_space);
422
Andy Green251f6fa2010-11-03 11:13:06 +0000423 free(wsi);
424}
425
Andy Green07034092011-02-13 08:37:12 +0000426/**
Andy Greenf7ee5492011-02-13 09:04:21 +0000427 * libwebsockets_hangup_on_client() - Server calls to terminate client
Andy Green6ee372f2012-04-09 15:09:01 +0800428 * connection
Peter Hinz56885f32011-03-02 22:03:47 +0000429 * @context: libwebsockets context
Andy Greenf7ee5492011-02-13 09:04:21 +0000430 * @fd: Connection socket descriptor
431 */
432
433void
Peter Hinz56885f32011-03-02 22:03:47 +0000434libwebsockets_hangup_on_client(struct libwebsocket_context *context, int fd)
Andy Greenf7ee5492011-02-13 09:04:21 +0000435{
Andy Greendfb23042013-01-17 12:26:48 +0800436 struct libwebsocket *wsi = context->lws_lookup[fd];
Andy Greenf7ee5492011-02-13 09:04:21 +0000437
Andy Greendfb23042013-01-17 12:26:48 +0800438 if (wsi) {
Andy Greenb5b23192013-02-11 17:13:32 +0800439 lwsl_info("close connection at hangup_on_client:\n");
Andy Greendfb23042013-01-17 12:26:48 +0800440 libwebsocket_close_and_free_session(context,
441 wsi, LWS_CLOSE_STATUS_NOSTATUS);
442 } else
443 close(fd);
Andy Greenf7ee5492011-02-13 09:04:21 +0000444}
445
446
447/**
Andy Green07034092011-02-13 08:37:12 +0000448 * libwebsockets_get_peer_addresses() - Get client address information
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800449 * @context: Libwebsockets context
450 * @wsi: Local struct libwebsocket associated with
Andy Green07034092011-02-13 08:37:12 +0000451 * @fd: Connection socket descriptor
452 * @name: Buffer to take client address name
453 * @name_len: Length of client address name buffer
454 * @rip: Buffer to take client address IP qotted quad
455 * @rip_len: Length of client address IP buffer
456 *
457 * This function fills in @name and @rip with the name and IP of
Andy Green6ee372f2012-04-09 15:09:01 +0800458 * the client connected with socket descriptor @fd. Names may be
459 * truncated if there is not enough room. If either cannot be
460 * determined, they will be returned as valid zero-length strings.
Andy Green07034092011-02-13 08:37:12 +0000461 */
462
463void
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800464libwebsockets_get_peer_addresses(struct libwebsocket_context *context,
465 struct libwebsocket *wsi, int fd, char *name, int name_len,
Andy Green07034092011-02-13 08:37:12 +0000466 char *rip, int rip_len)
467{
468 unsigned int len;
469 struct sockaddr_in sin;
470 struct hostent *host;
471 struct hostent *host1;
472 char ip[128];
Andy Greenf92def72011-03-09 15:02:20 +0000473 unsigned char *p;
Andy Green07034092011-02-13 08:37:12 +0000474 int n;
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800475 int ret = -1;
David Galeanocb193682013-01-09 15:29:00 +0800476#ifdef AF_LOCAL
Andy Greenb5b23192013-02-11 17:13:32 +0800477 struct sockaddr_un *un;
David Galeanocb193682013-01-09 15:29:00 +0800478#endif
Andy Green07034092011-02-13 08:37:12 +0000479
480 rip[0] = '\0';
481 name[0] = '\0';
482
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800483 lws_latency_pre(context, wsi);
484
Andy Greenb5b23192013-02-11 17:13:32 +0800485 len = sizeof(sin);
Andy Green07034092011-02-13 08:37:12 +0000486 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
487 perror("getpeername");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800488 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000489 }
Andy Green6ee372f2012-04-09 15:09:01 +0800490
Andy Greenb5b23192013-02-11 17:13:32 +0800491 host = gethostbyaddr((char *) &sin.sin_addr, sizeof(sin.sin_addr),
Andy Green07034092011-02-13 08:37:12 +0000492 AF_INET);
493 if (host == NULL) {
494 perror("gethostbyaddr");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800495 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000496 }
497
498 strncpy(name, host->h_name, name_len);
499 name[name_len - 1] = '\0';
500
501 host1 = gethostbyname(host->h_name);
502 if (host1 == NULL)
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800503 goto bail;
Andy Greenf92def72011-03-09 15:02:20 +0000504 p = (unsigned char *)host1;
Andy Green07034092011-02-13 08:37:12 +0000505 n = 0;
506 while (p != NULL) {
Andy Greenf92def72011-03-09 15:02:20 +0000507 p = (unsigned char *)host1->h_addr_list[n++];
Andy Green07034092011-02-13 08:37:12 +0000508 if (p == NULL)
509 continue;
Peter Hinzbb45a902011-03-10 18:14:01 +0000510 if ((host1->h_addrtype != AF_INET)
511#ifdef AF_LOCAL
512 && (host1->h_addrtype != AF_LOCAL)
513#endif
514 )
Andy Green07034092011-02-13 08:37:12 +0000515 continue;
516
Andy Green7627af52011-03-09 15:13:52 +0000517 if (host1->h_addrtype == AF_INET)
518 sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
Peter Hinzbb45a902011-03-10 18:14:01 +0000519#ifdef AF_LOCAL
Andy Green7627af52011-03-09 15:13:52 +0000520 else {
521 un = (struct sockaddr_un *)p;
Andy Green6ee372f2012-04-09 15:09:01 +0800522 strncpy(ip, un->sun_path, sizeof(ip) - 1);
Andy Green7627af52011-03-09 15:13:52 +0000523 ip[sizeof(ip) - 1] = '\0';
524 }
Peter Hinzbb45a902011-03-10 18:14:01 +0000525#endif
Andy Green07034092011-02-13 08:37:12 +0000526 p = NULL;
527 strncpy(rip, ip, rip_len);
528 rip[rip_len - 1] = '\0';
529 }
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800530
531 ret = 0;
532bail:
533 lws_latency(context, wsi, "libwebsockets_get_peer_addresses", ret, 1);
Andy Green07034092011-02-13 08:37:12 +0000534}
Andy Green9f990342011-02-12 11:57:45 +0000535
Peter Hinz56885f32011-03-02 22:03:47 +0000536int libwebsockets_get_random(struct libwebsocket_context *context,
537 void *buf, int len)
538{
539 int n;
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800540 char *p = (char *)buf;
Peter Hinz56885f32011-03-02 22:03:47 +0000541
542#ifdef WIN32
543 for (n = 0; n < len; n++)
544 p[n] = (unsigned char)rand();
545#else
546 n = read(context->fd_random, p, len);
547#endif
548
549 return n;
550}
551
Andy Greena690cd02013-02-09 12:25:31 +0800552int lws_set_socket_options(struct libwebsocket_context *context, int fd)
553{
554 int optval = 1;
555 socklen_t optlen = sizeof(optval);
556#ifdef WIN32
557 unsigned long optl = 0;
558#endif
559#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
560 struct protoent *tcp_proto;
561#endif
562
563 if (context->ka_time) {
564 /* enable keepalive on this socket */
565 optval = 1;
566 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
567 (const void *)&optval, optlen) < 0)
568 return 1;
569
Andy Greena47865f2013-02-10 09:39:47 +0800570#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
571
572 /*
573 * didn't find a way to set these per-socket, need to
574 * tune kernel systemwide values
575 */
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100576#elif WIN32
577 {
578 DWORD dwBytesRet;
579 struct tcp_keepalive alive;
580 alive.onoff = TRUE;
581 alive.keepalivetime = context->ka_time;
582 alive.keepaliveinterval = context->ka_interval;
Andy Greena47865f2013-02-10 09:39:47 +0800583
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100584 if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
585 NULL, 0, &dwBytesRet, NULL, NULL))
586 return 1;
587 }
Andy Greena47865f2013-02-10 09:39:47 +0800588#else
Andy Greena690cd02013-02-09 12:25:31 +0800589 /* set the keepalive conditions we want on it too */
590 optval = context->ka_time;
591 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPIDLE,
592 (const void *)&optval, optlen) < 0)
593 return 1;
594
595 optval = context->ka_probes;
596 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPINTVL,
597 (const void *)&optval, optlen) < 0)
598 return 1;
599
600 optval = context->ka_interval;
601 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPCNT,
602 (const void *)&optval, optlen) < 0)
603 return 1;
Andy Greena47865f2013-02-10 09:39:47 +0800604#endif
Andy Greena690cd02013-02-09 12:25:31 +0800605 }
606
607 /* Disable Nagle */
608 optval = 1;
609#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
610 setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen);
611#else
612 tcp_proto = getprotobyname("TCP");
613 setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen);
614#endif
615
616 /* We are nonblocking... */
617#ifdef WIN32
618 ioctlsocket(fd, FIONBIO, &optl);
619#else
620 fcntl(fd, F_SETFL, O_NONBLOCK);
621#endif
622
623 return 0;
624}
625
Andy Green95a7b5d2011-03-06 10:29:39 +0000626int lws_send_pipe_choked(struct libwebsocket *wsi)
627{
628 struct pollfd fds;
629
630 fds.fd = wsi->sock;
631 fds.events = POLLOUT;
632 fds.revents = 0;
633
634 if (poll(&fds, 1, 0) != 1)
635 return 1;
636
637 if ((fds.revents & POLLOUT) == 0)
638 return 1;
639
640 /* okay to send another packet without blocking */
641
642 return 0;
643}
644
Andy Greena41314f2011-05-23 10:00:03 +0100645int
Andy Green3b84c002011-03-06 13:14:42 +0000646lws_handle_POLLOUT_event(struct libwebsocket_context *context,
647 struct libwebsocket *wsi, struct pollfd *pollfd)
648{
Andy Green3b84c002011-03-06 13:14:42 +0000649 int n;
Andy Green6f520a52013-01-29 17:57:39 +0800650
Andy Green3182ece2013-01-20 17:08:31 +0800651#ifndef LWS_NO_EXTENSIONS
652 struct lws_tokens eff_buf;
Andy Green3b84c002011-03-06 13:14:42 +0000653 int ret;
654 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100655 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000656
Andy Greena41314f2011-05-23 10:00:03 +0100657 for (n = 0; n < wsi->count_active_extensions; n++) {
658 if (!wsi->active_extensions[n]->callback)
659 continue;
660
661 m = wsi->active_extensions[n]->callback(context,
662 wsi->active_extensions[n], wsi,
663 LWS_EXT_CALLBACK_IS_WRITEABLE,
664 wsi->active_extensions_user[n], NULL, 0);
665 if (m > handled)
666 handled = m;
667 }
668
669 if (handled == 1)
670 goto notify_action;
671
672 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000673 goto user_service;
674
675 /*
676 * check in on the active extensions, see if they
677 * had pending stuff to spill... they need to get the
678 * first look-in otherwise sequence will be disordered
679 *
680 * NULL, zero-length eff_buf means just spill pending
681 */
682
683 ret = 1;
684 while (ret == 1) {
685
686 /* default to nobody has more to spill */
687
688 ret = 0;
689 eff_buf.token = NULL;
690 eff_buf.token_len = 0;
691
692 /* give every extension a chance to spill */
693
694 for (n = 0; n < wsi->count_active_extensions; n++) {
695 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000696 wsi->protocol->owning_server,
697 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000698 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
699 wsi->active_extensions_user[n], &eff_buf, 0);
700 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800701 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000702 return -1;
703 }
704 if (m)
705 /*
706 * at least one extension told us he has more
707 * to spill, so we will go around again after
708 */
709 ret = 1;
710 }
711
712 /* assuming they gave us something to send, send it */
713
714 if (eff_buf.token_len) {
715 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
716 eff_buf.token_len))
717 return -1;
718 } else
719 continue;
720
721 /* no extension has more to spill */
722
723 if (!ret)
724 continue;
725
726 /*
727 * There's more to spill from an extension, but we just sent
728 * something... did that leave the pipe choked?
729 */
730
731 if (!lws_send_pipe_choked(wsi))
732 /* no we could add more */
733 continue;
734
Andy Green43db0452013-01-10 19:50:35 +0800735 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000736
737 /*
738 * Yes, he's choked. Leave the POLLOUT masked on so we will
739 * come back here when he is unchoked. Don't call the user
740 * callback to enforce ordering of spilling, he'll get called
741 * when we come back here and there's nothing more to spill.
742 */
743
744 return 0;
745 }
746
747 wsi->extension_data_pending = 0;
748
749user_service:
Andy Green3182ece2013-01-20 17:08:31 +0800750#endif
Andy Green3b84c002011-03-06 13:14:42 +0000751 /* one shot */
752
Andy Greena41314f2011-05-23 10:00:03 +0100753 if (pollfd) {
754 pollfd->events &= ~POLLOUT;
Andy Green3b84c002011-03-06 13:14:42 +0000755
Andy Greena41314f2011-05-23 10:00:03 +0100756 /* external POLL support via protocol 0 */
757 context->protocols[0].callback(context, wsi,
758 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
759 (void *)(long)wsi->sock, NULL, POLLOUT);
760 }
Andy Green3182ece2013-01-20 17:08:31 +0800761#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100762notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800763#endif
Andy Green3b84c002011-03-06 13:14:42 +0000764
Andy Green9e4c2b62011-03-07 20:47:39 +0000765 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
766 n = LWS_CALLBACK_CLIENT_WRITEABLE;
767 else
768 n = LWS_CALLBACK_SERVER_WRITEABLE;
769
Andy Greenb8b247d2013-01-22 07:20:08 +0800770 return user_callback_handle_rxflow(wsi->protocol->callback, context,
Andy Greenb5b23192013-02-11 17:13:32 +0800771 wsi, (enum libwebsocket_callback_reasons) n,
772 wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000773}
774
775
776
Andy Greena41314f2011-05-23 10:00:03 +0100777void
778libwebsocket_service_timeout_check(struct libwebsocket_context *context,
779 struct libwebsocket *wsi, unsigned int sec)
780{
Andy Green3182ece2013-01-20 17:08:31 +0800781#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100782 int n;
783
784 /*
785 * if extensions want in on it (eg, we are a mux parent)
786 * give them a chance to service child timeouts
787 */
788
789 for (n = 0; n < wsi->count_active_extensions; n++)
790 wsi->active_extensions[n]->callback(
791 context, wsi->active_extensions[n],
792 wsi, LWS_EXT_CALLBACK_1HZ,
793 wsi->active_extensions_user[n], NULL, sec);
794
Andy Green3182ece2013-01-20 17:08:31 +0800795#endif
Andy Greena41314f2011-05-23 10:00:03 +0100796 if (!wsi->pending_timeout)
797 return;
Andy Green6ee372f2012-04-09 15:09:01 +0800798
Andy Greena41314f2011-05-23 10:00:03 +0100799 /*
800 * if we went beyond the allowed time, kill the
801 * connection
802 */
803
804 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800805 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100806 libwebsocket_close_and_free_session(context,
807 wsi, LWS_CLOSE_STATUS_NOSTATUS);
808 }
809}
810
Andy Green9f990342011-02-12 11:57:45 +0000811/**
812 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000813 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000814 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800815 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000816 *
Andy Green75006172013-01-22 12:32:11 +0800817 * This function takes a pollfd that has POLLIN or POLLOUT activity and
Andy Greenb5b23192013-02-11 17:13:32 +0800818 * services it according to the state of the associated
819 * struct libwebsocket.
Andy Green75006172013-01-22 12:32:11 +0800820 *
821 * The one call deals with all "service" that might happen on a socket
822 * including listen accepts, http files as well as websocket protocol.
Andy Green9f990342011-02-12 11:57:45 +0000823 */
824
825int
Peter Hinz56885f32011-03-02 22:03:47 +0000826libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000827 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000828{
Andy Greena1ce6be2013-01-18 11:43:21 +0800829 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000830 int n;
Andy Green0d338332011-02-12 11:57:43 +0000831 int m;
Andy Greenf0b79e22013-02-10 10:46:45 +0800832 int listen_socket_fds_index = 0;
Andy Greena71eafc2011-02-14 17:59:43 +0000833 struct timeval tv;
Andy Green6f520a52013-01-29 17:57:39 +0800834
Andy Green3182ece2013-01-20 17:08:31 +0800835#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000836 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800837#endif
Andy Green98a717c2011-03-06 13:14:15 +0000838 struct lws_tokens eff_buf;
Andy Greenf0b79e22013-02-10 10:46:45 +0800839
840 if (context->listen_service_fd)
841 listen_socket_fds_index = context->lws_lookup[
842 context->listen_service_fd]->position_in_fds_table;
843
Andy Greena71eafc2011-02-14 17:59:43 +0000844 /*
845 * you can call us with pollfd = NULL to just allow the once-per-second
846 * global timeout checks; if less than a second since the last check
847 * it returns immediately then.
848 */
849
850 gettimeofday(&tv, NULL);
851
Peter Hinz56885f32011-03-02 22:03:47 +0000852 if (context->last_timeout_check_s != tv.tv_sec) {
853 context->last_timeout_check_s = tv.tv_sec;
Andy Greena71eafc2011-02-14 17:59:43 +0000854
Joakim Soderberg4c531232013-02-06 15:26:58 +0900855 #ifndef WIN32
Andy Green24cba922013-01-19 13:56:10 +0800856 /* if our parent went down, don't linger around */
Andy Greenb5b23192013-02-11 17:13:32 +0800857 if (context->started_with_parent &&
858 kill(context->started_with_parent, 0) < 0)
Andy Green24cba922013-01-19 13:56:10 +0800859 kill(getpid(), SIGTERM);
Joakim Soderberg4c531232013-02-06 15:26:58 +0900860 #endif
Andy Green24cba922013-01-19 13:56:10 +0800861
Andy Greena71eafc2011-02-14 17:59:43 +0000862 /* global timeout check once per second */
863
Peter Hinz56885f32011-03-02 22:03:47 +0000864 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +0800865 struct libwebsocket *new_wsi =
866 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +0800867 if (!new_wsi)
868 continue;
869 libwebsocket_service_timeout_check(context,
870 new_wsi, tv.tv_sec);
Andy Greena71eafc2011-02-14 17:59:43 +0000871 }
872 }
873
874 /* just here for timeout management? */
875
876 if (pollfd == NULL)
877 return 0;
878
879 /* no, here to service a socket descriptor */
880
Andy Green65b0e912013-01-16 07:59:47 +0800881 /*
882 * deal with listen service piggybacking
883 * every listen_service_modulo services of other fds, we
884 * sneak one in to service the listen socket if there's anything waiting
885 *
886 * To handle connection storms, as found in ab, if we previously saw a
887 * pending connection here, it causes us to check again next time.
888 */
889
Andy Greenb5b23192013-02-11 17:13:32 +0800890 if (context->listen_service_fd && pollfd !=
891 &context->fds[listen_socket_fds_index]) {
Andy Green65b0e912013-01-16 07:59:47 +0800892 context->listen_service_count++;
893 if (context->listen_service_extraseen ||
Andy Greenb5b23192013-02-11 17:13:32 +0800894 context->listen_service_count ==
895 context->listen_service_modulo) {
Andy Green65b0e912013-01-16 07:59:47 +0800896 context->listen_service_count = 0;
897 m = 1;
898 if (context->listen_service_extraseen > 5)
899 m = 2;
900 while (m--) {
Andy Greenb5b23192013-02-11 17:13:32 +0800901 /*
902 * even with extpoll, we prepared this
903 * internal fds for listen
904 */
905 n = poll(&context->fds[listen_socket_fds_index],
906 1, 0);
907 if (n > 0) { /* there's a conn waiting for us */
908 libwebsocket_service_fd(context,
909 &context->
910 fds[listen_socket_fds_index]);
Andy Green65b0e912013-01-16 07:59:47 +0800911 context->listen_service_extraseen++;
912 } else {
913 if (context->listen_service_extraseen)
Andy Greenb5b23192013-02-11 17:13:32 +0800914 context->
915 listen_service_extraseen--;
Andy Green65b0e912013-01-16 07:59:47 +0800916 break;
917 }
918 }
919 }
920
921 }
922
923 /* okay, what we came here to do... */
924
Andy Greendfb23042013-01-17 12:26:48 +0800925 wsi = context->lws_lookup[pollfd->fd];
Andy Greend280b6e2013-01-15 13:40:23 +0800926 if (wsi == NULL) {
Andy Greendfb23042013-01-17 12:26:48 +0800927 if (pollfd->fd > 11)
Andy Greenb5b23192013-02-11 17:13:32 +0800928 lwsl_err("unexpected NULL wsi fd=%d fds_count=%d\n",
929 pollfd->fd, context->fds_count);
Andy Greenfa3f4052012-10-07 20:40:35 +0800930 return 0;
Andy Greend280b6e2013-01-15 13:40:23 +0800931 }
Andy Green8f037e42010-12-19 22:13:26 +0000932
Andy Green0d338332011-02-12 11:57:43 +0000933 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +0800934
Andy Greena1ce6be2013-01-18 11:43:21 +0800935#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +0800936 case LWS_CONNMODE_HTTP_SERVING:
Andy Green0d338332011-02-12 11:57:43 +0000937 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +0800938 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greena1ce6be2013-01-18 11:43:21 +0800939 return lws_server_socket_service(context, wsi, pollfd);
940#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000941
Andy Green0d338332011-02-12 11:57:43 +0000942 case LWS_CONNMODE_WS_SERVING:
943 case LWS_CONNMODE_WS_CLIENT:
944
945 /* handle session socket closed */
946
947 if (pollfd->revents & (POLLERR | POLLHUP)) {
948
Andy Green43db0452013-01-10 19:50:35 +0800949 lwsl_debug("Session Socket %p (fd=%d) dead\n",
Andy Green0d338332011-02-12 11:57:43 +0000950 (void *)wsi, pollfd->fd);
951
Peter Hinz56885f32011-03-02 22:03:47 +0000952 libwebsocket_close_and_free_session(context, wsi,
Andy Green687b0182011-02-26 11:04:01 +0000953 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800954 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000955 }
956
Andy Green0d338332011-02-12 11:57:43 +0000957 /* the guy requested a callback when it was OK to write */
958
Andy Greenda527df2011-03-07 07:08:12 +0000959 if ((pollfd->revents & POLLOUT) &&
960 wsi->state == WSI_STATE_ESTABLISHED)
961 if (lws_handle_POLLOUT_event(context, wsi,
962 pollfd) < 0) {
Andy Green16ab3182013-02-10 18:02:31 +0800963 lwsl_info("libwebsocket_service_fd: closing");
Andy Greenda527df2011-03-07 07:08:12 +0000964 libwebsocket_close_and_free_session(
965 context, wsi, LWS_CLOSE_STATUS_NORMAL);
Andy Green040d2ef2013-01-16 13:40:43 +0800966 return 0;
Andy Green3b84c002011-03-06 13:14:42 +0000967 }
Andy Green0d338332011-02-12 11:57:43 +0000968
Andy Green0d338332011-02-12 11:57:43 +0000969
970 /* any incoming data ready? */
971
972 if (!(pollfd->revents & POLLIN))
973 break;
974
Andy Greenb45993c2010-12-18 15:13:50 +0000975#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +0800976read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +0800977 if (wsi->ssl) {
Andy Greene84652c2013-02-08 13:01:02 +0800978 eff_buf.token_len = SSL_read(wsi->ssl,
979 context->service_buffer,
Andy Greenb5b23192013-02-11 17:13:32 +0800980 sizeof(context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +0800981 if (!eff_buf.token_len) {
982 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
Andy Greene84652c2013-02-08 13:01:02 +0800983 lwsl_err("SSL_read returned 0 with reason %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +0800984 ERR_error_string(n,
985 (char *)context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +0800986 }
987 } else
Andy Greenb45993c2010-12-18 15:13:50 +0000988#endif
Andy Greene84652c2013-02-08 13:01:02 +0800989 eff_buf.token_len = recv(pollfd->fd,
Andy Greenb5b23192013-02-11 17:13:32 +0800990 context->service_buffer,
991 sizeof(context->service_buffer), 0);
Andy Greenb45993c2010-12-18 15:13:50 +0000992
Andy Green98a717c2011-03-06 13:14:15 +0000993 if (eff_buf.token_len < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800994 lwsl_debug("Socket read returned %d\n",
Andy Green98a717c2011-03-06 13:14:15 +0000995 eff_buf.token_len);
Alon Levydc93b7f2012-10-19 11:21:57 +0200996 if (errno != EINTR && errno != EAGAIN)
Andy Green6ee372f2012-04-09 15:09:01 +0800997 libwebsocket_close_and_free_session(context,
998 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800999 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +00001000 }
Andy Green98a717c2011-03-06 13:14:15 +00001001 if (!eff_buf.token_len) {
Andy Greenb5b23192013-02-11 17:13:32 +08001002 lwsl_info("closing connection due to 0 length read\n");
Peter Hinz56885f32011-03-02 22:03:47 +00001003 libwebsocket_close_and_free_session(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001004 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenfa3f4052012-10-07 20:40:35 +08001005 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +00001006 }
1007
Andy Green98a717c2011-03-06 13:14:15 +00001008 /*
1009 * give any active extensions a chance to munge the buffer
1010 * before parse. We pass in a pointer to an lws_tokens struct
1011 * prepared with the default buffer and content length that's in
1012 * there. Rather than rewrite the default buffer, extensions
1013 * that expect to grow the buffer can adapt .token to
1014 * point to their own per-connection buffer in the extension
1015 * user allocation. By default with no extensions or no
1016 * extension callback handling, just the normal input buffer is
1017 * used then so it is efficient.
1018 */
Andy Greenb45993c2010-12-18 15:13:50 +00001019
Andy Greene84652c2013-02-08 13:01:02 +08001020 eff_buf.token = (char *)context->service_buffer;
Andy Green3182ece2013-01-20 17:08:31 +08001021#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001022 more = 1;
1023 while (more) {
Andy Green0d338332011-02-12 11:57:43 +00001024
Andy Green98a717c2011-03-06 13:14:15 +00001025 more = 0;
1026
1027 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +00001028 m = wsi->active_extensions[n]->callback(context,
1029 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +00001030 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +00001031 wsi->active_extensions_user[n],
1032 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +00001033 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001034 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +08001035 "Extension reports fatal error\n");
1036 libwebsocket_close_and_free_session(
1037 context, wsi,
1038 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +08001039 return 0;
Andy Green98a717c2011-03-06 13:14:15 +00001040 }
1041 if (m)
1042 more = 1;
1043 }
Andy Green3182ece2013-01-20 17:08:31 +08001044#endif
Andy Green98a717c2011-03-06 13:14:15 +00001045 /* service incoming data */
1046
1047 if (eff_buf.token_len) {
1048 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001049 (unsigned char *)eff_buf.token,
1050 eff_buf.token_len);
Andy Green98a717c2011-03-06 13:14:15 +00001051 if (n < 0)
1052 /* we closed wsi */
Andy Green040d2ef2013-01-16 13:40:43 +08001053 return 0;
Andy Green98a717c2011-03-06 13:14:15 +00001054 }
Andy Green3182ece2013-01-20 17:08:31 +08001055#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001056 eff_buf.token = NULL;
1057 eff_buf.token_len = 0;
1058 }
Andy Green3182ece2013-01-20 17:08:31 +08001059#endif
David Galeano7ffbe1b2013-01-10 10:35:32 +08001060
1061#ifdef LWS_OPENSSL_SUPPORT
1062 if (wsi->ssl && SSL_pending(wsi->ssl))
1063 goto read_pending;
1064#endif
Andy Green98a717c2011-03-06 13:14:15 +00001065 break;
Andy Green76f61e72013-01-16 11:53:05 +08001066
1067 default:
Andy Green03674a62013-01-16 11:47:40 +08001068#ifdef LWS_NO_CLIENT
1069 break;
1070#else
Andy Green76f61e72013-01-16 11:53:05 +08001071 return lws_client_socket_service(context, wsi, pollfd);
Andy Green03674a62013-01-16 11:47:40 +08001072#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001073 }
1074
1075 return 0;
1076}
1077
Andy Green0d338332011-02-12 11:57:43 +00001078
Andy Green6964bb52011-01-23 16:50:33 +00001079/**
1080 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +00001081 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001082 *
1083 * This function closes any active connections and then frees the
1084 * context. After calling this, any further use of the context is
1085 * undefined.
1086 */
1087void
Peter Hinz56885f32011-03-02 22:03:47 +00001088libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +00001089{
Andy Green3182ece2013-01-20 17:08:31 +08001090#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +00001091 int n;
1092 int m;
Andy Greena41314f2011-05-23 10:00:03 +01001093 struct libwebsocket_extension *ext;
Andy Greena7109e62013-02-11 12:05:54 +08001094 struct libwebsocket_protocols *protocol = context->protocols;
Andy Green6964bb52011-01-23 16:50:33 +00001095
Andy Greend636e352013-01-29 12:36:17 +08001096#ifdef LWS_LATENCY
1097 if (context->worst_latency_info[0])
1098 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1099#endif
1100
Andy Greendfb23042013-01-17 12:26:48 +08001101 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +08001102 struct libwebsocket *wsi =
1103 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +08001104 libwebsocket_close_and_free_session(context,
Andy Green7b922052013-02-11 11:43:05 +08001105 wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */);
1106 n--;
Andy Greendfb23042013-01-17 12:26:48 +08001107 }
Andy Green6964bb52011-01-23 16:50:33 +00001108
Andy Greena41314f2011-05-23 10:00:03 +01001109 /*
1110 * give all extensions a chance to clean up any per-context
1111 * allocations they might have made
1112 */
1113
1114 ext = context->extensions;
1115 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
1116 if (context->listen_port)
1117 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +08001118 while (ext && ext->callback) {
Andy Greenb5b23192013-02-11 17:13:32 +08001119 ext->callback(context, ext, NULL,
1120 (enum libwebsocket_extension_callback_reasons)m,
1121 NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001122 ext++;
1123 }
Andy Greena7109e62013-02-11 12:05:54 +08001124
1125 /*
1126 * inform all the protocols that they are done and will have no more
1127 * callbacks
1128 */
1129
1130 while (protocol->callback) {
1131 protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY,
1132 NULL, NULL, 0);
1133 protocol++;
1134 }
1135
Andy Green3182ece2013-01-20 17:08:31 +08001136#endif
Andy Greena41314f2011-05-23 10:00:03 +01001137
Peter Hinz56885f32011-03-02 22:03:47 +00001138#ifdef WIN32
1139#else
1140 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00001141#endif
1142
Peter Hinz56885f32011-03-02 22:03:47 +00001143#ifdef LWS_OPENSSL_SUPPORT
1144 if (context->ssl_ctx)
1145 SSL_CTX_free(context->ssl_ctx);
1146 if (context->ssl_client_ctx)
1147 SSL_CTX_free(context->ssl_client_ctx);
Andy Greenad686392013-02-11 14:50:45 +08001148
1149 ERR_remove_state(0);
1150 ERR_free_strings();
1151 EVP_cleanup();
1152 CRYPTO_cleanup_all_ex_data();
Peter Hinz56885f32011-03-02 22:03:47 +00001153#endif
1154
Andy Green46596482013-02-11 11:04:01 +08001155 if (context->fds)
1156 free(context->fds);
1157 if (context->lws_lookup)
1158 free(context->lws_lookup);
1159
Peter Hinz56885f32011-03-02 22:03:47 +00001160 free(context);
1161
1162#ifdef WIN32
1163 WSACleanup();
1164#endif
Andy Green6964bb52011-01-23 16:50:33 +00001165}
1166
Andy Greend88146d2013-01-22 12:40:35 +08001167/**
Andy Greenb5b23192013-02-11 17:13:32 +08001168 * libwebsocket_context_user() - get the user data associated with the context
Andy Greend88146d2013-01-22 12:40:35 +08001169 * @context: Websocket context
1170 *
1171 * This returns the optional user allocation that can be attached to
1172 * the context the sockets live in at context_create time. It's a way
1173 * to let all sockets serviced in the same context share data without
1174 * using globals statics in the user code.
1175 */
Alon Levy0291eb32012-10-19 11:21:56 +02001176LWS_EXTERN void *
1177libwebsocket_context_user(struct libwebsocket_context *context)
1178{
Andy Greenb5b23192013-02-11 17:13:32 +08001179 return context->user_space;
Alon Levy0291eb32012-10-19 11:21:56 +02001180}
1181
Andy Green6964bb52011-01-23 16:50:33 +00001182/**
1183 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001184 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001185 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1186 * service otherwise block and service immediately, returning
1187 * after the timeout if nothing needed service.
1188 *
1189 * This function deals with any pending websocket traffic, for three
1190 * kinds of event. It handles these events on both server and client
1191 * types of connection the same.
1192 *
1193 * 1) Accept new connections to our context's server
1194 *
Andy Green6f520a52013-01-29 17:57:39 +08001195 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001196 * server or client connections.
1197 *
1198 * You need to call this service function periodically to all the above
1199 * functions to happen; if your application is single-threaded you can
1200 * just call it in your main event loop.
1201 *
1202 * Alternatively you can fork a new process that asynchronously handles
1203 * calling this service in a loop. In that case you are happy if this
1204 * call blocks your thread until it needs to take care of something and
1205 * would call it with a large nonzero timeout. Your loop then takes no
1206 * CPU while there is nothing happening.
1207 *
1208 * If you are calling it in a single-threaded app, you don't want it to
1209 * wait around blocking other things in your loop from happening, so you
1210 * would call it with a timeout_ms of 0, so it returns immediately if
1211 * nothing is pending, or as soon as it services whatever was pending.
1212 */
1213
Andy Greene92cd172011-01-19 13:11:55 +00001214int
Peter Hinz56885f32011-03-02 22:03:47 +00001215libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001216{
1217 int n;
Andy Greene92cd172011-01-19 13:11:55 +00001218
1219 /* stay dead once we are dead */
1220
Peter Hinz56885f32011-03-02 22:03:47 +00001221 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001222 return 1;
1223
Andy Green0d338332011-02-12 11:57:43 +00001224 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001225
Peter Hinz56885f32011-03-02 22:03:47 +00001226 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green3221f922011-02-12 13:14:11 +00001227 if (n == 0) /* poll timeout */
1228 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001229
Andy Greendfb23042013-01-17 12:26:48 +08001230 if (n < 0)
Andy Green3928f612012-07-20 12:58:38 +08001231 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001232
Andy Greendfb23042013-01-17 12:26:48 +08001233 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001234
Peter Hinz56885f32011-03-02 22:03:47 +00001235 for (n = 0; n < context->fds_count; n++)
1236 if (context->fds[n].revents)
Andy Green3928f612012-07-20 12:58:38 +08001237 if (libwebsocket_service_fd(context,
1238 &context->fds[n]) < 0)
1239 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001240 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001241}
1242
Andy Green3182ece2013-01-20 17:08:31 +08001243#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001244int
1245lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001246 struct libwebsocket *wsi,
1247 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001248 void *v, size_t len)
1249{
1250 int n;
1251 int handled = 0;
1252
1253 /* maybe an extension will take care of it for us */
1254
1255 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1256 if (!wsi->active_extensions[n]->callback)
1257 continue;
1258
1259 handled |= wsi->active_extensions[n]->callback(context,
1260 wsi->active_extensions[n], wsi,
1261 r, wsi->active_extensions_user[n], v, len);
1262 }
1263
1264 return handled;
1265}
1266
1267
1268void *
1269lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001270 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001271{
1272 int n = 0;
1273
Andy Green68b45042011-05-25 21:41:57 +01001274 if (wsi == NULL)
1275 return NULL;
1276
Andy Greena41314f2011-05-23 10:00:03 +01001277 while (n < wsi->count_active_extensions) {
1278 if (wsi->active_extensions[n] != ext) {
1279 n++;
1280 continue;
1281 }
1282 return wsi->active_extensions_user[n];
1283 }
1284
1285 return NULL;
1286}
Andy Green3182ece2013-01-20 17:08:31 +08001287#endif
Andy Greena41314f2011-05-23 10:00:03 +01001288
Andy Green90c7cbc2011-01-27 06:26:52 +00001289/**
1290 * libwebsocket_callback_on_writable() - Request a callback when this socket
1291 * becomes able to be written to without
1292 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001293 *
Peter Hinz56885f32011-03-02 22:03:47 +00001294 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001295 * @wsi: Websocket connection instance to get callback for
1296 */
1297
1298int
Peter Hinz56885f32011-03-02 22:03:47 +00001299libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001300 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001301{
Andy Green3182ece2013-01-20 17:08:31 +08001302#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001303 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001304 int handled = 0;
1305
1306 /* maybe an extension will take care of it for us */
1307
1308 for (n = 0; n < wsi->count_active_extensions; n++) {
1309 if (!wsi->active_extensions[n]->callback)
1310 continue;
1311
1312 handled |= wsi->active_extensions[n]->callback(context,
1313 wsi->active_extensions[n], wsi,
1314 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1315 wsi->active_extensions_user[n], NULL, 0);
1316 }
1317
1318 if (handled)
1319 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001320#endif
Andy Greendfb23042013-01-17 12:26:48 +08001321 if (wsi->position_in_fds_table < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001322 lwsl_err("libwebsocket_callback_on_writable: failed to find socket %d\n",
1323 wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001324 return -1;
1325 }
1326
1327 context->fds[wsi->position_in_fds_table].events |= POLLOUT;
Andy Greena41314f2011-05-23 10:00:03 +01001328
Andy Green3221f922011-02-12 13:14:11 +00001329 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00001330 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001331 LWS_CALLBACK_SET_MODE_POLL_FD,
1332 (void *)(long)wsi->sock, NULL, POLLOUT);
1333
Andy Green90c7cbc2011-01-27 06:26:52 +00001334 return 1;
1335}
1336
1337/**
1338 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1339 * all connections using the given protocol when it
1340 * becomes possible to write to each socket without
1341 * blocking in turn.
1342 *
1343 * @protocol: Protocol whose connections will get callbacks
1344 */
1345
1346int
1347libwebsocket_callback_on_writable_all_protocol(
1348 const struct libwebsocket_protocols *protocol)
1349{
Peter Hinz56885f32011-03-02 22:03:47 +00001350 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001351 int n;
Andy Green0d338332011-02-12 11:57:43 +00001352 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001353
Andy Greendfb23042013-01-17 12:26:48 +08001354 for (n = 0; n < context->fds_count; n++) {
1355 wsi = context->lws_lookup[context->fds[n].fd];
1356 if (!wsi)
1357 continue;
1358 if (wsi->protocol == protocol)
1359 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001360 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001361
1362 return 0;
1363}
1364
Andy Greenbe93fef2011-02-14 20:25:43 +00001365/**
1366 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1367 *
1368 * You will not need this unless you are doing something special
1369 *
1370 * @wsi: Websocket connection instance
1371 * @reason: timeout reason
1372 * @secs: how many seconds
1373 */
1374
1375void
1376libwebsocket_set_timeout(struct libwebsocket *wsi,
1377 enum pending_timeout reason, int secs)
1378{
1379 struct timeval tv;
1380
1381 gettimeofday(&tv, NULL);
1382
1383 wsi->pending_timeout_limit = tv.tv_sec + secs;
1384 wsi->pending_timeout = reason;
1385}
1386
Andy Greena6cbece2011-01-27 20:06:03 +00001387
1388/**
1389 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1390 *
1391 * You will not need this unless you are doing something special
1392 *
1393 * @wsi: Websocket connection instance
1394 */
1395
1396int
1397libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1398{
1399 return wsi->sock;
1400}
1401
Andy Greend636e352013-01-29 12:36:17 +08001402#ifdef LWS_LATENCY
1403void
Andy Greenb5b23192013-02-11 17:13:32 +08001404lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi,
1405 const char *action, int ret, int completed)
Andy Greend636e352013-01-29 12:36:17 +08001406{
1407 struct timeval tv;
1408 unsigned long u;
1409 char buf[256];
1410
1411 gettimeofday(&tv, NULL);
1412
1413 u = (tv.tv_sec * 1000000) + tv.tv_usec;
1414
1415 if (action) {
1416 if (completed) {
1417 if (wsi->action_start == wsi->latency_start)
Andy Greenb5b23192013-02-11 17:13:32 +08001418 sprintf(buf,
1419 "Completion first try lat %luus: %p: ret %d: %s\n",
1420 u - wsi->latency_start,
1421 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001422 else
Andy Greenb5b23192013-02-11 17:13:32 +08001423 sprintf(buf,
1424 "Completion %luus: lat %luus: %p: ret %d: %s\n",
1425 u - wsi->action_start,
1426 u - wsi->latency_start,
1427 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001428 wsi->action_start = 0;
1429 } else
Andy Greenb5b23192013-02-11 17:13:32 +08001430 sprintf(buf, "lat %luus: %p: ret %d: %s\n",
1431 u - wsi->latency_start,
1432 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001433 if (u - wsi->latency_start > context->worst_latency) {
1434 context->worst_latency = u - wsi->latency_start;
1435 strcpy(context->worst_latency_info, buf);
1436 }
1437 lwsl_latency("%s", buf);
1438 } else {
1439 wsi->latency_start = u;
1440 if (!wsi->action_start)
1441 wsi->action_start = u;
1442 }
1443}
1444#endif
1445
Andy Greena1ce6be2013-01-18 11:43:21 +08001446#ifdef LWS_NO_SERVER
1447int
1448_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1449{
1450 return 0;
1451}
1452#else
Andy Green706961d2013-01-17 16:50:35 +08001453int
1454_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1455{
1456 struct libwebsocket_context *context = wsi->protocol->owning_server;
1457 int n;
1458
Andy Green623a98d2013-01-21 11:04:23 +08001459 if (!(wsi->u.ws.rxflow_change_to & 2))
Andy Green706961d2013-01-17 16:50:35 +08001460 return 0;
1461
Andy Green623a98d2013-01-21 11:04:23 +08001462 wsi->u.ws.rxflow_change_to &= ~2;
Andy Green706961d2013-01-17 16:50:35 +08001463
Andy Greenb5b23192013-02-11 17:13:32 +08001464 lwsl_info("rxflow: wsi %p change_to %d\n",
1465 wsi, wsi->u.ws.rxflow_change_to);
Andy Green706961d2013-01-17 16:50:35 +08001466
1467 /* if we're letting it come again, did we interrupt anything? */
Andy Green623a98d2013-01-21 11:04:23 +08001468 if ((wsi->u.ws.rxflow_change_to & 1) && wsi->u.ws.rxflow_buffer) {
Andy Green706961d2013-01-17 16:50:35 +08001469 n = libwebsocket_interpret_incoming_packet(wsi, NULL, 0);
1470 if (n < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001471 lwsl_info("libwebsocket_rx_flow_control: close req\n");
Andy Green706961d2013-01-17 16:50:35 +08001472 return -1;
1473 }
1474 if (n)
1475 /* oh he stuck again, do nothing */
1476 return 0;
1477 }
1478
Andy Green623a98d2013-01-21 11:04:23 +08001479 if (wsi->u.ws.rxflow_change_to & 1)
Andy Green706961d2013-01-17 16:50:35 +08001480 context->fds[wsi->position_in_fds_table].events |= POLLIN;
1481 else
1482 context->fds[wsi->position_in_fds_table].events &= ~POLLIN;
1483
Andy Green623a98d2013-01-21 11:04:23 +08001484 if (wsi->u.ws.rxflow_change_to & 1)
Andy Green706961d2013-01-17 16:50:35 +08001485 /* external POLL support via protocol 0 */
1486 context->protocols[0].callback(context, wsi,
1487 LWS_CALLBACK_SET_MODE_POLL_FD,
1488 (void *)(long)wsi->sock, NULL, POLLIN);
1489 else
1490 /* external POLL support via protocol 0 */
1491 context->protocols[0].callback(context, wsi,
1492 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
1493 (void *)(long)wsi->sock, NULL, POLLIN);
1494
1495 return 1;
1496}
Andy Greena1ce6be2013-01-18 11:43:21 +08001497#endif
Andy Green706961d2013-01-17 16:50:35 +08001498
Andy Green90c7cbc2011-01-27 06:26:52 +00001499/**
1500 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1501 * receieved packets.
1502 *
1503 * If the output side of a server process becomes choked, this allows flow
1504 * control for the input side.
1505 *
1506 * @wsi: Websocket connection instance to get callback for
1507 * @enable: 0 = disable read servicing for this connection, 1 = enable
1508 */
1509
1510int
1511libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1512{
Andy Green623a98d2013-01-21 11:04:23 +08001513 wsi->u.ws.rxflow_change_to = 2 | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001514
Andy Green706961d2013-01-17 16:50:35 +08001515 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001516}
1517
Andy Green706961d2013-01-17 16:50:35 +08001518
Andy Green2ac5a6f2011-01-28 10:00:18 +00001519/**
1520 * libwebsocket_canonical_hostname() - returns this host's hostname
1521 *
1522 * This is typically used by client code to fill in the host parameter
1523 * when making a client connection. You can only call it after the context
1524 * has been created.
1525 *
Peter Hinz56885f32011-03-02 22:03:47 +00001526 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001527 */
1528
1529
1530extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001531libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001532{
Peter Hinz56885f32011-03-02 22:03:47 +00001533 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001534}
1535
1536
Andy Green90c7cbc2011-01-27 06:26:52 +00001537static void sigpipe_handler(int x)
1538{
1539}
1540
Andy Green6901cb32011-02-21 08:06:47 +00001541#ifdef LWS_OPENSSL_SUPPORT
1542static int
1543OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1544{
1545
1546 SSL *ssl;
1547 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001548 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001549
1550 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1551 SSL_get_ex_data_X509_STORE_CTX_idx());
1552
1553 /*
Andy Green2e24da02011-03-05 16:12:04 +00001554 * !!! nasty openssl requires the index to come as a library-scope
1555 * static
Andy Green6901cb32011-02-21 08:06:47 +00001556 */
Andy Green2e24da02011-03-05 16:12:04 +00001557 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001558
Peter Hinz56885f32011-03-02 22:03:47 +00001559 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001560 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1561 x509_ctx, ssl, preverify_ok);
1562
1563 /* convert return code from 0 = OK to 1 = OK */
1564
1565 if (!n)
1566 n = 1;
1567 else
1568 n = 0;
1569
1570 return n;
1571}
1572#endif
1573
Andy Green706961d2013-01-17 16:50:35 +08001574int user_callback_handle_rxflow(callback_function callback_function,
Andy Greenb5b23192013-02-11 17:13:32 +08001575 struct libwebsocket_context *context,
Andy Green706961d2013-01-17 16:50:35 +08001576 struct libwebsocket *wsi,
1577 enum libwebsocket_callback_reasons reason, void *user,
1578 void *in, size_t len)
1579{
1580 int n;
1581
1582 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenaedc9532013-02-10 21:21:24 +08001583 if (!n)
1584 n = _libwebsocket_rx_flow_control(wsi);
Andy Green706961d2013-01-17 16:50:35 +08001585
Andy Greenaedc9532013-02-10 21:21:24 +08001586 return n;
Andy Green706961d2013-01-17 16:50:35 +08001587}
1588
Andy Greenb45993c2010-12-18 15:13:50 +00001589
Andy Greenab990e42010-10-31 12:42:52 +00001590/**
Andy Green4739e5c2011-01-22 12:51:57 +00001591 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08001592 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00001593 *
Andy Green1b265272013-02-09 14:01:09 +08001594 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00001595 * of all initialization in one step.
1596 *
Andy Greene92cd172011-01-19 13:11:55 +00001597 * After initialization, it returns a struct libwebsocket_context * that
1598 * represents this server. After calling, user code needs to take care
1599 * of calling libwebsocket_service() with the context pointer to get the
1600 * server's sockets serviced. This can be done in the same process context
1601 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001602 *
Andy Green8f037e42010-12-19 22:13:26 +00001603 * The protocol callback functions are called for a handful of events
1604 * including http requests coming in, websocket connections becoming
1605 * established, and data arriving; it's also called periodically to allow
1606 * async transmission.
1607 *
1608 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1609 * at that time websocket protocol has not been negotiated. Other
1610 * protocols after the first one never see any HTTP callack activity.
1611 *
1612 * The server created is a simple http server by default; part of the
1613 * websocket standard is upgrading this http connection to a websocket one.
1614 *
1615 * This allows the same server to provide files like scripts and favicon /
1616 * images or whatever over http and dynamic data over websockets all in
1617 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001618 */
Andy Green4ea60062010-10-30 12:15:07 +01001619
Andy Greene92cd172011-01-19 13:11:55 +00001620struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08001621libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01001622{
Peter Hinz56885f32011-03-02 22:03:47 +00001623 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001624 char *p;
Andy Greencbb31222013-01-31 09:57:05 +08001625#ifndef LWS_NO_SERVER
Andy Green14f47292013-02-11 19:36:15 +08001626 int n;
Andy Greencbb31222013-01-31 09:57:05 +08001627 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00001628 struct libwebsocket *wsi;
Andy Greencbb31222013-01-31 09:57:05 +08001629 struct sockaddr_in serv_addr;
1630#endif
Andy Green3182ece2013-01-20 17:08:31 +08001631#ifndef LWS_NO_EXTENSIONS
1632 int m;
Andy Green1b265272013-02-09 14:01:09 +08001633 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08001634#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001635
Andy Green3faa9c72010-11-08 17:03:03 +00001636#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001637 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001638#endif
1639
Joakim Soderberg4c531232013-02-06 15:26:58 +09001640#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001641 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09001642#endif
1643
Andy Greenb3a614a2013-01-19 13:08:17 +08001644 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08001645 lwsl_notice("Library version: %s\n", library_version);
Andy Greenb5b23192013-02-11 17:13:32 +08001646 lwsl_info(" LWS_MAX_HEADER_NAME_LENGTH: %u\n",
1647 LWS_MAX_HEADER_NAME_LENGTH);
Andy Greenc0d6b632013-01-12 23:42:17 +08001648 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
Andy Greenc0d6b632013-01-12 23:42:17 +08001649 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001650#ifndef LWS_NO_EXTENSIONS
Andy Greenb5b23192013-02-11 17:13:32 +08001651 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n",
1652 LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001653#else
1654 lwsl_notice(" Configured without extension support\n");
1655#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001656 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1657 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
1658 lwsl_info(" CIPHERS_LIST_STRING: '%s'\n", CIPHERS_LIST_STRING);
1659 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1660 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001661
Peter Hinz56885f32011-03-02 22:03:47 +00001662#ifdef _WIN32
1663 {
1664 WORD wVersionRequested;
1665 WSADATA wsaData;
1666 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001667 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001668
1669 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1670 wVersionRequested = MAKEWORD(2, 2);
1671
1672 err = WSAStartup(wVersionRequested, &wsaData);
1673 if (err != 0) {
1674 /* Tell the user that we could not find a usable */
1675 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001676 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001677 return NULL;
1678 }
David Galeano7b11fec2011-10-04 19:55:18 +08001679
Andy Green6ee372f2012-04-09 15:09:01 +08001680 /* default to a poll() made out of select() */
1681 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001682
Andy Green6ee372f2012-04-09 15:09:01 +08001683 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001684 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001685 if (wsdll)
1686 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09001687
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001688 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09001689 if (!poll)
1690 poll = emulated_poll;
Peter Hinz56885f32011-03-02 22:03:47 +00001691 }
1692#endif
1693
Andy Greenb5b23192013-02-11 17:13:32 +08001694 context = (struct libwebsocket_context *)
1695 malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001696 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001697 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001698 return NULL;
1699 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001700 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08001701#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001702 context->started_with_parent = pid_daemon;
1703 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1704#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001705
Joakim Soderberg4c531232013-02-06 15:26:58 +09001706 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08001707 context->protocols = info->protocols;
1708 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00001709 context->http_proxy_port = 0;
1710 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08001711 context->options = info->options;
Andy Greendfb23042013-01-17 12:26:48 +08001712 /* to reduce this allocation, */
1713 context->max_fds = getdtablesize();
Andy Greena86f6342013-02-11 11:04:56 +08001714 lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n",
1715 sizeof(struct libwebsocket_context),
1716 sizeof(struct pollfd) + sizeof(struct libwebsocket *),
1717 context->max_fds,
Andy Greenb5b23192013-02-11 17:13:32 +08001718 sizeof(struct libwebsocket_context) +
1719 ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) *
1720 context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08001721
Andy Greenb5b23192013-02-11 17:13:32 +08001722 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) *
1723 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001724 if (context->fds == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001725 lwsl_err("Unable to allocate fds array for %d connections\n",
1726 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001727 free(context);
1728 return NULL;
1729 }
Andy Greenb5b23192013-02-11 17:13:32 +08001730 context->lws_lookup = (struct libwebsocket **)
1731 malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001732 if (context->lws_lookup == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001733 lwsl_err(
1734 "Unable to allocate lws_lookup array for %d connections\n",
1735 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001736 free(context->fds);
1737 free(context);
1738 return NULL;
1739 }
Andy Greena17c6922013-01-20 20:21:54 +08001740
Peter Hinz56885f32011-03-02 22:03:47 +00001741 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001742#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08001743 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08001744#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08001745 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08001746 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00001747
Peter Hinz56885f32011-03-02 22:03:47 +00001748#ifdef WIN32
1749 context->fd_random = 0;
1750#else
1751 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
1752 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001753 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001754 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001755 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00001756 }
Peter Hinz56885f32011-03-02 22:03:47 +00001757#endif
Andy Green44eee682011-02-10 09:32:24 +00001758
Peter Hinz56885f32011-03-02 22:03:47 +00001759#ifdef LWS_OPENSSL_SUPPORT
1760 context->use_ssl = 0;
1761 context->ssl_ctx = NULL;
1762 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00001763 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001764#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00001765
Andy Greena1ce6be2013-01-18 11:43:21 +08001766 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08001767
Andy Greena1ce6be2013-01-18 11:43:21 +08001768#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001769 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Greena1ce6be2013-01-18 11:43:21 +08001770 struct sockaddr sa;
Andy Greene310b0c2013-02-10 15:10:10 +08001771 context->service_buffer[0] = '\0';
Andy Green788c4a82012-10-22 12:29:57 +01001772
1773 /* find canonical hostname */
1774
Andy Greenb5b23192013-02-11 17:13:32 +08001775 context->service_buffer[
1776 sizeof(context->service_buffer) - 1] = '\0';
Andy Green788c4a82012-10-22 12:29:57 +01001777 memset(&sa, 0, sizeof(sa));
1778 sa.sa_family = AF_INET;
Andy Greenb5b23192013-02-11 17:13:32 +08001779 sa.sa_data[sizeof(sa.sa_data) - 1] = '\0';
1780 gethostname((char *)context->service_buffer,
1781 sizeof(context->service_buffer) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01001782
1783 n = 0;
1784
Andy Greenb5b23192013-02-11 17:13:32 +08001785 if (strlen((char *)context->service_buffer) <
1786 sizeof(sa.sa_data) - 1) {
Andy Greene310b0c2013-02-10 15:10:10 +08001787 strcpy(sa.sa_data, (char *)context->service_buffer);
Andy Greend09d7d42013-01-31 10:05:43 +08001788 lwsl_debug("my host name is %s\n", sa.sa_data);
Andy Greenb5b23192013-02-11 17:13:32 +08001789 n = getnameinfo(&sa, sizeof(sa),
1790 (char *)context->service_buffer,
1791 sizeof(context->service_buffer) - 1,
1792 NULL, 0, NI_NAMEREQD);
Andy Green788c4a82012-10-22 12:29:57 +01001793 }
1794
1795 if (!n) {
Andy Greenb5b23192013-02-11 17:13:32 +08001796 strncpy(context->canonical_hostname,
1797 (char *)context->service_buffer,
1798 sizeof(context->canonical_hostname) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01001799 context->canonical_hostname[
Andy Greenb5b23192013-02-11 17:13:32 +08001800 sizeof(context->canonical_hostname) - 1] = '\0';
Andy Green788c4a82012-10-22 12:29:57 +01001801 } else
Andy Greenb5b23192013-02-11 17:13:32 +08001802 strncpy(context->canonical_hostname,
1803 (char *)context->service_buffer,
1804 sizeof(context->canonical_hostname) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01001805
Andy Greenb5b23192013-02-11 17:13:32 +08001806 lwsl_notice(" canonical_hostname = %s\n",
1807 context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08001808 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001809#endif
Andy Greena69f0512012-05-03 12:32:38 +08001810
Andy Green9659f372011-01-27 22:01:43 +00001811 /* split the proxy ads:port if given */
1812
1813 p = getenv("http_proxy");
1814 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00001815 strncpy(context->http_proxy_address, p,
Andy Greenb5b23192013-02-11 17:13:32 +08001816 sizeof(context->http_proxy_address) - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00001817 context->http_proxy_address[
Andy Greenb5b23192013-02-11 17:13:32 +08001818 sizeof(context->http_proxy_address) - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00001819
Peter Hinz56885f32011-03-02 22:03:47 +00001820 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00001821 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001822 lwsl_err("http_proxy needs to be ads:port\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001823 goto bail;
Andy Green9659f372011-01-27 22:01:43 +00001824 }
1825 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00001826 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00001827
Andy Greenb3a614a2013-01-19 13:08:17 +08001828 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001829 context->http_proxy_address,
1830 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00001831 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001832
Andy Greena1ce6be2013-01-18 11:43:21 +08001833#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001834 if (info->port) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001835
Andy Green3faa9c72010-11-08 17:03:03 +00001836#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08001837 context->use_ssl = info->ssl_cert_filepath != NULL &&
1838 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09001839#ifdef USE_CYASSL
1840 lwsl_notice(" Compiled with CYASSL support\n");
1841#else
1842 lwsl_notice(" Compiled with OpenSSL support\n");
1843#endif
Peter Hinz56885f32011-03-02 22:03:47 +00001844 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09001845 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001846 else
Andy Green23c5f2e2013-02-06 15:43:00 +09001847 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00001848
Andy Green90c7cbc2011-01-27 06:26:52 +00001849#else
Andy Green2b40b792013-02-10 22:22:01 +08001850 if (info->ssl_cert_filepath != NULL &&
1851 info->ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08001852 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001853 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001854 }
Andy Greenb5b23192013-02-11 17:13:32 +08001855 lwsl_notice(" Compiled without SSL support\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001856#endif
Andy Greena17c6922013-01-20 20:21:54 +08001857
Andy Greenb5b23192013-02-11 17:13:32 +08001858 lwsl_notice(
1859 " per-conn mem: %u + %u headers + protocol rx buf\n",
1860 sizeof(struct libwebsocket),
1861 sizeof(struct allocated_headers));
Andy Green90c7cbc2011-01-27 06:26:52 +00001862 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001863#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001864
1865 /* ignore SIGPIPE */
Peter Hinz56885f32011-03-02 22:03:47 +00001866#ifdef WIN32
1867#else
Andy Green90c7cbc2011-01-27 06:26:52 +00001868 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00001869#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001870
1871
1872#ifdef LWS_OPENSSL_SUPPORT
1873
1874 /* basic openssl init */
1875
1876 SSL_library_init();
1877
1878 OpenSSL_add_all_algorithms();
1879 SSL_load_error_strings();
1880
Andy Green2e24da02011-03-05 16:12:04 +00001881 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00001882 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
1883
Andy Green90c7cbc2011-01-27 06:26:52 +00001884 /*
1885 * Firefox insists on SSLv23 not SSLv3
1886 * Konq disables SSLv2 by default now, SSLv23 works
1887 */
1888
1889 method = (SSL_METHOD *)SSLv23_server_method();
1890 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001891 lwsl_err("problem creating ssl method: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001892 ERR_error_string(ERR_get_error(),
1893 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001894 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001895 }
Peter Hinz56885f32011-03-02 22:03:47 +00001896 context->ssl_ctx = SSL_CTX_new(method); /* create context */
1897 if (!context->ssl_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001898 lwsl_err("problem creating ssl context: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001899 ERR_error_string(ERR_get_error(),
1900 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001901 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001902 }
1903
David Galeanocc148e42013-01-10 10:18:59 +08001904#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001905 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001906#endif
David Galeano77a677c2013-01-10 10:14:12 +08001907 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
David Galeanof177f2a2013-01-10 10:15:19 +08001908 SSL_CTX_set_cipher_list(context->ssl_ctx, CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001909
Andy Greena1ce6be2013-01-18 11:43:21 +08001910#ifndef LWS_NO_CLIENT
1911
Andy Green90c7cbc2011-01-27 06:26:52 +00001912 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08001913
Andy Green1b265272013-02-09 14:01:09 +08001914 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00001915 method = (SSL_METHOD *)SSLv23_client_method();
1916 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001917 lwsl_err("problem creating ssl method: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001918 ERR_error_string(ERR_get_error(),
1919 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001920 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001921 }
1922 /* create context */
1923 context->ssl_client_ctx = SSL_CTX_new(method);
1924 if (!context->ssl_client_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001925 lwsl_err("problem creating ssl context: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001926 ERR_error_string(ERR_get_error(),
1927 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001928 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001929 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001930
David Galeanocc148e42013-01-10 10:18:59 +08001931#ifdef SSL_OP_NO_COMPRESSION
Andy Greenb5b23192013-02-11 17:13:32 +08001932 SSL_CTX_set_options(context->ssl_client_ctx,
1933 SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001934#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001935 SSL_CTX_set_options(context->ssl_client_ctx,
1936 SSL_OP_CIPHER_SERVER_PREFERENCE);
1937 SSL_CTX_set_cipher_list(context->ssl_client_ctx,
1938 CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001939
Peter Hinz56885f32011-03-02 22:03:47 +00001940 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08001941 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08001942 if (!SSL_CTX_load_verify_locations(
1943 context->ssl_client_ctx, NULL,
1944 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08001945 lwsl_err(
Andy Greenb5b23192013-02-11 17:13:32 +08001946 "Unable to load SSL Client certs from %s "
1947 "(set by --with-client-cert-dir= "
1948 "in configure) -- client ssl isn't "
1949 "going to work", LWS_OPENSSL_CLIENT_CERTS);
David Galeano2f82be82013-01-09 16:25:54 +08001950 } else
1951 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08001952 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08001953 NULL))
Andy Green43db0452013-01-10 19:50:35 +08001954 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08001955 "Unable to load SSL Client certs "
1956 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08001957 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00001958
1959 /*
1960 * callback allowing user code to load extra verification certs
1961 * helping the client to verify server identity
1962 */
1963
1964 context->protocols[0].callback(context, NULL,
1965 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
1966 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00001967 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001968#endif
Andy Green6ee372f2012-04-09 15:09:01 +08001969
Andy Greenc6bf2c22011-02-20 11:10:47 +00001970 /* as a server, are we requiring clients to identify themselves? */
1971
Andy Greenb5b23192013-02-11 17:13:32 +08001972 if (info->options &
1973 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00001974
1975 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08001976
Peter Hinz56885f32011-03-02 22:03:47 +00001977 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00001978 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1979 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001980
1981 /*
1982 * give user code a chance to load certs into the server
1983 * allowing it to verify incoming client certs
1984 */
1985
Peter Hinz56885f32011-03-02 22:03:47 +00001986 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00001987 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00001988 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001989 }
1990
Peter Hinz56885f32011-03-02 22:03:47 +00001991 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001992
1993 /* openssl init for server sockets */
1994
Andy Green3faa9c72010-11-08 17:03:03 +00001995 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08001996 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08001997 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00001998 if (n != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001999 lwsl_err("problem getting cert '%s': %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002000 info->ssl_cert_filepath,
Andy Greenb5b23192013-02-11 17:13:32 +08002001 ERR_error_string(ERR_get_error(),
2002 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002003 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002004 }
2005 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00002006 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08002007 info->ssl_private_key_filepath,
2008 SSL_FILETYPE_PEM) != 1) {
Andy Green43db0452013-01-10 19:50:35 +08002009 lwsl_err("ssl problem getting key '%s': %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08002010 info->ssl_private_key_filepath,
2011 ERR_error_string(ERR_get_error(),
2012 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002013 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002014 }
2015 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00002016 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08002017 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002018 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00002019 }
2020
2021 /* SSL is happy and has a cert it's content with */
2022 }
2023#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002024
Andy Greendf736162011-01-18 15:39:02 +00002025 /* selftest */
2026
2027 if (lws_b64_selftest())
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002028 goto bail;
Andy Greendf736162011-01-18 15:39:02 +00002029
Andy Greena1ce6be2013-01-18 11:43:21 +08002030#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00002031 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00002032
Andy Green1b265272013-02-09 14:01:09 +08002033 if (info->port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08002034 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00002035
Andy Green4739e5c2011-01-22 12:51:57 +00002036 sockfd = socket(AF_INET, SOCK_STREAM, 0);
2037 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08002038 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002039 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002040 }
Andy Green775c0dd2010-10-29 14:15:22 +01002041
Joakim Soderberg4c531232013-02-06 15:26:58 +09002042#ifndef WIN32
Andy Greenb5b23192013-02-11 17:13:32 +08002043 /*
2044 * allow us to restart even if old sockets in TIME_WAIT
2045 * (REUSEADDR on Unix means, "don't hang on to this
2046 * address after the listener is closed." On Windows, though,
2047 * it means "don't keep other processes from binding to
2048 * this address while we're using it)
2049 */
Andy Green6ee372f2012-04-09 15:09:01 +08002050 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
2051 (const void *)&opt, sizeof(opt));
Joakim Soderberg4c531232013-02-06 15:26:58 +09002052#endif
Andy Green6c939552011-03-08 08:56:57 +00002053
2054 /* Disable Nagle */
2055 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08002056 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
2057 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002058
Joakim Soderberg4c531232013-02-06 15:26:58 +09002059 #ifdef WIN32
2060 opt = 0;
Andy Greenb5b23192013-02-11 17:13:32 +08002061 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002062 #else
Andy Greene2160712013-01-28 12:19:10 +08002063 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002064 #endif
Andy Greene2160712013-01-28 12:19:10 +08002065
Andy Green4739e5c2011-01-22 12:51:57 +00002066 bzero((char *) &serv_addr, sizeof(serv_addr));
2067 serv_addr.sin_family = AF_INET;
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002068 if (info->iface == NULL)
Andy Green32375b72011-02-19 08:32:53 +00002069 serv_addr.sin_addr.s_addr = INADDR_ANY;
2070 else
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002071 interface_to_sa(info->iface, &serv_addr,
Andy Green32375b72011-02-19 08:32:53 +00002072 sizeof(serv_addr));
Andy Green1b265272013-02-09 14:01:09 +08002073 serv_addr.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00002074
2075 n = bind(sockfd, (struct sockaddr *) &serv_addr,
2076 sizeof(serv_addr));
2077 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002078 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green1b265272013-02-09 14:01:09 +08002079 info->port, n, errno);
Andy Green41c58032013-01-12 13:21:08 +08002080 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002081 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002082 }
Andy Green0d338332011-02-12 11:57:43 +00002083
Andy Greenb5b23192013-02-11 17:13:32 +08002084 wsi = (struct libwebsocket *)malloc(
2085 sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08002086 if (wsi == NULL) {
2087 lwsl_err("Out of mem\n");
2088 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002089 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08002090 }
Andy Greenb5b23192013-02-11 17:13:32 +08002091 memset(wsi, 0, sizeof(struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00002092 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08002093#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00002094 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002095#endif
Andy Green0d338332011-02-12 11:57:43 +00002096 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08002097
2098 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002099
Andy Green65b0e912013-01-16 07:59:47 +08002100 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
2101 context->listen_service_count = 0;
2102 context->listen_service_fd = sockfd;
2103
Andy Greena824d182013-01-15 20:52:29 +08002104 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08002105 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00002106 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002107#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002108
Andy Green6ee372f2012-04-09 15:09:01 +08002109 /*
2110 * drop any root privs for this process
2111 * to listen on port < 1023 we would have needed root, but now we are
2112 * listening, we don't want the power for anything else
2113 */
Peter Hinz56885f32011-03-02 22:03:47 +00002114#ifdef WIN32
2115#else
Andy Green1b265272013-02-09 14:01:09 +08002116 if (info->gid != -1)
2117 if (setgid(info->gid))
Andy Green43db0452013-01-10 19:50:35 +08002118 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green1b265272013-02-09 14:01:09 +08002119 if (info->uid != -1)
2120 if (setuid(info->uid))
Andy Green43db0452013-01-10 19:50:35 +08002121 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00002122#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002123
Andy Green6f520a52013-01-29 17:57:39 +08002124 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00002125
Peter Hinz56885f32011-03-02 22:03:47 +00002126 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08002127 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00002128 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002129
Andy Green43db0452013-01-10 19:50:35 +08002130 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002131 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002132
Andy Green1b265272013-02-09 14:01:09 +08002133 info->protocols[context->count_protocols].owning_server =
2134 context;
2135 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00002136 context->count_protocols;
Andy Greena7109e62013-02-11 12:05:54 +08002137
2138 /*
2139 * inform all the protocols that they are doing their one-time
2140 * initialization if they want to
2141 */
2142 info->protocols[context->count_protocols].callback(context,
2143 NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00002144 }
Andy Greenf5bc1302013-01-21 09:09:52 +08002145
Andy Green3182ece2013-01-20 17:08:31 +08002146#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01002147 /*
2148 * give all extensions a chance to create any per-context
2149 * allocations they need
2150 */
2151
2152 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andy Green1b265272013-02-09 14:01:09 +08002153 if (info->port)
Andy Greena41314f2011-05-23 10:00:03 +01002154 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andy Greenb5b23192013-02-11 17:13:32 +08002155
Andy Green1b265272013-02-09 14:01:09 +08002156 if (info->extensions) {
2157 ext = info->extensions;
2158 while (ext->callback) {
2159 lwsl_ext(" Extension: %s\n", ext->name);
2160 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002161 (enum libwebsocket_extension_callback_reasons)m,
2162 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08002163 ext++;
2164 }
Andy Greena41314f2011-05-23 10:00:03 +01002165 }
Andy Green3182ece2013-01-20 17:08:31 +08002166#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002167 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002168
2169bail:
2170 libwebsocket_context_destroy(context);
2171 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00002172}
Andy Greenb45993c2010-12-18 15:13:50 +00002173
Andy Greenb45993c2010-12-18 15:13:50 +00002174/**
2175 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002176 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002177 * @wsi: pointer to struct websocket you want to know the protocol of
2178 *
Andy Green8f037e42010-12-19 22:13:26 +00002179 *
Andy Green6f520a52013-01-29 17:57:39 +08002180 * Some apis can act on all live connections of a given protocol,
2181 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002182 */
Andy Greenab990e42010-10-31 12:42:52 +00002183
Andy Greenb45993c2010-12-18 15:13:50 +00002184const struct libwebsocket_protocols *
2185libwebsockets_get_protocol(struct libwebsocket *wsi)
2186{
2187 return wsi->protocol;
2188}
2189
Andy Green82c3d542011-03-07 21:16:31 +00002190int
2191libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2192{
Andy Green623a98d2013-01-21 11:04:23 +08002193 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002194}
Alex Bligh49146db2011-11-07 17:19:25 +08002195
David Galeanoe2cf9922013-01-09 18:06:55 +08002196unsigned char
2197libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2198{
Andy Green623a98d2013-01-21 11:04:23 +08002199 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002200}
2201
Alex Bligh49146db2011-11-07 17:19:25 +08002202void *
2203libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2204{
2205 /* allocate the per-connection user memory (if any) */
2206
2207 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2208 wsi->user_space = malloc(
2209 wsi->protocol->per_session_data_size);
2210 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002211 lwsl_err("Out of memory for conn user space\n");
Alex Bligh49146db2011-11-07 17:19:25 +08002212 return NULL;
2213 }
Andy Green6ee372f2012-04-09 15:09:01 +08002214 memset(wsi->user_space, 0,
2215 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002216 }
2217 return wsi->user_space;
2218}
Andy Green43db0452013-01-10 19:50:35 +08002219
Andy Green0b319092013-01-19 11:17:56 +08002220static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002221{
Andy Green0b319092013-01-19 11:17:56 +08002222 char buf[300];
2223 struct timeval tv;
Andy Green0b319092013-01-19 11:17:56 +08002224 int n;
2225
2226 gettimeofday(&tv, NULL);
2227
2228 buf[0] = '\0';
2229 for (n = 0; n < LLL_COUNT; n++)
2230 if (level == (1 << n)) {
Andy Green058ba812013-01-19 11:32:18 +08002231 sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
Andy Greenb5b23192013-02-11 17:13:32 +08002232 (int)(tv.tv_usec / 100), log_level_names[n]);
Andy Green0b319092013-01-19 11:17:56 +08002233 break;
2234 }
Andy Greenb5b23192013-02-11 17:13:32 +08002235
Andy Green0b319092013-01-19 11:17:56 +08002236 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002237}
2238
Joakim Soderberg4c531232013-02-06 15:26:58 +09002239#ifdef WIN32
2240void lwsl_emit_syslog(int level, const char *line)
2241{
2242 lwsl_emit_stderr(level, line);
2243}
2244#else
Andy Greenc11db202013-01-19 11:12:16 +08002245void lwsl_emit_syslog(int level, const char *line)
2246{
2247 int syslog_level = LOG_DEBUG;
2248
2249 switch (level) {
2250 case LLL_ERR:
2251 syslog_level = LOG_ERR;
2252 break;
2253 case LLL_WARN:
2254 syslog_level = LOG_WARNING;
2255 break;
2256 case LLL_NOTICE:
2257 syslog_level = LOG_NOTICE;
2258 break;
2259 case LLL_INFO:
2260 syslog_level = LOG_INFO;
2261 break;
2262 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002263 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002264}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002265#endif
Andy Greenc11db202013-01-19 11:12:16 +08002266
Andy Green43db0452013-01-10 19:50:35 +08002267void _lws_log(int filter, const char *format, ...)
2268{
Andy Greende8f27a2013-01-12 09:17:42 +08002269 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002270 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002271
2272 if (!(log_level & filter))
2273 return;
2274
Andy Green43db0452013-01-10 19:50:35 +08002275 va_start(ap, format);
Andy Greenb5b23192013-02-11 17:13:32 +08002276 vsnprintf(buf, sizeof(buf), format, ap);
2277 buf[sizeof(buf) - 1] = '\0';
Andy Greende8f27a2013-01-12 09:17:42 +08002278 va_end(ap);
2279
Andy Green0b319092013-01-19 11:17:56 +08002280 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002281}
2282
2283/**
2284 * lws_set_log_level() - Set the logging bitfield
2285 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002286 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2287 * function to perform log string emission instead of
2288 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002289 *
Andy Greende8f27a2013-01-12 09:17:42 +08002290 * log level defaults to "err" and "warn" contexts enabled only and
2291 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002292 */
2293
Andy Greenb5b23192013-02-11 17:13:32 +08002294void lws_set_log_level(int level, void (*log_emit_function)(int level,
2295 const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002296{
2297 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002298 if (log_emit_function)
2299 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002300}