Commit 3f76299e authored by Philipp Schafft's avatar Philipp Schafft 🦁

Merge branch 'feature-relay-improvements'

parents 75314c2b 1c619037
Pipeline #264 failed with stage
in 12 seconds
......@@ -75,6 +75,9 @@
#define CONFIG_DEFAULT_GROUP NULL
#define CONFIG_MASTER_UPDATE_INTERVAL 120
#define CONFIG_YP_URL_TIMEOUT 10
#define CONFIG_DEFAULT_RELAY_SERVER "127.0.0.1"
#define CONFIG_DEFAULT_RELAY_PORT 80
#define CONFIG_DEFAULT_RELAY_MOUNT "/"
#define CONFIG_DEFAULT_CIPHER_LIST "ECDHE-RSA-AES128-GCM-SHA256:"\
"ECDHE-ECDSA-AES128-GCM-SHA256:"\
"ECDHE-RSA-AES256-GCM-SHA384:"\
......@@ -142,8 +145,8 @@ static void _parse_http_headers(xmlDocPtr doc,
xmlNodePtr node,
ice_config_http_header_t **http_headers);
static void _parse_relay(xmlDocPtr doc, xmlNodePtr node, ice_config_t *c);
static void _parse_mount(xmlDocPtr doc, xmlNodePtr node, ice_config_t *c);
static void _parse_relay(xmlDocPtr doc, xmlNodePtr node, ice_config_t *c, const char *mount);
static void _parse_mount(xmlDocPtr doc, xmlNodePtr parentnode, ice_config_t *c);
static void _parse_listen_socket(xmlDocPtr doc,
xmlNodePtr node,
......@@ -622,13 +625,9 @@ void config_clear(ice_config_t *c)
{
ice_config_dir_t *dirnode,
*nextdirnode;
relay_server *relay,
*nextrelay;
mount_proxy *mount,
*nextmount;
#ifdef USE_YP
int i;
#endif
size_t i;
free(c->config_filename);
......@@ -666,15 +665,10 @@ void config_clear(ice_config_t *c)
while ((c->listen_sock = config_clear_listener(c->listen_sock)));
thread_mutex_lock(&(_locks.relay_lock));
relay = c->relay;
while (relay) {
nextrelay = relay->next;
xmlFree(relay->server);
xmlFree(relay->mount);
xmlFree(relay->localmount);
free(relay);
relay = nextrelay;
for (i = 0; i < c->relay_length; i++) {
relay_config_free(c->relay[i]);
}
free(c->relay);
thread_mutex_unlock(&(_locks.relay_lock));
mount = c->mounts;
......@@ -694,10 +688,8 @@ void config_clear(ice_config_t *c)
dirnode = nextdirnode;
}
#ifdef USE_YP
i = 0;
while (i < c->num_yp_directories) {
for (i = 0; i < c->num_yp_directories; i++) {
xmlFree(c->yp_url[i]);
i++;
}
#endif
......@@ -1070,7 +1062,7 @@ static void _parse_root(xmlDocPtr doc,
} else if (xmlStrcmp(node->name, XMLSTR("http-headers")) == 0) {
_parse_http_headers(doc, node->xmlChildrenNode, &(configuration->http_headers));
} else if (xmlStrcmp(node->name, XMLSTR("relay")) == 0) {
_parse_relay(doc, node->xmlChildrenNode, configuration);
_parse_relay(doc, node->xmlChildrenNode, configuration, NULL);
} else if (xmlStrcmp(node->name, XMLSTR("mount")) == 0) {
_parse_mount(doc, node, configuration);
} else if (xmlStrcmp(node->name, XMLSTR("directory")) == 0) {
......@@ -1366,7 +1358,7 @@ static void _parse_mount_oldstyle_authentication(mount_proxy *mount,
}
static void _parse_mount(xmlDocPtr doc,
xmlNodePtr node,
xmlNodePtr parentnode,
ice_config_t *configuration)
{
char *tmp;
......@@ -1376,6 +1368,7 @@ static void _parse_mount(xmlDocPtr doc,
char *username = NULL;
char *password = NULL;
auth_stack_t *authstack = NULL;
xmlNodePtr node;
/* default <mount> settings */
mount->mounttype = MOUNT_TYPE_NORMAL;
......@@ -1386,7 +1379,7 @@ static void _parse_mount(xmlDocPtr doc,
mount->max_history = -1;
mount->next = NULL;
tmp = (char *)xmlGetProp(node, XMLSTR("type"));
tmp = (char *)xmlGetProp(parentnode, XMLSTR("type"));
if (tmp) {
if (strcmp(tmp, "normal") == 0) {
mount->mounttype = MOUNT_TYPE_NORMAL;
......@@ -1400,7 +1393,7 @@ static void _parse_mount(xmlDocPtr doc,
xmlFree(tmp);
}
node = node->xmlChildrenNode;
node = parentnode->xmlChildrenNode;
do {
if (node == NULL)
......@@ -1553,6 +1546,25 @@ static void _parse_mount(xmlDocPtr doc,
}
} while ((node = node->next));
/* Do a second interation as we need to know mount->mountname, and mount->mounttype first */
node = parentnode->xmlChildrenNode;
do {
if (node == NULL)
break;
if (xmlStrcmp(node->name, XMLSTR("relay")) == 0) {
if (mount->mounttype != MOUNT_TYPE_NORMAL) {
ICECAST_LOG_WARN("<relay> set within <mount> for mountpoint %s%s%s that is not type=\"normal\"",
(mount->mountname ? "\"" : ""), (mount->mountname ? mount->mountname : "<no name>"), (mount->mountname ? "\"" : ""));
} else if (!mount->mountname || mount->mountname[0] != '/') {
ICECAST_LOG_WARN("<relay> set within <mount> with no mountpoint defined.");
} else {
_parse_relay(doc, node->xmlChildrenNode, configuration, mount->mountname);
}
}
} while ((node = node->next));
if (password) {
auth_stack_t *old_style = NULL;
__append_old_style_auth(&old_style, "legacy-mount-source",
......@@ -1685,31 +1697,11 @@ static void _parse_http_headers(xmlDocPtr doc,
xmlFree(value);
}
static void _parse_relay(xmlDocPtr doc,
xmlNodePtr node,
ice_config_t *configuration)
static void _parse_relay_upstream(xmlDocPtr doc,
xmlNodePtr node,
relay_config_upstream_t *upstream)
{
char *tmp;
relay_server *relay = calloc(1, sizeof(relay_server));
relay_server *current = configuration->relay;
relay_server *last = NULL;
while(current) {
last = current;
current = current->next;
}
if (last) {
last->next = relay;
} else {
configuration->relay = relay;
}
relay->next = NULL;
relay->mp3metadata = 1;
relay->on_demand = configuration->on_demand;
relay->server = (char *) xmlCharStrdup("127.0.0.1");
relay->mount = (char *) xmlCharStrdup("/");
do {
if (node == NULL)
......@@ -1718,51 +1710,123 @@ static void _parse_relay(xmlDocPtr doc,
continue;
if (xmlStrcmp(node->name, XMLSTR("server")) == 0) {
if (relay->server)
xmlFree(relay->server);
relay->server = (char *)xmlNodeListGetString(doc,
if (upstream->server)
xmlFree(upstream->server);
upstream->server = (char *)xmlNodeListGetString(doc,
node->xmlChildrenNode, 1);
} else if (xmlStrcmp(node->name, XMLSTR("port")) == 0) {
__read_int(doc, node, &relay->port, "<port> setting must not be empty.");
__read_int(doc, node, &upstream->port, "<port> setting must not be empty.");
} else if (xmlStrcmp(node->name, XMLSTR("mount")) == 0) {
if (relay->mount)
xmlFree(relay->mount);
relay->mount = (char *)xmlNodeListGetString(doc,
node->xmlChildrenNode, 1);
} else if (xmlStrcmp(node->name, XMLSTR("local-mount")) == 0) {
if (relay->localmount)
xmlFree(relay->localmount);
relay->localmount = (char *)xmlNodeListGetString(doc,
if (upstream->mount)
xmlFree(upstream->mount);
upstream->mount = (char *)xmlNodeListGetString(doc,
node->xmlChildrenNode, 1);
} else if (xmlStrcmp(node->name, XMLSTR("relay-shoutcast-metadata")) == 0) {
tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
relay->mp3metadata = util_str_to_bool(tmp);
upstream->mp3metadata = util_str_to_bool(tmp);
if(tmp)
xmlFree(tmp);
} else if (xmlStrcmp(node->name, XMLSTR("username")) == 0) {
if (relay->username)
xmlFree(relay->username);
relay->username = (char *)xmlNodeListGetString(doc,
if (upstream->username)
xmlFree(upstream->username);
upstream->username = (char *)xmlNodeListGetString(doc,
node->xmlChildrenNode, 1);
} else if (xmlStrcmp(node->name, XMLSTR("password")) == 0) {
if (relay->password)
xmlFree(relay->password);
relay->password = (char *)xmlNodeListGetString(doc,
if (upstream->password)
xmlFree(upstream->password);
upstream->password = (char *)xmlNodeListGetString(doc,
node->xmlChildrenNode, 1);
} else if (xmlStrcmp(node->name, XMLSTR("bind")) == 0) {
if (upstream->bind)
xmlFree(upstream->bind);
upstream->bind = (char *)xmlNodeListGetString(doc,
node->xmlChildrenNode, 1);
}
} while ((node = node->next));
}
static void _parse_relay_upstream_apply_defaults(relay_config_upstream_t *upstream)
{
if (!upstream->server)
upstream->server = (char *)xmlCharStrdup(CONFIG_DEFAULT_RELAY_SERVER);
if (!upstream->port)
upstream->port = CONFIG_DEFAULT_RELAY_PORT;
if (!upstream->mount)
upstream->mount = (char *)xmlCharStrdup(CONFIG_DEFAULT_RELAY_MOUNT);
}
static void _parse_relay(xmlDocPtr doc,
xmlNodePtr node,
ice_config_t *configuration,
const char *mount)
{
char *tmp;
relay_config_t *relay = calloc(1, sizeof(relay_config_t));
relay_config_t **n = realloc(configuration->relay, sizeof(*configuration->relay)*(configuration->relay_length + 1));
if (!n) {
ICECAST_LOG_ERROR("Can not allocate memory for additional relay.");
return;
}
configuration->relay = n;
configuration->relay[configuration->relay_length++] = relay;
relay->upstream_default.mp3metadata = 1;
relay->on_demand = configuration->on_demand;
_parse_relay_upstream(doc, node, &(relay->upstream_default));
do {
if (node == NULL)
break;
if (xmlIsBlankNode(node))
continue;
if (xmlStrcmp(node->name, XMLSTR("local-mount")) == 0) {
if (mount) {
ICECAST_LOG_WARN("Relay defined within mount \"%s\" defines <local-mount> which is ignored.", mount);
} else {
if (relay->localmount)
xmlFree(relay->localmount);
relay->localmount = (char *)xmlNodeListGetString(doc,
node->xmlChildrenNode, 1);
}
} else if (xmlStrcmp(node->name, XMLSTR("on-demand")) == 0) {
tmp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
relay->on_demand = util_str_to_bool(tmp);
if (tmp)
xmlFree(tmp);
} else if (xmlStrcmp(node->name, XMLSTR("bind")) == 0) {
if (relay->bind)
xmlFree(relay->bind);
relay->bind = (char *)xmlNodeListGetString(doc,
node->xmlChildrenNode, 1);
} else if (xmlStrcmp(node->name, XMLSTR("upstream")) == 0) {
tmp = (char *)xmlGetProp(node, XMLSTR("type"));
if (tmp == NULL || strcmp(tmp, "normal") == 0) {
relay_config_upstream_t *n = realloc(relay->upstream, sizeof(*n)*(relay->upstreams + 1));
if (n) {
relay->upstream = n;
memset(&(n[relay->upstreams]), 0, sizeof(relay_config_upstream_t));
_parse_relay_upstream(doc, node->xmlChildrenNode, &(n[relay->upstreams]));
relay->upstreams++;
}
} else if (strcmp(tmp, "default") == 0) {
_parse_relay_upstream(doc, node->xmlChildrenNode, &(relay->upstream_default));
} else {
ICECAST_LOG_WARN("<upstream> of unknown type is ignored.");
}
if (tmp)
xmlFree(tmp);
}
} while ((node = node->next));
_parse_relay_upstream_apply_defaults(&(relay->upstream_default));
if (mount) {
relay->localmount = (char *)xmlStrdup(XMLSTR(mount));
}
if (relay->localmount == NULL)
relay->localmount = (char *)xmlStrdup(XMLSTR(relay->mount));
relay->localmount = (char *)xmlStrdup(XMLSTR(relay->upstream_default.mount));
}
static void _parse_listen_socket(xmlDocPtr doc,
......
......@@ -173,6 +173,24 @@ typedef struct _config_tls_context {
char *cipher_list;
} config_tls_context_t;
typedef struct {
char *server;
int port;
char *mount;
char *username;
char *password;
char *bind;
int mp3metadata;
} relay_config_upstream_t;
typedef struct {
char *localmount;
int on_demand;
size_t upstreams;
relay_config_upstream_t *upstream;
relay_config_upstream_t upstream_default;
} relay_config_t;
struct ice_config_tag {
char *config_filename;
......@@ -219,7 +237,8 @@ struct ice_config_tag {
/* is TLS supported by the server? */
int tls_ok;
relay_server *relay;
size_t relay_length;
relay_config_t **relay;
mount_proxy *mounts;
......
......@@ -36,9 +36,9 @@ typedef struct ice_global_tag
avl_tree *source_tree;
/* for locally defined relays */
relay_server *relays;
relay_t *relays;
/* relays retrieved from master */
relay_server *master_relays;
relay_t *master_relays;
module_container_t *modulecontainer;
......
......@@ -90,7 +90,7 @@ typedef enum {
/* ---[ slave.[ch] ]--- */
typedef struct _relay_server relay_server;
typedef struct relay_tag relay_t;
/* ---[ module.[ch] ]--- */
......
......@@ -56,6 +56,16 @@
#define CATMODULE "slave"
struct relay_tag {
relay_config_t *config;
source_t *source;
int running;
int cleanup;
time_t start;
thread_type *thread;
relay_t *next;
};
static void *_slave_thread(void *arg);
static thread_type *_slave_thread_id;
static int slave_running = 0;
......@@ -64,44 +74,115 @@ static volatile int update_all_mounts = 0;
static volatile unsigned int max_interval = 0;
static mutex_t _slave_mutex; // protects update_settings, update_all_mounts, max_interval
relay_server *relay_free (relay_server *relay)
static inline void relay_config_upstream_free (relay_config_upstream_t *upstream)
{
if (upstream->server)
xmlFree(upstream->server);
if (upstream->mount)
xmlFree(upstream->mount);
if (upstream->username)
xmlFree(upstream->username);
if (upstream->password)
xmlFree(upstream->password);
}
void relay_config_free (relay_config_t *relay)
{
size_t i;
ICECAST_LOG_DEBUG("freeing relay config for %s", relay->localmount);
for (i = 0; i < relay->upstreams; i++) {
relay_config_upstream_free(&(relay->upstream[i]));
}
relay_config_upstream_free(&(relay->upstream_default));
xmlFree(relay->localmount);
free(relay->upstream);
free(relay);
}
relay_t *relay_free (relay_t *relay)
{
relay_server *next = relay->next;
ICECAST_LOG_DEBUG("freeing relay %s", relay->localmount);
relay_t *next = relay->next;
ICECAST_LOG_DEBUG("freeing relay %s", relay->config->localmount);
if (relay->source)
source_free_source (relay->source);
xmlFree (relay->server);
xmlFree (relay->mount);
xmlFree (relay->localmount);
if (relay->username)
xmlFree (relay->username);
if (relay->password)
xmlFree (relay->password);
free (relay);
relay_config_free(relay->config);
free(relay);
return next;
}
relay_server *relay_copy (relay_server *r)
static inline void relay_config_upstream_copy(relay_config_upstream_t *dst, const relay_config_upstream_t *src)
{
relay_server *copy = calloc (1, sizeof (relay_server));
dst->server = (char *)xmlCharStrdup(src->server);
dst->mount = (char *)xmlCharStrdup(src->mount);
if (copy)
{
copy->server = (char *)xmlCharStrdup (r->server);
copy->mount = (char *)xmlCharStrdup (r->mount);
copy->localmount = (char *)xmlCharStrdup (r->localmount);
if (r->username)
copy->username = (char *)xmlCharStrdup (r->username);
if (r->password)
copy->password = (char *)xmlCharStrdup (r->password);
copy->port = r->port;
copy->mp3metadata = r->mp3metadata;
copy->on_demand = r->on_demand;
if (src->username)
dst->username = (char *)xmlCharStrdup(src->username);
if (src->password)
dst->password = (char *)xmlCharStrdup(src->password);
dst->port = src->port;
dst->mp3metadata = src->mp3metadata;
}
static inline relay_config_t *relay_config_copy (relay_config_t *r)
{
relay_config_t *copy = calloc (1, sizeof (relay_config_t));
relay_config_upstream_t *u = NULL;
size_t i;
if (r->upstreams) {
u = calloc(r->upstreams, sizeof(relay_config_upstream_t));
if (!u) {
free(copy);
return NULL;
}
}
if (!copy) {
free(u);
return NULL;
}
copy->upstream = u;
copy->upstreams = r->upstreams;
copy->localmount = (char *)xmlCharStrdup(r->localmount);
copy->on_demand = r->on_demand;
relay_config_upstream_copy(&(copy->upstream_default), &(r->upstream_default));
for (i = 0; i < r->upstreams; i++)
relay_config_upstream_copy(&(copy->upstream[i]), &(r->upstream[i]));
return copy;
}
static inline relay_t *relay_new(relay_config_t *config)
{
relay_t *r = calloc(1, sizeof(*r));
if (!r)
return NULL;
r->config = relay_config_copy(config);
if (!r->config) {
free(r);
return NULL;
}
return r;
}
/* force a recheck of the relays. This will recheck the master server if
* this is a slave and rebuild all mountpoints in the stats tree
......@@ -152,16 +233,17 @@ void slave_shutdown(void)
/* Actually open the connection and do some http parsing, handle any 302
* responses within here.
*/
static client_t *open_relay_connection (relay_server *relay)
#define _GET_UPSTREAM_SETTING(n) ((upstream && upstream->n) ? upstream->n : relay->config->upstream_default.n)
static client_t *open_relay_connection (relay_t *relay, relay_config_upstream_t *upstream)
{
int redirects = 0;
char *server_id = NULL;
ice_config_t *config;
http_parser_t *parser = NULL;
connection_t *con=NULL;
char *server = strdup (relay->server);
char *mount = strdup (relay->mount);
int port = relay->port;
char *server = strdup (_GET_UPSTREAM_SETTING(server));
char *mount = strdup (_GET_UPSTREAM_SETTING(mount));
int port = _GET_UPSTREAM_SETTING(port);
char *auth_header;
char header[4096];
......@@ -170,13 +252,13 @@ static client_t *open_relay_connection (relay_server *relay)
config_release_config ();
/* build any authentication header before connecting */
if (relay->username && relay->password)
if (_GET_UPSTREAM_SETTING(username) && _GET_UPSTREAM_SETTING(password))
{
char *esc_authorisation;
unsigned len = strlen(relay->username) + strlen(relay->password) + 2;
unsigned len = strlen(_GET_UPSTREAM_SETTING(username)) + strlen(_GET_UPSTREAM_SETTING(password)) + 2;
auth_header = malloc (len);
snprintf (auth_header, len, "%s:%s", relay->username, relay->password);
snprintf (auth_header, len, "%s:%s", _GET_UPSTREAM_SETTING(username), _GET_UPSTREAM_SETTING(password));
esc_authorisation = util_base64_encode(auth_header, len);
free(auth_header);
len = strlen (esc_authorisation) + 24;
......@@ -194,7 +276,7 @@ static client_t *open_relay_connection (relay_server *relay)
ICECAST_LOG_INFO("connecting to %s:%d", server, port);
streamsock = sock_connect_wto_bind (server, port, relay->bind, 10);
streamsock = sock_connect_wto_bind (server, port, _GET_UPSTREAM_SETTING(bind), 10);
if (streamsock == SOCK_ERROR)
{
ICECAST_LOG_WARN("Failed to connect to %s:%d", server, port);
......@@ -216,19 +298,19 @@ static client_t *open_relay_connection (relay_server *relay)
mount,
server_id,
server,
relay->mp3metadata?"Icy-MetaData: 1\r\n":"",
_GET_UPSTREAM_SETTING(mp3metadata) ? "Icy-MetaData: 1\r\n" : "",
auth_header);
memset (header, 0, sizeof(header));
if (util_read_header (con->sock, header, 4096, READ_ENTIRE_HEADER) == 0)
{
ICECAST_LOG_ERROR("Header read failed for %s (%s:%d%s)", relay->localmount, server, port, mount);
ICECAST_LOG_ERROR("Header read failed for %s (%s:%d%s)", relay->config->localmount, server, port, mount);
break;
}
parser = httpp_create_parser();
httpp_initialize (parser, NULL);
if (! httpp_parse_response (parser, header, strlen(header), relay->localmount))
if (! httpp_parse_response (parser, header, strlen(header), relay->config->localmount))
{
ICECAST_LOG_ERROR("Error parsing relay request for %s (%s:%d%s)", relay->localmount,
ICECAST_LOG_ERROR("Error parsing relay request for %s (%s:%d%s)", relay->config->localmount,
server, port, mount);
break;
}
......@@ -268,7 +350,7 @@ static client_t *open_relay_connection (relay_server *relay)
if (httpp_getvar (parser, HTTPP_VAR_ERROR_MESSAGE))
{
ICECAST_LOG_ERROR("Error from relay request: %s (%s)", relay->localmount,
ICECAST_LOG_ERROR("Error from relay request: %s (%s)", relay->config->localmount,
httpp_getvar(parser, HTTPP_VAR_ERROR_MESSAGE));
break;
}
......@@ -313,14 +395,27 @@ static client_t *open_relay_connection (relay_server *relay)
*/
static void *start_relay_stream (void *arg)
{
relay_server *relay = arg;
relay_t *relay = arg;
source_t *src = relay->source;
client_t *client;
ICECAST_LOG_INFO("Starting relayed source at mountpoint \"%s\"", relay->localmount);
ICECAST_LOG_INFO("Starting relayed source at mountpoint \"%s\"", relay->config->localmount);
do
{
client = open_relay_connection (relay);
size_t i;
for (i = 0; i < relay->config->upstreams; i++) {
ICECAST_LOG_DEBUG("For relay on mount \"%s\", trying upstream #%zu", relay->config->localmount, i);
client = open_relay_connection(relay, &(relay->config->upstream[i]));
if (client)
break;
}
/* if we have no upstreams defined, use the default upstream */
if (!relay->config->upstreams) {
ICECAST_LOG_DEBUG("For relay on mount \"%s\" with no upstreams trying upstream default", relay->config->localmount);
client = open_relay_connection(relay, NULL);
}