2017-08-12 14:01:57 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 Christian Meffert <christian.meffert@googlemail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
2021-07-05 15:40:31 -04:00
|
|
|
|
2017-08-12 14:01:57 -04:00
|
|
|
#include <json.h>
|
|
|
|
#include <libwebsockets.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include "conffile.h"
|
|
|
|
#include "listener.h"
|
|
|
|
#include "logger.h"
|
2021-07-05 15:40:31 -04:00
|
|
|
#include "misc.h"
|
2017-08-12 14:01:57 -04:00
|
|
|
|
|
|
|
|
|
|
|
static struct lws_context *context;
|
|
|
|
static pthread_t tid_websocket;
|
|
|
|
|
2021-02-27 17:01:26 -05:00
|
|
|
static const char *websocket_interface;
|
2017-08-12 14:01:57 -04:00
|
|
|
static int websocket_port;
|
2020-08-10 16:36:00 -04:00
|
|
|
static bool websocket_exit = false;
|
2017-08-12 14:01:57 -04:00
|
|
|
|
2021-05-13 16:54:11 -04:00
|
|
|
// Lock the event mask of events processed by the writeable callback
|
|
|
|
static pthread_mutex_t websocket_write_event_lock;
|
2017-08-12 14:01:57 -04:00
|
|
|
// Event mask of events processed by the writeable callback
|
2020-08-10 16:36:00 -04:00
|
|
|
static short websocket_write_events;
|
2017-08-12 14:01:57 -04:00
|
|
|
|
|
|
|
|
|
|
|
/* Thread: library (the thread the event occurred) */
|
|
|
|
static void
|
2017-11-13 15:58:54 -05:00
|
|
|
listener_cb(short event_mask)
|
2017-08-12 14:01:57 -04:00
|
|
|
{
|
2021-05-13 16:54:11 -04:00
|
|
|
pthread_mutex_lock(&websocket_write_event_lock);
|
|
|
|
websocket_write_events |= event_mask;
|
|
|
|
pthread_mutex_unlock(&websocket_write_event_lock);
|
2021-05-21 17:43:00 -04:00
|
|
|
|
2021-05-13 16:54:11 -04:00
|
|
|
lws_cancel_service(context);
|
2017-08-12 14:01:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Libwebsocket requires the HTTP protocol to be the first supported protocol
|
|
|
|
*
|
|
|
|
* This adds an empty implementation, because we are serving HTTP over libevent in httpd.h.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
|
|
|
|
{
|
2018-05-28 14:58:36 -04:00
|
|
|
return 0;
|
2017-08-12 14:01:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Each session of the "notify" protocol holds this event mask
|
|
|
|
*
|
|
|
|
* The client sends the events it wants to be notified of and the event mask is
|
|
|
|
* set accordingly translating them to the LISTENER enum (see listener.h)
|
|
|
|
*/
|
2021-05-13 16:54:11 -04:00
|
|
|
struct per_session_data {
|
|
|
|
struct per_session_data *pss_list;
|
|
|
|
struct lws *wsi;
|
|
|
|
short requested_events;
|
|
|
|
short write_events;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* one of these is created for each vhost our protocol is used with */
|
|
|
|
struct per_vhost_data {
|
|
|
|
struct per_session_data *pss_list; /* linked-list of live pss*/
|
2017-08-12 14:01:57 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Processes client requests to the notify-protocol
|
|
|
|
*
|
|
|
|
* Expects the message in "in" to be a JSON string of the form:
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* "notify": [ "update" ]
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
static int
|
2021-05-13 16:54:11 -04:00
|
|
|
process_notify_request(short *requested_events, void *in, size_t len)
|
2017-08-12 14:01:57 -04:00
|
|
|
{
|
|
|
|
json_tokener *tokener;
|
|
|
|
json_object *request;
|
|
|
|
json_object *item;
|
|
|
|
int count, i;
|
|
|
|
enum json_tokener_error jerr;
|
|
|
|
json_object *needle;
|
|
|
|
const char *event_type;
|
|
|
|
|
2021-05-13 16:54:11 -04:00
|
|
|
*requested_events = 0;
|
2017-08-12 14:01:57 -04:00
|
|
|
|
|
|
|
tokener = json_tokener_new();
|
|
|
|
request = json_tokener_parse_ex(tokener, in, len);
|
|
|
|
jerr = json_tokener_get_error(tokener);
|
|
|
|
|
|
|
|
if (jerr != json_tokener_success)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Failed to parse incoming request: %s\n", json_tokener_error_desc(jerr));
|
|
|
|
json_tokener_free(tokener);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DPRINTF(E_DBG, L_WEB, "notify callback request: %s\n", json_object_to_json_string(request));
|
|
|
|
|
|
|
|
if (json_object_object_get_ex(request, "notify", &needle) && json_object_get_type(needle) == json_type_array)
|
|
|
|
{
|
|
|
|
count = json_object_array_length(needle);
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
item = json_object_array_get_idx(needle, i);
|
|
|
|
|
|
|
|
if (json_object_get_type(item) == json_type_string)
|
|
|
|
{
|
|
|
|
event_type = json_object_get_string(item);
|
|
|
|
DPRINTF(E_DBG, L_WEB, "notify callback event received: %s\n", event_type);
|
|
|
|
|
|
|
|
if (0 == strcmp(event_type, "update"))
|
|
|
|
{
|
2021-05-13 16:54:11 -04:00
|
|
|
*requested_events |= LISTENER_UPDATE;
|
2017-08-12 14:01:57 -04:00
|
|
|
}
|
2018-12-30 03:04:00 -05:00
|
|
|
else if (0 == strcmp(event_type, "database"))
|
|
|
|
{
|
2021-05-13 16:54:11 -04:00
|
|
|
*requested_events |= LISTENER_DATABASE;
|
2018-12-30 03:04:00 -05:00
|
|
|
}
|
2017-08-12 14:01:57 -04:00
|
|
|
else if (0 == strcmp(event_type, "pairing"))
|
|
|
|
{
|
2021-05-13 16:54:11 -04:00
|
|
|
*requested_events |= LISTENER_PAIRING;
|
2017-08-12 14:01:57 -04:00
|
|
|
}
|
|
|
|
else if (0 == strcmp(event_type, "spotify"))
|
|
|
|
{
|
2021-05-13 16:54:11 -04:00
|
|
|
*requested_events |= LISTENER_SPOTIFY;
|
2017-08-12 14:01:57 -04:00
|
|
|
}
|
2017-09-24 00:57:49 -04:00
|
|
|
else if (0 == strcmp(event_type, "lastfm"))
|
|
|
|
{
|
2021-05-13 16:54:11 -04:00
|
|
|
*requested_events |= LISTENER_LASTFM;
|
2017-09-24 00:57:49 -04:00
|
|
|
}
|
2022-05-07 06:12:22 -04:00
|
|
|
else if (0 == strcmp(event_type, "outputs"))
|
2017-10-31 08:39:06 -04:00
|
|
|
{
|
2021-05-13 16:54:11 -04:00
|
|
|
*requested_events |= LISTENER_SPEAKER;
|
2017-10-31 08:39:06 -04:00
|
|
|
}
|
2017-11-01 10:09:37 -04:00
|
|
|
else if (0 == strcmp(event_type, "player"))
|
|
|
|
{
|
2021-05-13 16:54:11 -04:00
|
|
|
*requested_events |= LISTENER_PLAYER;
|
2017-11-01 10:09:37 -04:00
|
|
|
}
|
|
|
|
else if (0 == strcmp(event_type, "options"))
|
|
|
|
{
|
2021-05-13 16:54:11 -04:00
|
|
|
*requested_events |= LISTENER_OPTIONS;
|
2017-11-01 10:09:37 -04:00
|
|
|
}
|
|
|
|
else if (0 == strcmp(event_type, "volume"))
|
|
|
|
{
|
2021-05-13 16:54:11 -04:00
|
|
|
*requested_events |= LISTENER_VOLUME;
|
2017-11-01 10:09:37 -04:00
|
|
|
}
|
|
|
|
else if (0 == strcmp(event_type, "queue"))
|
|
|
|
{
|
2021-05-13 16:54:11 -04:00
|
|
|
*requested_events |= LISTENER_QUEUE;
|
2017-11-01 10:09:37 -04:00
|
|
|
}
|
2017-08-12 14:01:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
json_tokener_free(tokener);
|
|
|
|
json_object_put(request);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Notify clients of the notify-protocol about occurred events
|
|
|
|
*
|
|
|
|
* Sends a JSON message of the form:
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* "notify": [ "update" ]
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
send_notify_reply(short events, struct lws* wsi)
|
|
|
|
{
|
|
|
|
unsigned char* buf;
|
|
|
|
const char* json_response;
|
|
|
|
json_object* reply;
|
|
|
|
json_object* notify;
|
|
|
|
|
2021-05-13 16:54:11 -04:00
|
|
|
DPRINTF(E_DBG, L_WEB, "notify callback reply: %d\n", events);
|
|
|
|
|
2017-08-12 14:01:57 -04:00
|
|
|
notify = json_object_new_array();
|
|
|
|
if (events & LISTENER_UPDATE)
|
|
|
|
{
|
|
|
|
json_object_array_add(notify, json_object_new_string("update"));
|
|
|
|
}
|
2018-12-30 03:04:00 -05:00
|
|
|
if (events & LISTENER_DATABASE)
|
|
|
|
{
|
|
|
|
json_object_array_add(notify, json_object_new_string("database"));
|
|
|
|
}
|
2017-08-12 14:01:57 -04:00
|
|
|
if (events & LISTENER_PAIRING)
|
|
|
|
{
|
|
|
|
json_object_array_add(notify, json_object_new_string("pairing"));
|
|
|
|
}
|
|
|
|
if (events & LISTENER_SPOTIFY)
|
|
|
|
{
|
|
|
|
json_object_array_add(notify, json_object_new_string("spotify"));
|
|
|
|
}
|
2017-09-24 00:57:49 -04:00
|
|
|
if (events & LISTENER_LASTFM)
|
|
|
|
{
|
|
|
|
json_object_array_add(notify, json_object_new_string("lastfm"));
|
|
|
|
}
|
2017-10-31 08:39:06 -04:00
|
|
|
if (events & LISTENER_SPEAKER)
|
|
|
|
{
|
|
|
|
json_object_array_add(notify, json_object_new_string("outputs"));
|
|
|
|
}
|
2017-11-01 10:09:37 -04:00
|
|
|
if (events & LISTENER_PLAYER)
|
|
|
|
{
|
|
|
|
json_object_array_add(notify, json_object_new_string("player"));
|
|
|
|
}
|
|
|
|
if (events & LISTENER_OPTIONS)
|
|
|
|
{
|
|
|
|
json_object_array_add(notify, json_object_new_string("options"));
|
|
|
|
}
|
|
|
|
if (events & LISTENER_VOLUME)
|
|
|
|
{
|
|
|
|
json_object_array_add(notify, json_object_new_string("volume"));
|
|
|
|
}
|
|
|
|
if (events & LISTENER_QUEUE)
|
|
|
|
{
|
|
|
|
json_object_array_add(notify, json_object_new_string("queue"));
|
|
|
|
}
|
2017-08-12 14:01:57 -04:00
|
|
|
|
|
|
|
reply = json_object_new_object();
|
|
|
|
json_object_object_add(reply, "notify", notify);
|
|
|
|
|
|
|
|
json_response = json_object_to_json_string(reply);
|
|
|
|
|
|
|
|
buf = malloc(LWS_PRE + strlen(json_response));
|
|
|
|
memcpy(&buf[LWS_PRE], json_response, strlen(json_response));
|
|
|
|
lws_write(wsi, &buf[LWS_PRE], strlen(json_response), LWS_WRITE_TEXT);
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
json_object_put(reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Callback for the "notify" protocol
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
callback_notify(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
|
|
|
|
{
|
2021-05-13 16:54:11 -04:00
|
|
|
struct per_session_data *pss = user;
|
2021-05-17 06:47:57 -04:00
|
|
|
#if LWS_LIBRARY_VERSION_MAJOR < 3
|
|
|
|
struct per_session_data **ppss = NULL;
|
|
|
|
#endif
|
|
|
|
struct per_vhost_data *vhd = lws_protocol_vh_priv_get(
|
|
|
|
#if LWS_LIBRARY_VERSION_MAJOR >= 3
|
|
|
|
lws_get_vhost(wsi),
|
|
|
|
#else
|
|
|
|
lws_vhost_get(wsi),
|
|
|
|
#endif
|
|
|
|
lws_get_protocol(wsi));
|
2021-05-13 16:54:11 -04:00
|
|
|
short events = 0;
|
2017-08-12 14:01:57 -04:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
DPRINTF(E_DBG, L_WEB, "notify callback reason: %d\n", reason);
|
|
|
|
|
2021-05-13 16:54:11 -04:00
|
|
|
switch (reason)
|
|
|
|
{
|
|
|
|
case LWS_CALLBACK_PROTOCOL_INIT:
|
2021-05-17 06:47:57 -04:00
|
|
|
vhd = lws_protocol_vh_priv_zalloc(
|
|
|
|
#if LWS_LIBRARY_VERSION_MAJOR >= 3
|
|
|
|
lws_get_vhost(wsi),
|
|
|
|
#else
|
|
|
|
lws_vhost_get(wsi),
|
|
|
|
#endif
|
|
|
|
lws_get_protocol(wsi),
|
|
|
|
sizeof(struct per_vhost_data));
|
2021-05-13 16:54:11 -04:00
|
|
|
if (!vhd)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Failed to allocate websocket per-vhoststorage\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_ESTABLISHED:
|
|
|
|
/* add ourselves to the list of live pss held in the vhd */
|
2021-05-17 06:47:57 -04:00
|
|
|
if (vhd)
|
|
|
|
{
|
|
|
|
#if LWS_LIBRARY_VERSION_MAJOR >= 3
|
|
|
|
lws_ll_fwd_insert(pss, pss_list, vhd->pss_list);
|
|
|
|
#else
|
|
|
|
pss->pss_list = vhd->pss_list;
|
|
|
|
vhd->pss_list = pss;
|
|
|
|
#endif
|
|
|
|
pss->wsi = wsi;
|
|
|
|
}
|
2021-05-13 16:54:11 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_CLOSED:
|
|
|
|
/* remove our closing pss from the list of live pss */
|
2021-05-17 06:47:57 -04:00
|
|
|
if (vhd)
|
|
|
|
{
|
|
|
|
#if LWS_LIBRARY_VERSION_MAJOR >= 3
|
|
|
|
lws_ll_fwd_remove(struct per_session_data, pss_list, pss, vhd->pss_list);
|
|
|
|
#else
|
|
|
|
ppss = &(vhd->pss_list);
|
|
|
|
while (*ppss) {
|
|
|
|
if (*ppss == pss) {
|
|
|
|
*ppss = pss->pss_list;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ppss = &(*ppss)->pss_list;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2021-05-13 16:54:11 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_SERVER_WRITEABLE:
|
2021-05-17 06:47:57 -04:00
|
|
|
#if LWS_LIBRARY_VERSION_MAJOR < 3
|
|
|
|
pthread_mutex_lock(&websocket_write_event_lock);
|
|
|
|
events = websocket_write_events;
|
|
|
|
websocket_write_events = 0;
|
|
|
|
pthread_mutex_unlock(&websocket_write_event_lock);
|
|
|
|
if (vhd && events)
|
|
|
|
{
|
|
|
|
ppss = &(vhd->pss_list);
|
|
|
|
while (*ppss) {
|
|
|
|
(*ppss)->write_events |= events;
|
|
|
|
ppss = &(*ppss)->pss_list;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2021-05-13 16:54:11 -04:00
|
|
|
if (pss->requested_events & pss->write_events)
|
|
|
|
{
|
|
|
|
events = pss->requested_events & pss->write_events;
|
|
|
|
send_notify_reply(events, wsi);
|
|
|
|
pss->write_events = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_RECEIVE:
|
|
|
|
ret = process_notify_request(&pss->requested_events, in, len);
|
|
|
|
break;
|
|
|
|
|
2021-05-17 06:47:57 -04:00
|
|
|
#if LWS_LIBRARY_VERSION_MAJOR >= 3
|
2021-05-13 16:54:11 -04:00
|
|
|
case LWS_CALLBACK_EVENT_WAIT_CANCELLED:
|
|
|
|
if (vhd)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&websocket_write_event_lock);
|
|
|
|
events = websocket_write_events;
|
|
|
|
websocket_write_events = 0;
|
|
|
|
pthread_mutex_unlock(&websocket_write_event_lock);
|
|
|
|
lws_start_foreach_llp(struct per_session_data **, ppss, vhd->pss_list)
|
|
|
|
{
|
2021-05-17 06:47:57 -04:00
|
|
|
(*ppss)->write_events |= events;
|
2021-05-13 16:54:11 -04:00
|
|
|
lws_callback_on_writable((*ppss)->wsi);
|
|
|
|
} lws_end_foreach_llp(ppss, pss_list);
|
|
|
|
}
|
|
|
|
break;
|
2021-05-17 06:47:57 -04:00
|
|
|
#endif
|
2021-05-13 16:54:11 -04:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-08-12 14:01:57 -04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-05-17 06:47:57 -04:00
|
|
|
/*
|
|
|
|
* Supported protocols of the websocket, needs to be in line with the protocols array
|
|
|
|
*/
|
|
|
|
enum ws_protocols
|
|
|
|
{
|
|
|
|
WS_PROTOCOL_HTTP = 0,
|
|
|
|
WS_PROTOCOL_NOTIFY,
|
|
|
|
};
|
|
|
|
|
2017-08-12 14:01:57 -04:00
|
|
|
static struct lws_protocols protocols[] =
|
|
|
|
{
|
|
|
|
// The first protocol must always be the HTTP handler
|
|
|
|
{
|
|
|
|
"http-only", // Protocol name
|
|
|
|
callback_http, // Callback function
|
|
|
|
0, // Size of per session data
|
|
|
|
0, // Frame size / rx buffer (0 = max frame size)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"notify",
|
|
|
|
callback_notify,
|
2021-05-13 16:54:11 -04:00
|
|
|
sizeof(struct per_session_data),
|
2017-08-12 14:01:57 -04:00
|
|
|
0,
|
|
|
|
},
|
|
|
|
{ NULL, NULL, 0, 0 } // terminator
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Thread: websocket */
|
|
|
|
static void *
|
|
|
|
websocket(void *arg)
|
|
|
|
{
|
2018-12-30 03:04:00 -05:00
|
|
|
listener_add(listener_cb, LISTENER_UPDATE | LISTENER_DATABASE | LISTENER_PAIRING | LISTENER_SPOTIFY | LISTENER_LASTFM | LISTENER_SPEAKER
|
2021-05-13 16:54:11 -04:00
|
|
|
| LISTENER_PLAYER | LISTENER_OPTIONS | LISTENER_VOLUME | LISTENER_QUEUE);
|
2017-08-12 14:01:57 -04:00
|
|
|
|
2020-08-10 16:36:00 -04:00
|
|
|
while(!websocket_exit)
|
2021-05-13 16:54:11 -04:00
|
|
|
{
|
2021-05-17 06:47:57 -04:00
|
|
|
#if LWS_LIBRARY_VERSION_MAJOR >= 3
|
2021-05-13 16:54:11 -04:00
|
|
|
if (lws_service(context, 0))
|
|
|
|
websocket_exit = true;
|
2021-05-17 06:47:57 -04:00
|
|
|
#else
|
|
|
|
lws_service(context, 10000);
|
|
|
|
if (websocket_write_events)
|
|
|
|
lws_callback_on_writable_all_protocol(context, &protocols[WS_PROTOCOL_NOTIFY]);
|
|
|
|
#endif
|
2021-05-13 16:54:11 -04:00
|
|
|
}
|
2017-08-12 14:01:57 -04:00
|
|
|
|
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
|
2018-05-28 14:58:36 -04:00
|
|
|
static void
|
|
|
|
logger_libwebsockets(int level, const char *line)
|
|
|
|
{
|
|
|
|
int severity;
|
|
|
|
|
|
|
|
switch (level)
|
|
|
|
{
|
|
|
|
case LLL_ERR:
|
2021-05-13 16:54:11 -04:00
|
|
|
severity = E_LOG;
|
|
|
|
break;
|
2018-05-28 14:58:36 -04:00
|
|
|
|
|
|
|
case LLL_WARN:
|
2021-05-13 16:54:11 -04:00
|
|
|
severity = E_WARN;
|
|
|
|
break;
|
2018-05-28 14:58:36 -04:00
|
|
|
|
|
|
|
case LLL_NOTICE:
|
2021-05-13 16:54:11 -04:00
|
|
|
severity = E_DBG;
|
|
|
|
break;
|
2018-05-28 14:58:36 -04:00
|
|
|
|
2018-09-07 18:24:49 -04:00
|
|
|
case LLL_INFO:
|
2018-05-28 14:58:36 -04:00
|
|
|
case LLL_DEBUG:
|
2021-05-13 16:54:11 -04:00
|
|
|
severity = E_SPAM;
|
|
|
|
break;
|
2018-05-28 14:58:36 -04:00
|
|
|
|
|
|
|
default:
|
2021-05-13 16:54:11 -04:00
|
|
|
severity = E_LOG;
|
|
|
|
break;
|
2018-05-28 14:58:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
DPRINTF(severity, L_WEB, "LWS %s", line);
|
|
|
|
}
|
|
|
|
|
2017-08-12 14:01:57 -04:00
|
|
|
int
|
|
|
|
websocket_init(void)
|
|
|
|
{
|
|
|
|
struct lws_context_creation_info info;
|
|
|
|
int ret;
|
|
|
|
|
2021-02-27 17:01:26 -05:00
|
|
|
websocket_interface = cfg_getstr(cfg_getsec(cfg, "general"), "websocket_interface");
|
2017-08-12 14:01:57 -04:00
|
|
|
websocket_port = cfg_getint(cfg_getsec(cfg, "general"), "websocket_port");
|
|
|
|
|
|
|
|
if (websocket_port <= 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Websocket disabled. To enable it, set websocket_port in config to a valid port number.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
info.port = websocket_port;
|
2021-02-27 17:01:26 -05:00
|
|
|
info.iface = websocket_interface;
|
2017-08-12 14:01:57 -04:00
|
|
|
info.protocols = protocols;
|
2021-05-21 13:59:12 -04:00
|
|
|
#ifdef LWS_SERVER_OPTION_IPV6_V6ONLY_MODIFY // Debian Buster's libwebsockets does not have this flag
|
2021-05-21 10:13:44 -04:00
|
|
|
if (cfg_getbool(cfg_getsec(cfg, "general"), "ipv6"))
|
|
|
|
info.options |= LWS_SERVER_OPTION_IPV6_V6ONLY_MODIFY; // Assures dual stack is enabled by switching off IPV6_V6ONLY
|
|
|
|
else
|
2019-05-05 09:15:28 -04:00
|
|
|
info.options |= LWS_SERVER_OPTION_DISABLE_IPV6;
|
2021-05-21 13:59:12 -04:00
|
|
|
#else
|
|
|
|
if (!cfg_getbool(cfg_getsec(cfg, "general"), "ipv6"))
|
|
|
|
info.options |= LWS_SERVER_OPTION_DISABLE_IPV6;
|
|
|
|
#endif
|
2017-08-12 14:01:57 -04:00
|
|
|
info.gid = -1;
|
|
|
|
info.uid = -1;
|
|
|
|
|
2020-07-03 03:53:39 -04:00
|
|
|
// Set header space to avoid "LWS Ran out of header data space" error
|
|
|
|
info.max_http_header_data = 4096;
|
|
|
|
|
2018-05-28 14:58:36 -04:00
|
|
|
// Log levels below NOTICE are only emmited if libwebsockets was built with DEBUG defined
|
|
|
|
lws_set_log_level(LLL_ERR | LLL_WARN | LLL_NOTICE | LLL_INFO | LLL_DEBUG,
|
2021-05-13 16:54:11 -04:00
|
|
|
logger_libwebsockets);
|
2017-08-12 14:01:57 -04:00
|
|
|
|
|
|
|
context = lws_create_context(&info);
|
|
|
|
if (context == NULL)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Failed to create websocket context\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-01-20 14:09:50 -05:00
|
|
|
ret = mutex_init(&websocket_write_event_lock);
|
2017-08-12 14:01:57 -04:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-01-20 14:09:50 -05:00
|
|
|
DPRINTF(E_LOG, L_WEB, "Failed to initialize mutex: %s\n", strerror(ret));
|
|
|
|
lws_context_destroy(context);
|
|
|
|
return -1;
|
|
|
|
}
|
2017-08-12 14:01:57 -04:00
|
|
|
|
2022-01-20 14:09:50 -05:00
|
|
|
ret = pthread_create(&tid_websocket, NULL, websocket, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Could not spawn websocket thread (%d): %s\n", ret, strerror(ret));
|
|
|
|
pthread_mutex_destroy(&websocket_write_event_lock);
|
2017-08-12 14:01:57 -04:00
|
|
|
lws_context_destroy(context);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-07-05 15:40:31 -04:00
|
|
|
thread_setname(tid_websocket, "websocket");
|
|
|
|
|
2017-08-12 14:01:57 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
websocket_deinit(void)
|
|
|
|
{
|
2022-01-20 14:09:50 -05:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (websocket_port <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
websocket_exit = true;
|
2022-02-22 09:54:55 -05:00
|
|
|
lws_cancel_service(context);
|
2022-01-20 14:09:50 -05:00
|
|
|
ret = pthread_join(tid_websocket, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
DPRINTF(E_LOG, L_WEB, "Error joining websocket thread (%d): %s\n", ret, strerror(ret));
|
|
|
|
|
2022-02-22 09:54:55 -05:00
|
|
|
lws_context_destroy(context);
|
2022-01-20 14:09:50 -05:00
|
|
|
pthread_mutex_destroy(&websocket_write_event_lock);
|
2017-08-12 14:01:57 -04:00
|
|
|
}
|