blob: 74359a2b609077df504540b3db560bbbcda42f92 [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 Green623a98d2013-01-21 11:04:23 +0800203 wsi->u.ws.close_reason = reason;
Andy Greenda527df2011-03-07 07:08:12 +0000204
Andy Greenb8b247d2013-01-22 07:20:08 +0800205 if (wsi->mode == LWS_CONNMODE_HTTP_SERVING && wsi->u.http.fd) {
206 close(wsi->u.http.fd);
207 wsi->u.http.fd = 0;
208 }
209
Andy Green3182ece2013-01-20 17:08:31 +0800210#ifndef LWS_NO_EXTENSIONS
Andy Greenda527df2011-03-07 07:08:12 +0000211 /*
Andy Green68b45042011-05-25 21:41:57 +0100212 * are his extensions okay with him closing? Eg he might be a mux
213 * parent and just his ch1 aspect is closing?
214 */
215
Andy Green68b45042011-05-25 21:41:57 +0100216 for (n = 0; n < wsi->count_active_extensions; n++) {
217 if (!wsi->active_extensions[n]->callback)
218 continue;
219
220 m = wsi->active_extensions[n]->callback(context,
221 wsi->active_extensions[n], wsi,
222 LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE,
223 wsi->active_extensions_user[n], NULL, 0);
224
225 /*
226 * if somebody vetoed actually closing him at this time....
227 * up to the extension to track the attempted close, let's
228 * just bail
229 */
230
231 if (m) {
Andy Green43db0452013-01-10 19:50:35 +0800232 lwsl_ext("extension vetoed close\n");
Andy Green68b45042011-05-25 21:41:57 +0100233 return;
234 }
235 }
236
Andy Green68b45042011-05-25 21:41:57 +0100237 /*
Andy Greenc44159f2011-03-07 07:08:18 +0000238 * flush any tx pending from extensions, since we may send close packet
239 * if there are problems with send, just nuke the connection
240 */
241
242 ret = 1;
243 while (ret == 1) {
244
245 /* default to nobody has more to spill */
246
247 ret = 0;
248 eff_buf.token = NULL;
249 eff_buf.token_len = 0;
250
251 /* show every extension the new incoming data */
252
253 for (n = 0; n < wsi->count_active_extensions; n++) {
254 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000255 wsi->protocol->owning_server,
256 wsi->active_extensions[n], wsi,
Andy Greenc44159f2011-03-07 07:08:18 +0000257 LWS_EXT_CALLBACK_FLUSH_PENDING_TX,
258 wsi->active_extensions_user[n], &eff_buf, 0);
259 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800260 lwsl_ext("Extension reports fatal error\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000261 goto just_kill_connection;
262 }
263 if (m)
264 /*
265 * at least one extension told us he has more
266 * to spill, so we will go around again after
267 */
268 ret = 1;
269 }
270
271 /* assuming they left us something to send, send it */
272
273 if (eff_buf.token_len)
274 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
Andy Greenb5b23192013-02-11 17:13:32 +0800275 eff_buf.token_len)) {
276 lwsl_debug("close: ext spill failed\n");
Andy Greenc44159f2011-03-07 07:08:18 +0000277 goto just_kill_connection;
Andy Green0303db42013-01-17 14:46:43 +0800278 }
Andy Greenc44159f2011-03-07 07:08:18 +0000279 }
Andy Green3182ece2013-01-20 17:08:31 +0800280#endif
Andy Greenc44159f2011-03-07 07:08:18 +0000281
282 /*
Andy Greenda527df2011-03-07 07:08:12 +0000283 * signal we are closing, libsocket_write will
284 * add any necessary version-specific stuff. If the write fails,
285 * no worries we are closing anyway. If we didn't initiate this
286 * close, then our state has been changed to
287 * WSI_STATE_RETURNED_CLOSE_ALREADY and we will skip this.
288 *
289 * Likewise if it's a second call to close this connection after we
290 * sent the close indication to the peer already, we are in state
291 * WSI_STATE_AWAITING_CLOSE_ACK and will skip doing this a second time.
292 */
293
294 if (old_state == WSI_STATE_ESTABLISHED &&
295 reason != LWS_CLOSE_STATUS_NOSTATUS) {
Andy Green66a16f32011-05-24 22:07:45 +0100296
Andy Green43db0452013-01-10 19:50:35 +0800297 lwsl_debug("sending close indication...\n");
Andy Green66a16f32011-05-24 22:07:45 +0100298
Andy Greenfdd305a2013-02-11 14:32:48 +0800299 /* make valgrind happy */
Andy Greenb5b23192013-02-11 17:13:32 +0800300 memset(buf, 0, sizeof(buf));
301 n = libwebsocket_write(wsi,
302 &buf[LWS_SEND_BUFFER_PRE_PADDING + 2],
Andy Greenda527df2011-03-07 07:08:12 +0000303 0, LWS_WRITE_CLOSE);
304 if (!n) {
305 /*
306 * we have sent a nice protocol level indication we
307 * now wish to close, we should not send anything more
308 */
309
310 wsi->state = WSI_STATE_AWAITING_CLOSE_ACK;
311
Andy Greenb5b23192013-02-11 17:13:32 +0800312 /*
313 * ...and we should wait for a reply for a bit
314 * out of politeness
315 */
Andy Greenda527df2011-03-07 07:08:12 +0000316
317 libwebsocket_set_timeout(wsi,
Andy Green0303db42013-01-17 14:46:43 +0800318 PENDING_TIMEOUT_CLOSE_ACK, 1);
Andy Greenda527df2011-03-07 07:08:12 +0000319
Andy Green43db0452013-01-10 19:50:35 +0800320 lwsl_debug("sent close indication, awaiting ack\n");
Andy Greenda527df2011-03-07 07:08:12 +0000321
322 return;
323 }
324
Andy Greenb5b23192013-02-11 17:13:32 +0800325 lwsl_info("close: sending close packet failed, hanging up\n");
Andy Green0303db42013-01-17 14:46:43 +0800326
Andy Greenda527df2011-03-07 07:08:12 +0000327 /* else, the send failed and we should just hang up */
328 }
329
Andy Green3182ece2013-01-20 17:08:31 +0800330#ifndef LWS_NO_EXTENSIONS
Andy Greenc44159f2011-03-07 07:08:18 +0000331just_kill_connection:
Andy Green3182ece2013-01-20 17:08:31 +0800332#endif
Andy Green66a16f32011-05-24 22:07:45 +0100333
Andy Greenb5b23192013-02-11 17:13:32 +0800334 lwsl_debug("close: just_kill_connection\n");
Andy Green66a16f32011-05-24 22:07:45 +0100335
Andy Greenda527df2011-03-07 07:08:12 +0000336 /*
337 * we won't be servicing or receiving anything further from this guy
Andy Greendfb23042013-01-17 12:26:48 +0800338 * delete socket from the internal poll list if still present
Andy Greenda527df2011-03-07 07:08:12 +0000339 */
Andy Green4b6fbe12011-02-14 08:03:48 +0000340
Andy Greendfb23042013-01-17 12:26:48 +0800341 remove_wsi_socket_from_fds(context, wsi);
Andy Green4b6fbe12011-02-14 08:03:48 +0000342
Andy Green251f6fa2010-11-03 11:13:06 +0000343 wsi->state = WSI_STATE_DEAD_SOCKET;
344
Andy Greenb5b23192013-02-11 17:13:32 +0800345 if ((old_state == WSI_STATE_ESTABLISHED ||
346 wsi->mode == LWS_CONNMODE_WS_SERVING ||
Andy Green3ee9b312013-02-12 12:52:39 +0800347 wsi->mode == LWS_CONNMODE_WS_CLIENT)) {
348
349 if (wsi->u.ws.rx_user_buffer) {
350 free(wsi->u.ws.rx_user_buffer);
351 wsi->u.ws.rx_user_buffer = NULL;
352 }
353 if (wsi->u.ws.rxflow_buffer) {
354 free(wsi->u.ws.rxflow_buffer);
355 wsi->u.ws.rxflow_buffer = NULL;
356 }
Andy Green54495112013-02-06 21:10:16 +0900357 }
358
Andy Green4b6fbe12011-02-14 08:03:48 +0000359 /* tell the user it's all over for this guy */
360
Andy Greend4302732011-02-28 07:45:29 +0000361 if (wsi->protocol && wsi->protocol->callback &&
Andy Green6ee372f2012-04-09 15:09:01 +0800362 ((old_state == WSI_STATE_ESTABLISHED) ||
363 (old_state == WSI_STATE_RETURNED_CLOSE_ALREADY) ||
364 (old_state == WSI_STATE_AWAITING_CLOSE_ACK))) {
Andy Green43db0452013-01-10 19:50:35 +0800365 lwsl_debug("calling back CLOSED\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000366 wsi->protocol->callback(context, wsi, LWS_CALLBACK_CLOSED,
Andy Greene77ddd82010-11-13 10:03:47 +0000367 wsi->user_space, NULL, 0);
Andy Greencc012472011-11-07 19:53:23 +0800368 } else
Andy Greenb5b23192013-02-11 17:13:32 +0800369 lwsl_debug("not calling back closed\n");
Andy Green251f6fa2010-11-03 11:13:06 +0000370
Andy Green3182ece2013-01-20 17:08:31 +0800371#ifndef LWS_NO_EXTENSIONS
Andy Greenef660a92011-03-06 10:29:38 +0000372 /* deallocate any active extension contexts */
373
374 for (n = 0; n < wsi->count_active_extensions; n++) {
375 if (!wsi->active_extensions[n]->callback)
376 continue;
377
Andy Green46c2ea02011-03-22 09:04:01 +0000378 wsi->active_extensions[n]->callback(context,
379 wsi->active_extensions[n], wsi,
380 LWS_EXT_CALLBACK_DESTROY,
381 wsi->active_extensions_user[n], NULL, 0);
Andy Greenef660a92011-03-06 10:29:38 +0000382
383 free(wsi->active_extensions_user[n]);
384 }
385
Andy Greena41314f2011-05-23 10:00:03 +0100386 /*
387 * inform all extensions in case they tracked this guy out of band
388 * even though not active on him specifically
389 */
390
391 ext = context->extensions;
392 while (ext && ext->callback) {
393 ext->callback(context, ext, wsi,
394 LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING,
395 NULL, NULL, 0);
396 ext++;
397 }
Andy Green3182ece2013-01-20 17:08:31 +0800398#endif
Andy Greena41314f2011-05-23 10:00:03 +0100399
Andy Green43db0452013-01-10 19:50:35 +0800400/* lwsl_info("closing fd=%d\n", wsi->sock); */
Andy Green251f6fa2010-11-03 11:13:06 +0000401
Andy Green3faa9c72010-11-08 17:03:03 +0000402#ifdef LWS_OPENSSL_SUPPORT
Andy Green90c7cbc2011-01-27 06:26:52 +0000403 if (wsi->ssl) {
Andy Green3faa9c72010-11-08 17:03:03 +0000404 n = SSL_get_fd(wsi->ssl);
405 SSL_shutdown(wsi->ssl);
Andy Green3fc2c652013-01-14 15:35:02 +0800406 compatible_close(n);
Andy Green3faa9c72010-11-08 17:03:03 +0000407 SSL_free(wsi->ssl);
408 } else {
409#endif
Andy Green0303db42013-01-17 14:46:43 +0800410 if (wsi->sock) {
411 n = shutdown(wsi->sock, SHUT_RDWR);
412 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800413 lwsl_debug("closing: shutdown returned %d\n",
414 errno);
Andy Green3fc2c652013-01-14 15:35:02 +0800415
Andy Green0303db42013-01-17 14:46:43 +0800416 n = compatible_close(wsi->sock);
417 if (n)
Andy Greenb5b23192013-02-11 17:13:32 +0800418 lwsl_debug("closing: close returned %d\n",
419 errno);
Andy Green0303db42013-01-17 14:46:43 +0800420 }
Andy Green3faa9c72010-11-08 17:03:03 +0000421#ifdef LWS_OPENSSL_SUPPORT
422 }
423#endif
Andy Greenb5b23192013-02-11 17:13:32 +0800424 if (wsi->protocol && wsi->protocol->per_session_data_size &&
425 wsi->user_space) /* user code may own */
Andy Green4f3943a2010-11-12 10:44:16 +0000426 free(wsi->user_space);
427
Andy Green251f6fa2010-11-03 11:13:06 +0000428 free(wsi);
429}
430
Andy Green07034092011-02-13 08:37:12 +0000431/**
432 * libwebsockets_get_peer_addresses() - Get client address information
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800433 * @context: Libwebsockets context
434 * @wsi: Local struct libwebsocket associated with
Andy Green07034092011-02-13 08:37:12 +0000435 * @fd: Connection socket descriptor
436 * @name: Buffer to take client address name
437 * @name_len: Length of client address name buffer
438 * @rip: Buffer to take client address IP qotted quad
439 * @rip_len: Length of client address IP buffer
440 *
441 * This function fills in @name and @rip with the name and IP of
Andy Green6ee372f2012-04-09 15:09:01 +0800442 * the client connected with socket descriptor @fd. Names may be
443 * truncated if there is not enough room. If either cannot be
444 * determined, they will be returned as valid zero-length strings.
Andy Green07034092011-02-13 08:37:12 +0000445 */
446
447void
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800448libwebsockets_get_peer_addresses(struct libwebsocket_context *context,
449 struct libwebsocket *wsi, int fd, char *name, int name_len,
Andy Green07034092011-02-13 08:37:12 +0000450 char *rip, int rip_len)
451{
452 unsigned int len;
453 struct sockaddr_in sin;
454 struct hostent *host;
455 struct hostent *host1;
456 char ip[128];
Andy Greenf92def72011-03-09 15:02:20 +0000457 unsigned char *p;
Andy Green07034092011-02-13 08:37:12 +0000458 int n;
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800459 int ret = -1;
David Galeanocb193682013-01-09 15:29:00 +0800460#ifdef AF_LOCAL
Andy Greenb5b23192013-02-11 17:13:32 +0800461 struct sockaddr_un *un;
David Galeanocb193682013-01-09 15:29:00 +0800462#endif
Andy Green07034092011-02-13 08:37:12 +0000463
464 rip[0] = '\0';
465 name[0] = '\0';
466
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800467 lws_latency_pre(context, wsi);
468
Andy Greenb5b23192013-02-11 17:13:32 +0800469 len = sizeof(sin);
Andy Green07034092011-02-13 08:37:12 +0000470 if (getpeername(fd, (struct sockaddr *) &sin, &len) < 0) {
471 perror("getpeername");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800472 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000473 }
Andy Green6ee372f2012-04-09 15:09:01 +0800474
Andy Greenb5b23192013-02-11 17:13:32 +0800475 host = gethostbyaddr((char *) &sin.sin_addr, sizeof(sin.sin_addr),
Andy Green07034092011-02-13 08:37:12 +0000476 AF_INET);
477 if (host == NULL) {
478 perror("gethostbyaddr");
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800479 goto bail;
Andy Green07034092011-02-13 08:37:12 +0000480 }
481
482 strncpy(name, host->h_name, name_len);
483 name[name_len - 1] = '\0';
484
485 host1 = gethostbyname(host->h_name);
486 if (host1 == NULL)
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800487 goto bail;
Andy Greenf92def72011-03-09 15:02:20 +0000488 p = (unsigned char *)host1;
Andy Green07034092011-02-13 08:37:12 +0000489 n = 0;
490 while (p != NULL) {
Andy Greenf92def72011-03-09 15:02:20 +0000491 p = (unsigned char *)host1->h_addr_list[n++];
Andy Green07034092011-02-13 08:37:12 +0000492 if (p == NULL)
493 continue;
Peter Hinzbb45a902011-03-10 18:14:01 +0000494 if ((host1->h_addrtype != AF_INET)
495#ifdef AF_LOCAL
496 && (host1->h_addrtype != AF_LOCAL)
497#endif
498 )
Andy Green07034092011-02-13 08:37:12 +0000499 continue;
500
Andy Green7627af52011-03-09 15:13:52 +0000501 if (host1->h_addrtype == AF_INET)
502 sprintf(ip, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
Peter Hinzbb45a902011-03-10 18:14:01 +0000503#ifdef AF_LOCAL
Andy Green7627af52011-03-09 15:13:52 +0000504 else {
505 un = (struct sockaddr_un *)p;
Andy Green6ee372f2012-04-09 15:09:01 +0800506 strncpy(ip, un->sun_path, sizeof(ip) - 1);
Andy Green7627af52011-03-09 15:13:52 +0000507 ip[sizeof(ip) - 1] = '\0';
508 }
Peter Hinzbb45a902011-03-10 18:14:01 +0000509#endif
Andy Green07034092011-02-13 08:37:12 +0000510 p = NULL;
511 strncpy(rip, ip, rip_len);
512 rip[rip_len - 1] = '\0';
513 }
Andy Greenaaf0b9f2013-01-30 08:12:20 +0800514
515 ret = 0;
516bail:
517 lws_latency(context, wsi, "libwebsockets_get_peer_addresses", ret, 1);
Andy Green07034092011-02-13 08:37:12 +0000518}
Andy Green9f990342011-02-12 11:57:45 +0000519
Peter Hinz56885f32011-03-02 22:03:47 +0000520int libwebsockets_get_random(struct libwebsocket_context *context,
521 void *buf, int len)
522{
523 int n;
Aaron Zinman4550f1d2013-01-10 12:35:18 +0800524 char *p = (char *)buf;
Peter Hinz56885f32011-03-02 22:03:47 +0000525
526#ifdef WIN32
527 for (n = 0; n < len; n++)
528 p[n] = (unsigned char)rand();
529#else
530 n = read(context->fd_random, p, len);
531#endif
532
533 return n;
534}
535
Andy Greena690cd02013-02-09 12:25:31 +0800536int lws_set_socket_options(struct libwebsocket_context *context, int fd)
537{
538 int optval = 1;
539 socklen_t optlen = sizeof(optval);
540#ifdef WIN32
541 unsigned long optl = 0;
542#endif
543#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
544 struct protoent *tcp_proto;
545#endif
546
547 if (context->ka_time) {
548 /* enable keepalive on this socket */
549 optval = 1;
550 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
551 (const void *)&optval, optlen) < 0)
552 return 1;
553
Andy Greena47865f2013-02-10 09:39:47 +0800554#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
555
556 /*
557 * didn't find a way to set these per-socket, need to
558 * tune kernel systemwide values
559 */
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100560#elif WIN32
561 {
562 DWORD dwBytesRet;
563 struct tcp_keepalive alive;
564 alive.onoff = TRUE;
565 alive.keepalivetime = context->ka_time;
566 alive.keepaliveinterval = context->ka_interval;
Andy Greena47865f2013-02-10 09:39:47 +0800567
Joakim Soderberg63ff1202013-02-11 17:52:23 +0100568 if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
569 NULL, 0, &dwBytesRet, NULL, NULL))
570 return 1;
571 }
Andy Greena47865f2013-02-10 09:39:47 +0800572#else
Andy Greena690cd02013-02-09 12:25:31 +0800573 /* set the keepalive conditions we want on it too */
574 optval = context->ka_time;
575 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPIDLE,
576 (const void *)&optval, optlen) < 0)
577 return 1;
578
579 optval = context->ka_probes;
580 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPINTVL,
581 (const void *)&optval, optlen) < 0)
582 return 1;
583
584 optval = context->ka_interval;
585 if (setsockopt(fd, IPPROTO_IP, TCP_KEEPCNT,
586 (const void *)&optval, optlen) < 0)
587 return 1;
Andy Greena47865f2013-02-10 09:39:47 +0800588#endif
Andy Greena690cd02013-02-09 12:25:31 +0800589 }
590
591 /* Disable Nagle */
592 optval = 1;
593#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
594 setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen);
595#else
596 tcp_proto = getprotobyname("TCP");
597 setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen);
598#endif
599
600 /* We are nonblocking... */
601#ifdef WIN32
602 ioctlsocket(fd, FIONBIO, &optl);
603#else
604 fcntl(fd, F_SETFL, O_NONBLOCK);
605#endif
606
607 return 0;
608}
609
Andy Green95a7b5d2011-03-06 10:29:39 +0000610int lws_send_pipe_choked(struct libwebsocket *wsi)
611{
612 struct pollfd fds;
613
614 fds.fd = wsi->sock;
615 fds.events = POLLOUT;
616 fds.revents = 0;
617
618 if (poll(&fds, 1, 0) != 1)
619 return 1;
620
621 if ((fds.revents & POLLOUT) == 0)
622 return 1;
623
624 /* okay to send another packet without blocking */
625
626 return 0;
627}
628
Andy Greena41314f2011-05-23 10:00:03 +0100629int
Andy Green3b84c002011-03-06 13:14:42 +0000630lws_handle_POLLOUT_event(struct libwebsocket_context *context,
631 struct libwebsocket *wsi, struct pollfd *pollfd)
632{
Andy Green3b84c002011-03-06 13:14:42 +0000633 int n;
Andy Green6f520a52013-01-29 17:57:39 +0800634
Andy Green3182ece2013-01-20 17:08:31 +0800635#ifndef LWS_NO_EXTENSIONS
636 struct lws_tokens eff_buf;
Andy Green3b84c002011-03-06 13:14:42 +0000637 int ret;
638 int m;
Andy Greena41314f2011-05-23 10:00:03 +0100639 int handled = 0;
Andy Green3b84c002011-03-06 13:14:42 +0000640
Andy Greena41314f2011-05-23 10:00:03 +0100641 for (n = 0; n < wsi->count_active_extensions; n++) {
642 if (!wsi->active_extensions[n]->callback)
643 continue;
644
645 m = wsi->active_extensions[n]->callback(context,
646 wsi->active_extensions[n], wsi,
647 LWS_EXT_CALLBACK_IS_WRITEABLE,
648 wsi->active_extensions_user[n], NULL, 0);
649 if (m > handled)
650 handled = m;
651 }
652
653 if (handled == 1)
654 goto notify_action;
655
656 if (!wsi->extension_data_pending || handled == 2)
Andy Green3b84c002011-03-06 13:14:42 +0000657 goto user_service;
658
659 /*
660 * check in on the active extensions, see if they
661 * had pending stuff to spill... they need to get the
662 * first look-in otherwise sequence will be disordered
663 *
664 * NULL, zero-length eff_buf means just spill pending
665 */
666
667 ret = 1;
668 while (ret == 1) {
669
670 /* default to nobody has more to spill */
671
672 ret = 0;
673 eff_buf.token = NULL;
674 eff_buf.token_len = 0;
675
676 /* give every extension a chance to spill */
677
678 for (n = 0; n < wsi->count_active_extensions; n++) {
679 m = wsi->active_extensions[n]->callback(
Andy Green46c2ea02011-03-22 09:04:01 +0000680 wsi->protocol->owning_server,
681 wsi->active_extensions[n], wsi,
Andy Green3b84c002011-03-06 13:14:42 +0000682 LWS_EXT_CALLBACK_PACKET_TX_PRESEND,
683 wsi->active_extensions_user[n], &eff_buf, 0);
684 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800685 lwsl_err("ext reports fatal error\n");
Andy Green3b84c002011-03-06 13:14:42 +0000686 return -1;
687 }
688 if (m)
689 /*
690 * at least one extension told us he has more
691 * to spill, so we will go around again after
692 */
693 ret = 1;
694 }
695
696 /* assuming they gave us something to send, send it */
697
698 if (eff_buf.token_len) {
699 if (lws_issue_raw(wsi, (unsigned char *)eff_buf.token,
700 eff_buf.token_len))
701 return -1;
702 } else
703 continue;
704
705 /* no extension has more to spill */
706
707 if (!ret)
708 continue;
709
710 /*
711 * There's more to spill from an extension, but we just sent
712 * something... did that leave the pipe choked?
713 */
714
715 if (!lws_send_pipe_choked(wsi))
716 /* no we could add more */
717 continue;
718
Andy Green43db0452013-01-10 19:50:35 +0800719 lwsl_info("choked in POLLOUT service\n");
Andy Green3b84c002011-03-06 13:14:42 +0000720
721 /*
722 * Yes, he's choked. Leave the POLLOUT masked on so we will
723 * come back here when he is unchoked. Don't call the user
724 * callback to enforce ordering of spilling, he'll get called
725 * when we come back here and there's nothing more to spill.
726 */
727
728 return 0;
729 }
730
731 wsi->extension_data_pending = 0;
732
733user_service:
Andy Green3182ece2013-01-20 17:08:31 +0800734#endif
Andy Green3b84c002011-03-06 13:14:42 +0000735 /* one shot */
736
Andy Greena41314f2011-05-23 10:00:03 +0100737 if (pollfd) {
738 pollfd->events &= ~POLLOUT;
Andy Green3b84c002011-03-06 13:14:42 +0000739
Andy Greena41314f2011-05-23 10:00:03 +0100740 /* external POLL support via protocol 0 */
741 context->protocols[0].callback(context, wsi,
742 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +0800743 wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
Andy Greena41314f2011-05-23 10:00:03 +0100744 }
Andy Green3182ece2013-01-20 17:08:31 +0800745#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100746notify_action:
Andy Green3182ece2013-01-20 17:08:31 +0800747#endif
Andy Green3b84c002011-03-06 13:14:42 +0000748
Andy Green9e4c2b62011-03-07 20:47:39 +0000749 if (wsi->mode == LWS_CONNMODE_WS_CLIENT)
750 n = LWS_CALLBACK_CLIENT_WRITEABLE;
751 else
752 n = LWS_CALLBACK_SERVER_WRITEABLE;
753
Andy Greenb8b247d2013-01-22 07:20:08 +0800754 return user_callback_handle_rxflow(wsi->protocol->callback, context,
Andy Greenb5b23192013-02-11 17:13:32 +0800755 wsi, (enum libwebsocket_callback_reasons) n,
756 wsi->user_space, NULL, 0);
Andy Green3b84c002011-03-06 13:14:42 +0000757}
758
759
760
Andy Greena41314f2011-05-23 10:00:03 +0100761void
762libwebsocket_service_timeout_check(struct libwebsocket_context *context,
763 struct libwebsocket *wsi, unsigned int sec)
764{
Andy Green3182ece2013-01-20 17:08:31 +0800765#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +0100766 int n;
767
768 /*
769 * if extensions want in on it (eg, we are a mux parent)
770 * give them a chance to service child timeouts
771 */
772
773 for (n = 0; n < wsi->count_active_extensions; n++)
774 wsi->active_extensions[n]->callback(
775 context, wsi->active_extensions[n],
776 wsi, LWS_EXT_CALLBACK_1HZ,
777 wsi->active_extensions_user[n], NULL, sec);
778
Andy Green3182ece2013-01-20 17:08:31 +0800779#endif
Andy Greena41314f2011-05-23 10:00:03 +0100780 if (!wsi->pending_timeout)
781 return;
Andy Green6ee372f2012-04-09 15:09:01 +0800782
Andy Greena41314f2011-05-23 10:00:03 +0100783 /*
784 * if we went beyond the allowed time, kill the
785 * connection
786 */
787
788 if (sec > wsi->pending_timeout_limit) {
Andy Green43db0452013-01-10 19:50:35 +0800789 lwsl_info("TIMEDOUT WAITING\n");
Andy Greena41314f2011-05-23 10:00:03 +0100790 libwebsocket_close_and_free_session(context,
791 wsi, LWS_CLOSE_STATUS_NOSTATUS);
792 }
793}
794
Andy Green9f990342011-02-12 11:57:45 +0000795/**
796 * libwebsocket_service_fd() - Service polled socket with something waiting
Peter Hinz56885f32011-03-02 22:03:47 +0000797 * @context: Websocket context
Andy Green9f990342011-02-12 11:57:45 +0000798 * @pollfd: The pollfd entry describing the socket fd and which events
Andy Green6ee372f2012-04-09 15:09:01 +0800799 * happened.
Andy Green9f990342011-02-12 11:57:45 +0000800 *
Andy Green75006172013-01-22 12:32:11 +0800801 * This function takes a pollfd that has POLLIN or POLLOUT activity and
Andy Greenb5b23192013-02-11 17:13:32 +0800802 * services it according to the state of the associated
803 * struct libwebsocket.
Andy Green75006172013-01-22 12:32:11 +0800804 *
805 * The one call deals with all "service" that might happen on a socket
806 * including listen accepts, http files as well as websocket protocol.
Andy Green9f990342011-02-12 11:57:45 +0000807 */
808
809int
Peter Hinz56885f32011-03-02 22:03:47 +0000810libwebsocket_service_fd(struct libwebsocket_context *context,
Andy Green0d338332011-02-12 11:57:43 +0000811 struct pollfd *pollfd)
Andy Greenb45993c2010-12-18 15:13:50 +0000812{
Andy Greena1ce6be2013-01-18 11:43:21 +0800813 struct libwebsocket *wsi;
Andy Greenb45993c2010-12-18 15:13:50 +0000814 int n;
Andy Green0d338332011-02-12 11:57:43 +0000815 int m;
Andy Greenf0b79e22013-02-10 10:46:45 +0800816 int listen_socket_fds_index = 0;
Andy Greena71eafc2011-02-14 17:59:43 +0000817 struct timeval tv;
Andy Green6f520a52013-01-29 17:57:39 +0800818
Andy Green3182ece2013-01-20 17:08:31 +0800819#ifndef LWS_NO_EXTENSIONS
Andy Green2366b1c2011-03-06 13:15:31 +0000820 int more = 1;
Andy Green3182ece2013-01-20 17:08:31 +0800821#endif
Andy Green98a717c2011-03-06 13:14:15 +0000822 struct lws_tokens eff_buf;
Andy Greenf0b79e22013-02-10 10:46:45 +0800823
824 if (context->listen_service_fd)
825 listen_socket_fds_index = context->lws_lookup[
826 context->listen_service_fd]->position_in_fds_table;
827
Andy Greena71eafc2011-02-14 17:59:43 +0000828 /*
829 * you can call us with pollfd = NULL to just allow the once-per-second
830 * global timeout checks; if less than a second since the last check
831 * it returns immediately then.
832 */
833
834 gettimeofday(&tv, NULL);
835
Peter Hinz56885f32011-03-02 22:03:47 +0000836 if (context->last_timeout_check_s != tv.tv_sec) {
837 context->last_timeout_check_s = tv.tv_sec;
Andy Greena71eafc2011-02-14 17:59:43 +0000838
Joakim Soderberg4c531232013-02-06 15:26:58 +0900839 #ifndef WIN32
Andy Green24cba922013-01-19 13:56:10 +0800840 /* if our parent went down, don't linger around */
Andy Greenb5b23192013-02-11 17:13:32 +0800841 if (context->started_with_parent &&
842 kill(context->started_with_parent, 0) < 0)
Andy Green24cba922013-01-19 13:56:10 +0800843 kill(getpid(), SIGTERM);
Joakim Soderberg4c531232013-02-06 15:26:58 +0900844 #endif
Andy Green24cba922013-01-19 13:56:10 +0800845
Andy Greena71eafc2011-02-14 17:59:43 +0000846 /* global timeout check once per second */
847
Peter Hinz56885f32011-03-02 22:03:47 +0000848 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +0800849 struct libwebsocket *new_wsi =
850 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +0800851 if (!new_wsi)
852 continue;
853 libwebsocket_service_timeout_check(context,
854 new_wsi, tv.tv_sec);
Andy Greena71eafc2011-02-14 17:59:43 +0000855 }
856 }
857
858 /* just here for timeout management? */
859
860 if (pollfd == NULL)
861 return 0;
862
863 /* no, here to service a socket descriptor */
864
Andy Green65b0e912013-01-16 07:59:47 +0800865 /*
866 * deal with listen service piggybacking
867 * every listen_service_modulo services of other fds, we
868 * sneak one in to service the listen socket if there's anything waiting
869 *
870 * To handle connection storms, as found in ab, if we previously saw a
871 * pending connection here, it causes us to check again next time.
872 */
873
Andy Greenb5b23192013-02-11 17:13:32 +0800874 if (context->listen_service_fd && pollfd !=
875 &context->fds[listen_socket_fds_index]) {
Andy Green65b0e912013-01-16 07:59:47 +0800876 context->listen_service_count++;
877 if (context->listen_service_extraseen ||
Andy Greenb5b23192013-02-11 17:13:32 +0800878 context->listen_service_count ==
879 context->listen_service_modulo) {
Andy Green65b0e912013-01-16 07:59:47 +0800880 context->listen_service_count = 0;
881 m = 1;
882 if (context->listen_service_extraseen > 5)
883 m = 2;
884 while (m--) {
Andy Greenb5b23192013-02-11 17:13:32 +0800885 /*
886 * even with extpoll, we prepared this
887 * internal fds for listen
888 */
889 n = poll(&context->fds[listen_socket_fds_index],
890 1, 0);
891 if (n > 0) { /* there's a conn waiting for us */
892 libwebsocket_service_fd(context,
893 &context->
894 fds[listen_socket_fds_index]);
Andy Green65b0e912013-01-16 07:59:47 +0800895 context->listen_service_extraseen++;
896 } else {
897 if (context->listen_service_extraseen)
Andy Greenb5b23192013-02-11 17:13:32 +0800898 context->
899 listen_service_extraseen--;
Andy Green65b0e912013-01-16 07:59:47 +0800900 break;
901 }
902 }
903 }
904
905 }
906
907 /* okay, what we came here to do... */
908
Andy Greendfb23042013-01-17 12:26:48 +0800909 wsi = context->lws_lookup[pollfd->fd];
Andy Greend280b6e2013-01-15 13:40:23 +0800910 if (wsi == NULL) {
Andy Greendfb23042013-01-17 12:26:48 +0800911 if (pollfd->fd > 11)
Andy Greenb5b23192013-02-11 17:13:32 +0800912 lwsl_err("unexpected NULL wsi fd=%d fds_count=%d\n",
913 pollfd->fd, context->fds_count);
Andy Greenfa3f4052012-10-07 20:40:35 +0800914 return 0;
Andy Greend280b6e2013-01-15 13:40:23 +0800915 }
Andy Green8f037e42010-12-19 22:13:26 +0000916
Andy Green0d338332011-02-12 11:57:43 +0000917 switch (wsi->mode) {
Andy Greend280b6e2013-01-15 13:40:23 +0800918
Andy Greena1ce6be2013-01-18 11:43:21 +0800919#ifndef LWS_NO_SERVER
Andy Greend280b6e2013-01-15 13:40:23 +0800920 case LWS_CONNMODE_HTTP_SERVING:
Andy Green0d338332011-02-12 11:57:43 +0000921 case LWS_CONNMODE_SERVER_LISTENER:
Andy Greene2160712013-01-28 12:19:10 +0800922 case LWS_CONNMODE_SSL_ACK_PENDING:
Andy Greena1ce6be2013-01-18 11:43:21 +0800923 return lws_server_socket_service(context, wsi, pollfd);
924#endif
Andy Greenbe93fef2011-02-14 20:25:43 +0000925
Andy Green0d338332011-02-12 11:57:43 +0000926 case LWS_CONNMODE_WS_SERVING:
927 case LWS_CONNMODE_WS_CLIENT:
928
929 /* handle session socket closed */
930
931 if (pollfd->revents & (POLLERR | POLLHUP)) {
932
Andy Green43db0452013-01-10 19:50:35 +0800933 lwsl_debug("Session Socket %p (fd=%d) dead\n",
Andy Green0d338332011-02-12 11:57:43 +0000934 (void *)wsi, pollfd->fd);
935
Peter Hinz56885f32011-03-02 22:03:47 +0000936 libwebsocket_close_and_free_session(context, wsi,
Andy Green687b0182011-02-26 11:04:01 +0000937 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800938 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000939 }
940
Andy Green0d338332011-02-12 11:57:43 +0000941 /* the guy requested a callback when it was OK to write */
942
Andy Greenda527df2011-03-07 07:08:12 +0000943 if ((pollfd->revents & POLLOUT) &&
944 wsi->state == WSI_STATE_ESTABLISHED)
945 if (lws_handle_POLLOUT_event(context, wsi,
946 pollfd) < 0) {
Andy Green0878b9e2013-02-13 11:44:20 +0800947 lwsl_info("libwebsocket_service_fd: closing\n");
Andy Greenda527df2011-03-07 07:08:12 +0000948 libwebsocket_close_and_free_session(
Andy Greenb0593712013-02-15 22:32:53 +0800949 context, wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800950 return 0;
Andy Green3b84c002011-03-06 13:14:42 +0000951 }
Andy Green0d338332011-02-12 11:57:43 +0000952
Andy Green0d338332011-02-12 11:57:43 +0000953
954 /* any incoming data ready? */
955
956 if (!(pollfd->revents & POLLIN))
957 break;
958
Andy Greenb45993c2010-12-18 15:13:50 +0000959#ifdef LWS_OPENSSL_SUPPORT
David Galeano7ffbe1b2013-01-10 10:35:32 +0800960read_pending:
Andy Green467c7ef2013-01-30 12:28:34 +0800961 if (wsi->ssl) {
Andy Greene84652c2013-02-08 13:01:02 +0800962 eff_buf.token_len = SSL_read(wsi->ssl,
963 context->service_buffer,
Andy Greenb5b23192013-02-11 17:13:32 +0800964 sizeof(context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +0800965 if (!eff_buf.token_len) {
966 n = SSL_get_error(wsi->ssl, eff_buf.token_len);
Andy Greene84652c2013-02-08 13:01:02 +0800967 lwsl_err("SSL_read returned 0 with reason %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +0800968 ERR_error_string(n,
969 (char *)context->service_buffer));
Andy Green467c7ef2013-01-30 12:28:34 +0800970 }
971 } else
Andy Greenb45993c2010-12-18 15:13:50 +0000972#endif
Andy Greene84652c2013-02-08 13:01:02 +0800973 eff_buf.token_len = recv(pollfd->fd,
Andy Greenb5b23192013-02-11 17:13:32 +0800974 context->service_buffer,
975 sizeof(context->service_buffer), 0);
Andy Greenb45993c2010-12-18 15:13:50 +0000976
Andy Green98a717c2011-03-06 13:14:15 +0000977 if (eff_buf.token_len < 0) {
Andy Green43db0452013-01-10 19:50:35 +0800978 lwsl_debug("Socket read returned %d\n",
Andy Green98a717c2011-03-06 13:14:15 +0000979 eff_buf.token_len);
Alon Levydc93b7f2012-10-19 11:21:57 +0200980 if (errno != EINTR && errno != EAGAIN)
Andy Green6ee372f2012-04-09 15:09:01 +0800981 libwebsocket_close_and_free_session(context,
982 wsi, LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +0800983 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000984 }
Andy Green98a717c2011-03-06 13:14:15 +0000985 if (!eff_buf.token_len) {
Andy Greenb5b23192013-02-11 17:13:32 +0800986 lwsl_info("closing connection due to 0 length read\n");
Peter Hinz56885f32011-03-02 22:03:47 +0000987 libwebsocket_close_and_free_session(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +0800988 LWS_CLOSE_STATUS_NOSTATUS);
Andy Greenfa3f4052012-10-07 20:40:35 +0800989 return 0;
Andy Greenb45993c2010-12-18 15:13:50 +0000990 }
991
Andy Green98a717c2011-03-06 13:14:15 +0000992 /*
993 * give any active extensions a chance to munge the buffer
994 * before parse. We pass in a pointer to an lws_tokens struct
995 * prepared with the default buffer and content length that's in
996 * there. Rather than rewrite the default buffer, extensions
997 * that expect to grow the buffer can adapt .token to
998 * point to their own per-connection buffer in the extension
999 * user allocation. By default with no extensions or no
1000 * extension callback handling, just the normal input buffer is
1001 * used then so it is efficient.
1002 */
Andy Greenb45993c2010-12-18 15:13:50 +00001003
Andy Greene84652c2013-02-08 13:01:02 +08001004 eff_buf.token = (char *)context->service_buffer;
Andy Green3182ece2013-01-20 17:08:31 +08001005#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001006 more = 1;
1007 while (more) {
Andy Green0d338332011-02-12 11:57:43 +00001008
Andy Green98a717c2011-03-06 13:14:15 +00001009 more = 0;
1010
1011 for (n = 0; n < wsi->count_active_extensions; n++) {
Andy Green46c2ea02011-03-22 09:04:01 +00001012 m = wsi->active_extensions[n]->callback(context,
1013 wsi->active_extensions[n], wsi,
Andy Green98a717c2011-03-06 13:14:15 +00001014 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE,
Andy Green46c2ea02011-03-22 09:04:01 +00001015 wsi->active_extensions_user[n],
1016 &eff_buf, 0);
Andy Green98a717c2011-03-06 13:14:15 +00001017 if (m < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001018 lwsl_ext(
Andy Green6ee372f2012-04-09 15:09:01 +08001019 "Extension reports fatal error\n");
1020 libwebsocket_close_and_free_session(
1021 context, wsi,
1022 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green040d2ef2013-01-16 13:40:43 +08001023 return 0;
Andy Green98a717c2011-03-06 13:14:15 +00001024 }
1025 if (m)
1026 more = 1;
1027 }
Andy Green3182ece2013-01-20 17:08:31 +08001028#endif
Andy Green98a717c2011-03-06 13:14:15 +00001029 /* service incoming data */
1030
1031 if (eff_buf.token_len) {
1032 n = libwebsocket_read(context, wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001033 (unsigned char *)eff_buf.token,
1034 eff_buf.token_len);
Andy Green98a717c2011-03-06 13:14:15 +00001035 if (n < 0)
1036 /* we closed wsi */
Andy Green040d2ef2013-01-16 13:40:43 +08001037 return 0;
Andy Green98a717c2011-03-06 13:14:15 +00001038 }
Andy Green3182ece2013-01-20 17:08:31 +08001039#ifndef LWS_NO_EXTENSIONS
Andy Green98a717c2011-03-06 13:14:15 +00001040 eff_buf.token = NULL;
1041 eff_buf.token_len = 0;
1042 }
Andy Green3182ece2013-01-20 17:08:31 +08001043#endif
David Galeano7ffbe1b2013-01-10 10:35:32 +08001044
1045#ifdef LWS_OPENSSL_SUPPORT
1046 if (wsi->ssl && SSL_pending(wsi->ssl))
1047 goto read_pending;
1048#endif
Andy Green98a717c2011-03-06 13:14:15 +00001049 break;
Andy Green76f61e72013-01-16 11:53:05 +08001050
1051 default:
Andy Green03674a62013-01-16 11:47:40 +08001052#ifdef LWS_NO_CLIENT
1053 break;
1054#else
Andy Green76f61e72013-01-16 11:53:05 +08001055 return lws_client_socket_service(context, wsi, pollfd);
Andy Green03674a62013-01-16 11:47:40 +08001056#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001057 }
1058
1059 return 0;
1060}
1061
Andy Green0d338332011-02-12 11:57:43 +00001062
Andy Green6964bb52011-01-23 16:50:33 +00001063/**
1064 * libwebsocket_context_destroy() - Destroy the websocket context
Peter Hinz56885f32011-03-02 22:03:47 +00001065 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001066 *
1067 * This function closes any active connections and then frees the
1068 * context. After calling this, any further use of the context is
1069 * undefined.
1070 */
1071void
Peter Hinz56885f32011-03-02 22:03:47 +00001072libwebsocket_context_destroy(struct libwebsocket_context *context)
Andy Green6964bb52011-01-23 16:50:33 +00001073{
Andy Green3182ece2013-01-20 17:08:31 +08001074#ifndef LWS_NO_EXTENSIONS
Andy Green0d338332011-02-12 11:57:43 +00001075 int n;
1076 int m;
Andy Greena41314f2011-05-23 10:00:03 +01001077 struct libwebsocket_extension *ext;
Andy Greena7109e62013-02-11 12:05:54 +08001078 struct libwebsocket_protocols *protocol = context->protocols;
Andy Green6964bb52011-01-23 16:50:33 +00001079
Andy Greend636e352013-01-29 12:36:17 +08001080#ifdef LWS_LATENCY
1081 if (context->worst_latency_info[0])
1082 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1083#endif
1084
Andy Greendfb23042013-01-17 12:26:48 +08001085 for (n = 0; n < context->fds_count; n++) {
Andy Greenb5b23192013-02-11 17:13:32 +08001086 struct libwebsocket *wsi =
1087 context->lws_lookup[context->fds[n].fd];
Andy Greendfb23042013-01-17 12:26:48 +08001088 libwebsocket_close_and_free_session(context,
Andy Green7b922052013-02-11 11:43:05 +08001089 wsi, LWS_CLOSE_STATUS_NOSTATUS /* no protocol close */);
1090 n--;
Andy Greendfb23042013-01-17 12:26:48 +08001091 }
Andy Green6964bb52011-01-23 16:50:33 +00001092
Andy Greena41314f2011-05-23 10:00:03 +01001093 /*
1094 * give all extensions a chance to clean up any per-context
1095 * allocations they might have made
1096 */
1097
1098 ext = context->extensions;
1099 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT;
1100 if (context->listen_port)
1101 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT;
Paulo Roberto Urio1f680ab2012-06-04 08:40:28 +08001102 while (ext && ext->callback) {
Andy Greenb5b23192013-02-11 17:13:32 +08001103 ext->callback(context, ext, NULL,
1104 (enum libwebsocket_extension_callback_reasons)m,
1105 NULL, NULL, 0);
Andy Greena41314f2011-05-23 10:00:03 +01001106 ext++;
1107 }
Andy Greena7109e62013-02-11 12:05:54 +08001108
1109 /*
1110 * inform all the protocols that they are done and will have no more
1111 * callbacks
1112 */
1113
1114 while (protocol->callback) {
1115 protocol->callback(context, NULL, LWS_CALLBACK_PROTOCOL_DESTROY,
1116 NULL, NULL, 0);
1117 protocol++;
1118 }
1119
Andy Green3182ece2013-01-20 17:08:31 +08001120#endif
Andy Greena41314f2011-05-23 10:00:03 +01001121
Peter Hinz56885f32011-03-02 22:03:47 +00001122#ifdef WIN32
1123#else
1124 close(context->fd_random);
Andy Green6964bb52011-01-23 16:50:33 +00001125#endif
1126
Peter Hinz56885f32011-03-02 22:03:47 +00001127#ifdef LWS_OPENSSL_SUPPORT
1128 if (context->ssl_ctx)
1129 SSL_CTX_free(context->ssl_ctx);
1130 if (context->ssl_client_ctx)
1131 SSL_CTX_free(context->ssl_client_ctx);
Andy Greenad686392013-02-11 14:50:45 +08001132
1133 ERR_remove_state(0);
1134 ERR_free_strings();
1135 EVP_cleanup();
1136 CRYPTO_cleanup_all_ex_data();
Peter Hinz56885f32011-03-02 22:03:47 +00001137#endif
1138
Andy Green46596482013-02-11 11:04:01 +08001139 if (context->fds)
1140 free(context->fds);
1141 if (context->lws_lookup)
1142 free(context->lws_lookup);
1143
Peter Hinz56885f32011-03-02 22:03:47 +00001144 free(context);
1145
1146#ifdef WIN32
1147 WSACleanup();
1148#endif
Andy Green6964bb52011-01-23 16:50:33 +00001149}
1150
Andy Greend88146d2013-01-22 12:40:35 +08001151/**
Andy Greenb5b23192013-02-11 17:13:32 +08001152 * libwebsocket_context_user() - get the user data associated with the context
Andy Greend88146d2013-01-22 12:40:35 +08001153 * @context: Websocket context
1154 *
1155 * This returns the optional user allocation that can be attached to
1156 * the context the sockets live in at context_create time. It's a way
1157 * to let all sockets serviced in the same context share data without
1158 * using globals statics in the user code.
1159 */
Alon Levy0291eb32012-10-19 11:21:56 +02001160LWS_EXTERN void *
1161libwebsocket_context_user(struct libwebsocket_context *context)
1162{
Andy Greenb5b23192013-02-11 17:13:32 +08001163 return context->user_space;
Alon Levy0291eb32012-10-19 11:21:56 +02001164}
1165
Andy Green6964bb52011-01-23 16:50:33 +00001166/**
1167 * libwebsocket_service() - Service any pending websocket activity
Peter Hinz56885f32011-03-02 22:03:47 +00001168 * @context: Websocket context
Andy Green6964bb52011-01-23 16:50:33 +00001169 * @timeout_ms: Timeout for poll; 0 means return immediately if nothing needed
1170 * service otherwise block and service immediately, returning
1171 * after the timeout if nothing needed service.
1172 *
1173 * This function deals with any pending websocket traffic, for three
1174 * kinds of event. It handles these events on both server and client
1175 * types of connection the same.
1176 *
1177 * 1) Accept new connections to our context's server
1178 *
Andy Green6f520a52013-01-29 17:57:39 +08001179 * 2) Call the receive callback for incoming frame data received by
Andy Green6964bb52011-01-23 16:50:33 +00001180 * server or client connections.
1181 *
1182 * You need to call this service function periodically to all the above
1183 * functions to happen; if your application is single-threaded you can
1184 * just call it in your main event loop.
1185 *
1186 * Alternatively you can fork a new process that asynchronously handles
1187 * calling this service in a loop. In that case you are happy if this
1188 * call blocks your thread until it needs to take care of something and
1189 * would call it with a large nonzero timeout. Your loop then takes no
1190 * CPU while there is nothing happening.
1191 *
1192 * If you are calling it in a single-threaded app, you don't want it to
1193 * wait around blocking other things in your loop from happening, so you
1194 * would call it with a timeout_ms of 0, so it returns immediately if
1195 * nothing is pending, or as soon as it services whatever was pending.
1196 */
1197
Andy Greene92cd172011-01-19 13:11:55 +00001198int
Peter Hinz56885f32011-03-02 22:03:47 +00001199libwebsocket_service(struct libwebsocket_context *context, int timeout_ms)
Andy Greene92cd172011-01-19 13:11:55 +00001200{
1201 int n;
Andy Greene92cd172011-01-19 13:11:55 +00001202
1203 /* stay dead once we are dead */
1204
Peter Hinz56885f32011-03-02 22:03:47 +00001205 if (context == NULL)
Andy Greene92cd172011-01-19 13:11:55 +00001206 return 1;
1207
Andy Green0d338332011-02-12 11:57:43 +00001208 /* wait for something to need service */
Andy Green4739e5c2011-01-22 12:51:57 +00001209
Peter Hinz56885f32011-03-02 22:03:47 +00001210 n = poll(context->fds, context->fds_count, timeout_ms);
Andy Green3221f922011-02-12 13:14:11 +00001211 if (n == 0) /* poll timeout */
1212 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001213
Andy Greendfb23042013-01-17 12:26:48 +08001214 if (n < 0)
Andy Green3928f612012-07-20 12:58:38 +08001215 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001216
Andy Greendfb23042013-01-17 12:26:48 +08001217 /* any socket with events to service? */
Andy Greene92cd172011-01-19 13:11:55 +00001218
Peter Hinz56885f32011-03-02 22:03:47 +00001219 for (n = 0; n < context->fds_count; n++)
1220 if (context->fds[n].revents)
Andy Green3928f612012-07-20 12:58:38 +08001221 if (libwebsocket_service_fd(context,
1222 &context->fds[n]) < 0)
1223 return -1;
Andy Greene92cd172011-01-19 13:11:55 +00001224 return 0;
Andy Greene92cd172011-01-19 13:11:55 +00001225}
1226
Andy Green3182ece2013-01-20 17:08:31 +08001227#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01001228int
1229lws_any_extension_handled(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001230 struct libwebsocket *wsi,
1231 enum libwebsocket_extension_callback_reasons r,
Andy Greena41314f2011-05-23 10:00:03 +01001232 void *v, size_t len)
1233{
1234 int n;
1235 int handled = 0;
1236
1237 /* maybe an extension will take care of it for us */
1238
1239 for (n = 0; n < wsi->count_active_extensions && !handled; n++) {
1240 if (!wsi->active_extensions[n]->callback)
1241 continue;
1242
1243 handled |= wsi->active_extensions[n]->callback(context,
1244 wsi->active_extensions[n], wsi,
1245 r, wsi->active_extensions_user[n], v, len);
1246 }
1247
1248 return handled;
1249}
1250
1251
1252void *
1253lws_get_extension_user_matching_ext(struct libwebsocket *wsi,
Andy Green6ee372f2012-04-09 15:09:01 +08001254 struct libwebsocket_extension *ext)
Andy Greena41314f2011-05-23 10:00:03 +01001255{
1256 int n = 0;
1257
Andy Green68b45042011-05-25 21:41:57 +01001258 if (wsi == NULL)
1259 return NULL;
1260
Andy Greena41314f2011-05-23 10:00:03 +01001261 while (n < wsi->count_active_extensions) {
1262 if (wsi->active_extensions[n] != ext) {
1263 n++;
1264 continue;
1265 }
1266 return wsi->active_extensions_user[n];
1267 }
1268
1269 return NULL;
1270}
Andy Green3182ece2013-01-20 17:08:31 +08001271#endif
Andy Greena41314f2011-05-23 10:00:03 +01001272
Andy Green90c7cbc2011-01-27 06:26:52 +00001273/**
1274 * libwebsocket_callback_on_writable() - Request a callback when this socket
1275 * becomes able to be written to without
1276 * blocking
Andy Green32375b72011-02-19 08:32:53 +00001277 *
Peter Hinz56885f32011-03-02 22:03:47 +00001278 * @context: libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +00001279 * @wsi: Websocket connection instance to get callback for
1280 */
1281
1282int
Peter Hinz56885f32011-03-02 22:03:47 +00001283libwebsocket_callback_on_writable(struct libwebsocket_context *context,
Andy Green6ee372f2012-04-09 15:09:01 +08001284 struct libwebsocket *wsi)
Andy Green90c7cbc2011-01-27 06:26:52 +00001285{
Andy Green3182ece2013-01-20 17:08:31 +08001286#ifndef LWS_NO_EXTENSIONS
Andy Green90c7cbc2011-01-27 06:26:52 +00001287 int n;
Andy Greena41314f2011-05-23 10:00:03 +01001288 int handled = 0;
1289
1290 /* maybe an extension will take care of it for us */
1291
1292 for (n = 0; n < wsi->count_active_extensions; n++) {
1293 if (!wsi->active_extensions[n]->callback)
1294 continue;
1295
1296 handled |= wsi->active_extensions[n]->callback(context,
1297 wsi->active_extensions[n], wsi,
1298 LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE,
1299 wsi->active_extensions_user[n], NULL, 0);
1300 }
1301
1302 if (handled)
1303 return 1;
Andy Green3182ece2013-01-20 17:08:31 +08001304#endif
Andy Greendfb23042013-01-17 12:26:48 +08001305 if (wsi->position_in_fds_table < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001306 lwsl_err("libwebsocket_callback_on_writable: failed to find socket %d\n",
1307 wsi->sock);
Andy Greendfb23042013-01-17 12:26:48 +08001308 return -1;
1309 }
1310
1311 context->fds[wsi->position_in_fds_table].events |= POLLOUT;
Andy Greena41314f2011-05-23 10:00:03 +01001312
Andy Green3221f922011-02-12 13:14:11 +00001313 /* external POLL support via protocol 0 */
Peter Hinz56885f32011-03-02 22:03:47 +00001314 context->protocols[0].callback(context, wsi,
Andy Green3221f922011-02-12 13:14:11 +00001315 LWS_CALLBACK_SET_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001316 wsi->user_space, (void *)(long)wsi->sock, POLLOUT);
Andy Green3221f922011-02-12 13:14:11 +00001317
Andy Green90c7cbc2011-01-27 06:26:52 +00001318 return 1;
1319}
1320
1321/**
1322 * libwebsocket_callback_on_writable_all_protocol() - Request a callback for
1323 * all connections using the given protocol when it
1324 * becomes possible to write to each socket without
1325 * blocking in turn.
1326 *
1327 * @protocol: Protocol whose connections will get callbacks
1328 */
1329
1330int
1331libwebsocket_callback_on_writable_all_protocol(
1332 const struct libwebsocket_protocols *protocol)
1333{
Peter Hinz56885f32011-03-02 22:03:47 +00001334 struct libwebsocket_context *context = protocol->owning_server;
Andy Green90c7cbc2011-01-27 06:26:52 +00001335 int n;
Andy Green0d338332011-02-12 11:57:43 +00001336 struct libwebsocket *wsi;
Andy Green90c7cbc2011-01-27 06:26:52 +00001337
Andy Greendfb23042013-01-17 12:26:48 +08001338 for (n = 0; n < context->fds_count; n++) {
1339 wsi = context->lws_lookup[context->fds[n].fd];
1340 if (!wsi)
1341 continue;
1342 if (wsi->protocol == protocol)
1343 libwebsocket_callback_on_writable(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00001344 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001345
1346 return 0;
1347}
1348
Andy Greenbe93fef2011-02-14 20:25:43 +00001349/**
1350 * libwebsocket_set_timeout() - marks the wsi as subject to a timeout
1351 *
1352 * You will not need this unless you are doing something special
1353 *
1354 * @wsi: Websocket connection instance
1355 * @reason: timeout reason
1356 * @secs: how many seconds
1357 */
1358
1359void
1360libwebsocket_set_timeout(struct libwebsocket *wsi,
1361 enum pending_timeout reason, int secs)
1362{
1363 struct timeval tv;
1364
1365 gettimeofday(&tv, NULL);
1366
1367 wsi->pending_timeout_limit = tv.tv_sec + secs;
1368 wsi->pending_timeout = reason;
1369}
1370
Andy Greena6cbece2011-01-27 20:06:03 +00001371
1372/**
1373 * libwebsocket_get_socket_fd() - returns the socket file descriptor
1374 *
1375 * You will not need this unless you are doing something special
1376 *
1377 * @wsi: Websocket connection instance
1378 */
1379
1380int
1381libwebsocket_get_socket_fd(struct libwebsocket *wsi)
1382{
1383 return wsi->sock;
1384}
1385
Andy Greend636e352013-01-29 12:36:17 +08001386#ifdef LWS_LATENCY
1387void
Andy Greenb5b23192013-02-11 17:13:32 +08001388lws_latency(struct libwebsocket_context *context, struct libwebsocket *wsi,
1389 const char *action, int ret, int completed)
Andy Greend636e352013-01-29 12:36:17 +08001390{
1391 struct timeval tv;
1392 unsigned long u;
1393 char buf[256];
1394
1395 gettimeofday(&tv, NULL);
1396
1397 u = (tv.tv_sec * 1000000) + tv.tv_usec;
1398
1399 if (action) {
1400 if (completed) {
1401 if (wsi->action_start == wsi->latency_start)
Andy Greenb5b23192013-02-11 17:13:32 +08001402 sprintf(buf,
1403 "Completion first try lat %luus: %p: ret %d: %s\n",
1404 u - wsi->latency_start,
1405 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001406 else
Andy Greenb5b23192013-02-11 17:13:32 +08001407 sprintf(buf,
1408 "Completion %luus: lat %luus: %p: ret %d: %s\n",
1409 u - wsi->action_start,
1410 u - wsi->latency_start,
1411 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001412 wsi->action_start = 0;
1413 } else
Andy Greenb5b23192013-02-11 17:13:32 +08001414 sprintf(buf, "lat %luus: %p: ret %d: %s\n",
1415 u - wsi->latency_start,
1416 (void *)wsi, ret, action);
Andy Greend636e352013-01-29 12:36:17 +08001417 if (u - wsi->latency_start > context->worst_latency) {
1418 context->worst_latency = u - wsi->latency_start;
1419 strcpy(context->worst_latency_info, buf);
1420 }
1421 lwsl_latency("%s", buf);
1422 } else {
1423 wsi->latency_start = u;
1424 if (!wsi->action_start)
1425 wsi->action_start = u;
1426 }
1427}
1428#endif
1429
Andy Greena1ce6be2013-01-18 11:43:21 +08001430#ifdef LWS_NO_SERVER
1431int
1432_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1433{
1434 return 0;
1435}
1436#else
Andy Green706961d2013-01-17 16:50:35 +08001437int
1438_libwebsocket_rx_flow_control(struct libwebsocket *wsi)
1439{
1440 struct libwebsocket_context *context = wsi->protocol->owning_server;
1441 int n;
1442
Andy Green623a98d2013-01-21 11:04:23 +08001443 if (!(wsi->u.ws.rxflow_change_to & 2))
Andy Green706961d2013-01-17 16:50:35 +08001444 return 0;
1445
Andy Green623a98d2013-01-21 11:04:23 +08001446 wsi->u.ws.rxflow_change_to &= ~2;
Andy Green706961d2013-01-17 16:50:35 +08001447
Andy Greenb5b23192013-02-11 17:13:32 +08001448 lwsl_info("rxflow: wsi %p change_to %d\n",
1449 wsi, wsi->u.ws.rxflow_change_to);
Andy Green706961d2013-01-17 16:50:35 +08001450
1451 /* if we're letting it come again, did we interrupt anything? */
Andy Green623a98d2013-01-21 11:04:23 +08001452 if ((wsi->u.ws.rxflow_change_to & 1) && wsi->u.ws.rxflow_buffer) {
Andy Green706961d2013-01-17 16:50:35 +08001453 n = libwebsocket_interpret_incoming_packet(wsi, NULL, 0);
1454 if (n < 0) {
Andy Greenb5b23192013-02-11 17:13:32 +08001455 lwsl_info("libwebsocket_rx_flow_control: close req\n");
Andy Green706961d2013-01-17 16:50:35 +08001456 return -1;
1457 }
1458 if (n)
1459 /* oh he stuck again, do nothing */
1460 return 0;
1461 }
1462
Andy Green623a98d2013-01-21 11:04:23 +08001463 if (wsi->u.ws.rxflow_change_to & 1)
Andy Green706961d2013-01-17 16:50:35 +08001464 context->fds[wsi->position_in_fds_table].events |= POLLIN;
1465 else
1466 context->fds[wsi->position_in_fds_table].events &= ~POLLIN;
1467
Andy Green623a98d2013-01-21 11:04:23 +08001468 if (wsi->u.ws.rxflow_change_to & 1)
Andy Green706961d2013-01-17 16:50:35 +08001469 /* external POLL support via protocol 0 */
1470 context->protocols[0].callback(context, wsi,
1471 LWS_CALLBACK_SET_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001472 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001473 else
1474 /* external POLL support via protocol 0 */
1475 context->protocols[0].callback(context, wsi,
1476 LWS_CALLBACK_CLEAR_MODE_POLL_FD,
Andy Green50097dd2013-02-15 22:36:30 +08001477 wsi->user_space, (void *)(long)wsi->sock, POLLIN);
Andy Green706961d2013-01-17 16:50:35 +08001478
1479 return 1;
1480}
Andy Greena1ce6be2013-01-18 11:43:21 +08001481#endif
Andy Green706961d2013-01-17 16:50:35 +08001482
Andy Green90c7cbc2011-01-27 06:26:52 +00001483/**
1484 * libwebsocket_rx_flow_control() - Enable and disable socket servicing for
1485 * receieved packets.
1486 *
1487 * If the output side of a server process becomes choked, this allows flow
1488 * control for the input side.
1489 *
1490 * @wsi: Websocket connection instance to get callback for
1491 * @enable: 0 = disable read servicing for this connection, 1 = enable
1492 */
1493
1494int
1495libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable)
1496{
Andy Green623a98d2013-01-21 11:04:23 +08001497 wsi->u.ws.rxflow_change_to = 2 | !!enable;
Andy Green90c7cbc2011-01-27 06:26:52 +00001498
Andy Green706961d2013-01-17 16:50:35 +08001499 return 0;
Andy Green90c7cbc2011-01-27 06:26:52 +00001500}
1501
Andy Green706961d2013-01-17 16:50:35 +08001502
Andy Green2ac5a6f2011-01-28 10:00:18 +00001503/**
1504 * libwebsocket_canonical_hostname() - returns this host's hostname
1505 *
1506 * This is typically used by client code to fill in the host parameter
1507 * when making a client connection. You can only call it after the context
1508 * has been created.
1509 *
Peter Hinz56885f32011-03-02 22:03:47 +00001510 * @context: Websocket context
Andy Green2ac5a6f2011-01-28 10:00:18 +00001511 */
1512
1513
1514extern const char *
Peter Hinz56885f32011-03-02 22:03:47 +00001515libwebsocket_canonical_hostname(struct libwebsocket_context *context)
Andy Green2ac5a6f2011-01-28 10:00:18 +00001516{
Peter Hinz56885f32011-03-02 22:03:47 +00001517 return (const char *)context->canonical_hostname;
Andy Green2ac5a6f2011-01-28 10:00:18 +00001518}
1519
1520
Andy Green90c7cbc2011-01-27 06:26:52 +00001521static void sigpipe_handler(int x)
1522{
1523}
1524
Andy Green6901cb32011-02-21 08:06:47 +00001525#ifdef LWS_OPENSSL_SUPPORT
1526static int
1527OpenSSL_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
1528{
1529
1530 SSL *ssl;
1531 int n;
Andy Green2e24da02011-03-05 16:12:04 +00001532 struct libwebsocket_context *context;
Andy Green6901cb32011-02-21 08:06:47 +00001533
1534 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1535 SSL_get_ex_data_X509_STORE_CTX_idx());
1536
1537 /*
Andy Green2e24da02011-03-05 16:12:04 +00001538 * !!! nasty openssl requires the index to come as a library-scope
1539 * static
Andy Green6901cb32011-02-21 08:06:47 +00001540 */
Andy Green2e24da02011-03-05 16:12:04 +00001541 context = SSL_get_ex_data(ssl, openssl_websocket_private_data_index);
Andy Green6ee372f2012-04-09 15:09:01 +08001542
Peter Hinz56885f32011-03-02 22:03:47 +00001543 n = context->protocols[0].callback(NULL, NULL,
Andy Green6901cb32011-02-21 08:06:47 +00001544 LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION,
1545 x509_ctx, ssl, preverify_ok);
1546
1547 /* convert return code from 0 = OK to 1 = OK */
1548
1549 if (!n)
1550 n = 1;
1551 else
1552 n = 0;
1553
1554 return n;
1555}
1556#endif
1557
Andy Green706961d2013-01-17 16:50:35 +08001558int user_callback_handle_rxflow(callback_function callback_function,
Andy Greenb5b23192013-02-11 17:13:32 +08001559 struct libwebsocket_context *context,
Andy Green706961d2013-01-17 16:50:35 +08001560 struct libwebsocket *wsi,
1561 enum libwebsocket_callback_reasons reason, void *user,
1562 void *in, size_t len)
1563{
1564 int n;
1565
1566 n = callback_function(context, wsi, reason, user, in, len);
Andy Greenaedc9532013-02-10 21:21:24 +08001567 if (!n)
1568 n = _libwebsocket_rx_flow_control(wsi);
Andy Green706961d2013-01-17 16:50:35 +08001569
Andy Greenaedc9532013-02-10 21:21:24 +08001570 return n;
Andy Green706961d2013-01-17 16:50:35 +08001571}
1572
Andy Greenb45993c2010-12-18 15:13:50 +00001573
Andy Greenab990e42010-10-31 12:42:52 +00001574/**
Andy Green4739e5c2011-01-22 12:51:57 +00001575 * libwebsocket_create_context() - Create the websocket handler
Andy Green1b265272013-02-09 14:01:09 +08001576 * @info: pointer to struct with parameters
Andy Green05464c62010-11-12 10:44:18 +00001577 *
Andy Green1b265272013-02-09 14:01:09 +08001578 * This function creates the listening socket (if serving) and takes care
Andy Green8f037e42010-12-19 22:13:26 +00001579 * of all initialization in one step.
1580 *
Andy Greene92cd172011-01-19 13:11:55 +00001581 * After initialization, it returns a struct libwebsocket_context * that
1582 * represents this server. After calling, user code needs to take care
1583 * of calling libwebsocket_service() with the context pointer to get the
1584 * server's sockets serviced. This can be done in the same process context
1585 * or a forked process, or another thread,
Andy Green05464c62010-11-12 10:44:18 +00001586 *
Andy Green8f037e42010-12-19 22:13:26 +00001587 * The protocol callback functions are called for a handful of events
1588 * including http requests coming in, websocket connections becoming
1589 * established, and data arriving; it's also called periodically to allow
1590 * async transmission.
1591 *
1592 * HTTP requests are sent always to the FIRST protocol in @protocol, since
1593 * at that time websocket protocol has not been negotiated. Other
1594 * protocols after the first one never see any HTTP callack activity.
1595 *
1596 * The server created is a simple http server by default; part of the
1597 * websocket standard is upgrading this http connection to a websocket one.
1598 *
1599 * This allows the same server to provide files like scripts and favicon /
1600 * images or whatever over http and dynamic data over websockets all in
1601 * one place; they're all handled in the user callback.
Andy Greenab990e42010-10-31 12:42:52 +00001602 */
Andy Green4ea60062010-10-30 12:15:07 +01001603
Andy Greene92cd172011-01-19 13:11:55 +00001604struct libwebsocket_context *
Andy Green1b265272013-02-09 14:01:09 +08001605libwebsocket_create_context(struct lws_context_creation_info *info)
Andy Greenff95d7a2010-10-28 22:36:01 +01001606{
Peter Hinz56885f32011-03-02 22:03:47 +00001607 struct libwebsocket_context *context = NULL;
Andy Green9659f372011-01-27 22:01:43 +00001608 char *p;
Andy Greencbb31222013-01-31 09:57:05 +08001609#ifndef LWS_NO_SERVER
Andy Green14f47292013-02-11 19:36:15 +08001610 int n;
Andy Greencbb31222013-01-31 09:57:05 +08001611 int opt = 1;
Andy Green0d338332011-02-12 11:57:43 +00001612 struct libwebsocket *wsi;
Andy Greencbb31222013-01-31 09:57:05 +08001613 struct sockaddr_in serv_addr;
1614#endif
Andy Green3182ece2013-01-20 17:08:31 +08001615#ifndef LWS_NO_EXTENSIONS
1616 int m;
Andy Green1b265272013-02-09 14:01:09 +08001617 struct libwebsocket_extension *ext;
Andy Green3182ece2013-01-20 17:08:31 +08001618#endif
Andy Greenff95d7a2010-10-28 22:36:01 +01001619
Andy Green3faa9c72010-11-08 17:03:03 +00001620#ifdef LWS_OPENSSL_SUPPORT
Andy Greenf2f54d52010-11-15 22:08:00 +00001621 SSL_METHOD *method;
Andy Green3faa9c72010-11-08 17:03:03 +00001622#endif
1623
Joakim Soderberg4c531232013-02-06 15:26:58 +09001624#ifndef LWS_NO_DAEMONIZE
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001625 int pid_daemon = get_daemonize_pid();
Joakim Soderberg4c531232013-02-06 15:26:58 +09001626#endif
1627
Andy Greenb3a614a2013-01-19 13:08:17 +08001628 lwsl_notice("Initial logging level %d\n", log_level);
Andy Green7b405452013-02-01 10:50:15 +08001629 lwsl_notice("Library version: %s\n", library_version);
Andy Greenb5b23192013-02-11 17:13:32 +08001630 lwsl_info(" LWS_MAX_HEADER_NAME_LENGTH: %u\n",
1631 LWS_MAX_HEADER_NAME_LENGTH);
Andy Greenc0d6b632013-01-12 23:42:17 +08001632 lwsl_info(" LWS_MAX_HEADER_LEN: %u\n", LWS_MAX_HEADER_LEN);
Andy Greenc0d6b632013-01-12 23:42:17 +08001633 lwsl_info(" LWS_MAX_PROTOCOLS: %u\n", LWS_MAX_PROTOCOLS);
Andy Green3182ece2013-01-20 17:08:31 +08001634#ifndef LWS_NO_EXTENSIONS
Andy Greenb5b23192013-02-11 17:13:32 +08001635 lwsl_info(" LWS_MAX_EXTENSIONS_ACTIVE: %u\n",
1636 LWS_MAX_EXTENSIONS_ACTIVE);
Andy Green3182ece2013-01-20 17:08:31 +08001637#else
1638 lwsl_notice(" Configured without extension support\n");
1639#endif
Andy Greenc0d6b632013-01-12 23:42:17 +08001640 lwsl_info(" SPEC_LATEST_SUPPORTED: %u\n", SPEC_LATEST_SUPPORTED);
1641 lwsl_info(" AWAITING_TIMEOUT: %u\n", AWAITING_TIMEOUT);
1642 lwsl_info(" CIPHERS_LIST_STRING: '%s'\n", CIPHERS_LIST_STRING);
1643 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
1644 lwsl_info(" LWS_MAX_ZLIB_CONN_BUFFER: %u\n", LWS_MAX_ZLIB_CONN_BUFFER);
Andy Green43db0452013-01-10 19:50:35 +08001645
Peter Hinz56885f32011-03-02 22:03:47 +00001646#ifdef _WIN32
1647 {
1648 WORD wVersionRequested;
1649 WSADATA wsaData;
1650 int err;
Andy Green6ee372f2012-04-09 15:09:01 +08001651 HMODULE wsdll;
Peter Hinz56885f32011-03-02 22:03:47 +00001652
1653 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
1654 wVersionRequested = MAKEWORD(2, 2);
1655
1656 err = WSAStartup(wVersionRequested, &wsaData);
1657 if (err != 0) {
1658 /* Tell the user that we could not find a usable */
1659 /* Winsock DLL. */
Andy Green43db0452013-01-10 19:50:35 +08001660 lwsl_err("WSAStartup failed with error: %d\n", err);
Peter Hinz56885f32011-03-02 22:03:47 +00001661 return NULL;
1662 }
David Galeano7b11fec2011-10-04 19:55:18 +08001663
Andy Green6ee372f2012-04-09 15:09:01 +08001664 /* default to a poll() made out of select() */
1665 poll = emulated_poll;
David Galeano7b11fec2011-10-04 19:55:18 +08001666
Andy Green6ee372f2012-04-09 15:09:01 +08001667 /* if windows socket lib available, use his WSAPoll */
David Galeanocb193682013-01-09 15:29:00 +08001668 wsdll = GetModuleHandle(_T("Ws2_32.dll"));
Andy Green6ee372f2012-04-09 15:09:01 +08001669 if (wsdll)
1670 poll = (PFNWSAPOLL)GetProcAddress(wsdll, "WSAPoll");
Joakim Soderberg4c531232013-02-06 15:26:58 +09001671
Joakim Söderberg68e8d732013-02-06 15:27:39 +09001672 /* Finally fall back to emulated poll if all else fails */
Joakim Soderberg4c531232013-02-06 15:26:58 +09001673 if (!poll)
1674 poll = emulated_poll;
Peter Hinz56885f32011-03-02 22:03:47 +00001675 }
1676#endif
1677
Andy Greenb5b23192013-02-11 17:13:32 +08001678 context = (struct libwebsocket_context *)
1679 malloc(sizeof(struct libwebsocket_context));
Peter Hinz56885f32011-03-02 22:03:47 +00001680 if (!context) {
Andy Green43db0452013-01-10 19:50:35 +08001681 lwsl_err("No memory for websocket context\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001682 return NULL;
1683 }
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001684 memset(context, 0, sizeof(*context));
Andy Green35f332b2013-01-21 13:06:38 +08001685#ifndef LWS_NO_DAEMONIZE
Andy Green24cba922013-01-19 13:56:10 +08001686 context->started_with_parent = pid_daemon;
1687 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
1688#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001689
Joakim Soderberg4c531232013-02-06 15:26:58 +09001690 context->listen_service_extraseen = 0;
Andy Green1b265272013-02-09 14:01:09 +08001691 context->protocols = info->protocols;
1692 context->listen_port = info->port;
Peter Hinz56885f32011-03-02 22:03:47 +00001693 context->http_proxy_port = 0;
1694 context->http_proxy_address[0] = '\0';
Andy Green1b265272013-02-09 14:01:09 +08001695 context->options = info->options;
Andy Greendfb23042013-01-17 12:26:48 +08001696 /* to reduce this allocation, */
1697 context->max_fds = getdtablesize();
Andy Greena86f6342013-02-11 11:04:56 +08001698 lwsl_notice(" static allocation: %u + (%u x %u fds) = %u bytes\n",
1699 sizeof(struct libwebsocket_context),
1700 sizeof(struct pollfd) + sizeof(struct libwebsocket *),
1701 context->max_fds,
Andy Greenb5b23192013-02-11 17:13:32 +08001702 sizeof(struct libwebsocket_context) +
1703 ((sizeof(struct pollfd) + sizeof(struct libwebsocket *)) *
1704 context->max_fds));
Andy Greendfb23042013-01-17 12:26:48 +08001705
Andy Greenb5b23192013-02-11 17:13:32 +08001706 context->fds = (struct pollfd *)malloc(sizeof(struct pollfd) *
1707 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001708 if (context->fds == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001709 lwsl_err("Unable to allocate fds array for %d connections\n",
1710 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001711 free(context);
1712 return NULL;
1713 }
Andy Greenb5b23192013-02-11 17:13:32 +08001714 context->lws_lookup = (struct libwebsocket **)
1715 malloc(sizeof(struct libwebsocket *) * context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001716 if (context->lws_lookup == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +08001717 lwsl_err(
1718 "Unable to allocate lws_lookup array for %d connections\n",
1719 context->max_fds);
Andy Greendfb23042013-01-17 12:26:48 +08001720 free(context->fds);
1721 free(context);
1722 return NULL;
1723 }
Andy Greena17c6922013-01-20 20:21:54 +08001724
Peter Hinz56885f32011-03-02 22:03:47 +00001725 context->fds_count = 0;
Andy Green3182ece2013-01-20 17:08:31 +08001726#ifndef LWS_NO_EXTENSIONS
Andy Green1b265272013-02-09 14:01:09 +08001727 context->extensions = info->extensions;
Andy Green3182ece2013-01-20 17:08:31 +08001728#endif
Paulo Roberto Urio1e326632012-06-04 10:52:19 +08001729 context->last_timeout_check_s = 0;
Andy Green1b265272013-02-09 14:01:09 +08001730 context->user_space = info->user;
Andy Green9659f372011-01-27 22:01:43 +00001731
Peter Hinz56885f32011-03-02 22:03:47 +00001732#ifdef WIN32
1733 context->fd_random = 0;
1734#else
1735 context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
1736 if (context->fd_random < 0) {
Andy Green43db0452013-01-10 19:50:35 +08001737 lwsl_err("Unable to open random device %s %d\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001738 SYSTEM_RANDOM_FILEPATH, context->fd_random);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001739 goto bail;
Andy Green44eee682011-02-10 09:32:24 +00001740 }
Peter Hinz56885f32011-03-02 22:03:47 +00001741#endif
Andy Green44eee682011-02-10 09:32:24 +00001742
Peter Hinz56885f32011-03-02 22:03:47 +00001743#ifdef LWS_OPENSSL_SUPPORT
1744 context->use_ssl = 0;
1745 context->ssl_ctx = NULL;
1746 context->ssl_client_ctx = NULL;
Andy Green2e24da02011-03-05 16:12:04 +00001747 openssl_websocket_private_data_index = 0;
Peter Hinz56885f32011-03-02 22:03:47 +00001748#endif
Andy Green2ac5a6f2011-01-28 10:00:18 +00001749
Andy Greena1ce6be2013-01-18 11:43:21 +08001750 strcpy(context->canonical_hostname, "unknown");
Andy Greena69f0512012-05-03 12:32:38 +08001751
Andy Greena1ce6be2013-01-18 11:43:21 +08001752#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001753 if (!(info->options & LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME)) {
Andy Green788c4a82012-10-22 12:29:57 +01001754 /* find canonical hostname */
Andy Greenadc71462013-02-14 17:11:22 +08001755 gethostname((char *)context->canonical_hostname,
Andy Greenb5b23192013-02-11 17:13:32 +08001756 sizeof(context->canonical_hostname) - 1);
Andy Green788c4a82012-10-22 12:29:57 +01001757
Andy Greenb5b23192013-02-11 17:13:32 +08001758 lwsl_notice(" canonical_hostname = %s\n",
1759 context->canonical_hostname);
Andy Greena69f0512012-05-03 12:32:38 +08001760 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001761#endif
Andy Greena69f0512012-05-03 12:32:38 +08001762
Andy Green9659f372011-01-27 22:01:43 +00001763 /* split the proxy ads:port if given */
1764
1765 p = getenv("http_proxy");
1766 if (p) {
Peter Hinz56885f32011-03-02 22:03:47 +00001767 strncpy(context->http_proxy_address, p,
Andy Greenb5b23192013-02-11 17:13:32 +08001768 sizeof(context->http_proxy_address) - 1);
Peter Hinz56885f32011-03-02 22:03:47 +00001769 context->http_proxy_address[
Andy Greenb5b23192013-02-11 17:13:32 +08001770 sizeof(context->http_proxy_address) - 1] = '\0';
Andy Green9659f372011-01-27 22:01:43 +00001771
Peter Hinz56885f32011-03-02 22:03:47 +00001772 p = strchr(context->http_proxy_address, ':');
Andy Green9659f372011-01-27 22:01:43 +00001773 if (p == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08001774 lwsl_err("http_proxy needs to be ads:port\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001775 goto bail;
Andy Green9659f372011-01-27 22:01:43 +00001776 }
1777 *p = '\0';
Peter Hinz56885f32011-03-02 22:03:47 +00001778 context->http_proxy_port = atoi(p + 1);
Andy Green9659f372011-01-27 22:01:43 +00001779
Andy Greenb3a614a2013-01-19 13:08:17 +08001780 lwsl_notice(" Proxy %s:%u\n",
Peter Hinz56885f32011-03-02 22:03:47 +00001781 context->http_proxy_address,
1782 context->http_proxy_port);
Andy Green9659f372011-01-27 22:01:43 +00001783 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001784
Andy Greena1ce6be2013-01-18 11:43:21 +08001785#ifndef LWS_NO_SERVER
Andy Green1b265272013-02-09 14:01:09 +08001786 if (info->port) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001787
Andy Green3faa9c72010-11-08 17:03:03 +00001788#ifdef LWS_OPENSSL_SUPPORT
Andy Green1b265272013-02-09 14:01:09 +08001789 context->use_ssl = info->ssl_cert_filepath != NULL &&
1790 info->ssl_private_key_filepath != NULL;
Andy Green23c5f2e2013-02-06 15:43:00 +09001791#ifdef USE_CYASSL
1792 lwsl_notice(" Compiled with CYASSL support\n");
1793#else
1794 lwsl_notice(" Compiled with OpenSSL support\n");
1795#endif
Peter Hinz56885f32011-03-02 22:03:47 +00001796 if (context->use_ssl)
Andy Green23c5f2e2013-02-06 15:43:00 +09001797 lwsl_notice(" Using SSL mode\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001798 else
Andy Green23c5f2e2013-02-06 15:43:00 +09001799 lwsl_notice(" Using non-SSL mode\n");
Andy Green3faa9c72010-11-08 17:03:03 +00001800
Andy Green90c7cbc2011-01-27 06:26:52 +00001801#else
Andy Green2b40b792013-02-10 22:22:01 +08001802 if (info->ssl_cert_filepath != NULL &&
1803 info->ssl_private_key_filepath != NULL) {
Andy Greenb3a614a2013-01-19 13:08:17 +08001804 lwsl_notice(" Not compiled for OpenSSl support!\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001805 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001806 }
Andy Greenb5b23192013-02-11 17:13:32 +08001807 lwsl_notice(" Compiled without SSL support\n");
Andy Green90c7cbc2011-01-27 06:26:52 +00001808#endif
Andy Greena17c6922013-01-20 20:21:54 +08001809
Andy Greenb5b23192013-02-11 17:13:32 +08001810 lwsl_notice(
1811 " per-conn mem: %u + %u headers + protocol rx buf\n",
1812 sizeof(struct libwebsocket),
1813 sizeof(struct allocated_headers));
Andy Green90c7cbc2011-01-27 06:26:52 +00001814 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001815#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001816
1817 /* ignore SIGPIPE */
Peter Hinz56885f32011-03-02 22:03:47 +00001818#ifdef WIN32
1819#else
Andy Green90c7cbc2011-01-27 06:26:52 +00001820 signal(SIGPIPE, sigpipe_handler);
Peter Hinz56885f32011-03-02 22:03:47 +00001821#endif
Andy Green90c7cbc2011-01-27 06:26:52 +00001822
1823
1824#ifdef LWS_OPENSSL_SUPPORT
1825
1826 /* basic openssl init */
1827
1828 SSL_library_init();
1829
1830 OpenSSL_add_all_algorithms();
1831 SSL_load_error_strings();
1832
Andy Green2e24da02011-03-05 16:12:04 +00001833 openssl_websocket_private_data_index =
Andy Green6901cb32011-02-21 08:06:47 +00001834 SSL_get_ex_new_index(0, "libwebsockets", NULL, NULL, NULL);
1835
Andy Green90c7cbc2011-01-27 06:26:52 +00001836 /*
1837 * Firefox insists on SSLv23 not SSLv3
1838 * Konq disables SSLv2 by default now, SSLv23 works
1839 */
1840
1841 method = (SSL_METHOD *)SSLv23_server_method();
1842 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001843 lwsl_err("problem creating ssl method: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001844 ERR_error_string(ERR_get_error(),
1845 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001846 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001847 }
Peter Hinz56885f32011-03-02 22:03:47 +00001848 context->ssl_ctx = SSL_CTX_new(method); /* create context */
1849 if (!context->ssl_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001850 lwsl_err("problem creating ssl context: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001851 ERR_error_string(ERR_get_error(),
1852 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001853 goto bail;
Andy Green90c7cbc2011-01-27 06:26:52 +00001854 }
1855
David Galeanocc148e42013-01-10 10:18:59 +08001856#ifdef SSL_OP_NO_COMPRESSION
David Galeanoc72f6f92013-01-10 10:11:57 +08001857 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001858#endif
David Galeano77a677c2013-01-10 10:14:12 +08001859 SSL_CTX_set_options(context->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
David Galeanof177f2a2013-01-10 10:15:19 +08001860 SSL_CTX_set_cipher_list(context->ssl_ctx, CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001861
Andy Greena1ce6be2013-01-18 11:43:21 +08001862#ifndef LWS_NO_CLIENT
1863
Andy Green90c7cbc2011-01-27 06:26:52 +00001864 /* client context */
Andy Green6ee372f2012-04-09 15:09:01 +08001865
Andy Green1b265272013-02-09 14:01:09 +08001866 if (info->port == CONTEXT_PORT_NO_LISTEN) {
Peter Hinz56885f32011-03-02 22:03:47 +00001867 method = (SSL_METHOD *)SSLv23_client_method();
1868 if (!method) {
Andy Green43db0452013-01-10 19:50:35 +08001869 lwsl_err("problem creating ssl method: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001870 ERR_error_string(ERR_get_error(),
1871 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001872 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001873 }
1874 /* create context */
1875 context->ssl_client_ctx = SSL_CTX_new(method);
1876 if (!context->ssl_client_ctx) {
Andy Green43db0452013-01-10 19:50:35 +08001877 lwsl_err("problem creating ssl context: %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001878 ERR_error_string(ERR_get_error(),
1879 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001880 goto bail;
Peter Hinz56885f32011-03-02 22:03:47 +00001881 }
Andy Green90c7cbc2011-01-27 06:26:52 +00001882
David Galeanocc148e42013-01-10 10:18:59 +08001883#ifdef SSL_OP_NO_COMPRESSION
Andy Greenb5b23192013-02-11 17:13:32 +08001884 SSL_CTX_set_options(context->ssl_client_ctx,
1885 SSL_OP_NO_COMPRESSION);
David Galeanocc148e42013-01-10 10:18:59 +08001886#endif
Andy Greenb5b23192013-02-11 17:13:32 +08001887 SSL_CTX_set_options(context->ssl_client_ctx,
1888 SSL_OP_CIPHER_SERVER_PREFERENCE);
1889 SSL_CTX_set_cipher_list(context->ssl_client_ctx,
1890 CIPHERS_LIST_STRING);
David Galeanoc72f6f92013-01-10 10:11:57 +08001891
Peter Hinz56885f32011-03-02 22:03:47 +00001892 /* openssl init for cert verification (for client sockets) */
Andy Green1b265272013-02-09 14:01:09 +08001893 if (!info->ssl_ca_filepath) {
David Galeano2f82be82013-01-09 16:25:54 +08001894 if (!SSL_CTX_load_verify_locations(
1895 context->ssl_client_ctx, NULL,
1896 LWS_OPENSSL_CLIENT_CERTS))
Andy Green43db0452013-01-10 19:50:35 +08001897 lwsl_err(
Andy Greenb5b23192013-02-11 17:13:32 +08001898 "Unable to load SSL Client certs from %s "
1899 "(set by --with-client-cert-dir= "
1900 "in configure) -- client ssl isn't "
1901 "going to work", LWS_OPENSSL_CLIENT_CERTS);
David Galeano2f82be82013-01-09 16:25:54 +08001902 } else
1903 if (!SSL_CTX_load_verify_locations(
Andy Green1b265272013-02-09 14:01:09 +08001904 context->ssl_client_ctx, info->ssl_ca_filepath,
David Galeano2f82be82013-01-09 16:25:54 +08001905 NULL))
Andy Green43db0452013-01-10 19:50:35 +08001906 lwsl_err(
David Galeano2f82be82013-01-09 16:25:54 +08001907 "Unable to load SSL Client certs "
1908 "file from %s -- client ssl isn't "
Andy Green1b265272013-02-09 14:01:09 +08001909 "going to work", info->ssl_ca_filepath);
Peter Hinz56885f32011-03-02 22:03:47 +00001910
1911 /*
1912 * callback allowing user code to load extra verification certs
1913 * helping the client to verify server identity
1914 */
1915
1916 context->protocols[0].callback(context, NULL,
1917 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS,
1918 context->ssl_client_ctx, NULL, 0);
Andy Green90c7cbc2011-01-27 06:26:52 +00001919 }
Andy Greena1ce6be2013-01-18 11:43:21 +08001920#endif
Andy Green6ee372f2012-04-09 15:09:01 +08001921
Andy Greenc6bf2c22011-02-20 11:10:47 +00001922 /* as a server, are we requiring clients to identify themselves? */
1923
Andy Greenb5b23192013-02-11 17:13:32 +08001924 if (info->options &
1925 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT) {
Andy Greenc6bf2c22011-02-20 11:10:47 +00001926
1927 /* absolutely require the client cert */
Andy Green6ee372f2012-04-09 15:09:01 +08001928
Peter Hinz56885f32011-03-02 22:03:47 +00001929 SSL_CTX_set_verify(context->ssl_ctx,
Andy Green6901cb32011-02-21 08:06:47 +00001930 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
1931 OpenSSL_verify_callback);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001932
1933 /*
1934 * give user code a chance to load certs into the server
1935 * allowing it to verify incoming client certs
1936 */
1937
Peter Hinz56885f32011-03-02 22:03:47 +00001938 context->protocols[0].callback(context, NULL,
Andy Greenc6bf2c22011-02-20 11:10:47 +00001939 LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS,
Peter Hinz56885f32011-03-02 22:03:47 +00001940 context->ssl_ctx, NULL, 0);
Andy Greenc6bf2c22011-02-20 11:10:47 +00001941 }
1942
Peter Hinz56885f32011-03-02 22:03:47 +00001943 if (context->use_ssl) {
Andy Green90c7cbc2011-01-27 06:26:52 +00001944
1945 /* openssl init for server sockets */
1946
Andy Green3faa9c72010-11-08 17:03:03 +00001947 /* set the local certificate from CertFile */
David Galeano9b3d4b22013-01-10 10:11:21 +08001948 n = SSL_CTX_use_certificate_chain_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08001949 info->ssl_cert_filepath);
Andy Green3faa9c72010-11-08 17:03:03 +00001950 if (n != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001951 lwsl_err("problem getting cert '%s': %s\n",
Andy Green1b265272013-02-09 14:01:09 +08001952 info->ssl_cert_filepath,
Andy Greenb5b23192013-02-11 17:13:32 +08001953 ERR_error_string(ERR_get_error(),
1954 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001955 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001956 }
1957 /* set the private key from KeyFile */
Peter Hinz56885f32011-03-02 22:03:47 +00001958 if (SSL_CTX_use_PrivateKey_file(context->ssl_ctx,
Andy Green1b265272013-02-09 14:01:09 +08001959 info->ssl_private_key_filepath,
1960 SSL_FILETYPE_PEM) != 1) {
Andy Green43db0452013-01-10 19:50:35 +08001961 lwsl_err("ssl problem getting key '%s': %s\n",
Andy Greenb5b23192013-02-11 17:13:32 +08001962 info->ssl_private_key_filepath,
1963 ERR_error_string(ERR_get_error(),
1964 (char *)context->service_buffer));
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001965 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001966 }
1967 /* verify private key */
Peter Hinz56885f32011-03-02 22:03:47 +00001968 if (!SSL_CTX_check_private_key(context->ssl_ctx)) {
Andy Green43db0452013-01-10 19:50:35 +08001969 lwsl_err("Private SSL key doesn't match cert\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001970 goto bail;
Andy Green3faa9c72010-11-08 17:03:03 +00001971 }
1972
1973 /* SSL is happy and has a cert it's content with */
1974 }
1975#endif
Andy Greenb45993c2010-12-18 15:13:50 +00001976
Andy Greendf736162011-01-18 15:39:02 +00001977 /* selftest */
1978
1979 if (lws_b64_selftest())
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001980 goto bail;
Andy Greendf736162011-01-18 15:39:02 +00001981
Andy Greena1ce6be2013-01-18 11:43:21 +08001982#ifndef LWS_NO_SERVER
Andy Greenb45993c2010-12-18 15:13:50 +00001983 /* set up our external listening socket we serve on */
Andy Green8f037e42010-12-19 22:13:26 +00001984
Andy Green1b265272013-02-09 14:01:09 +08001985 if (info->port) {
Andy Greena1ce6be2013-01-18 11:43:21 +08001986 int sockfd;
Andy Green8f037e42010-12-19 22:13:26 +00001987
Andy Green4739e5c2011-01-22 12:51:57 +00001988 sockfd = socket(AF_INET, SOCK_STREAM, 0);
1989 if (sockfd < 0) {
Andy Greenf7609e92013-01-14 13:10:55 +08001990 lwsl_err("ERROR opening socket\n");
Peter Pentchev3b233cb2013-02-07 16:31:19 +02001991 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00001992 }
Andy Green775c0dd2010-10-29 14:15:22 +01001993
Joakim Soderberg4c531232013-02-06 15:26:58 +09001994#ifndef WIN32
Andy Greenb5b23192013-02-11 17:13:32 +08001995 /*
1996 * allow us to restart even if old sockets in TIME_WAIT
1997 * (REUSEADDR on Unix means, "don't hang on to this
1998 * address after the listener is closed." On Windows, though,
1999 * it means "don't keep other processes from binding to
2000 * this address while we're using it)
2001 */
Andy Green6ee372f2012-04-09 15:09:01 +08002002 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
2003 (const void *)&opt, sizeof(opt));
Joakim Soderberg4c531232013-02-06 15:26:58 +09002004#endif
Andy Green6c939552011-03-08 08:56:57 +00002005
2006 /* Disable Nagle */
2007 opt = 1;
Andy Green6ee372f2012-04-09 15:09:01 +08002008 setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
2009 (const void *)&opt, sizeof(opt));
Andy Green6c939552011-03-08 08:56:57 +00002010
Joakim Soderberg4c531232013-02-06 15:26:58 +09002011 #ifdef WIN32
2012 opt = 0;
Andy Greenb5b23192013-02-11 17:13:32 +08002013 ioctlsocket(sockfd, FIONBIO, (unsigned long *)&opt);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002014 #else
Andy Greene2160712013-01-28 12:19:10 +08002015 fcntl(sockfd, F_SETFL, O_NONBLOCK);
Joakim Soderberg4c531232013-02-06 15:26:58 +09002016 #endif
Andy Greene2160712013-01-28 12:19:10 +08002017
Andy Green4739e5c2011-01-22 12:51:57 +00002018 bzero((char *) &serv_addr, sizeof(serv_addr));
2019 serv_addr.sin_family = AF_INET;
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002020 if (info->iface == NULL)
Andy Green32375b72011-02-19 08:32:53 +00002021 serv_addr.sin_addr.s_addr = INADDR_ANY;
2022 else
Joakim Soderberg63ff1202013-02-11 17:52:23 +01002023 interface_to_sa(info->iface, &serv_addr,
Andy Green32375b72011-02-19 08:32:53 +00002024 sizeof(serv_addr));
Andy Green1b265272013-02-09 14:01:09 +08002025 serv_addr.sin_port = htons(info->port);
Andy Green4739e5c2011-01-22 12:51:57 +00002026
2027 n = bind(sockfd, (struct sockaddr *) &serv_addr,
2028 sizeof(serv_addr));
2029 if (n < 0) {
Andy Green43db0452013-01-10 19:50:35 +08002030 lwsl_err("ERROR on binding to port %d (%d %d)\n",
Andy Green1b265272013-02-09 14:01:09 +08002031 info->port, n, errno);
Andy Green41c58032013-01-12 13:21:08 +08002032 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002033 goto bail;
Andy Green4739e5c2011-01-22 12:51:57 +00002034 }
Andy Green0d338332011-02-12 11:57:43 +00002035
Andy Greenb5b23192013-02-11 17:13:32 +08002036 wsi = (struct libwebsocket *)malloc(
2037 sizeof(struct libwebsocket));
Andy Green41c58032013-01-12 13:21:08 +08002038 if (wsi == NULL) {
2039 lwsl_err("Out of mem\n");
2040 close(sockfd);
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002041 goto bail;
Andy Green41c58032013-01-12 13:21:08 +08002042 }
Andy Greenb5b23192013-02-11 17:13:32 +08002043 memset(wsi, 0, sizeof(struct libwebsocket));
Andy Green0d338332011-02-12 11:57:43 +00002044 wsi->sock = sockfd;
Andy Green3182ece2013-01-20 17:08:31 +08002045#ifndef LWS_NO_EXTENSIONS
Andy Greend6e09112011-03-05 16:12:15 +00002046 wsi->count_active_extensions = 0;
Andy Green3182ece2013-01-20 17:08:31 +08002047#endif
Andy Green0d338332011-02-12 11:57:43 +00002048 wsi->mode = LWS_CONNMODE_SERVER_LISTENER;
Andy Greendfb23042013-01-17 12:26:48 +08002049
2050 insert_wsi_socket_into_fds(context, wsi);
Andy Green0d338332011-02-12 11:57:43 +00002051
Andy Green65b0e912013-01-16 07:59:47 +08002052 context->listen_service_modulo = LWS_LISTEN_SERVICE_MODULO;
2053 context->listen_service_count = 0;
2054 context->listen_service_fd = sockfd;
2055
Andy Greena824d182013-01-15 20:52:29 +08002056 listen(sockfd, LWS_SOMAXCONN);
Andy Green1b265272013-02-09 14:01:09 +08002057 lwsl_notice(" Listening on port %d\n", info->port);
Andy Green8f037e42010-12-19 22:13:26 +00002058 }
Andy Greena1ce6be2013-01-18 11:43:21 +08002059#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002060
Andy Green6ee372f2012-04-09 15:09:01 +08002061 /*
2062 * drop any root privs for this process
2063 * to listen on port < 1023 we would have needed root, but now we are
2064 * listening, we don't want the power for anything else
2065 */
Peter Hinz56885f32011-03-02 22:03:47 +00002066#ifdef WIN32
2067#else
Andy Green1b265272013-02-09 14:01:09 +08002068 if (info->gid != -1)
2069 if (setgid(info->gid))
Andy Green43db0452013-01-10 19:50:35 +08002070 lwsl_warn("setgid: %s\n", strerror(errno));
Andy Green1b265272013-02-09 14:01:09 +08002071 if (info->uid != -1)
2072 if (setuid(info->uid))
Andy Green43db0452013-01-10 19:50:35 +08002073 lwsl_warn("setuid: %s\n", strerror(errno));
Peter Hinz56885f32011-03-02 22:03:47 +00002074#endif
Andy Greenb45993c2010-12-18 15:13:50 +00002075
Andy Green6f520a52013-01-29 17:57:39 +08002076 /* initialize supported protocols */
Andy Greenb45993c2010-12-18 15:13:50 +00002077
Peter Hinz56885f32011-03-02 22:03:47 +00002078 for (context->count_protocols = 0;
Andy Green1b265272013-02-09 14:01:09 +08002079 info->protocols[context->count_protocols].callback;
Peter Hinz56885f32011-03-02 22:03:47 +00002080 context->count_protocols++) {
Andy Green2d1301e2011-05-24 10:14:41 +01002081
Andy Green43db0452013-01-10 19:50:35 +08002082 lwsl_parser(" Protocol: %s\n",
Andy Green1b265272013-02-09 14:01:09 +08002083 info->protocols[context->count_protocols].name);
Andy Green2d1301e2011-05-24 10:14:41 +01002084
Andy Green1b265272013-02-09 14:01:09 +08002085 info->protocols[context->count_protocols].owning_server =
2086 context;
2087 info->protocols[context->count_protocols].protocol_index =
Peter Hinz56885f32011-03-02 22:03:47 +00002088 context->count_protocols;
Andy Greena7109e62013-02-11 12:05:54 +08002089
2090 /*
2091 * inform all the protocols that they are doing their one-time
2092 * initialization if they want to
2093 */
2094 info->protocols[context->count_protocols].callback(context,
2095 NULL, LWS_CALLBACK_PROTOCOL_INIT, NULL, NULL, 0);
Andy Greenb45993c2010-12-18 15:13:50 +00002096 }
Andy Greenf5bc1302013-01-21 09:09:52 +08002097
Andy Green3182ece2013-01-20 17:08:31 +08002098#ifndef LWS_NO_EXTENSIONS
Andy Greena41314f2011-05-23 10:00:03 +01002099 /*
2100 * give all extensions a chance to create any per-context
2101 * allocations they need
2102 */
2103
2104 m = LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT;
Andy Green1b265272013-02-09 14:01:09 +08002105 if (info->port)
Andy Greena41314f2011-05-23 10:00:03 +01002106 m = LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT;
Andy Greenb5b23192013-02-11 17:13:32 +08002107
Andy Green1b265272013-02-09 14:01:09 +08002108 if (info->extensions) {
2109 ext = info->extensions;
2110 while (ext->callback) {
2111 lwsl_ext(" Extension: %s\n", ext->name);
2112 ext->callback(context, ext, NULL,
Aaron Zinman4550f1d2013-01-10 12:35:18 +08002113 (enum libwebsocket_extension_callback_reasons)m,
2114 NULL, NULL, 0);
Andy Green1b265272013-02-09 14:01:09 +08002115 ext++;
2116 }
Andy Greena41314f2011-05-23 10:00:03 +01002117 }
Andy Green3182ece2013-01-20 17:08:31 +08002118#endif
Peter Hinz56885f32011-03-02 22:03:47 +00002119 return context;
Peter Pentchev3b233cb2013-02-07 16:31:19 +02002120
2121bail:
2122 libwebsocket_context_destroy(context);
2123 return NULL;
Andy Greene92cd172011-01-19 13:11:55 +00002124}
Andy Greenb45993c2010-12-18 15:13:50 +00002125
Andy Greenb45993c2010-12-18 15:13:50 +00002126/**
2127 * libwebsockets_get_protocol() - Returns a protocol pointer from a websocket
Andy Green8f037e42010-12-19 22:13:26 +00002128 * connection.
Andy Greenb45993c2010-12-18 15:13:50 +00002129 * @wsi: pointer to struct websocket you want to know the protocol of
2130 *
Andy Green8f037e42010-12-19 22:13:26 +00002131 *
Andy Green6f520a52013-01-29 17:57:39 +08002132 * Some apis can act on all live connections of a given protocol,
2133 * this is how you can get a pointer to the active protocol if needed.
Andy Greenb45993c2010-12-18 15:13:50 +00002134 */
Andy Greenab990e42010-10-31 12:42:52 +00002135
Andy Greenb45993c2010-12-18 15:13:50 +00002136const struct libwebsocket_protocols *
2137libwebsockets_get_protocol(struct libwebsocket *wsi)
2138{
2139 return wsi->protocol;
2140}
2141
Andy Green82c3d542011-03-07 21:16:31 +00002142int
2143libwebsocket_is_final_fragment(struct libwebsocket *wsi)
2144{
Andy Green623a98d2013-01-21 11:04:23 +08002145 return wsi->u.ws.final;
Andy Green82c3d542011-03-07 21:16:31 +00002146}
Alex Bligh49146db2011-11-07 17:19:25 +08002147
David Galeanoe2cf9922013-01-09 18:06:55 +08002148unsigned char
2149libwebsocket_get_reserved_bits(struct libwebsocket *wsi)
2150{
Andy Green623a98d2013-01-21 11:04:23 +08002151 return wsi->u.ws.rsv;
David Galeanoe2cf9922013-01-09 18:06:55 +08002152}
2153
Andy Green67d556c2013-02-15 22:31:55 +08002154/**
2155 * libwebsocket_ensure_user_space(): return the user context for the connection if possible
2156 * @wsi: websocket connection instance
2157 */
2158
Alex Bligh49146db2011-11-07 17:19:25 +08002159void *
2160libwebsocket_ensure_user_space(struct libwebsocket *wsi)
2161{
Andy Green67d556c2013-02-15 22:31:55 +08002162 if (!wsi->protocol)
2163 return NULL;
2164
Alex Bligh49146db2011-11-07 17:19:25 +08002165 /* allocate the per-connection user memory (if any) */
2166
2167 if (wsi->protocol->per_session_data_size && !wsi->user_space) {
2168 wsi->user_space = malloc(
2169 wsi->protocol->per_session_data_size);
2170 if (wsi->user_space == NULL) {
Andy Green43db0452013-01-10 19:50:35 +08002171 lwsl_err("Out of memory for conn user space\n");
Alex Bligh49146db2011-11-07 17:19:25 +08002172 return NULL;
2173 }
Andy Green6ee372f2012-04-09 15:09:01 +08002174 memset(wsi->user_space, 0,
2175 wsi->protocol->per_session_data_size);
Alex Bligh49146db2011-11-07 17:19:25 +08002176 }
2177 return wsi->user_space;
2178}
Andy Green43db0452013-01-10 19:50:35 +08002179
Andy Green0b319092013-01-19 11:17:56 +08002180static void lwsl_emit_stderr(int level, const char *line)
Andy Greende8f27a2013-01-12 09:17:42 +08002181{
Andy Green0b319092013-01-19 11:17:56 +08002182 char buf[300];
2183 struct timeval tv;
Andy Green0b319092013-01-19 11:17:56 +08002184 int n;
2185
2186 gettimeofday(&tv, NULL);
2187
2188 buf[0] = '\0';
2189 for (n = 0; n < LLL_COUNT; n++)
2190 if (level == (1 << n)) {
Andy Green058ba812013-01-19 11:32:18 +08002191 sprintf(buf, "[%ld:%04d] %s: ", tv.tv_sec,
Andy Greenb5b23192013-02-11 17:13:32 +08002192 (int)(tv.tv_usec / 100), log_level_names[n]);
Andy Green0b319092013-01-19 11:17:56 +08002193 break;
2194 }
Andy Greenb5b23192013-02-11 17:13:32 +08002195
Andy Green0b319092013-01-19 11:17:56 +08002196 fprintf(stderr, "%s%s", buf, line);
Andy Greende8f27a2013-01-12 09:17:42 +08002197}
2198
Joakim Soderberg4c531232013-02-06 15:26:58 +09002199#ifdef WIN32
2200void lwsl_emit_syslog(int level, const char *line)
2201{
2202 lwsl_emit_stderr(level, line);
2203}
2204#else
Andy Greenc11db202013-01-19 11:12:16 +08002205void lwsl_emit_syslog(int level, const char *line)
2206{
2207 int syslog_level = LOG_DEBUG;
2208
2209 switch (level) {
2210 case LLL_ERR:
2211 syslog_level = LOG_ERR;
2212 break;
2213 case LLL_WARN:
2214 syslog_level = LOG_WARNING;
2215 break;
2216 case LLL_NOTICE:
2217 syslog_level = LOG_NOTICE;
2218 break;
2219 case LLL_INFO:
2220 syslog_level = LOG_INFO;
2221 break;
2222 }
Edwin van den Oetelaarf6eeabc2013-01-19 20:01:01 +08002223 syslog(syslog_level, "%s", line);
Andy Greenc11db202013-01-19 11:12:16 +08002224}
Joakim Soderberg4c531232013-02-06 15:26:58 +09002225#endif
Andy Greenc11db202013-01-19 11:12:16 +08002226
Andy Green43db0452013-01-10 19:50:35 +08002227void _lws_log(int filter, const char *format, ...)
2228{
Andy Greende8f27a2013-01-12 09:17:42 +08002229 char buf[256];
Andy Green43db0452013-01-10 19:50:35 +08002230 va_list ap;
Andy Green43db0452013-01-10 19:50:35 +08002231
2232 if (!(log_level & filter))
2233 return;
2234
Andy Green43db0452013-01-10 19:50:35 +08002235 va_start(ap, format);
Andy Greenb5b23192013-02-11 17:13:32 +08002236 vsnprintf(buf, sizeof(buf), format, ap);
2237 buf[sizeof(buf) - 1] = '\0';
Andy Greende8f27a2013-01-12 09:17:42 +08002238 va_end(ap);
2239
Andy Green0b319092013-01-19 11:17:56 +08002240 lwsl_emit(filter, buf);
Andy Green43db0452013-01-10 19:50:35 +08002241}
2242
2243/**
2244 * lws_set_log_level() - Set the logging bitfield
2245 * @level: OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +08002246 * @log_emit_function: NULL to leave it as it is, or a user-supplied
2247 * function to perform log string emission instead of
2248 * the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +08002249 *
Andy Greende8f27a2013-01-12 09:17:42 +08002250 * log level defaults to "err" and "warn" contexts enabled only and
2251 * emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +08002252 */
2253
Andy Greenb5b23192013-02-11 17:13:32 +08002254void lws_set_log_level(int level, void (*log_emit_function)(int level,
2255 const char *line))
Andy Green43db0452013-01-10 19:50:35 +08002256{
2257 log_level = level;
Andy Greende8f27a2013-01-12 09:17:42 +08002258 if (log_emit_function)
2259 lwsl_emit = log_emit_function;
Andy Green43db0452013-01-10 19:50:35 +08002260}