blob: ee950b65ab6c6cc55c801f2e5a6738c210ec95ef [file] [log] [blame]
Andy Green05a0a7b2010-10-31 17:51:39 +00001/*
Andy Greena0da8a82010-11-08 17:12:19 +00002 * libwebsockets - small server side websockets and web server implementation
3 *
4 * 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
Andy Green3faa9c72010-11-08 17:03:03 +000024#ifdef LWS_OPENSSL_SUPPORT
Andy Green3faa9c72010-11-08 17:03:03 +000025SSL_CTX *ssl_ctx;
26int use_ssl;
27#endif
28
Andy Green3faa9c72010-11-08 17:03:03 +000029
Andy Green7c212cc2010-11-08 20:20:42 +000030extern int
Andy Green251f6fa2010-11-03 11:13:06 +000031libwebsocket_read(struct libwebsocket *wsi, unsigned char * buf, size_t len);
Andy Greenff95d7a2010-10-28 22:36:01 +010032
Andy Green775c0dd2010-10-29 14:15:22 +010033
Andy Green4f3943a2010-11-12 10:44:16 +000034/* document the generic callback (it's a fake prototype under this) */
35/**
36 * callback() - User server actions
37 * @wsi: Opaque websocket instance pointer
38 * @reason: The reason for the call
39 * @user: Pointer to per-session user data allocated by library
40 * @in: Pointer used for some callback reasons
41 * @len: Length set for some callback reasons
42 *
43 * This callback is the way the user controls what is served. All the
44 * protocol detail is hidden and handled by the library.
45 *
46 * For each connection / session there is user data allocated that is
47 * pointed to by "user". You set the size of this user data area when
48 * the library is initialized with libwebsocket_create_server.
49 *
50 * You get an opportunity to initialize user data when called back with
51 * LWS_CALLBACK_ESTABLISHED reason.
52 *
53 * LWS_CALLBACK_ESTABLISHED: after successful websocket handshake
54 *
55 * LWS_CALLBACK_CLOSED: when the websocket session ends
56 *
57 * LWS_CALLBACK_SEND: opportunity to send to client (you would use
58 * libwebsocket_write() taking care about the
59 * special buffer requirements
60 * LWS_CALLBACK_RECEIVE: data has appeared for the server, it can be
61 * found at *in and is len bytes long
62 *
63 * LWS_CALLBACK_HTTP: an http request has come from a client that is not
64 * asking to upgrade the connection to a websocket
65 * one. This is a chance to serve http content,
66 * for example, to send a script to the client
67 * which will then open the websockets connection.
68 * @in points to the URI path requested and
69 * libwebsockets_serve_http_file() makes it very
70 * simple to send back a file to the client.
71 */
72extern int callback(struct libwebsocket * wsi,
73 enum libwebsocket_callback_reasons reason, void * user,
74 void *in, size_t len);
75
Andy Green775c0dd2010-10-29 14:15:22 +010076
Andy Green7c212cc2010-11-08 20:20:42 +000077void
Andy Green251f6fa2010-11-03 11:13:06 +000078libwebsocket_close_and_free_session(struct libwebsocket *wsi)
79{
80 int n = wsi->state;
81
82 wsi->state = WSI_STATE_DEAD_SOCKET;
83
Andy Green4f3943a2010-11-12 10:44:16 +000084 if (wsi->protocol->callback && n == WSI_STATE_ESTABLISHED)
Andy Greene77ddd82010-11-13 10:03:47 +000085 wsi->protocol->callback(wsi, LWS_CALLBACK_CLOSED,
86 wsi->user_space, NULL, 0);
Andy Green251f6fa2010-11-03 11:13:06 +000087
88 for (n = 0; n < WSI_TOKEN_COUNT; n++)
89 if (wsi->utf8_token[n].token)
90 free(wsi->utf8_token[n].token);
91
92// fprintf(stderr, "closing fd=%d\n", wsi->sock);
93
Andy Green3faa9c72010-11-08 17:03:03 +000094#ifdef LWS_OPENSSL_SUPPORT
95 if (use_ssl) {
96 n = SSL_get_fd(wsi->ssl);
97 SSL_shutdown(wsi->ssl);
98 close(n);
99 SSL_free(wsi->ssl);
100 } else {
101#endif
102 shutdown(wsi->sock, SHUT_RDWR);
103 close(wsi->sock);
104#ifdef LWS_OPENSSL_SUPPORT
105 }
106#endif
Andy Green4f3943a2010-11-12 10:44:16 +0000107 if (wsi->user_space)
108 free(wsi->user_space);
109
Andy Green251f6fa2010-11-03 11:13:06 +0000110 free(wsi);
111}
112
Andy Greenab990e42010-10-31 12:42:52 +0000113/**
114 * libwebsocket_create_server() - Create the listening websockets server
115 * @port: Port to listen on
Andy Green4f3943a2010-11-12 10:44:16 +0000116 * @protocols: Array of structures listing supported protocols and a protocol-
117 * specific callback for each one. The list is ended with an
118 * entry that has a NULL callback pointer.
Andy Green3faa9c72010-11-08 17:03:03 +0000119 * @ssl_cert_filepath: If libwebsockets was compiled to use ssl, and you want
120 * to listen using SSL, set to the filepath to fetch the
121 * server cert from, otherwise NULL for unencrypted
122 * @ssl_private_key_filepath: filepath to private key if wanting SSL mode,
123 * else ignored
124 * @gid: group id to change to after setting listen socket, or -1.
125 * @uid: user id to change to after setting listen socket, or -1.
Andy Greenab990e42010-10-31 12:42:52 +0000126 *
Andy Green05464c62010-11-12 10:44:18 +0000127 * This function creates the listening socket and takes care
Andy Greenab990e42010-10-31 12:42:52 +0000128 * of all initialization in one step.
Andy Green05464c62010-11-12 10:44:18 +0000129 *
130 * It does not return since it sits in a service loop and operates via the
131 * callbacks given in @protocol. User code should fork before calling
132 * libwebsocket_create_server() if it wants to do other things in
133 * parallel other than serve websockets.
Andy Greenab990e42010-10-31 12:42:52 +0000134 *
Andy Green05464c62010-11-12 10:44:18 +0000135 * The protocol callback functions are called for a handful of events
136 * including http requests coming in, websocket connections becoming
Andy Greenab990e42010-10-31 12:42:52 +0000137 * established, and data arriving; it's also called periodically to allow
138 * async transmission.
Andy Green05464c62010-11-12 10:44:18 +0000139 *
140 * HTTP requests are sent always to the FIRST protocol in @protocol, since
141 * at that time websocket protocol has not been negotiated. Other
142 * protocols after the first one never see any HTTP callack activity.
Andy Greenab990e42010-10-31 12:42:52 +0000143 *
144 * The server created is a simple http server by default; part of the
145 * websocket standard is upgrading this http connection to a websocket one.
146 *
147 * This allows the same server to provide files like scripts and favicon /
148 * images or whatever over http and dynamic data over websockets all in
149 * one place; they're all handled in the user callback.
150 */
Andy Green4ea60062010-10-30 12:15:07 +0100151
Andy Greenea71ed12010-10-31 07:40:33 +0000152int libwebsocket_create_server(int port,
Andy Green4f3943a2010-11-12 10:44:16 +0000153 const struct libwebsocket_protocols *protocols,
Andy Greence510c62010-11-11 12:48:13 +0000154 const char * ssl_cert_filepath,
155 const char * ssl_private_key_filepath,
156 int gid, int uid)
Andy Greenff95d7a2010-10-28 22:36:01 +0100157{
158 int n;
Andy Green251f6fa2010-11-03 11:13:06 +0000159 int client;
Andy Green69fa0722010-11-03 08:25:13 +0000160 int sockfd;
Andy Green251f6fa2010-11-03 11:13:06 +0000161 int fd;
Andy Greenff95d7a2010-10-28 22:36:01 +0100162 unsigned int clilen;
163 struct sockaddr_in serv_addr, cli_addr;
Andy Green251f6fa2010-11-03 11:13:06 +0000164 struct libwebsocket *wsi[MAX_CLIENTS + 1];
165 struct pollfd fds[MAX_CLIENTS + 1];
166 int fds_count = 0;
Andy Green3faa9c72010-11-08 17:03:03 +0000167 unsigned char buf[1024];
Andy Green251f6fa2010-11-03 11:13:06 +0000168 int opt = 1;
Andy Greenff95d7a2010-10-28 22:36:01 +0100169
Andy Green3faa9c72010-11-08 17:03:03 +0000170#ifdef LWS_OPENSSL_SUPPORT
171 const SSL_METHOD *method;
172 char ssl_err_buf[512];
173
174 use_ssl = ssl_cert_filepath != NULL && ssl_private_key_filepath != NULL;
175 if (use_ssl)
176 fprintf(stderr, " Compiled with SSL support, using it\n");
177 else
Andy Green018d8eb2010-11-08 21:04:23 +0000178 fprintf(stderr, " Compiled with SSL support, not using it\n");
Andy Green3faa9c72010-11-08 17:03:03 +0000179
180#else
181 if (ssl_cert_filepath != NULL && ssl_private_key_filepath != NULL) {
182 fprintf(stderr, " Not compiled for OpenSSl support!\n");
183 return -1;
184 }
Andy Green018d8eb2010-11-08 21:04:23 +0000185 fprintf(stderr, " Compiled without SSL support, serving unencrypted\n");
Andy Green3faa9c72010-11-08 17:03:03 +0000186#endif
187
188#ifdef LWS_OPENSSL_SUPPORT
189 if (use_ssl) {
190 SSL_library_init();
191
192 OpenSSL_add_all_algorithms();
193 SSL_load_error_strings();
194
195 // Firefox insists on SSLv23 not SSLv3
196 // Konq disables SSLv2 by default now, SSLv23 works
197
198 method = SSLv23_server_method(); // create server instance
199 if (!method) {
200 fprintf(stderr, "problem creating ssl method: %s\n",
201 ERR_error_string(ERR_get_error(), ssl_err_buf));
202 return -1;
203 }
204 ssl_ctx = SSL_CTX_new(method); /* create context */
205 if (!ssl_ctx) {
206 printf("problem creating ssl context: %s\n",
207 ERR_error_string(ERR_get_error(), ssl_err_buf));
208 return -1;
209 }
210 /* set the local certificate from CertFile */
211 n = SSL_CTX_use_certificate_file(ssl_ctx,
212 ssl_cert_filepath, SSL_FILETYPE_PEM);
213 if (n != 1) {
214 fprintf(stderr, "problem getting cert '%s': %s\n",
215 ssl_cert_filepath,
216 ERR_error_string(ERR_get_error(), ssl_err_buf));
217 return -1;
218 }
219 /* set the private key from KeyFile */
Andy Green018d8eb2010-11-08 21:04:23 +0000220 if (SSL_CTX_use_PrivateKey_file(ssl_ctx,
221 ssl_private_key_filepath,
222 SSL_FILETYPE_PEM) != 1) {
223 fprintf(stderr, "ssl problem getting key '%s': %s\n",
224 ssl_private_key_filepath,
225 ERR_error_string(ERR_get_error(), ssl_err_buf));
Andy Green3faa9c72010-11-08 17:03:03 +0000226 return (-1);
227 }
228 /* verify private key */
229 if (!SSL_CTX_check_private_key(ssl_ctx)) {
Andy Green018d8eb2010-11-08 21:04:23 +0000230 fprintf(stderr, "Private SSL key doesn't match cert\n");
Andy Green3faa9c72010-11-08 17:03:03 +0000231 return (-1);
232 }
233
234 /* SSL is happy and has a cert it's content with */
235 }
236#endif
Andy Green4f3943a2010-11-12 10:44:16 +0000237
Andy Green775c0dd2010-10-29 14:15:22 +0100238 sockfd = socket(AF_INET, SOCK_STREAM, 0);
239 if (sockfd < 0) {
240 fprintf(stderr, "ERROR opening socket");
Andy Green251f6fa2010-11-03 11:13:06 +0000241 return -1;
Andy Green775c0dd2010-10-29 14:15:22 +0100242 }
Andy Green251f6fa2010-11-03 11:13:06 +0000243
244 /* allow us to restart even if old sockets in TIME_WAIT */
245 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
Andy Green775c0dd2010-10-29 14:15:22 +0100246
Andy Green251f6fa2010-11-03 11:13:06 +0000247 bzero((char *) &serv_addr, sizeof(serv_addr));
Andy Green775c0dd2010-10-29 14:15:22 +0100248 serv_addr.sin_family = AF_INET;
249 serv_addr.sin_addr.s_addr = INADDR_ANY;
250 serv_addr.sin_port = htons(port);
Andy Greene77ddd82010-11-13 10:03:47 +0000251
Andy Green775c0dd2010-10-29 14:15:22 +0100252 n = bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
253 if (n < 0) {
Andy Greenea71ed12010-10-31 07:40:33 +0000254 fprintf(stderr, "ERROR on binding to port %d (%d %d)\n", port, n,
255 errno);
Andy Green775c0dd2010-10-29 14:15:22 +0100256 return -1;
257 }
258
Andy Greene77ddd82010-11-13 10:03:47 +0000259 /* drop any root privs for this process */
Andy Green3faa9c72010-11-08 17:03:03 +0000260
261 if (gid != -1)
262 if (setgid(gid))
263 fprintf(stderr, "setgid: %s\n", strerror(errno));
264 if (uid != -1)
265 if (setuid(uid))
266 fprintf(stderr, "setuid: %s\n", strerror(errno));
267
Andy Green4f3943a2010-11-12 10:44:16 +0000268 /*
269 * sit there listening for connects, accept and service connections
Andy Green05464c62010-11-12 10:44:18 +0000270 * in a poll loop, without any forking
Andy Green4f3943a2010-11-12 10:44:16 +0000271 */
272
Andy Greenff95d7a2010-10-28 22:36:01 +0100273 listen(sockfd, 5);
Andy Green251f6fa2010-11-03 11:13:06 +0000274 fprintf(stderr, " Listening on port %d\n", port);
275
276 fds[0].fd = sockfd;
277 fds_count = 1;
278 fds[0].events = POLLIN;
Andy Greenff95d7a2010-10-28 22:36:01 +0100279
280 while (1) {
Andy Greenff95d7a2010-10-28 22:36:01 +0100281
Andy Green251f6fa2010-11-03 11:13:06 +0000282 n = poll(fds, fds_count, 50);
283 if (n < 0 || fds[0].revents & (POLLERR | POLLHUP)) {
Andy Green85ba32f2010-11-12 11:47:39 +0000284 fprintf(stderr, "Listen Socket dead\n");
Andy Green251f6fa2010-11-03 11:13:06 +0000285 goto fatal;
Andy Green775c0dd2010-10-29 14:15:22 +0100286 }
Andy Green251f6fa2010-11-03 11:13:06 +0000287 if (n == 0) /* poll timeout */
288 goto poll_out;
289
290 if (fds[0].revents & POLLIN) {
291
Andy Green3faa9c72010-11-08 17:03:03 +0000292 /* listen socket got an unencrypted connection... */
293
Andy Green251f6fa2010-11-03 11:13:06 +0000294 clilen = sizeof(cli_addr);
Andy Green3faa9c72010-11-08 17:03:03 +0000295 fd = accept(sockfd,
296 (struct sockaddr *)&cli_addr,
297 &clilen);
Andy Green251f6fa2010-11-03 11:13:06 +0000298 if (fd < 0) {
299 fprintf(stderr, "ERROR on accept");
300 continue;
301 }
Andy Green3faa9c72010-11-08 17:03:03 +0000302
Andy Green251f6fa2010-11-03 11:13:06 +0000303 if (fds_count >= MAX_CLIENTS) {
304 fprintf(stderr, "too busy");
305 close(fd);
306 continue;
307 }
Andy Green3faa9c72010-11-08 17:03:03 +0000308
Andy Green4f3943a2010-11-12 10:44:16 +0000309 wsi[fds_count] = malloc(sizeof(struct libwebsocket));
Andy Green251f6fa2010-11-03 11:13:06 +0000310 if (!wsi[fds_count])
311 return -1;
Andy Green3faa9c72010-11-08 17:03:03 +0000312
Andy Green3faa9c72010-11-08 17:03:03 +0000313#ifdef LWS_OPENSSL_SUPPORT
314 if (use_ssl) {
315
Andy Green018d8eb2010-11-08 21:04:23 +0000316 wsi[fds_count]->ssl = SSL_new(ssl_ctx);
Andy Green3faa9c72010-11-08 17:03:03 +0000317 if (wsi[fds_count]->ssl == NULL) {
318 fprintf(stderr, "SSL_new failed: %s\n",
Andy Green018d8eb2010-11-08 21:04:23 +0000319 ERR_error_string(SSL_get_error(
320 wsi[fds_count]->ssl, 0), NULL));
Andy Green3faa9c72010-11-08 17:03:03 +0000321 free(wsi[fds_count]);
322 continue;
323 }
324
Andy Green018d8eb2010-11-08 21:04:23 +0000325 SSL_set_fd(wsi[fds_count]->ssl, fd);
Andy Green3faa9c72010-11-08 17:03:03 +0000326
327 n = SSL_accept(wsi[fds_count]->ssl);
328 if (n != 1) {
Andy Green018d8eb2010-11-08 21:04:23 +0000329 /*
330 * browsers seem to probe with various
331 * ssl params which fail then retry
332 * and succeed
333 */
334 debug("SSL_accept failed skt %u: %s\n",
Andy Green3faa9c72010-11-08 17:03:03 +0000335 fd,
Andy Green018d8eb2010-11-08 21:04:23 +0000336 ERR_error_string(SSL_get_error(
337 wsi[fds_count]->ssl, n), NULL));
Andy Green3faa9c72010-11-08 17:03:03 +0000338 SSL_free(wsi[fds_count]->ssl);
339 free(wsi[fds_count]);
340 continue;
341 }
Andy Green018d8eb2010-11-08 21:04:23 +0000342 debug("accepted new SSL conn "
343 "port %u on fd=%d SSL ver %s\n",
344 ntohs(cli_addr.sin_port), fd,
345 SSL_get_version(wsi[fds_count]->ssl));
Andy Green3faa9c72010-11-08 17:03:03 +0000346
Andy Green4f3943a2010-11-12 10:44:16 +0000347 } else
Andy Green3faa9c72010-11-08 17:03:03 +0000348#endif
Andy Green4f3943a2010-11-12 10:44:16 +0000349 debug("accepted new conn port %u on fd=%d\n",
350 ntohs(cli_addr.sin_port), fd);
Andy Green3faa9c72010-11-08 17:03:03 +0000351
352 /* intialize the instance struct */
353
Andy Green251f6fa2010-11-03 11:13:06 +0000354 wsi[fds_count]->sock = fd;
355 wsi[fds_count]->state = WSI_STATE_HTTP;
356 wsi[fds_count]->name_buffer_pos = 0;
357
358 for (n = 0; n < WSI_TOKEN_COUNT; n++) {
359 wsi[fds_count]->utf8_token[n].token = NULL;
360 wsi[fds_count]->utf8_token[n].token_len = 0;
361 }
362
Andy Green4f3943a2010-11-12 10:44:16 +0000363 /*
364 * these can only be set once the protocol is known
365 * we set an unestablished connection's protocol pointer
366 * to the start of the supported list, so it can look
367 * for matching ones during the handshake
368 */
369 wsi[fds_count]->protocol = protocols;
370 wsi[fds_count]->user_space = NULL;
371
Andy Greence510c62010-11-11 12:48:13 +0000372 /*
373 * Default protocol is 76
374 * After 76, there's a header specified to inform which
Andy Green4f3943a2010-11-12 10:44:16 +0000375 * draft the client wants, when that's seen we modify
376 * the individual connection's spec revision accordingly
Andy Greence510c62010-11-11 12:48:13 +0000377 */
378 wsi[fds_count]->ietf_spec_revision = 76;
Andy Green251f6fa2010-11-03 11:13:06 +0000379
380 fds[fds_count].events = POLLIN;
381 fds[fds_count++].fd = fd;
Andy Green85ba32f2010-11-12 11:47:39 +0000382
383 /*
384 * make sure NO events are seen yet on this new socket
385 * (otherwise we inherit old fds[client].revents from
386 * previous socket there and die mysteriously! )
387 */
388 fds[client].revents = 0;
Andy Green775c0dd2010-10-29 14:15:22 +0100389 }
390
Andy Green251f6fa2010-11-03 11:13:06 +0000391 /* check for activity on client sockets */
392
393 for (client = 1; client < fds_count; client++) {
394
395 /* handle session socket closed */
396
397 if (fds[client].revents & (POLLERR | POLLHUP)) {
398
Andy Green85ba32f2010-11-12 11:47:39 +0000399 debug("Session Socket %d %p (fd=%d) dead\n",
400 client, wsi[client], fds[client]);
Andy Green251f6fa2010-11-03 11:13:06 +0000401
Andy Green018d8eb2010-11-08 21:04:23 +0000402 libwebsocket_close_and_free_session(
403 wsi[client]);
Andy Green251f6fa2010-11-03 11:13:06 +0000404 goto nuke_this;
405 }
406
407 /* any incoming data ready? */
408
409 if (!(fds[client].revents & POLLIN))
410 continue;
Andy Green3faa9c72010-11-08 17:03:03 +0000411
412#ifdef LWS_OPENSSL_SUPPORT
413 if (use_ssl)
414 n = SSL_read(wsi[client]->ssl, buf, sizeof buf);
415 else
416#endif
417 n = recv(fds[client].fd, buf, sizeof(buf), 0);
418
Andy Green251f6fa2010-11-03 11:13:06 +0000419 if (n < 0) {
420 fprintf(stderr, "Socket read returned %d\n", n);
421 continue;
422 }
423 if (!n) {
424// fprintf(stderr, "POLLIN with 0 len waiting\n");
Andy Green018d8eb2010-11-08 21:04:23 +0000425 libwebsocket_close_and_free_session(
426 wsi[client]);
Andy Green251f6fa2010-11-03 11:13:06 +0000427 goto nuke_this;
428 }
429
430 /* service incoming data */
431
432 if (libwebsocket_read(wsi[client], buf, n) >= 0)
433 continue;
434
Andy Green4f3943a2010-11-12 10:44:16 +0000435 /*
436 * it closed and nuked wsi[client], so remove the
437 * socket handle and wsi from our service list
438 */
Andy Green251f6fa2010-11-03 11:13:06 +0000439nuke_this:
Andy Green85ba32f2010-11-12 11:47:39 +0000440
441 debug("nuking wsi %p, fsd_count = %d\n",
442 wsi[client], fds_count - 1);
443
444 fds_count--;
445 for (n = client; n < fds_count; n++) {
Andy Green251f6fa2010-11-03 11:13:06 +0000446 fds[n] = fds[n + 1];
447 wsi[n] = wsi[n + 1];
448 }
Andy Green85ba32f2010-11-12 11:47:39 +0000449 break;
Andy Green775c0dd2010-10-29 14:15:22 +0100450 }
451
Andy Green251f6fa2010-11-03 11:13:06 +0000452poll_out:
453 for (client = 1; client < fds_count; client++) {
Andy Green775c0dd2010-10-29 14:15:22 +0100454
Andy Green251f6fa2010-11-03 11:13:06 +0000455 if (wsi[client]->state != WSI_STATE_ESTABLISHED)
456 continue;
Andy Green69fa0722010-11-03 08:25:13 +0000457
Andy Greene77ddd82010-11-13 10:03:47 +0000458 wsi[client]->protocol->callback(wsi[client],
459 LWS_CALLBACK_SEND,
460 wsi[client]->user_space,
461 NULL, 0);
Andy Green251f6fa2010-11-03 11:13:06 +0000462 }
463
464 continue;
Andy Greenff95d7a2010-10-28 22:36:01 +0100465 }
Andy Green251f6fa2010-11-03 11:13:06 +0000466
467fatal:
Andy Green3faa9c72010-11-08 17:03:03 +0000468 /* listening socket */
Andy Green251f6fa2010-11-03 11:13:06 +0000469 close(fds[0].fd);
470 for (client = 1; client < fds_count; client++)
471 libwebsocket_close_and_free_session(wsi[client]);
Andy Greenff95d7a2010-10-28 22:36:01 +0100472
Andy Green3faa9c72010-11-08 17:03:03 +0000473#ifdef LWS_OPENSSL_SUPPORT
474 SSL_CTX_free(ssl_ctx);
475#endif
Andy Green251f6fa2010-11-03 11:13:06 +0000476 kill(0, SIGTERM);
477
478 return 0;
Andy Greenff95d7a2010-10-28 22:36:01 +0100479}
480
Andy Greenab990e42010-10-31 12:42:52 +0000481