blob: 80a4ce0f6b8583f03fec02fed9a5fcad7aed58f5 [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,
Andy Green50097dd2013-02-15 22:36:30 +0800130 wsi->user_space, (void *)(long)wsi->sock, 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,
Andy Green50097dd2013-02-15 22:36:30 +0800173 LWS_CALLBACK_DEL_POLL_FD, wsi->user_space,
174 (void *)(long)wsi->sock, 0);
Andy Greendfb23042013-01-17 12:26:48 +0800175
176 return 0;
Andy Green0d338332011-02-12 11:57:43 +0000177}
178
Andy Green32375b72011-02-19 08:32:53 +0000179
Andy Green8f037e42010-12-19 22:13:26 +0000180void
Peter Hinz56885f32011-03-02 22:03:47 +0000181libwebsocket_close_and_free_session(struct libwebsocket_context *context,
Andy Green687b0182011-02-26 11:04:01 +0000182 struct libwebsocket *wsi, enum lws_close_status reason)
Andy Green251f6fa2010-11-03 11:13:06 +0000183{
Andy Greenb45993c2010-12-18 15:13:50 +0000184 int n;
Andy Green62c54d22011-02-14 09:14:25 +0000185 int old_state;
Andy Green5e1fa172011-02-10 09:07:05 +0000186 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 2 +
187 LWS_SEND_BUFFER_POST_PADDING];
Andy Green3182ece2013-01-20 17:08:31 +0800188#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000189 int ret;
190 int m;
191 struct lws_tokens eff_buf;
Andy Greena41314f2011-05-23 10:00:03 +0100192 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +0800193#endif
Andy Greenb45993c2010-12-18 15:13:50 +0000194
Andy Green4b6fbe12011-02-14 08:03:48 +0000195 if (!wsi)
Andy Greenb45993c2010-12-18 15:13:50 +0000196 return;
197
Andy Green62c54d22011-02-14 09:14:25 +0000198 old_state = wsi->state;
Andy Green251f6fa2010-11-03 11:13:06 +0000199
Andy Green62c54d22011-02-14 09:14:25 +0000200 if (old_state == WSI_STATE_DEAD_SOCKET)
Andy Green5e1fa172011-02-10 09:07:05 +0000201 return;
202
Andy Green22524a62013-02-15 22:48:58 +0800203 /* we tried the polite way... */
204 if (old_state == WSI_STATE_AWAITING_CLOSE_ACK)
205 goto just_kill_connection;
206
Andy Green623a98d2013-01-21 11:04:23 +0800207 wsi->u.ws.close_reason = reason;
Andy Greenda527df2011-03-07 07:08:12 +0000208
Andy Greenb8b247d2013-01-22 07:20:08 +0800209 if (wsi->mode == LWS_CONNMODE_HTTP_SERVING && wsi->u.http.fd) {
210 close(wsi->u.http.fd);
211 wsi->u.http.fd = 0;
212 }
213
Andy Green3182ece2013-01-20 17:08:31 +0800214#ifndef LWS_NO_EXTENSIONS
Andy Greenda527df2011-03-07 07:08:12 +0000215 /*
Andy Green68b45042011-05-25 21:41:57 +0100216 * are his extensions okay with him closing? Eg he might be a mux
217 * parent and just his ch1 aspect is closing?
218 */
219
Andy Green68b45042011-05-25 21:41:57 +0100220 for (n = 0; n < wsi->count_active_extensions; n++) {
221 if (!wsi->active_extensions[n]->callback)
222 continue;
223
224 m = wsi->active_extensions[n]->callback(context,
225 wsi->active_extensions[n], wsi,
226 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
227 wsi->active_extensions_user[n], NULL, 0);
228
229 /*
230 * if somebody vetoed actually closing him at this time....
231 * up to the extension to track the attempted close, let's
232 * just bail
233 */
234
235 if (m) {
Andy Green43db0452013-01-10 19:50:35 +0800236 lwsl_ext("extension vetoed close\n");
Andy Green68b45042011-05-25 21:41:57 +0100237 return;
238 }
239 }
240
Andy Green68b45042011-05-25 21:41:57 +0100241 /*
Andy Greenc44159f2011-03-07 07:08:18 +0000242 * flush any tx pending from extensions, since we may send close packet
243 * if there are problems with send, just nuke the connection
244 */
245
246 ret = 1;
247 while (ret == 1) {
248
249 /* default to nobody has more to spill */
250
251 ret = 0;
252 eff_buf.token = NULL;
253 eff_buf.token_len = 0;
254
255 /* show every extension the new incoming data */
256
257 for (n = 0; n < wsi->count_active_extensions; n++) {
258 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000259 wsi->protocol->owning_server,
260 wsi->active_extensions[n], wsi,
Andy Greenc44159f2011-03-07 07:08:18 +0000261 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
262 wsi->active_extensions_user[n], &eff_buf, 0);
263 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800264 lwsl_ext("Extension reports fatal error\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000265 goto just_kill_connection;
266 }
267 if (m)
268 /*
269 * at least one extension told us he has more
270 * to spill, so we will go around again after
271 */
272 ret = 1;
273 }
274
275 /* assuming they left us something to send, send it */
276
277 if (eff_buf.token_len)
278 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
Andy Greenb5b23192013-02-11 17:13:32 +0800279 eff_buf.token_len)) {
280 lwsl_debug("close: ext spill failed\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000281 goto just_kill_connection;
Andy Green0303db42013-01-17 14:46:43 +0800282 }
Andy Greenc44159f2011-03-07 07:08:18 +0000283 }
Andy Green3182ece2013-01-20 17:08:31 +0800284#endif
Andy Greenc44159f2011-03-07 07:08:18 +0000285
286 /*
Andy Greenda527df2011-03-07 07:08:12 +0000287 * signal we are closing, libsocket_write will
288 * add any necessary version-specific stuff. If the write fails,
289 * no worries we are closing anyway. If we didn't initiate this
290 * close, then our state has been changed to
291 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this.
292 *
293 * Likewise if it's a second call to close this connection after we
294 * sent the close indication to the peer already, we are in state
295 * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time.
296 */
297
298 if (old_state == WSI_STATE_ESTABLISHED &&
299 reason != LWS_CLOSE_STATUS_NOSTATUS) {
Andy Green66a16f32011-05-24 22:07:45 +0100300
Andy Green43db0452013-01-10 19:50:35 +0800301 lwsl_debug("sending close indication...\n");
Andy Green66a16f32011-05-24 22:07:45 +0100302
Andy Greenfdd305a2013-02-11 14:32:48 +0800303 /* make valgrind happy */
Andy Greenb5b23192013-02-11 17:13:32 +0800304 memset(buf, 0, sizeof(buf));
305 n = libwebsocket_write(wsi,
306 &buf[LWS_SEND_BUFFER_PRE_PADDING + 2],
Andy Greenda527df2011-03-07 07:08:12 +0000307 0, LWS_WRITE_CLOSE);
308 if (!n) {
309 /*
310 * we have sent a nice protocol level indication we
311 * now wish to close, we should not send anything more
312 */
313
314 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
315
Andy Greenb5b23192013-02-11 17:13:32 +0800316 /*
317 * ...and we should wait for a reply for a bit
318 * out of politeness
319 */
Andy Greenda527df2011-03-07 07:08:12 +0000320
321 libwebsocket_set_timeout(wsi,
Andy Green0303db42013-01-17 14:46:43 +0800322 PENDING_TIMEOUT_CLOSE_ACK, 1);
Andy Greenda527df2011-03-07 07:08:12 +0000323
Andy Green43db0452013-01-10 19:50:35 +0800324 lwsl_debug("sent close indication, awaiting ack\n");
Andy Greenda527df2011-03-07 07:08:12 +0000325
326 return;
327 }
328
Andy Greenb5b23192013-02-11 17:13:32 +0800329 lwsl_info("close: sending close packet failed, hanging up\n");
Andy Green0303db42013-01-17 14:46:43 +0800330
Andy Greenda527df2011-03-07 07:08:12 +0000331 /* else, the send failed and we should just hang up */
332 }
333
Andy Greenc44159f2011-03-07 07:08:18 +0000334just_kill_connection:
Andy Green66a16f32011-05-24 22:07:45 +0100335
Andy Greenb5b23192013-02-11 17:13:32 +0800336 lwsl_debug("close: just_kill_connection\n");
Andy Green66a16f32011-05-24 22:07:45 +0100337
Andy Greenda527df2011-03-07 07:08:12 +0000338 /*
339 * we won't be servicing or receiving anything further from this guy
Andy Greendfb23042013-01-17 12:26:48 +0800340 * delete socket from the internal poll list if still present
Andy Greenda527df2011-03-07 07:08:12 +0000341 */
Andy Green4b6fbe12011-02-14 08:03:48 +0000342
Andy Greendfb23042013-01-17 12:26:48 +0800343 remove_wsi_socket_from_fds(context, wsi);
Andy Green4b6fbe12011-02-14 08:03:48 +0000344
Andy Green251f6fa2010-11-03 11:13:06 +0000345 wsi->state = WSI_STATE_DEAD_SOCKET;
346
Andy Greenb5b23192013-02-11 17:13:32 +0800347 if ((old_state == WSI_STATE_ESTABLISHED ||
348 wsi->mode == LWS_CONNMODE_WS_SERVING ||
Andy Green3ee9b312013-02-12 12:52:39 +0800349 wsi->mode == LWS_CONNMODE_WS_CLIENT)) {
350
351 if (wsi->u.ws.rx_user_buffer) {
352 free(wsi->u.ws.rx_user_buffer);
353 wsi->u.ws.rx_user_buffer = NULL;
354 }
355 if (wsi->u.ws.rxflow_buffer) {
356 free(wsi->u.ws.rxflow_buffer);
357 wsi->u.ws.rxflow_buffer = NULL;
358 }
Andy Green54495112013-02-06 21:10:16 +0900359 }
360
Andy Green4b6fbe12011-02-14 08:03:48 +0000361 /* tell the user it's all over for this guy */
362
Andy Greend4302732011-02-28 07:45:29 +0000363 if (wsi->protocol && wsi->protocol->callback &&
Andy Green6ee372f2012-04-09 15:09:01 +0800364 ((old_state == WSI_STATE_ESTABLISHED) ||
365 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
366 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
Andy Green43db0452013-01-10 19:50:35 +0800367 lwsl_debug("calling back CLOSED\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000368 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000369 wsi->user_space, NULL, 0);
Andy Greencc012472011-11-07 19:53:23 +0800370 } else
Andy Greenb5b23192013-02-11 17:13:32 +0800371 lwsl_debug("not calling back closed\n");
Andy Green251f6fa2010-11-03 11:13:06 +0000372
Andy Green3182ece2013-01-20 17:08:31 +0800373#ifndef LWS_NO_EXTENSIONS
Andy Greenef660a92011-03-06 10:29:38 +0000374 /* deallocate any active extension contexts */
375
376 for (n = 0; n < wsi->count_active_extensions; n++) {
377 if (!wsi->active_extensions[n]->callback)
378 continue;
379
Andy Green46c2ea02011-03-22 09:04:01 +0000380 wsi->active_extensions[n]->callback(context,
381 wsi->active_extensions[n], wsi,
382 LWS_EXT_CALLBACK_DESTROY,
383 wsi->active_extensions_user[n], NULL, 0);
Andy Greenef660a92011-03-06 10:29:38 +0000384
385 free(wsi->active_extensions_user[n]);
386 }
387
Andy Greena41314f2011-05-23 10:00:03 +0100388 /*
389 * inform all extensions in case they tracked this guy out of band
390 * even though not active on him specifically
391 */
392
393 ext = context->extensions;
394 while (ext && ext->callback) {
395 ext->callback(context, ext, wsi,
396 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
397 NULL, NULL, 0);
398 ext++;
399 }
Andy Green3182ece2013-01-20 17:08:31 +0800400#endif
Andy Greena41314f2011-05-23 10:00:03 +0100401
Andy Green43db0452013-01-10 19:50:35 +0800402/* lwsl_info("closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000403
Andy Green3faa9c72010-11-08 17:03:03 +0000404#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000405 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000406 n = SSL_get_fd(wsi->ssl);
407 SSL_shutdown(wsi->ssl);
Andy Green3fc2c652013-01-14 15:35:02 +0800408 compatible_close(n);
Andy Green3faa9c72010-11-08 17:03:03 +0000409 SSL_free(wsi->ssl);
410 } else {
411#endif
Andy Green0303db42013-01-17 14:46:43 +0800412 if (wsi->sock) {
413 n = shutdown(wsi->sock, SHUT_RDWR);
414 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800415 lwsl_debug("closing: shutdown returned %d\n",
416 errno);
Andy Green3fc2c652013-01-14 15:35:02 +0800417
Andy Green0303db42013-01-17 14:46:43 +0800418 n = compatible_close(wsi->sock);
419 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800420 lwsl_debug("closing: close returned %d\n",
421 errno);
Andy Green0303db42013-01-17 14:46:43 +0800422 }
Andy Green3faa9c72010-11-08 17:03:03 +0000423#ifdef LWS_OPENSSL_SUPPORT
424 }
425#endif
Andy Greenb5b23192013-02-11 17:13:32 +0800426 if (wsi->protocol && wsi->protocol->per_session_data_size &&
427 wsi->user_space) /* user code may own */
Andy Green4f3943a2010-11-12 10:44:16 +0000428 free(wsi->user_space);
429
Andy Green251f6fa2010-11-03 11:13:06 +0000430 free(wsi);
431}
432
Andy Green07034092011-02-13 08:37:12 +0000433/**
434 * libwebsockets_get_peer_addresses() - Get client address information
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800435 * @context: Libwebsockets context
436 * @wsi: Local struct libwebsocket associated with
Andy Green07034092011-02-13 08:37:12 +0000437 * @fd: Connection socket descriptor
438 * @name: Buffer to take client address name
439 * @name_len: Length of client address name buffer
440 * @rip: Buffer to take client address IP qotted quad
441 * @rip_len: Length of client address IP buffer
442 *
443 * This function fills in @name and @rip with the name and IP of
Andy Green6ee372f2012-04-09 15:09:01 +0800444 * the client connected with socket descriptor @fd. Names may be
445 * truncated if there is not enough room. If either cannot be
446 * determined, they will be returned as valid zero-length strings.
Andy Green07034092011-02-13 08:37:12 +0000447 */
448
449void
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800450libwebsockets_get_peer_addresses(struct libwebsocket_context *context,
451 struct libwebsocket *wsi, int fd, char *name, int name_len,
Andy Green07034092011-02-13 08:37:12 +0000452 char *rip, int rip_len)
453{
454 unsigned int len;
455 struct sockaddr_in sin;
456 struct hostent *host;
457 struct hostent *host1;
458 char ip[128];
Andy Greenf92def72011-03-09 15:02:20 +0000459 unsigned char *p;
Andy Green07034092011-02-13 08:37:12 +0000460 int n;
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800461 int ret = -1;
David Galeanocb193682013-01-09 15:29:00 +0800462#ifdef AF_LOCAL
Andy Greenb5b23192013-02-11 17:13:32 +0800463 struct sockaddr_un *un;
David Galeanocb193682013-01-09 15:29:00 +0800464#endif
Andy Green07034092011-02-13 08:37:12 +0000465
466 rip[0] = '\0';
467 name[0] = '\0';
468
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800469 lws_latency_pre(context, wsi);
470
Andy Greenb5b23192013-02-11 17:13:32 +0800471 len = sizeof(sin);
Andy Green07034092011-02-13 08:37:12 +0000472 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
473 perror("getpeername");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800474 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000475 }
Andy Green6ee372f2012-04-09 15:09:01 +0800476
Andy Greenb5b23192013-02-11 17:13:32 +0800477 host = gethostbyaddr((char *) &sin.sin_addr, sizeof(sin.sin_addr),
Andy Green07034092011-02-13 08:37:12 +0000478 AF_INET);
479 if (host == NULL) {
480 perror("gethostbyaddr");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800481 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000482 }
483
484 strncpy(name, host->h_name, name_len);
485 name[name_len - 1] = '\0';
486
487 host1 = gethostbyname(host->h_name);
488 if (host1 == NULL)
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800489 goto bail;
Andy Greenf92def72011-03-09 15:02:20 +0000490 p = (unsigned char *)host1;
Andy Green07034092011-02-13 08:37:12 +0000491 n = 0;
492 while (p != NULL) {
Andy Greenf92def72011-03-09 15:02:20 +0000493 p = (unsigned char *)host1->h_addr_list[n++];
Andy Green07034092011-02-13 08:37:12 +0000494 if (p == NULL)
495 continue;
Peter Hinzbb45a902011-03-10 18:14:01 +0000496 if ((host1->h_addrtype != AF_INET)
497#ifdef AF_LOCAL
498 && (host1->h_addrtype != AF_LOCAL)
499#endif
500 )
Andy Green07034092011-02-13 08:37:12 +0000501 continue;
502
Andy Green7627af52011-03-09 15:13:52 +0000503 if (host1->h_addrtype == AF_INET)
504 sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
Peter Hinzbb45a902011-03-10 18:14:01 +0000505#ifdef AF_LOCAL
Andy Green7627af52011-03-09 15:13:52 +0000506 else {
507 un = (struct sockaddr_un *)p;
Andy Green6ee372f2012-04-09 15:09:01 +0800508 strncpy(ip, un->sun_path, sizeof(ip) - 1);
Andy Green7627af52011-03-09 15:13:52 +0000509 ip[sizeof(ip) - 1] = '\0';
510 }
Peter Hinzbb45a902011-03-10 18:14:01 +0000511#endif
Andy Green07034092011-02-13 08:37:12 +0000512 p = NULL;
513 strncpy(rip, ip, rip_len);
514 rip[rip_len - 1] = '\0';
515 }
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800516
517 ret = 0;
518bail:
519 lws_latency(context, wsi, "libwebsockets_get_peer_addresses", ret, 1);
Andy Green07034092011-02-13 08:37:12 +0000520}
Andy Green9f990342011-02-12 11:57:45 +0000521
Peter Hinz56885f32011-03-02 22:03:47 +0000522int libwebsockets_get_random(struct libwebsocket_context *context,
523 void *buf, int len)
524{
525 int n;
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800526 char *p = (char *)buf;
Peter Hinz56885f32011-03-02 22:03:47 +0000527
528#ifdef WIN32
529 for (n = 0; n < len; n++)
530 p[n] = (unsigned char)rand();
531#else
532 n = read(context->fd_random, p, len);
533#endif
534
535 return n;
536}
537
Andy Greena690cd02013-02-09 12:25:31 +0800538int lws_set_socket_options(struct libwebsocket_context *context, int fd)
539{
540 int optval = 1;
541 socklen_t optlen = sizeof(optval);
542#ifdef WIN32
543 unsigned long optl = 0;
544#endif
545#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
546 struct protoent *tcp_proto;
547#endif
548
549 if (context->ka_time) {
550 /* enable keepalive on this socket */
551 optval = 1;
552 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
553 (const void *)&optval, optlen) < 0)
554 return 1;
555
Andy Greena47865f2013-02-10 09:39:47 +0800556#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
557
558 /*
559 * didn't find a way to set these per-socket, need to
560 * tune kernel systemwide values
561 */
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100562#elif WIN32
563 {
564 DWORD dwBytesRet;
565 struct tcp_keepalive alive;
566 alive.onoff = TRUE;
567 alive.keepalivetime = context->ka_time;
568 alive.keepaliveinterval = context->ka_interval;
Andy Greena47865f2013-02-10 09:39:47 +0800569
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100570 if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
571 NULL, 0, &dwBytesRet, NULL, NULL))
572 return 1;
573 }
Andy Greena47865f2013-02-10 09:39:47 +0800574#else
Andy Greena690cd02013-02-09 12:25:31 +0800575 /* set the keepalive conditions we want on it too */
576 optval = context->ka_time;
577 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPIDLE,
578 (const void *)&optval, optlen) < 0)
579 return 1;
580
Larry Hayesbb66ac62013-02-22 09:16:20 +0800581 optval = context->ka_interval;
Andy Greena690cd02013-02-09 12:25:31 +0800582 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPINTVL,
583 (const void *)&optval, optlen) < 0)
584 return 1;
585
Larry Hayesbb66ac62013-02-22 09:16:20 +0800586 optval = context->ka_probes;
Andy Greena690cd02013-02-09 12:25:31 +0800587 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPCNT,
588 (const void *)&optval, optlen) < 0)
589 return 1;
Andy Greena47865f2013-02-10 09:39:47 +0800590#endif
Andy Greena690cd02013-02-09 12:25:31 +0800591 }
592
593 /* Disable Nagle */
594 optval = 1;
595#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
596 setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen);
597#else
598 tcp_proto = getprotobyname("TCP");
599 setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen);
600#endif
601
602 /* We are nonblocking... */
603#ifdef WIN32
604 ioctlsocket(fd, FIONBIO, &optl);
605#else
606 fcntl(fd, F_SETFL, O_NONBLOCK);
607#endif
608
609 return 0;
610}
611
Andy Green95a7b5d2011-03-06 10:29:39 +0000612int lws_send_pipe_choked(struct libwebsocket *wsi)
613{
614 struct pollfd fds;
615
616 fds.fd = wsi->sock;
617 fds.events = POLLOUT;
618 fds.revents = 0;
619
620 if (poll(&fds, 1, 0) != 1)
621 return 1;
622
623 if ((fds.revents & POLLOUT) == 0)
624 return 1;
625
626 /* okay to send another packet without blocking */
627
628 return 0;
629}
630
Andy Greena41314f2011-05-23 10:00:03 +0100631int
Andy Green3b84c002011-03-06 13:14:42 +0000632lws_handle_POLLOUT_event(struct libwebsocket_context *context,
633 struct libwebsocket *wsi, struct pollfd *pollfd)
634{
Andy Green3b84c002011-03-06 13:14:42 +0000635 int n;
Andy Green6f520a52013-01-29 17:57:39 +0800636
Andy Green3182ece2013-01-20 17:08:31 +0800637#ifndef LWS_NO_EXTENSIONS
638 struct lws_tokens eff_buf;
Andy Green3b84c002011-03-06 13:14:42 +0000639 int ret;
640 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100641 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000642
Andy Greena41314f2011-05-23 10:00:03 +0100643 for (n = 0; n < wsi->count_active_extensions; n++) {
644 if (!wsi->active_extensions[n]->callback)
645 continue;
646
647 m = wsi->active_extensions[n]->callback(context,
648 wsi->active_extensions[n], wsi,
649 LWS_EXT_CALLBACK_IS_WRITEABLE,
650 wsi->active_extensions_user[n], NULL, 0);
651 if (m > handled)
652 handled = m;
653 }
654
655 if (handled == 1)
656 goto notify_action;
657
658 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000659 goto user_service;
660
661 /*
662 * check in on the active extensions, see if they
663 * had pending stuff to spill... they need to get the
664 * first look-in otherwise sequence will be disordered
665 *
666 * NULL, zero-length eff_buf means just spill pending
667 */
668
669 ret = 1;
670 while (ret == 1) {
671
672 /* default to nobody has more to spill */
673
674 ret = 0;
675 eff_buf.token = NULL;
676 eff_buf.token_len = 0;
677
678 /* give every extension a chance to spill */
679
680 for (n = 0; n < wsi->count_active_extensions; n++) {
681 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000682 wsi->protocol->owning_server,
683 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000684 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
685 wsi->active_extensions_user[n], &eff_buf, 0);
686 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800687 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000688 return -1;
689 }
690 if (m)
691 /*
692 * at least one extension told us he has more
693 * to spill, so we will go around again after
694 */
695 ret = 1;
696 }
697
698 /* assuming they gave us something to send, send it */
699
700 if (eff_buf.token_len) {
701 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
702 eff_buf.token_len))
703 return -1;
704 } else
705 continue;
706
707 /* no extension has more to spill */
708
709 if (!ret)
710 continue;
711
712 /*
713 * There's more to spill from an extension, but we just sent
714 * something... did that leave the pipe choked?
715 */
716
717 if (!lws_send_pipe_choked(wsi))
718 /* no we could add more */
719 continue;
720
Andy Green43db0452013-01-10 19:50:35 +0800721 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000722
723 /*
724 * Yes, he's choked. Leave the POLLOUT masked on so we will
725 * come back here when he is unchoked. Don't call the user
726 * callback to enforce ordering of spilling, he'll get called
727 * when we come back here and there's nothing more to spill.
728 */
729
730 return 0;
731 }
732
733 wsi->extension_data_pending = 0;
734
735user_service:
Andy Green3182ece2013-01-20 17:08:31 +0800736#endif
Andy Green3b84c002011-03-06 13:14:42 +0000737 /* one shot */
738
Andy Greena41314f2011-05-23 10:00:03 +0100739 if (pollfd) {
740 pollfd->events &= ~POLLOUT;
Andy Green3b84c002011-03-06 13:14:42 +0000741
Andy Greena41314f2011-05-23 10:00:03 +0100742 /* external POLL support via protocol 0 */
743 context->protocols[0].callback(context, wsi,
744 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +0800745 wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
Andy Greena41314f2011-05-23 10:00:03 +0100746 }
Andy Green3182ece2013-01-20 17:08:31 +0800747#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100748notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800749#endif
Andy Green3b84c002011-03-06 13:14:42 +0000750
Andy Green9e4c2b62011-03-07 20:47:39 +0000751 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
752 n = LWS_CALLBACK_CLIENT_WRITEABLE;
753 else
754 n = LWS_CALLBACK_SERVER_WRITEABLE;
755
Andy Greenb8b247d2013-01-22 07:20:08 +0800756 return user_callback_handle_rxflow(wsi->protocol->callback, context,
Andy Greenb5b23192013-02-11 17:13:32 +0800757 wsi, (enum libwebsocket_callback_reasons) n,
758 wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000759}
760
761
762
Andy Green1c6e1422013-02-20 19:11:31 +0800763int
Andy Greena41314f2011-05-23 10:00:03 +0100764libwebsocket_service_timeout_check(struct libwebsocket_context *context,
765 struct libwebsocket *wsi, unsigned int sec)
766{
Andy Green3182ece2013-01-20 17:08:31 +0800767#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100768 int n;
769
770 /*
771 * if extensions want in on it (eg, we are a mux parent)
772 * give them a chance to service child timeouts
773 */
774
775 for (n = 0; n < wsi->count_active_extensions; n++)
776 wsi->active_extensions[n]->callback(
777 context, wsi->active_extensions[n],
778 wsi, LWS_EXT_CALLBACK_1HZ,
779 wsi->active_extensions_user[n], NULL, sec);
780
Andy Green3182ece2013-01-20 17:08:31 +0800781#endif
Andy Greena41314f2011-05-23 10:00:03 +0100782 if (!wsi->pending_timeout)
Andy Green1c6e1422013-02-20 19:11:31 +0800783 return 0;
Andy Green6ee372f2012-04-09 15:09:01 +0800784
Andy Greena41314f2011-05-23 10:00:03 +0100785 /*
786 * if we went beyond the allowed time, kill the
787 * connection
788 */
789
790 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800791 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100792 libwebsocket_close_and_free_session(context,
793 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green1c6e1422013-02-20 19:11:31 +0800794 return 1;
Andy Greena41314f2011-05-23 10:00:03 +0100795 }
Andy Green1c6e1422013-02-20 19:11:31 +0800796
797 return 0;
Andy Greena41314f2011-05-23 10:00:03 +0100798}
799
Andy Green9f990342011-02-12 11:57:45 +0000800/**
801 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000802 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000803 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800804 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000805 *
Andy Green75006172013-01-22 12:32:11 +0800806 * This function takes a pollfd that has POLLIN or POLLOUT activity and
Andy Greenb5b23192013-02-11 17:13:32 +0800807 * services it according to the state of the associated
808 * struct libwebsocket.
Andy Green75006172013-01-22 12:32:11 +0800809 *
810 * The one call deals with all "service" that might happen on a socket
811 * including listen accepts, http files as well as websocket protocol.
Andy Green9f990342011-02-12 11:57:45 +0000812 */
813
814int
Peter Hinz56885f32011-03-02 22:03:47 +0000815libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000816 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000817{
Andy Greena1ce6be2013-01-18 11:43:21 +0800818 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000819 int n;
Andy Green0d338332011-02-12 11:57:43 +0000820 int m;
Andy Greenf0b79e22013-02-10 10:46:45 +0800821 int listen_socket_fds_index = 0;
Andy Greena71eafc2011-02-14 17:59:43 +0000822 struct timeval tv;
Andy Green1c6e1422013-02-20 19:11:31 +0800823 int timed_out = 0;
824 int our_fd = 0;
Andy Green6f520a52013-01-29 17:57:39 +0800825
Andy Green3182ece2013-01-20 17:08:31 +0800826#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000827 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800828#endif
Andy Green98a717c2011-03-06 13:14:15 +0000829 struct lws_tokens eff_buf;
Andy Greenf0b79e22013-02-10 10:46:45 +0800830
831 if (context->listen_service_fd)
832 listen_socket_fds_index = context->lws_lookup[
833 context->listen_service_fd]->position_in_fds_table;
834
Andy Greena71eafc2011-02-14 17:59:43 +0000835 /*
836 * you can call us with pollfd = NULL to just allow the once-per-second
837 * global timeout checks; if less than a second since the last check
838 * it returns immediately then.
839 */
840
841 gettimeofday(&tv, NULL);
842
Peter Hinz56885f32011-03-02 22:03:47 +0000843 if (context->last_timeout_check_s != tv.tv_sec) {
844 context->last_timeout_check_s = tv.tv_sec;
Andy Greena71eafc2011-02-14 17:59:43 +0000845
Joakim Soderberg4c531232013-02-06 15:26:58 +0900846 #ifndef WIN32
Andy Green24cba922013-01-19 13:56:10 +0800847 /* if our parent went down, don't linger around */
Andy Greenb5b23192013-02-11 17:13:32 +0800848 if (context->started_with_parent &&
849 kill(context->started_with_parent, 0) < 0)
Andy Green24cba922013-01-19 13:56:10 +0800850 kill(getpid(), SIGTERM);
Joakim Soderberg4c531232013-02-06 15:26:58 +0900851 #endif
Andy Green24cba922013-01-19 13:56:10 +0800852
Andy Greena71eafc2011-02-14 17:59:43 +0000853 /* global timeout check once per second */
854
Andy Green1c6e1422013-02-20 19:11:31 +0800855 if (pollfd)
856 our_fd = pollfd->fd;
857
Peter Hinz56885f32011-03-02 22:03:47 +0000858 for (n = 0; n < context->fds_count; n++) {
Andy Green1c6e1422013-02-20 19:11:31 +0800859 m = context->fds[n].fd;
860 wsi = context->lws_lookup[m];
861 if (!wsi)
Andy Greendfb23042013-01-17 12:26:48 +0800862 continue;
Andy Green1c6e1422013-02-20 19:11:31 +0800863
864 if (libwebsocket_service_timeout_check(context, wsi,
865 tv.tv_sec))
866 /* he did time out... */
867 if (m == our_fd)
868 /* it was the guy we came to service! */
869 timed_out = 1;
Andy Greena71eafc2011-02-14 17:59:43 +0000870 }
871 }
872
Andy Green1c6e1422013-02-20 19:11:31 +0800873 /* the socket we came to service timed out, nothing to do */
874 if (timed_out)
875 return 0;
876
Andy Greena71eafc2011-02-14 17:59:43 +0000877 /* just here for timeout management? */
878
879 if (pollfd == NULL)
880 return 0;
881
882 /* no, here to service a socket descriptor */
883
Andy Green65b0e912013-01-16 07:59:47 +0800884 /*
885 * deal with listen service piggybacking
886 * every listen_service_modulo services of other fds, we
887 * sneak one in to service the listen socket if there's anything waiting
888 *
889 * To handle connection storms, as found in ab, if we previously saw a
890 * pending connection here, it causes us to check again next time.
891 */
892
Andy Greenb5b23192013-02-11 17:13:32 +0800893 if (context->listen_service_fd && pollfd !=
894 &context->fds[listen_socket_fds_index]) {
Andy Green65b0e912013-01-16 07:59:47 +0800895 context->listen_service_count++;
896 if (context->listen_service_extraseen ||
Andy Greenb5b23192013-02-11 17:13:32 +0800897 context->listen_service_count ==
898 context->listen_service_modulo) {
Andy Green65b0e912013-01-16 07:59:47 +0800899 context->listen_service_count = 0;
900 m = 1;
901 if (context->listen_service_extraseen > 5)
902 m = 2;
903 while (m--) {
Andy Greenb5b23192013-02-11 17:13:32 +0800904 /*
905 * even with extpoll, we prepared this
906 * internal fds for listen
907 */
908 n = poll(&context->fds[listen_socket_fds_index],
909 1, 0);
910 if (n > 0) { /* there's a conn waiting for us */
911 libwebsocket_service_fd(context,
912 &context->
913 fds[listen_socket_fds_index]);
Andy Green65b0e912013-01-16 07:59:47 +0800914 context->listen_service_extraseen++;
915 } else {
916 if (context->listen_service_extraseen)
Andy Greenb5b23192013-02-11 17:13:32 +0800917 context->
918 listen_service_extraseen--;
Andy Green65b0e912013-01-16 07:59:47 +0800919 break;
920 }
921 }
922 }
923
924 }
925
926 /* okay, what we came here to do... */
927
Andy Greendfb23042013-01-17 12:26:48 +0800928 wsi = context->lws_lookup[pollfd->fd];
Andy Greend280b6e2013-01-15 13:40:23 +0800929 if (wsi == NULL) {
Andy Greendfb23042013-01-17 12:26:48 +0800930 if (pollfd->fd > 11)
Andy Greenb5b23192013-02-11 17:13:32 +0800931 lwsl_err("unexpected NULL wsi fd=%d fds_count=%d\n",
932 pollfd->fd, context->fds_count);
Andy Greenfa3f4052012-10-07 20:40:35 +0800933 return 0;
Andy Greend280b6e2013-01-15 13:40:23 +0800934 }
Andy Green8f037e42010-12-19 22:13:26 +0000935
Andy Green0d338332011-02-12 11:57:43 +0000936 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +0800937
Andy Greena1ce6be2013-01-18 11:43:21 +0800938#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +0800939 case LWS_CONNMODE_HTTP_SERVING:
Andy Green0d338332011-02-12 11:57:43 +0000940 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +0800941 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greena1ce6be2013-01-18 11:43:21 +0800942 return lws_server_socket_service(context, wsi, pollfd);
943#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000944
Andy Green0d338332011-02-12 11:57:43 +0000945 case LWS_CONNMODE_WS_SERVING:
946 case LWS_CONNMODE_WS_CLIENT:
947
948 /* handle session socket closed */
949
Andy Greenc9ac31e2013-02-16 10:17:52 +0800950 if ((!pollfd->revents & POLLIN) &&
951 (pollfd->revents & (POLLERR | POLLHUP))) {
Andy Green0d338332011-02-12 11:57:43 +0000952
Andy Green43db0452013-01-10 19:50:35 +0800953 lwsl_debug("Session Socket %p (fd=%d) dead\n",
Andy Green0d338332011-02-12 11:57:43 +0000954 (void *)wsi, pollfd->fd);
955
Peter Hinz56885f32011-03-02 22:03:47 +0000956 libwebsocket_close_and_free_session(context, wsi,
Andy Green687b0182011-02-26 11:04:01 +0000957 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800958 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000959 }
960
Andy Green0d338332011-02-12 11:57:43 +0000961 /* the guy requested a callback when it was OK to write */
962
Andy Greenda527df2011-03-07 07:08:12 +0000963 if ((pollfd->revents & POLLOUT) &&
964 wsi->state == WSI_STATE_ESTABLISHED)
965 if (lws_handle_POLLOUT_event(context, wsi,
966 pollfd) < 0) {
Andy Green0878b9e2013-02-13 11:44:20 +0800967 lwsl_info("libwebsocket_service_fd: closing\n");
Andy Greenda527df2011-03-07 07:08:12 +0000968 libwebsocket_close_and_free_session(
Andy Greenb0593712013-02-15 22:32:53 +0800969 context, wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800970 return 0;
Andy Green3b84c002011-03-06 13:14:42 +0000971 }
Andy Green0d338332011-02-12 11:57:43 +0000972
Andy Green0d338332011-02-12 11:57:43 +0000973
974 /* any incoming data ready? */
975
976 if (!(pollfd->revents & POLLIN))
977 break;
978
Andy Greenb45993c2010-12-18 15:13:50 +0000979#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +0800980read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +0800981 if (wsi->ssl) {
Andy Greene84652c2013-02-08 13:01:02 +0800982 eff_buf.token_len = SSL_read(wsi->ssl,
983 context->service_buffer,
Andy Greenb5b23192013-02-11 17:13:32 +0800984 sizeof(context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +0800985 if (!eff_buf.token_len) {
986 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
Andy Greene84652c2013-02-08 13:01:02 +0800987 lwsl_err("SSL_read returned 0 with reason %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +0800988 ERR_error_string(n,
989 (char *)context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +0800990 }
991 } else
Andy Greenb45993c2010-12-18 15:13:50 +0000992#endif
Andy Greene84652c2013-02-08 13:01:02 +0800993 eff_buf.token_len = recv(pollfd->fd,
Andy Greenb5b23192013-02-11 17:13:32 +0800994 context->service_buffer,
995 sizeof(context->service_buffer), 0);
Andy Greenb45993c2010-12-18 15:13:50 +0000996
Andy Green98a717c2011-03-06 13:14:15 +0000997 if (eff_buf.token_len < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800998 lwsl_debug("Socket read returned %d\n",
Andy Green98a717c2011-03-06 13:14:15 +0000999 eff_buf.token_len);
Alon Levydc93b7f2012-10-19 11:21:57 +02001000 if (errno != EINTR && errno != EAGAIN)
Andy Green6ee372f2012-04-09 15:09:01 +08001001 libwebsocket_close_and_free_session(context,
1002 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +08001003 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +00001004 }
Andy Green98a717c2011-03-06 13:14:15 +00001005 if (!eff_buf.token_len) {
Andy Greenb5b23192013-02-11 17:13:32 +08001006 lwsl_info("closing connection due to 0 length read\n");
Peter Hinz56885f32011-03-02 22:03:47 +00001007 libwebsocket_close_and_free_session(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001008 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenfa3f4052012-10-07 20:40:35 +08001009 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +00001010 }
1011
Andy Green98a717c2011-03-06 13:14:15 +00001012 /*
1013 * give any active extensions a chance to munge the buffer
1014 * before parse. We pass in a pointer to an lws_tokens struct
1015 * prepared with the default buffer and content length that's in
1016 * there. Rather than rewrite the default buffer, extensions
1017 * that expect to grow the buffer can adapt .token to
1018 * point to their own per-connection buffer in the extension
1019 * user allocation. By default with no extensions or no
1020 * extension callback handling, just the normal input buffer is
1021 * used then so it is efficient.
1022 */
Andy Greenb45993c2010-12-18 15:13:50 +00001023
Andy Greene84652c2013-02-08 13:01:02 +08001024 eff_buf.token = (char *)context->service_buffer;
Andy Green3182ece2013-01-20 17:08:31 +08001025#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001026 more = 1;
1027 while (more) {
Andy Green0d338332011-02-12 11:57:43 +00001028
Andy Green98a717c2011-03-06 13:14:15 +00001029 more = 0;
1030
1031 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +00001032 m = wsi->active_extensions[n]->callback(context,
1033 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +00001034 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +00001035 wsi->active_extensions_user[n],
1036 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +00001037 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001038 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +08001039 "Extension reports fatal error\n");
1040 libwebsocket_close_and_free_session(
1041 context, wsi,
1042 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +08001043 return 0;
Andy Green98a717c2011-03-06 13:14:15 +00001044 }
1045 if (m)
1046 more = 1;
1047 }
Andy Green3182ece2013-01-20 17:08:31 +08001048#endif
Andy Green98a717c2011-03-06 13:14:15 +00001049 /* service incoming data */
1050
1051 if (eff_buf.token_len) {
1052 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001053 (unsigned char *)eff_buf.token,
1054 eff_buf.token_len);
Andy Green98a717c2011-03-06 13:14:15 +00001055 if (n < 0)
1056 /* we closed wsi */
Andy Green040d2ef2013-01-16 13:40:43 +08001057 return 0;
Andy Green98a717c2011-03-06 13:14:15 +00001058 }
Andy Green3182ece2013-01-20 17:08:31 +08001059#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001060 eff_buf.token = NULL;
1061 eff_buf.token_len = 0;
1062 }
Andy Green3182ece2013-01-20 17:08:31 +08001063#endif
David Galeano7ffbe1b2013-01-10 10:35:32 +08001064
1065#ifdef LWS_OPENSSL_SUPPORT
1066 if (wsi->ssl && SSL_pending(wsi->ssl))
1067 goto read_pending;
1068#endif
Andy Green98a717c2011-03-06 13:14:15 +00001069 break;
Andy Green76f61e72013-01-16 11:53:05 +08001070
1071 default:
Andy Green03674a62013-01-16 11:47:40 +08001072#ifdef LWS_NO_CLIENT
1073 break;
1074#else
Andy Green76f61e72013-01-16 11:53:05 +08001075 return lws_client_socket_service(context, wsi, pollfd);
Andy Green03674a62013-01-16 11:47:40 +08001076#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001077 }
1078
1079 return 0;
1080}
1081
Andy Green0d338332011-02-12 11:57:43 +00001082
Andy Green6964bb52011-01-23 16:50:33 +00001083/**
1084 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +00001085 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001086 *
1087 * This function closes any active connections and then frees the
1088 * context. After calling this, any further use of the context is
1089 * undefined.
1090 */
1091void
Peter Hinz56885f32011-03-02 22:03:47 +00001092libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +00001093{
Andy Green3182ece2013-01-20 17:08:31 +08001094#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +00001095 int n;
1096 int m;
Andy Greena41314f2011-05-23 10:00:03 +01001097 struct libwebsocket_extension *ext;
Andy Greena7109e62013-02-11 12:05:54 +08001098 struct libwebsocket_protocols *protocol = context->protocols;
Andy Green6964bb52011-01-23 16:50:33 +00001099
Andy Greend636e352013-01-29 12:36:17 +08001100#ifdef LWS_LATENCY
1101 if (context->worst_latency_info[0])
1102 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1103#endif
1104
Andy Greendfb23042013-01-17 12:26:48 +08001105 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +08001106 struct libwebsocket *wsi =
1107 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +08001108 libwebsocket_close_and_free_session(context,
Andy Green7b922052013-02-11 11:43:05 +08001109 wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */);
1110 n--;
Andy Greendfb23042013-01-17 12:26:48 +08001111 }
Andy Green6964bb52011-01-23 16:50:33 +00001112
Andy Greena41314f2011-05-23 10:00:03 +01001113 /*
1114 * give all extensions a chance to clean up any per-context
1115 * allocations they might have made
1116 */
1117
1118 ext = context->extensions;
1119 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
1120 if (context->listen_port)
1121 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +08001122 while (ext && ext->callback) {
Andy Greenb5b23192013-02-11 17:13:32 +08001123 ext->callback(context, ext, NULL,
1124 (enum libwebsocket_extension_callback_reasons)m,
1125 NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001126 ext++;
1127 }
Andy Greena7109e62013-02-11 12:05:54 +08001128
1129 /*
1130 * inform all the protocols that they are done and will have no more
1131 * callbacks
1132 */
1133
1134 while (protocol->callback) {
1135 protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY,
1136 NULL, NULL, 0);
1137 protocol++;
1138 }
1139
Andy Green3182ece2013-01-20 17:08:31 +08001140#endif
Andy Greena41314f2011-05-23 10:00:03 +01001141
Peter Hinz56885f32011-03-02 22:03:47 +00001142#ifdef WIN32
1143#else
1144 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00001145#endif
1146
Peter Hinz56885f32011-03-02 22:03:47 +00001147#ifdef LWS_OPENSSL_SUPPORT
1148 if (context->ssl_ctx)
1149 SSL_CTX_free(context->ssl_ctx);
1150 if (context->ssl_client_ctx)
1151 SSL_CTX_free(context->ssl_client_ctx);
Andy Greenad686392013-02-11 14:50:45 +08001152
1153 ERR_remove_state(0);
1154 ERR_free_strings();
1155 EVP_cleanup();
1156 CRYPTO_cleanup_all_ex_data();
Peter Hinz56885f32011-03-02 22:03:47 +00001157#endif
1158
Andy Green46596482013-02-11 11:04:01 +08001159 if (context->fds)
1160 free(context->fds);
1161 if (context->lws_lookup)
1162 free(context->lws_lookup);
1163
Peter Hinz56885f32011-03-02 22:03:47 +00001164 free(context);
1165
1166#ifdef WIN32
1167 WSACleanup();
1168#endif
Andy Green6964bb52011-01-23 16:50:33 +00001169}
1170
Andy Greend88146d2013-01-22 12:40:35 +08001171/**
Andy Greenb5b23192013-02-11 17:13:32 +08001172 * libwebsocket_context_user() - get the user data associated with the context
Andy Greend88146d2013-01-22 12:40:35 +08001173 * @context: Websocket context
1174 *
1175 * This returns the optional user allocation that can be attached to
1176 * the context the sockets live in at context_create time. It's a way
1177 * to let all sockets serviced in the same context share data without
1178 * using globals statics in the user code.
1179 */
Alon Levy0291eb32012-10-19 11:21:56 +02001180LWS_EXTERN void *
1181libwebsocket_context_user(struct libwebsocket_context *context)
1182{
Andy Greenb5b23192013-02-11 17:13:32 +08001183 return context->user_space;
Alon Levy0291eb32012-10-19 11:21:56 +02001184}
1185
Andy Green6964bb52011-01-23 16:50:33 +00001186/**
1187 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001188 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001189 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1190 * service otherwise block and service immediately, returning
1191 * after the timeout if nothing needed service.
1192 *
1193 * This function deals with any pending websocket traffic, for three
1194 * kinds of event. It handles these events on both server and client
1195 * types of connection the same.
1196 *
1197 * 1) Accept new connections to our context's server
1198 *
Andy Green6f520a52013-01-29 17:57:39 +08001199 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001200 * server or client connections.
1201 *
1202 * You need to call this service function periodically to all the above
1203 * functions to happen; if your application is single-threaded you can
1204 * just call it in your main event loop.
1205 *
1206 * Alternatively you can fork a new process that asynchronously handles
1207 * calling this service in a loop. In that case you are happy if this
1208 * call blocks your thread until it needs to take care of something and
1209 * would call it with a large nonzero timeout. Your loop then takes no
1210 * CPU while there is nothing happening.
1211 *
1212 * If you are calling it in a single-threaded app, you don't want it to
1213 * wait around blocking other things in your loop from happening, so you
1214 * would call it with a timeout_ms of 0, so it returns immediately if
1215 * nothing is pending, or as soon as it services whatever was pending.
1216 */
1217
Andy Greene92cd172011-01-19 13:11:55 +00001218int
Peter Hinz56885f32011-03-02 22:03:47 +00001219libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001220{
1221 int n;
Andy Greene92cd172011-01-19 13:11:55 +00001222
1223 /* stay dead once we are dead */
1224
Peter Hinz56885f32011-03-02 22:03:47 +00001225 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001226 return 1;
1227
Andy Green0d338332011-02-12 11:57:43 +00001228 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001229
Peter Hinz56885f32011-03-02 22:03:47 +00001230 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green3221f922011-02-12 13:14:11 +00001231 if (n == 0) /* poll timeout */
1232 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001233
Andy Greendfb23042013-01-17 12:26:48 +08001234 if (n < 0)
Andy Green3928f612012-07-20 12:58:38 +08001235 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001236
Andy Greendfb23042013-01-17 12:26:48 +08001237 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001238
Peter Hinz56885f32011-03-02 22:03:47 +00001239 for (n = 0; n < context->fds_count; n++)
1240 if (context->fds[n].revents)
Andy Green3928f612012-07-20 12:58:38 +08001241 if (libwebsocket_service_fd(context,
1242 &context->fds[n]) < 0)
1243 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001244 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001245}
1246
Andy Green3182ece2013-01-20 17:08:31 +08001247#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001248int
1249lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001250 struct libwebsocket *wsi,
1251 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001252 void *v, size_t len)
1253{
1254 int n;
1255 int handled = 0;
1256
1257 /* maybe an extension will take care of it for us */
1258
1259 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1260 if (!wsi->active_extensions[n]->callback)
1261 continue;
1262
1263 handled |= wsi->active_extensions[n]->callback(context,
1264 wsi->active_extensions[n], wsi,
1265 r, wsi->active_extensions_user[n], v, len);
1266 }
1267
1268 return handled;
1269}
1270
1271
1272void *
1273lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001274 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001275{
1276 int n = 0;
1277
Andy Green68b45042011-05-25 21:41:57 +01001278 if (wsi == NULL)
1279 return NULL;
1280
Andy Greena41314f2011-05-23 10:00:03 +01001281 while (n < wsi->count_active_extensions) {
1282 if (wsi->active_extensions[n] != ext) {
1283 n++;
1284 continue;
1285 }
1286 return wsi->active_extensions_user[n];
1287 }
1288
1289 return NULL;
1290}
Andy Green3182ece2013-01-20 17:08:31 +08001291#endif
Andy Greena41314f2011-05-23 10:00:03 +01001292
Andy Green90c7cbc2011-01-27 06:26:52 +00001293/**
1294 * libwebsocket_callback_on_writable() - Request a callback when this socket
1295 * becomes able to be written to without
1296 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001297 *
Peter Hinz56885f32011-03-02 22:03:47 +00001298 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001299 * @wsi: Websocket connection instance to get callback for
1300 */
1301
1302int
Peter Hinz56885f32011-03-02 22:03:47 +00001303libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001304 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001305{
Andy Green3182ece2013-01-20 17:08:31 +08001306#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001307 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001308 int handled = 0;
1309
1310 /* maybe an extension will take care of it for us */
1311
1312 for (n = 0; n < wsi->count_active_extensions; n++) {
1313 if (!wsi->active_extensions[n]->callback)
1314 continue;
1315
1316 handled |= wsi->active_extensions[n]->callback(context,
1317 wsi->active_extensions[n], wsi,
1318 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1319 wsi->active_extensions_user[n], NULL, 0);
1320 }
1321
1322 if (handled)
1323 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001324#endif
Andy Greendfb23042013-01-17 12:26:48 +08001325 if (wsi->position_in_fds_table < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001326 lwsl_err("libwebsocket_callback_on_writable: failed to find socket %d\n",
1327 wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001328 return -1;
1329 }
1330
1331 context->fds[wsi->position_in_fds_table].events |= POLLOUT;
Andy Greena41314f2011-05-23 10:00:03 +01001332
Andy Green3221f922011-02-12 13:14:11 +00001333 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00001334 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001335 LWS_CALLBACK_SET_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001336 wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
Andy Green3221f922011-02-12 13:14:11 +00001337
Andy Green90c7cbc2011-01-27 06:26:52 +00001338 return 1;
1339}
1340
1341/**
1342 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1343 * all connections using the given protocol when it
1344 * becomes possible to write to each socket without
1345 * blocking in turn.
1346 *
1347 * @protocol: Protocol whose connections will get callbacks
1348 */
1349
1350int
1351libwebsocket_callback_on_writable_all_protocol(
1352 const struct libwebsocket_protocols *protocol)
1353{
Peter Hinz56885f32011-03-02 22:03:47 +00001354 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001355 int n;
Andy Green0d338332011-02-12 11:57:43 +00001356 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001357
Andy Greendfb23042013-01-17 12:26:48 +08001358 for (n = 0; n < context->fds_count; n++) {
1359 wsi = context->lws_lookup[context->fds[n].fd];
1360 if (!wsi)
1361 continue;
1362 if (wsi->protocol == protocol)
1363 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001364 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001365
1366 return 0;
1367}
1368
Andy Greenbe93fef2011-02-14 20:25:43 +00001369/**
1370 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1371 *
1372 * You will not need this unless you are doing something special
1373 *
1374 * @wsi: Websocket connection instance
1375 * @reason: timeout reason
1376 * @secs: how many seconds
1377 */
1378
1379void
1380libwebsocket_set_timeout(struct libwebsocket *wsi,
1381 enum pending_timeout reason, int secs)
1382{
1383 struct timeval tv;
1384
1385 gettimeofday(&tv, NULL);
1386
1387 wsi->pending_timeout_limit = tv.tv_sec + secs;
1388 wsi->pending_timeout = reason;
1389}
1390
Andy Greena6cbece2011-01-27 20:06:03 +00001391
1392/**
1393 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1394 *
1395 * You will not need this unless you are doing something special
1396 *
1397 * @wsi: Websocket connection instance
1398 */
1399
1400int
1401libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1402{
1403 return wsi->sock;
1404}
1405
Andy Greend636e352013-01-29 12:36:17 +08001406#ifdef LWS_LATENCY
1407void
Andy Greenb5b23192013-02-11 17:13:32 +08001408lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi,
1409 const char *action, int ret, int completed)
Andy Greend636e352013-01-29 12:36:17 +08001410{
1411 struct timeval tv;
1412 unsigned long u;
1413 char buf[256];
1414
1415 gettimeofday(&tv, NULL);
1416
1417 u = (tv.tv_sec * 1000000) + tv.tv_usec;
1418
1419 if (action) {
1420 if (completed) {
1421 if (wsi->action_start == wsi->latency_start)
Andy Greenb5b23192013-02-11 17:13:32 +08001422 sprintf(buf,
1423 "Completion first try lat %luus: %p: ret %d: %s\n",
1424 u - wsi->latency_start,
1425 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001426 else
Andy Greenb5b23192013-02-11 17:13:32 +08001427 sprintf(buf,
1428 "Completion %luus: lat %luus: %p: ret %d: %s\n",
1429 u - wsi->action_start,
1430 u - wsi->latency_start,
1431 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001432 wsi->action_start = 0;
1433 } else
Andy Greenb5b23192013-02-11 17:13:32 +08001434 sprintf(buf, "lat %luus: %p: ret %d: %s\n",
1435 u - wsi->latency_start,
1436 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001437 if (u - wsi->latency_start > context->worst_latency) {
1438 context->worst_latency = u - wsi->latency_start;
1439 strcpy(context->worst_latency_info, buf);
1440 }
1441 lwsl_latency("%s", buf);
1442 } else {
1443 wsi->latency_start = u;
1444 if (!wsi->action_start)
1445 wsi->action_start = u;
1446 }
1447}
1448#endif
1449
Andy Greena1ce6be2013-01-18 11:43:21 +08001450#ifdef LWS_NO_SERVER
1451int
1452_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1453{
1454 return 0;
1455}
1456#else
Andy Green706961d2013-01-17 16:50:35 +08001457int
1458_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1459{
1460 struct libwebsocket_context *context = wsi->protocol->owning_server;
1461 int n;
1462
Andy Green623a98d2013-01-21 11:04:23 +08001463 if (!(wsi->u.ws.rxflow_change_to & 2))
Andy Green706961d2013-01-17 16:50:35 +08001464 return 0;
1465
Andy Green623a98d2013-01-21 11:04:23 +08001466 wsi->u.ws.rxflow_change_to &= ~2;
Andy Green706961d2013-01-17 16:50:35 +08001467
Andy Greenb5b23192013-02-11 17:13:32 +08001468 lwsl_info("rxflow: wsi %p change_to %d\n",
1469 wsi, wsi->u.ws.rxflow_change_to);
Andy Green706961d2013-01-17 16:50:35 +08001470
1471 /* if we're letting it come again, did we interrupt anything? */
Andy Green623a98d2013-01-21 11:04:23 +08001472 if ((wsi->u.ws.rxflow_change_to & 1) && wsi->u.ws.rxflow_buffer) {
Andy Green706961d2013-01-17 16:50:35 +08001473 n = libwebsocket_interpret_incoming_packet(wsi, NULL, 0);
1474 if (n < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001475 lwsl_info("libwebsocket_rx_flow_control: close req\n");
Andy Green706961d2013-01-17 16:50:35 +08001476 return -1;
1477 }
1478 if (n)
1479 /* oh he stuck again, do nothing */
1480 return 0;
1481 }
1482
Andy Green623a98d2013-01-21 11:04:23 +08001483 if (wsi->u.ws.rxflow_change_to & 1)
Andy Green706961d2013-01-17 16:50:35 +08001484 context->fds[wsi->position_in_fds_table].events |= POLLIN;
1485 else
1486 context->fds[wsi->position_in_fds_table].events &= ~POLLIN;
1487
Andy Green623a98d2013-01-21 11:04:23 +08001488 if (wsi->u.ws.rxflow_change_to & 1)
Andy Green706961d2013-01-17 16:50:35 +08001489 /* external POLL support via protocol 0 */
1490 context->protocols[0].callback(context, wsi,
1491 LWS_CALLBACK_SET_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001492 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001493 else
1494 /* external POLL support via protocol 0 */
1495 context->protocols[0].callback(context, wsi,
1496 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001497 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001498
1499 return 1;
1500}
Andy Greena1ce6be2013-01-18 11:43:21 +08001501#endif
Andy Green706961d2013-01-17 16:50:35 +08001502
Andy Green90c7cbc2011-01-27 06:26:52 +00001503/**
1504 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1505 * receieved packets.
1506 *
1507 * If the output side of a server process becomes choked, this allows flow
1508 * control for the input side.
1509 *
1510 * @wsi: Websocket connection instance to get callback for
1511 * @enable: 0 = disable read servicing for this connection, 1 = enable
1512 */
1513
1514int
1515libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1516{
Andy Green623a98d2013-01-21 11:04:23 +08001517 wsi->u.ws.rxflow_change_to = 2 | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001518
Andy Green706961d2013-01-17 16:50:35 +08001519 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001520}
1521
Andy Green706961d2013-01-17 16:50:35 +08001522
Andy Green2ac5a6f2011-01-28 10:00:18 +00001523/**
1524 * libwebsocket_canonical_hostname() - returns this host's hostname
1525 *
1526 * This is typically used by client code to fill in the host parameter
1527 * when making a client connection. You can only call it after the context
1528 * has been created.
1529 *
Peter Hinz56885f32011-03-02 22:03:47 +00001530 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001531 */
1532
1533
1534extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001535libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001536{
Peter Hinz56885f32011-03-02 22:03:47 +00001537 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001538}
1539
1540
Andy Green90c7cbc2011-01-27 06:26:52 +00001541static void sigpipe_handler(int x)
1542{
1543}
1544
Andy Green6901cb32011-02-21 08:06:47 +00001545#ifdef LWS_OPENSSL_SUPPORT
1546static int
1547OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1548{
1549
1550 SSL *ssl;
1551 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001552 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001553
1554 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1555 SSL_get_ex_data_X509_STORE_CTX_idx());
1556
1557 /*
Andy Green2e24da02011-03-05 16:12:04 +00001558 * !!! nasty openssl requires the index to come as a library-scope
1559 * static
Andy Green6901cb32011-02-21 08:06:47 +00001560 */
Andy Green2e24da02011-03-05 16:12:04 +00001561 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001562
Peter Hinz56885f32011-03-02 22:03:47 +00001563 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001564 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1565 x509_ctx, ssl, preverify_ok);
1566
1567 /* convert return code from 0 = OK to 1 = OK */
1568
1569 if (!n)
1570 n = 1;
1571 else
1572 n = 0;
1573
1574 return n;
1575}
1576#endif
1577
Andy Green706961d2013-01-17 16:50:35 +08001578int user_callback_handle_rxflow(callback_function callback_function,
Andy Greenb5b23192013-02-11 17:13:32 +08001579 struct libwebsocket_context *context,
Andy Green706961d2013-01-17 16:50:35 +08001580 struct libwebsocket *wsi,
1581 enum libwebsocket_callback_reasons reason, void *user,
1582 void *in, size_t len)
1583{
1584 int n;
1585
1586 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenaedc9532013-02-10 21:21:24 +08001587 if (!n)
1588 n = _libwebsocket_rx_flow_control(wsi);
Andy Green706961d2013-01-17 16:50:35 +08001589
Andy Greenaedc9532013-02-10 21:21:24 +08001590 return n;
Andy Green706961d2013-01-17 16:50:35 +08001591}
1592
Andy Greenb45993c2010-12-18 15:13:50 +00001593
Andy Greenab990e42010-10-31 12:42:52 +00001594/**
Andy Green4739e5c2011-01-22 12:51:57 +00001595 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08001596 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00001597 *
Andy Green1b265272013-02-09 14:01:09 +08001598 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00001599 * of all initialization in one step.
1600 *
Andy Greene92cd172011-01-19 13:11:55 +00001601 * After initialization, it returns a struct libwebsocket_context * that
1602 * represents this server. After calling, user code needs to take care
1603 * of calling libwebsocket_service() with the context pointer to get the
1604 * server's sockets serviced. This can be done in the same process context
1605 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001606 *
Andy Green8f037e42010-12-19 22:13:26 +00001607 * The protocol callback functions are called for a handful of events
1608 * including http requests coming in, websocket connections becoming
1609 * established, and data arriving; it's also called periodically to allow
1610 * async transmission.
1611 *
1612 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1613 * at that time websocket protocol has not been negotiated. Other
1614 * protocols after the first one never see any HTTP callack activity.
1615 *
1616 * The server created is a simple http server by default; part of the
1617 * websocket standard is upgrading this http connection to a websocket one.
1618 *
1619 * This allows the same server to provide files like scripts and favicon /
1620 * images or whatever over http and dynamic data over websockets all in
1621 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001622 */
Andy Green4ea60062010-10-30 12:15:07 +01001623
Andy Greene92cd172011-01-19 13:11:55 +00001624struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08001625libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01001626{
Peter Hinz56885f32011-03-02 22:03:47 +00001627 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001628 char *p;
Andy Green14f47292013-02-11 19:36:15 +08001629 int n;
Joakim Soderberg3baa08c2013-02-22 09:27:59 +08001630#ifndef LWS_NO_SERVER
Andy Greencbb31222013-01-31 09:57:05 +08001631 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00001632 struct libwebsocket *wsi;
Andy Greencbb31222013-01-31 09:57:05 +08001633 struct sockaddr_in serv_addr;
1634#endif
Andy Green3182ece2013-01-20 17:08:31 +08001635#ifndef LWS_NO_EXTENSIONS
1636 int m;
Andy Green1b265272013-02-09 14:01:09 +08001637 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08001638#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001639
Andy Green3faa9c72010-11-08 17:03:03 +00001640#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001641 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001642#endif
1643
Joakim Soderberg4c531232013-02-06 15:26:58 +09001644#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001645 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09001646#endif
1647
Andy Greenb3a614a2013-01-19 13:08:17 +08001648 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08001649 lwsl_notice("Library version: %s\n", library_version);
Andy Greenb5b23192013-02-11 17:13:32 +08001650 lwsl_info(" LWS_MAX_HEADER_NAME_LENGTH: %u\n",
1651 LWS_MAX_HEADER_NAME_LENGTH);
Andy Greenc0d6b632013-01-12 23:42:17 +08001652 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
Andy Greenc0d6b632013-01-12 23:42:17 +08001653 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001654#ifndef LWS_NO_EXTENSIONS
Andy Greenb5b23192013-02-11 17:13:32 +08001655 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n",
1656 LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001657#else
1658 lwsl_notice(" Configured without extension support\n");
1659#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001660 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1661 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
1662 lwsl_info(" CIPHERS_LIST_STRING: '%s'\n", CIPHERS_LIST_STRING);
1663 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1664 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001665
Peter Hinz56885f32011-03-02 22:03:47 +00001666#ifdef _WIN32
1667 {
1668 WORD wVersionRequested;
1669 WSADATA wsaData;
1670 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001671 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001672
1673 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1674 wVersionRequested = MAKEWORD(2, 2);
1675
1676 err = WSAStartup(wVersionRequested, &wsaData);
1677 if (err != 0) {
1678 /* Tell the user that we could not find a usable */
1679 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001680 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001681 return NULL;
1682 }
David Galeano7b11fec2011-10-04 19:55:18 +08001683
Andy Green6ee372f2012-04-09 15:09:01 +08001684 /* default to a poll() made out of select() */
1685 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001686
Andy Green6ee372f2012-04-09 15:09:01 +08001687 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001688 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001689 if (wsdll)
1690 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09001691
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001692 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09001693 if (!poll)
1694 poll = emulated_poll;
Peter Hinz56885f32011-03-02 22:03:47 +00001695 }
1696#endif
1697
Andy Greenb5b23192013-02-11 17:13:32 +08001698 context = (struct libwebsocket_context *)
1699 malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001700 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001701 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001702 return NULL;
1703 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001704 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08001705#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001706 context->started_with_parent = pid_daemon;
1707 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1708#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001709
Joakim Soderberg4c531232013-02-06 15:26:58 +09001710 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08001711 context->protocols = info->protocols;
1712 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00001713 context->http_proxy_port = 0;
1714 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08001715 context->options = info->options;
Andy Greendfb23042013-01-17 12:26:48 +08001716 /* to reduce this allocation, */
1717 context->max_fds = getdtablesize();
Andy Greena86f6342013-02-11 11:04:56 +08001718 lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n",
1719 sizeof(struct libwebsocket_context),
1720 sizeof(struct pollfd) + sizeof(struct libwebsocket *),
1721 context->max_fds,
Andy Greenb5b23192013-02-11 17:13:32 +08001722 sizeof(struct libwebsocket_context) +
1723 ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) *
1724 context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08001725
Andy Greenb5b23192013-02-11 17:13:32 +08001726 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) *
1727 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001728 if (context->fds == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001729 lwsl_err("Unable to allocate fds array for %d connections\n",
1730 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001731 free(context);
1732 return NULL;
1733 }
Andy Greenb5b23192013-02-11 17:13:32 +08001734 context->lws_lookup = (struct libwebsocket **)
1735 malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001736 if (context->lws_lookup == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001737 lwsl_err(
1738 "Unable to allocate lws_lookup array for %d connections\n",
1739 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001740 free(context->fds);
1741 free(context);
1742 return NULL;
1743 }
Andy Greena17c6922013-01-20 20:21:54 +08001744
Peter Hinz56885f32011-03-02 22:03:47 +00001745 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001746#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08001747 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08001748#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08001749 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08001750 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00001751
Peter Hinz56885f32011-03-02 22:03:47 +00001752#ifdef WIN32
1753 context->fd_random = 0;
1754#else
1755 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
1756 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001757 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001758 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001759 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00001760 }
Peter Hinz56885f32011-03-02 22:03:47 +00001761#endif
Andy Green44eee682011-02-10 09:32:24 +00001762
Peter Hinz56885f32011-03-02 22:03:47 +00001763#ifdef LWS_OPENSSL_SUPPORT
1764 context->use_ssl = 0;
1765 context->ssl_ctx = NULL;
1766 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00001767 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001768#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00001769
Andy Greena1ce6be2013-01-18 11:43:21 +08001770 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08001771
Andy Greena1ce6be2013-01-18 11:43:21 +08001772#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001773 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Green788c4a82012-10-22 12:29:57 +01001774 /* find canonical hostname */
Andy Greenadc71462013-02-14 17:11:22 +08001775 gethostname((char *)context->canonical_hostname,
Andy Greenb5b23192013-02-11 17:13:32 +08001776 sizeof(context->canonical_hostname) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01001777
Andy Greenb5b23192013-02-11 17:13:32 +08001778 lwsl_notice(" canonical_hostname = %s\n",
1779 context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08001780 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001781#endif
Andy Greena69f0512012-05-03 12:32:38 +08001782
Andy Green9659f372011-01-27 22:01:43 +00001783 /* split the proxy ads:port if given */
1784
1785 p = getenv("http_proxy");
1786 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00001787 strncpy(context->http_proxy_address, p,
Andy Greenb5b23192013-02-11 17:13:32 +08001788 sizeof(context->http_proxy_address) - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00001789 context->http_proxy_address[
Andy Greenb5b23192013-02-11 17:13:32 +08001790 sizeof(context->http_proxy_address) - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00001791
Peter Hinz56885f32011-03-02 22:03:47 +00001792 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00001793 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001794 lwsl_err("http_proxy needs to be ads:port\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001795 goto bail;
Andy Green9659f372011-01-27 22:01:43 +00001796 }
1797 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00001798 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00001799
Andy Greenb3a614a2013-01-19 13:08:17 +08001800 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001801 context->http_proxy_address,
1802 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00001803 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001804
Andy Greena1ce6be2013-01-18 11:43:21 +08001805#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001806 if (info->port) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001807
Andy Green3faa9c72010-11-08 17:03:03 +00001808#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08001809 context->use_ssl = info->ssl_cert_filepath != NULL &&
1810 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09001811#ifdef USE_CYASSL
1812 lwsl_notice(" Compiled with CYASSL support\n");
1813#else
1814 lwsl_notice(" Compiled with OpenSSL support\n");
1815#endif
Peter Hinz56885f32011-03-02 22:03:47 +00001816 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09001817 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001818 else
Andy Green23c5f2e2013-02-06 15:43:00 +09001819 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00001820
Andy Green90c7cbc2011-01-27 06:26:52 +00001821#else
Andy Green2b40b792013-02-10 22:22:01 +08001822 if (info->ssl_cert_filepath != NULL &&
1823 info->ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08001824 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001825 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001826 }
Andy Greenb5b23192013-02-11 17:13:32 +08001827 lwsl_notice(" Compiled without SSL support\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001828#endif
Andy Greena17c6922013-01-20 20:21:54 +08001829
Andy Greenb5b23192013-02-11 17:13:32 +08001830 lwsl_notice(
1831 " per-conn mem: %u + %u headers + protocol rx buf\n",
1832 sizeof(struct libwebsocket),
1833 sizeof(struct allocated_headers));
Andy Green90c7cbc2011-01-27 06:26:52 +00001834 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001835#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001836
1837 /* ignore SIGPIPE */
Peter Hinz56885f32011-03-02 22:03:47 +00001838#ifdef WIN32
1839#else
Andy Green90c7cbc2011-01-27 06:26:52 +00001840 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00001841#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001842
1843
1844#ifdef LWS_OPENSSL_SUPPORT
1845
1846 /* basic openssl init */
1847
1848 SSL_library_init();
1849
1850 OpenSSL_add_all_algorithms();
1851 SSL_load_error_strings();
1852
Andy Green2e24da02011-03-05 16:12:04 +00001853 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00001854 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
1855
Andy Green90c7cbc2011-01-27 06:26:52 +00001856 /*
1857 * Firefox insists on SSLv23 not SSLv3
1858 * Konq disables SSLv2 by default now, SSLv23 works
1859 */
1860
1861 method = (SSL_METHOD *)SSLv23_server_method();
1862 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001863 lwsl_err("problem creating ssl method: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001864 ERR_error_string(ERR_get_error(),
1865 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001866 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001867 }
Peter Hinz56885f32011-03-02 22:03:47 +00001868 context->ssl_ctx = SSL_CTX_new(method); /* create context */
1869 if (!context->ssl_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001870 lwsl_err("problem creating ssl context: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001871 ERR_error_string(ERR_get_error(),
1872 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001873 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001874 }
1875
David Galeanocc148e42013-01-10 10:18:59 +08001876#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001877 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001878#endif
David Galeano77a677c2013-01-10 10:14:12 +08001879 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
David Galeanof177f2a2013-01-10 10:15:19 +08001880 SSL_CTX_set_cipher_list(context->ssl_ctx, CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001881
Andy Greena1ce6be2013-01-18 11:43:21 +08001882#ifndef LWS_NO_CLIENT
1883
Andy Green90c7cbc2011-01-27 06:26:52 +00001884 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08001885
Andy Green1b265272013-02-09 14:01:09 +08001886 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00001887 method = (SSL_METHOD *)SSLv23_client_method();
1888 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001889 lwsl_err("problem creating ssl method: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001890 ERR_error_string(ERR_get_error(),
1891 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001892 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001893 }
1894 /* create context */
1895 context->ssl_client_ctx = SSL_CTX_new(method);
1896 if (!context->ssl_client_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001897 lwsl_err("problem creating ssl context: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001898 ERR_error_string(ERR_get_error(),
1899 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001900 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001901 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001902
David Galeanocc148e42013-01-10 10:18:59 +08001903#ifdef SSL_OP_NO_COMPRESSION
Andy Greenb5b23192013-02-11 17:13:32 +08001904 SSL_CTX_set_options(context->ssl_client_ctx,
1905 SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001906#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001907 SSL_CTX_set_options(context->ssl_client_ctx,
1908 SSL_OP_CIPHER_SERVER_PREFERENCE);
1909 SSL_CTX_set_cipher_list(context->ssl_client_ctx,
1910 CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001911
Peter Hinz56885f32011-03-02 22:03:47 +00001912 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08001913 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08001914 if (!SSL_CTX_load_verify_locations(
1915 context->ssl_client_ctx, NULL,
1916 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08001917 lwsl_err(
Andy Greenb5b23192013-02-11 17:13:32 +08001918 "Unable to load SSL Client certs from %s "
1919 "(set by --with-client-cert-dir= "
1920 "in configure) -- client ssl isn't "
1921 "going to work", LWS_OPENSSL_CLIENT_CERTS);
David Galeano2f82be82013-01-09 16:25:54 +08001922 } else
1923 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08001924 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08001925 NULL))
Andy Green43db0452013-01-10 19:50:35 +08001926 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08001927 "Unable to load SSL Client certs "
1928 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08001929 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00001930
1931 /*
1932 * callback allowing user code to load extra verification certs
1933 * helping the client to verify server identity
1934 */
1935
1936 context->protocols[0].callback(context, NULL,
1937 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
1938 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00001939 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001940#endif
Andy Green6ee372f2012-04-09 15:09:01 +08001941
Andy Greenc6bf2c22011-02-20 11:10:47 +00001942 /* as a server, are we requiring clients to identify themselves? */
1943
Andy Greenb5b23192013-02-11 17:13:32 +08001944 if (info->options &
1945 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00001946
1947 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08001948
Peter Hinz56885f32011-03-02 22:03:47 +00001949 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00001950 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1951 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001952
1953 /*
1954 * give user code a chance to load certs into the server
1955 * allowing it to verify incoming client certs
1956 */
1957
Peter Hinz56885f32011-03-02 22:03:47 +00001958 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00001959 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00001960 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001961 }
1962
Peter Hinz56885f32011-03-02 22:03:47 +00001963 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001964
1965 /* openssl init for server sockets */
1966
Andy Green3faa9c72010-11-08 17:03:03 +00001967 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08001968 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08001969 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00001970 if (n != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001971 lwsl_err("problem getting cert '%s': %s\n",
Andy Green1b265272013-02-09 14:01:09 +08001972 info->ssl_cert_filepath,
Andy Greenb5b23192013-02-11 17:13:32 +08001973 ERR_error_string(ERR_get_error(),
1974 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001975 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001976 }
1977 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00001978 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08001979 info->ssl_private_key_filepath,
1980 SSL_FILETYPE_PEM) != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001981 lwsl_err("ssl problem getting key '%s': %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001982 info->ssl_private_key_filepath,
1983 ERR_error_string(ERR_get_error(),
1984 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001985 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001986 }
1987 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00001988 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08001989 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001990 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001991 }
1992
1993 /* SSL is happy and has a cert it's content with */
1994 }
1995#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001996
Andy Greendf736162011-01-18 15:39:02 +00001997 /* selftest */
1998
1999 if (lws_b64_selftest())
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002000 goto bail;
Andy Greendf736162011-01-18 15:39:02 +00002001
Andy Greena1ce6be2013-01-18 11:43:21 +08002002#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00002003 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00002004
Andy Green1b265272013-02-09 14:01:09 +08002005 if (info->port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08002006 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00002007
Andy Green4739e5c2011-01-22 12:51:57 +00002008 sockfd = socket(AF_INET, SOCK_STREAM, 0);
2009 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08002010 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002011 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002012 }
Andy Green775c0dd2010-10-29 14:15:22 +01002013
Joakim Soderberg4c531232013-02-06 15:26:58 +09002014#ifndef WIN32
Andy Greenb5b23192013-02-11 17:13:32 +08002015 /*
2016 * allow us to restart even if old sockets in TIME_WAIT
2017 * (REUSEADDR on Unix means, "don't hang on to this
2018 * address after the listener is closed." On Windows, though,
2019 * it means "don't keep other processes from binding to
2020 * this address while we're using it)
2021 */
Andy Green6ee372f2012-04-09 15:09:01 +08002022 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
2023 (const void *)&opt, sizeof(opt));
Joakim Soderberg4c531232013-02-06 15:26:58 +09002024#endif
Andy Green6c939552011-03-08 08:56:57 +00002025
2026 /* Disable Nagle */
2027 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08002028 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
2029 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002030
Joakim Soderberg4c531232013-02-06 15:26:58 +09002031 #ifdef WIN32
2032 opt = 0;
Andy Greenb5b23192013-02-11 17:13:32 +08002033 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002034 #else
Andy Greene2160712013-01-28 12:19:10 +08002035 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002036 #endif
Andy Greene2160712013-01-28 12:19:10 +08002037
Andy Green4739e5c2011-01-22 12:51:57 +00002038 bzero((char *) &serv_addr, sizeof(serv_addr));
2039 serv_addr.sin_family = AF_INET;
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002040 if (info->iface == NULL)
Andy Green32375b72011-02-19 08:32:53 +00002041 serv_addr.sin_addr.s_addr = INADDR_ANY;
2042 else
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002043 interface_to_sa(info->iface, &serv_addr,
Andy Green32375b72011-02-19 08:32:53 +00002044 sizeof(serv_addr));
Andy Green1b265272013-02-09 14:01:09 +08002045 serv_addr.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00002046
2047 n = bind(sockfd, (struct sockaddr *) &serv_addr,
2048 sizeof(serv_addr));
2049 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002050 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green1b265272013-02-09 14:01:09 +08002051 info->port, n, errno);
Andy Green41c58032013-01-12 13:21:08 +08002052 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002053 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002054 }
Andy Green0d338332011-02-12 11:57:43 +00002055
Andy Greenb5b23192013-02-11 17:13:32 +08002056 wsi = (struct libwebsocket *)malloc(
2057 sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08002058 if (wsi == NULL) {
2059 lwsl_err("Out of mem\n");
2060 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002061 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08002062 }
Andy Greenb5b23192013-02-11 17:13:32 +08002063 memset(wsi, 0, sizeof(struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00002064 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08002065#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00002066 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002067#endif
Andy Green0d338332011-02-12 11:57:43 +00002068 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08002069
2070 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002071
Andy Green65b0e912013-01-16 07:59:47 +08002072 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
2073 context->listen_service_count = 0;
2074 context->listen_service_fd = sockfd;
2075
Andy Greena824d182013-01-15 20:52:29 +08002076 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08002077 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00002078 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002079#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002080
Andy Green6ee372f2012-04-09 15:09:01 +08002081 /*
2082 * drop any root privs for this process
2083 * to listen on port < 1023 we would have needed root, but now we are
2084 * listening, we don't want the power for anything else
2085 */
Peter Hinz56885f32011-03-02 22:03:47 +00002086#ifdef WIN32
2087#else
Andy Green1b265272013-02-09 14:01:09 +08002088 if (info->gid != -1)
2089 if (setgid(info->gid))
Andy Green43db0452013-01-10 19:50:35 +08002090 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green1b265272013-02-09 14:01:09 +08002091 if (info->uid != -1)
2092 if (setuid(info->uid))
Andy Green43db0452013-01-10 19:50:35 +08002093 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00002094#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002095
Andy Green6f520a52013-01-29 17:57:39 +08002096 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00002097
Peter Hinz56885f32011-03-02 22:03:47 +00002098 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08002099 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00002100 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002101
Andy Green43db0452013-01-10 19:50:35 +08002102 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002103 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002104
Andy Green1b265272013-02-09 14:01:09 +08002105 info->protocols[context->count_protocols].owning_server =
2106 context;
2107 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00002108 context->count_protocols;
Andy Greena7109e62013-02-11 12:05:54 +08002109
2110 /*
2111 * inform all the protocols that they are doing their one-time
2112 * initialization if they want to
2113 */
2114 info->protocols[context->count_protocols].callback(context,
2115 NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00002116 }
Andy Greenf5bc1302013-01-21 09:09:52 +08002117
Andy Green3182ece2013-01-20 17:08:31 +08002118#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01002119 /*
2120 * give all extensions a chance to create any per-context
2121 * allocations they need
2122 */
2123
2124 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andy Green1b265272013-02-09 14:01:09 +08002125 if (info->port)
Andy Greena41314f2011-05-23 10:00:03 +01002126 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andy Greenb5b23192013-02-11 17:13:32 +08002127
Andy Green1b265272013-02-09 14:01:09 +08002128 if (info->extensions) {
2129 ext = info->extensions;
2130 while (ext->callback) {
2131 lwsl_ext(" Extension: %s\n", ext->name);
2132 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002133 (enum libwebsocket_extension_callback_reasons)m,
2134 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08002135 ext++;
2136 }
Andy Greena41314f2011-05-23 10:00:03 +01002137 }
Andy Green3182ece2013-01-20 17:08:31 +08002138#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002139 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002140
2141bail:
2142 libwebsocket_context_destroy(context);
2143 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00002144}
Andy Greenb45993c2010-12-18 15:13:50 +00002145
Andy Greenb45993c2010-12-18 15:13:50 +00002146/**
2147 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002148 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002149 * @wsi: pointer to struct websocket you want to know the protocol of
2150 *
Andy Green8f037e42010-12-19 22:13:26 +00002151 *
Andy Green6f520a52013-01-29 17:57:39 +08002152 * Some apis can act on all live connections of a given protocol,
2153 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002154 */
Andy Greenab990e42010-10-31 12:42:52 +00002155
Andy Greenb45993c2010-12-18 15:13:50 +00002156const struct libwebsocket_protocols *
2157libwebsockets_get_protocol(struct libwebsocket *wsi)
2158{
2159 return wsi->protocol;
2160}
2161
Andy Green82c3d542011-03-07 21:16:31 +00002162int
2163libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2164{
Andy Green623a98d2013-01-21 11:04:23 +08002165 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002166}
Alex Bligh49146db2011-11-07 17:19:25 +08002167
David Galeanoe2cf9922013-01-09 18:06:55 +08002168unsigned char
2169libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2170{
Andy Green623a98d2013-01-21 11:04:23 +08002171 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002172}
2173
Andy Green2af4d5b2013-02-18 16:30:10 +08002174int
Alex Bligh49146db2011-11-07 17:19:25 +08002175libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2176{
Andy Green67d556c2013-02-15 22:31:55 +08002177 if (!wsi->protocol)
Andy Green2af4d5b2013-02-18 16:30:10 +08002178 return 1;
Andy Green67d556c2013-02-15 22:31:55 +08002179
Alex Bligh49146db2011-11-07 17:19:25 +08002180 /* allocate the per-connection user memory (if any) */
2181
2182 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2183 wsi->user_space = malloc(
2184 wsi->protocol->per_session_data_size);
2185 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002186 lwsl_err("Out of memory for conn user space\n");
Andy Green2af4d5b2013-02-18 16:30:10 +08002187 return 1;
Alex Bligh49146db2011-11-07 17:19:25 +08002188 }
Andy Green6ee372f2012-04-09 15:09:01 +08002189 memset(wsi->user_space, 0,
2190 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002191 }
Andy Green2af4d5b2013-02-18 16:30:10 +08002192 return 0;
Alex Bligh49146db2011-11-07 17:19:25 +08002193}
Andy Green43db0452013-01-10 19:50:35 +08002194
Andy Green0b319092013-01-19 11:17:56 +08002195static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002196{
Andy Green0b319092013-01-19 11:17:56 +08002197 char buf[300];
2198 struct timeval tv;
Andy Green0b319092013-01-19 11:17:56 +08002199 int n;
2200
2201 gettimeofday(&tv, NULL);
2202
2203 buf[0] = '\0';
2204 for (n = 0; n < LLL_COUNT; n++)
2205 if (level == (1 << n)) {
Andy Green058ba812013-01-19 11:32:18 +08002206 sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
Andy Greenb5b23192013-02-11 17:13:32 +08002207 (int)(tv.tv_usec / 100), log_level_names[n]);
Andy Green0b319092013-01-19 11:17:56 +08002208 break;
2209 }
Andy Greenb5b23192013-02-11 17:13:32 +08002210
Andy Green0b319092013-01-19 11:17:56 +08002211 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002212}
2213
Joakim Soderberg4c531232013-02-06 15:26:58 +09002214#ifdef WIN32
2215void lwsl_emit_syslog(int level, const char *line)
2216{
2217 lwsl_emit_stderr(level, line);
2218}
2219#else
Andy Greenc11db202013-01-19 11:12:16 +08002220void lwsl_emit_syslog(int level, const char *line)
2221{
2222 int syslog_level = LOG_DEBUG;
2223
2224 switch (level) {
2225 case LLL_ERR:
2226 syslog_level = LOG_ERR;
2227 break;
2228 case LLL_WARN:
2229 syslog_level = LOG_WARNING;
2230 break;
2231 case LLL_NOTICE:
2232 syslog_level = LOG_NOTICE;
2233 break;
2234 case LLL_INFO:
2235 syslog_level = LOG_INFO;
2236 break;
2237 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002238 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002239}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002240#endif
Andy Greenc11db202013-01-19 11:12:16 +08002241
Andy Green43db0452013-01-10 19:50:35 +08002242void _lws_log(int filter, const char *format, ...)
2243{
Andy Greende8f27a2013-01-12 09:17:42 +08002244 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002245 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002246
2247 if (!(log_level & filter))
2248 return;
2249
Andy Green43db0452013-01-10 19:50:35 +08002250 va_start(ap, format);
Andy Greenb5b23192013-02-11 17:13:32 +08002251 vsnprintf(buf, sizeof(buf), format, ap);
2252 buf[sizeof(buf) - 1] = '\0';
Andy Greende8f27a2013-01-12 09:17:42 +08002253 va_end(ap);
2254
Andy Green0b319092013-01-19 11:17:56 +08002255 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002256}
2257
2258/**
2259 * lws_set_log_level() - Set the logging bitfield
2260 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002261 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2262 * function to perform log string emission instead of
2263 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002264 *
Andy Greenc6511a02013-02-19 10:01:48 +08002265 * log level defaults to "err", "warn" and "notice" contexts enabled and
Andy Greende8f27a2013-01-12 09:17:42 +08002266 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002267 */
2268
Andy Greenb5b23192013-02-11 17:13:32 +08002269void lws_set_log_level(int level, void (*log_emit_function)(int level,
2270 const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002271{
2272 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002273 if (log_emit_function)
2274 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002275}