Commit c26ee2fc authored by Philipp Schafft's avatar Philipp Schafft 🦁

Merge branch 'ph3-listen-sockets'

parents 1a426f7f d9e96a55
Pipeline #238 passed with stage
in 45 seconds
......@@ -32,6 +32,7 @@ noinst_HEADERS = \
refobject.h \
module.h \
reportxml.h \
listensocket.h \
event.h \
event_log.h \
event_exec.h \
......@@ -74,6 +75,7 @@ icecast_SOURCES = \
refobject.c \
module.c \
reportxml.c \
listensocket.c \
format.c \
format_ogg.c \
format_mp3.c \
......
......@@ -747,7 +747,6 @@ static void command_buildm3u(client_t *client, source_t *source, admin_format_t
const char *mount = source->mount;
const char *username = NULL;
const char *password = NULL;
ice_config_t *config;
ssize_t ret;
COMMAND_REQUIRE(client, "username", username);
......@@ -766,17 +765,7 @@ static void command_buildm3u(client_t *client, source_t *source, admin_format_t
}
config = config_get_config();
snprintf(client->refbuf->data + ret, PER_CLIENT_REFBUF_SIZE - ret,
"Content-Disposition: attachment; filename=listen.m3u\r\n\r\n"
"http://%s:%s@%s:%d%s\r\n",
username,
password,
config->hostname,
config->port,
mount
);
config_release_config();
client_get_baseurl(client, NULL, client->refbuf->data + ret, PER_CLIENT_REFBUF_SIZE - ret, username, password, "Content-Disposition: attachment; filename=listen.m3u\r\n\r\n", mount, "\r\n");
client->respcode = 200;
client->refbuf->len = strlen (client->refbuf->data);
......@@ -1131,7 +1120,7 @@ static void command_stats(client_t *client, source_t *source, admin_format_t res
ICECAST_LOG_DEBUG("Stats request, sending xml stats");
doc = stats_get_xml(1, mount, client->mode);
doc = stats_get_xml(1, mount, client);
admin_send_response(doc, client, response, STATS_HTML_REQUEST);
xmlFreeDoc(doc);
return;
......
......@@ -10,7 +10,7 @@
* and others (see AUTHORS for details).
* Copyright 2011, Dave 'justdave' Miller <justdave@mozilla.com>.
* Copyright 2011-2014, Thomas B. "dm8tbr" Ruecker <thomas@ruecker.fi>,
* Copyright 2011-2014, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
* Copyright 2011-2018, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
*/
#ifdef HAVE_CONFIG_H
......@@ -172,6 +172,33 @@ operation_mode config_str_to_omode(const char *str)
}
}
static listener_type_t config_str_to_listener_type(const char *str)
{
if (!str || !*str) {
return LISTENER_TYPE_NORMAL;
} else if (strcasecmp(str, "normal") == 0) {
return LISTENER_TYPE_NORMAL;
} else if (strcasecmp(str, "virtual") == 0) {
return LISTENER_TYPE_VIRTUAL;
} else {
ICECAST_LOG_ERROR("Unknown listener type \"%s\", falling back to NORMAL.", str);
return LISTENER_TYPE_NORMAL;
}
}
char * config_href_to_id(const char *href)
{
if (!href || !*href)
return NULL;
if (*href != '#') {
ICECAST_LOG_ERROR("Can not convert string \"%H\" to ID.", href);
return NULL;
}
return strdup(href+1);
}
static void create_locks(void)
{
thread_mutex_create(&_locks.relay_lock);
......@@ -569,6 +596,7 @@ static void config_clear_resource(resource_t *resource)
xmlFree(resource->vhost);
xmlFree(resource->module);
xmlFree(resource->handler);
free(resource->listen_socket);
free(resource);
resource = nextresource;
}
......@@ -580,6 +608,8 @@ listener_t *config_clear_listener(listener_t *listener)
if (listener)
{
next = listener->next;
if (listener->id) xmlFree(listener->id);
if (listener->on_behalf_of) free(listener->on_behalf_of);
if (listener->bind_address) xmlFree(listener->bind_address);
if (listener->shoutcast_mount) xmlFree(listener->shoutcast_mount);
free (listener);
......@@ -999,7 +1029,7 @@ static void _parse_root(xmlDocPtr doc,
xmlFree(configuration->mimetypes_fn);
configuration->mimetypes_fn = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
} else if (xmlStrcmp(node->name, XMLSTR("listen-socket")) == 0) {
_parse_listen_socket(doc, node->xmlChildrenNode, configuration);
_parse_listen_socket(doc, node, configuration);
} else if (xmlStrcmp(node->name, XMLSTR("port")) == 0) {
tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
if (tmp && *tmp) {
......@@ -1744,6 +1774,20 @@ static void _parse_listen_socket(xmlDocPtr doc,
return;
listener->port = 8000;
listener->id = (char *)xmlGetProp(node, XMLSTR("id"));
tmp = (char*)xmlGetProp(node, XMLSTR("on-behalf-of"));
if (tmp) {
listener->on_behalf_of = config_href_to_id(tmp);
xmlFree(tmp);
}
tmp = (char *)xmlGetProp(node, XMLSTR("type"));
listener->type = config_str_to_listener_type(tmp);
xmlFree(tmp);
node = node->xmlChildrenNode;
do {
if (node == NULL)
break;
......@@ -1964,6 +2008,12 @@ static void _parse_resource(xmlDocPtr doc,
resource->bind_address = (char *)xmlGetProp(node, XMLSTR("bind-address"));
temp = (char *)xmlGetProp(node, XMLSTR("listen-socket"));
if (temp) {
resource->listen_socket = config_href_to_id(temp);
xmlFree(temp);
}
resource->vhost = (char *)xmlGetProp(node, XMLSTR("vhost"));
resource->module = (char *)xmlGetProp(node, XMLSTR("module"));
......@@ -2473,24 +2523,28 @@ mount_proxy *config_find_mount (ice_config_t *config,
return mountinfo;
}
/* Helper function to locate the configuration details of the listening
* socket
*/
listener_t *config_get_listen_sock(ice_config_t *config, connection_t *con)
{
listener_t *listener;
int i = 0;
listener_t *config_copy_listener_one(const listener_t *listener) {
listener_t *n;
listener = config->listen_sock;
while (listener) {
if (i >= global.server_sockets) {
listener = NULL;
} else {
if (global.serversock[i] == con->serversock)
break;
listener = listener->next;
i++;
}
if (listener == NULL)
return NULL;
n = calloc(1, sizeof(*n));
if (n == NULL)
return NULL;
n->next = NULL;
n->port = listener->port;
n->so_sndbuf = listener->so_sndbuf;
n->type = listener->type;
n->id = (char*)xmlStrdup(XMLSTR(listener->id));
if (listener->on_behalf_of) {
n->on_behalf_of = strdup(listener->on_behalf_of);
}
return listener;
n->bind_address = (char*)xmlStrdup(XMLSTR(listener->bind_address));
n->shoutcast_compat = listener->shoutcast_compat;
n->shoutcast_mount = (char*)xmlStrdup(XMLSTR(listener->shoutcast_mount));
n->tls = listener->tls;
return n;
}
......@@ -9,7 +9,7 @@
* Karl Heyes <karl@xiph.org>
* and others (see AUTHORS for details).
* Copyright 2011, Dave 'justdave' Miller <justdave@mozilla.com>.
* Copyright 2011-2014, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
* Copyright 2011-2018, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
*/
#ifndef __CFGFILE_H__
......@@ -139,6 +139,7 @@ typedef struct _resource {
char *destination;
int port;
char *bind_address;
char *listen_socket;
char *vhost;
char *module;
char *handler;
......@@ -147,8 +148,17 @@ typedef struct _resource {
struct _resource *next;
} resource_t;
typedef enum _listener_type_tag {
LISTENER_TYPE_ERROR,
LISTENER_TYPE_NORMAL,
LISTENER_TYPE_VIRTUAL
} listener_type_t;
typedef struct _listener_t {
struct _listener_t *next;
char *id;
char *on_behalf_of;
listener_type_t type;
int port;
int so_sndbuf;
char *bind_address;
......@@ -262,7 +272,8 @@ void config_set_config(ice_config_t *config);
listener_t *config_clear_listener (listener_t *listener);
void config_clear(ice_config_t *config);
mount_proxy *config_find_mount(ice_config_t *config, const char *mount, mount_type type);
listener_t *config_get_listen_sock(ice_config_t *config, connection_t *con);
listener_t *config_copy_listener_one(const listener_t *listener);
config_options_t *config_parse_options(xmlNodePtr node);
void config_clear_options(config_options_t *options);
......
......@@ -48,6 +48,7 @@
#include "util.h"
#include "acl.h"
#include "listensocket.h"
/* for ADMIN_COMMAND_ERROR */
#include "admin.h"
......@@ -110,7 +111,7 @@ static inline void client_reuseconnection(client_t *client) {
return;
con = client->con;
con = connection_create(con->sock, con->serversock, strdup(con->ip));
con = connection_create(con->sock, con->listensocket_real, con->listensocket_effective, strdup(con->ip));
reuse = client->reuse;
client->con->sock = -1; /* TODO: do not use magic */
......@@ -766,3 +767,91 @@ client_slurp_result_t client_body_skip(client_t *client)
break;
}
}
ssize_t client_get_baseurl(client_t *client, listensocket_t *listensocket, char *buf, size_t len, const char *user, const char *pw, const char *prefix, const char *suffix0, const char *suffix1)
{
const listener_t *listener = NULL;
const ice_config_t *config = NULL;
const char *host = NULL;
const char *proto = "http";
int port = 0;
ssize_t ret;
tlsmode_t tlsmode = ICECAST_TLSMODE_AUTO;
protocol_t protocol = ICECAST_PROTOCOL_HTTP;
if (!buf || !len)
return -1;
if (!prefix)
prefix = "";
if (!suffix0)
suffix0 = "";
if (!suffix1)
suffix1 = "";
if (client) {
host = httpp_getvar(client->parser, "host");
/* at least a couple of players (fb2k/winamp) are reported to send a
* host header but without the port number. So if we are missing the
* port then lets treat it as if no host line was sent */
if (host && strchr(host, ':') == NULL)
host = NULL;
listensocket = client->con->listensocket_effective;
tlsmode = client->con->tlsmode;
protocol = client->protocol;
}
if (!host && listensocket) {
listener = listensocket_get_listener(listensocket);
if (listener) {
host = listener->bind_address;
port = listener->port;
if (!client)
tlsmode = listener->tls;
}
}
if (!host) {
config = config_get_config();
host = config->hostname;
if (!port)
port = config->port;
}
switch (tlsmode) {
case ICECAST_TLSMODE_DISABLED:
case ICECAST_TLSMODE_AUTO:
switch (protocol) {
case ICECAST_PROTOCOL_HTTP: proto = "http"; break;
case ICECAST_PROTOCOL_SHOUTCAST: proto = "icy"; break;
}
break;
case ICECAST_TLSMODE_AUTO_NO_PLAIN:
case ICECAST_TLSMODE_RFC2817:
case ICECAST_TLSMODE_RFC2818:
switch (protocol) {
case ICECAST_PROTOCOL_HTTP: proto = "https"; break;
case ICECAST_PROTOCOL_SHOUTCAST: proto = "icys"; break;
}
break;
}
if (host && port) {
ret = snprintf(buf, len, "%s%s://%s%s%s%s%s:%i%s%s", prefix, proto, user ? user : "", pw ? ":" : "", pw ? pw : "", (user || pw) ? "@" : "", host, port, suffix0, suffix1);
} else if (host) {
ret = snprintf(buf, len, "%s%s://%s%s%s%s%s%s%s", prefix, proto, user ? user : "", pw ? ":" : "", pw ? pw : "", (user || pw) ? "@" : "", host, suffix0, suffix1);
} else {
ret = -1;
}
if (config)
config_release_config();
if (listener)
listensocket_release_listener(listensocket);
return ret;
}
......@@ -151,5 +151,6 @@ ssize_t client_body_read(client_t *client, void *buf, size_t len);
int client_body_eof(client_t *client);
client_slurp_result_t client_body_slurp(client_t *client, void *buf, size_t *len);
client_slurp_result_t client_body_skip(client_t *client);
ssize_t client_get_baseurl(client_t *client, listensocket_t *listensocket, char *buf, size_t len, const char *user, const char *pw, const char *prefix, const char *suffix0, const char *suffix1);
#endif /* __CLIENT_H__ */
......@@ -58,6 +58,8 @@
#include "matchfile.h"
#include "tls.h"
#include "acl.h"
#include "refobject.h"
#include "listensocket.h"
#define CATMODULE "connection"
......@@ -144,6 +146,7 @@ void connection_shutdown(void)
void connection_reread_config(ice_config_t *config)
{
get_tls_certificate(config);
listensocket_container_configure_and_setup(global.listensockets, config);
}
static unsigned long _next_connection_id(void)
......@@ -245,13 +248,21 @@ static int connection_send(connection_t *con, const void *buf, size_t len)
return bytes;
}
connection_t *connection_create (sock_t sock, sock_t serversock, char *ip)
connection_t *connection_create(sock_t sock, listensocket_t *listensocket_real, listensocket_t* listensocket_effective, char *ip)
{
connection_t *con;
if (!matchfile_match_allow_deny(allowed_ip, banned_ip, ip))
return NULL;
con = (connection_t *)calloc(1, sizeof(connection_t));
if (con) {
refobject_ref(listensocket_real);
refobject_ref(listensocket_effective);
con->sock = sock;
con->serversock = serversock;
con->listensocket_real = listensocket_real;
con->listensocket_effective = listensocket_effective;
con->con_time = time(NULL);
con->id = _next_connection_id();
con->ip = ip;
......@@ -355,117 +366,6 @@ int connection_read_put_back(connection_t *con, const void *buf, size_t len)
}
}
static sock_t wait_for_serversock(int timeout)
{
#ifdef HAVE_POLL
struct pollfd ufds [global.server_sockets];
int i, ret;
for(i=0; i < global.server_sockets; i++) {
ufds[i].fd = global.serversock[i];
ufds[i].events = POLLIN;
ufds[i].revents = 0;
}
ret = poll(ufds, global.server_sockets, timeout);
if(ret < 0) {
return SOCK_ERROR;
} else if(ret == 0) {
return SOCK_ERROR;
} else {
int dst;
for(i=0; i < global.server_sockets; i++) {
if(ufds[i].revents & POLLIN)
return ufds[i].fd;
if(ufds[i].revents & (POLLHUP|POLLERR|POLLNVAL)) {
if (ufds[i].revents & (POLLHUP|POLLERR)) {
sock_close (global.serversock[i]);
ICECAST_LOG_WARN("Had to close a listening socket");
}
global.serversock[i] = SOCK_ERROR;
}
}
/* remove any closed sockets */
for(i=0, dst=0; i < global.server_sockets; i++) {
if (global.serversock[i] == SOCK_ERROR)
continue;
if (i!=dst)
global.serversock[dst] = global.serversock[i];
dst++;
}
global.server_sockets = dst;
return SOCK_ERROR;
}
#else
fd_set rfds;
struct timeval tv, *p=NULL;
int i, ret;
sock_t max = SOCK_ERROR;
FD_ZERO(&rfds);
for(i=0; i < global.server_sockets; i++) {
FD_SET(global.serversock[i], &rfds);
if (max == SOCK_ERROR || global.serversock[i] > max)
max = global.serversock[i];
}
if(timeout >= 0) {
tv.tv_sec = timeout/1000;
tv.tv_usec = (timeout % 1000) * 1000;
p = &tv;
}
ret = select(max+1, &rfds, NULL, NULL, p);
if(ret < 0) {
return SOCK_ERROR;
} else if(ret == 0) {
return SOCK_ERROR;
} else {
for(i=0; i < global.server_sockets; i++) {
if(FD_ISSET(global.serversock[i], &rfds))
return global.serversock[i];
}
return SOCK_ERROR; /* Should be impossible, stop compiler warnings */
}
#endif
}
static connection_t *_accept_connection(int duration)
{
sock_t sock, serversock;
char *ip;
serversock = wait_for_serversock (duration);
if (serversock == SOCK_ERROR)
return NULL;
/* malloc enough room for a full IP address (including ipv6) */
ip = (char *)malloc(MAX_ADDR_LEN);
sock = sock_accept(serversock, ip, MAX_ADDR_LEN);
if (sock != SOCK_ERROR) {
connection_t *con = NULL;
/* Make any IPv4 mapped IPv6 address look like a normal IPv4 address */
if (strncmp(ip, "::ffff:", 7) == 0)
memmove(ip, ip+7, strlen (ip+7)+1);
if (matchfile_match_allow_deny(allowed_ip, banned_ip, ip))
con = connection_create (sock, serversock, ip);
if (con)
return con;
sock_close(sock);
} else {
if (!sock_recoverable(sock_error())) {
ICECAST_LOG_WARN("accept() failed with error %d: %s", sock_error(), strerror(sock_error()));
thread_sleep(500000);
}
}
free(ip);
return NULL;
}
/* add client to connection queue. At this point some header information
* has been collected, so we now pass it onto the connection thread for
* further processing
......@@ -704,16 +604,14 @@ static void _add_request_queue(client_queue_t *node)
static client_queue_t *create_client_node(client_t *client)
{
client_queue_t *node = calloc (1, sizeof (client_queue_t));
ice_config_t *config;
listener_t *listener;
const listener_t *listener;
if (!node)
return NULL;
node->client = client;
config = config_get_config();
listener = config_get_listen_sock(config, client->con);
listener = listensocket_get_listener(client->con->listensocket_effective);
if (listener) {
if (listener->shoutcast_compat)
......@@ -725,7 +623,7 @@ static client_queue_t *create_client_node(client_t *client)
node->shoutcast_mount = strdup(listener->shoutcast_mount);
}
config_release_config();
listensocket_release_listener(client->con->listensocket_effective);
return node;
}
......@@ -776,7 +674,7 @@ void connection_accept_loop(void)
config_release_config();
while (global.running == ICECAST_RUNNING) {
con = _accept_connection (duration);
con = listensocket_container_accept(global.listensockets, duration);
if (con) {
connection_queue(con);
......@@ -1234,7 +1132,7 @@ static int _handle_resources(client_t *client, char **uri)
char *vhost_colon;
char *new_uri = NULL;
ice_config_t *config;
listener_t *listen_sock;
const listener_t *listen_sock;
resource_t *resource;
if (http_host) {
......@@ -1246,8 +1144,8 @@ static int _handle_resources(client_t *client, char **uri)
}
}
listen_sock = listensocket_get_listener(client->con->listensocket_effective);
config = config_get_config();
listen_sock = config_get_listen_sock (config, client->con);
if (listen_sock) {
serverhost = listen_sock->bind_address;
serverport = listen_sock->port;
......@@ -1278,6 +1176,9 @@ static int _handle_resources(client_t *client, char **uri)
if (resource->bind_address != NULL && serverhost != NULL && strcmp(resource->bind_address, serverhost) != 0)
continue;
if (resource->listen_socket != NULL && (listen_sock->id == NULL || strcmp(resource->listen_socket, listen_sock->id) != 0))
continue;
/* Check for the vhost to match. */
if (resource->vhost != NULL && vhost != NULL && strcmp(resource->vhost, vhost) != 0)
continue;
......@@ -1320,6 +1221,7 @@ static int _handle_resources(client_t *client, char **uri)
break;
}
listensocket_release_listener(client->con->listensocket_effective);
config_release_config();
if (new_uri) {
......@@ -1497,7 +1399,7 @@ static void __prepare_shoutcast_admin_cgi_request(client_t *client)
ice_config_t *config;
const char *sc_mount;
const char *pass = httpp_get_query_param(client->parser, "pass");
listener_t *listener;
const listener_t *listener;
if (pass == NULL) {
ICECAST_LOG_ERROR("missing pass parameter");
......@@ -1509,15 +1411,18 @@ static void __prepare_shoutcast_admin_cgi_request(client_t *client)
return;
}
/* Why do we acquire a global lock here? -- ph3-der-loewe, 2018-05-11 */
global_lock();
config = config_get_config();
sc_mount = config->shoutcast_mount;
listener = config_get_listen_sock(config, client->con);
listener = listensocket_get_listener(client->con->listensocket_effective);
if (listener && listener->shoutcast_mount)
sc_mount = listener->shoutcast_mount;
httpp_set_query_param(client->parser, "mount", sc_mount);
listensocket_release_listener(client->con->listensocket_effective);
httpp_setvar(client->parser, HTTPP_VAR_PROTOCOL, "ICY");
client->password = strdup(pass);
config_release_config();
......@@ -1732,22 +1637,27 @@ static void _handle_connection(void)
}
/* called when listening thread is not checking for incoming connections */
int connection_setup_sockets (ice_config_t *config)
static void __on_sock_count(size_t count, void *userdata)
{
int count = 0;
listener_t *listener, **prev;
(void)userdata;
global_lock();
if (global.serversock) {
for (; count < global.server_sockets; count++)
sock_close (global.serversock [count]);
free (global.serversock);
global.serversock = NULL;
ICECAST_LOG_DEBUG("Listen socket count is now %zu.", count);
if (count == 0 && global.running == ICECAST_RUNNING) {
ICECAST_LOG_INFO("No more listen sockets. Exiting.");
global.running = ICECAST_HALTING;
}
}
/* called when listening thread is not checking for incoming connections */
void connection_setup_sockets (ice_config_t *config)
{
global_lock();
refobject_unref(global.listensockets);
if (config == NULL) {
global_unlock();
return 0;
return;
}
/* setup the banned/allowed IP filenames from the xml */
......@@ -1763,57 +1673,13 @@ int connection_setup_sockets (ice_config_t *config)
allowed_ip = matchfile_new(config->allowfile);
}
count = 0;
global.serversock = calloc(config->listen_sock_count, sizeof(sock_t));
listener = config->listen_sock;
prev = &config->listen_sock;
while (listener) {
int successful = 0;
do {
sock_t sock = sock_get_server_socket (listener->port, listener->bind_address);
if (sock == SOCK_ERROR)
break;
if (sock_listen (sock, ICECAST_LISTEN_QUEUE) == SOCK_ERROR) {
sock_close (sock);
break;
}
/* some win32 setups do not do TCP win scaling well, so allow an override */
if (listener->so_sndbuf)
sock_set_send_buffer (sock, listener->so_sndbuf);
sock_set_blocking (sock, 0);
successful = 1;
global.serversock [count] = sock;
count++;
} while(0);
if (successful == 0) {
if (listener->bind_address) {
ICECAST_LOG_ERROR("Could not create listener socket on port %d bind %s",
listener->port, listener->bind_address);
} else {