2004-01-28 20:02:12 -05:00
|
|
|
/* Icecast
|
|
|
|
*
|
|
|
|
* This program is distributed under the GNU General Public License, version 2.
|
|
|
|
* A copy of this license is included with this source.
|
|
|
|
*
|
2015-01-10 13:53:44 -05:00
|
|
|
* Copyright 2000-2004, Jack Moffitt <jack@xiph.org,
|
2004-01-28 20:02:12 -05:00
|
|
|
* Michael Smith <msmith@xiph.org>,
|
|
|
|
* oddsock <oddsock@xiph.org>,
|
|
|
|
* Karl Heyes <karl@xiph.org>
|
|
|
|
* and others (see AUTHORS for details).
|
|
|
|
*/
|
|
|
|
|
2003-03-27 12:10:14 -05:00
|
|
|
/* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- */
|
2003-07-20 21:58:54 -04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2003-02-02 09:24:13 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2003-02-06 08:10:48 -05:00
|
|
|
#include <stdlib.h>
|
2003-02-02 09:24:13 -05:00
|
|
|
|
2014-12-02 16:50:57 -05:00
|
|
|
#include "common/thread/thread.h"
|
2003-02-02 09:24:13 -05:00
|
|
|
|
2018-05-28 07:14:56 -04:00
|
|
|
#include "global.h"
|
2015-04-08 03:44:36 -04:00
|
|
|
#include "curl.h"
|
2003-02-02 09:24:13 -05:00
|
|
|
#include "connection.h"
|
|
|
|
#include "refbuf.h"
|
|
|
|
#include "client.h"
|
|
|
|
#include "logging.h"
|
|
|
|
#include "format.h"
|
|
|
|
#include "source.h"
|
2003-07-20 21:58:54 -04:00
|
|
|
#include "cfgfile.h"
|
2003-07-24 12:21:22 -04:00
|
|
|
#include "stats.h"
|
2004-06-02 15:34:14 -04:00
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
#define snprintf _snprintf
|
|
|
|
#endif
|
2003-02-02 09:24:13 -05:00
|
|
|
|
2015-01-10 13:53:44 -05:00
|
|
|
#define CATMODULE "yp"
|
2003-02-02 09:24:13 -05:00
|
|
|
|
2004-05-10 12:17:56 -04:00
|
|
|
struct yp_server
|
|
|
|
{
|
|
|
|
char *url;
|
2007-11-07 22:21:33 -05:00
|
|
|
char *server_id;
|
2004-05-10 12:17:56 -04:00
|
|
|
unsigned url_timeout;
|
|
|
|
unsigned touch_interval;
|
|
|
|
int remove;
|
|
|
|
|
|
|
|
CURL *curl;
|
|
|
|
struct ypdata_tag *mounts, *pending_mounts;
|
|
|
|
struct yp_server *next;
|
|
|
|
char curl_error[CURL_ERROR_SIZE];
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct ypdata_tag
|
|
|
|
{
|
|
|
|
int remove;
|
2004-09-15 10:21:04 -04:00
|
|
|
int release;
|
2004-05-10 12:17:56 -04:00
|
|
|
int cmd_ok;
|
|
|
|
|
|
|
|
char *sid;
|
|
|
|
char *mount;
|
|
|
|
char *url;
|
|
|
|
char *listen_url;
|
|
|
|
char *server_name;
|
|
|
|
char *server_desc;
|
|
|
|
char *server_genre;
|
|
|
|
char *cluster_password;
|
|
|
|
char *bitrate;
|
|
|
|
char *audio_info;
|
|
|
|
char *server_type;
|
|
|
|
char *current_song;
|
2004-12-09 19:11:16 -05:00
|
|
|
char *subtype;
|
2004-05-10 12:17:56 -04:00
|
|
|
|
|
|
|
struct yp_server *server;
|
2014-11-30 15:32:30 -05:00
|
|
|
time_t next_update;
|
|
|
|
unsigned touch_interval;
|
|
|
|
char *error_msg;
|
|
|
|
int (*process)(struct ypdata_tag *yp, char *s, unsigned len);
|
2004-05-10 12:17:56 -04:00
|
|
|
|
|
|
|
struct ypdata_tag *next;
|
|
|
|
} ypdata_t;
|
|
|
|
|
|
|
|
|
|
|
|
static rwlock_t yp_lock;
|
|
|
|
static mutex_t yp_pending_lock;
|
|
|
|
|
2005-08-08 22:14:20 -04:00
|
|
|
static volatile struct yp_server *active_yps = NULL, *pending_yps = NULL;
|
2004-10-25 17:10:10 -04:00
|
|
|
static volatile int yp_update = 0;
|
2004-05-10 12:17:56 -04:00
|
|
|
static int yp_running;
|
|
|
|
static time_t now;
|
|
|
|
static thread_type *yp_thread;
|
2005-08-08 22:14:20 -04:00
|
|
|
static volatile unsigned client_limit = 0;
|
2007-09-02 20:55:27 -04:00
|
|
|
static volatile char *server_version = NULL;
|
2004-05-10 12:17:56 -04:00
|
|
|
|
|
|
|
static void *yp_update_thread(void *arg);
|
2014-11-30 15:32:30 -05:00
|
|
|
static void add_yp_info(ypdata_t *yp, void *info, int type);
|
|
|
|
static int do_yp_remove(ypdata_t *yp, char *s, unsigned len);
|
|
|
|
static int do_yp_add(ypdata_t *yp, char *s, unsigned len);
|
|
|
|
static int do_yp_touch(ypdata_t *yp, char *s, unsigned len);
|
2015-01-18 12:51:58 -05:00
|
|
|
static void add_pending_yp (struct yp_server *server);
|
|
|
|
static void delete_marked_yp(struct yp_server *server);
|
2004-05-10 12:17:56 -04:00
|
|
|
static void yp_destroy_ypdata(ypdata_t *ypdata);
|
|
|
|
|
|
|
|
|
|
|
|
/* curl callback used to parse headers coming back from the YP server */
|
2014-12-14 14:18:22 -05:00
|
|
|
static size_t handle_returned_header (void *ptr, size_t size, size_t nmemb, void *stream)
|
2004-05-10 12:17:56 -04:00
|
|
|
{
|
|
|
|
ypdata_t *yp = stream;
|
2014-12-14 14:18:22 -05:00
|
|
|
size_t bytes = size * nmemb;
|
2004-05-10 12:17:56 -04:00
|
|
|
|
2014-10-31 04:46:58 -04:00
|
|
|
/* ICECAST_LOG_DEBUG("header from YP is \"%.*s\"", bytes, ptr); */
|
2013-02-23 19:53:09 -05:00
|
|
|
if (strncasecmp (ptr, "YPResponse: 1", 13) == 0)
|
2004-05-10 12:17:56 -04:00
|
|
|
yp->cmd_ok = 1;
|
|
|
|
|
2013-02-23 19:53:09 -05:00
|
|
|
if (strncasecmp (ptr, "YPMessage: ", 11) == 0)
|
2004-05-10 12:17:56 -04:00
|
|
|
{
|
2014-12-14 14:18:22 -05:00
|
|
|
size_t len = bytes - 11;
|
2004-05-10 12:17:56 -04:00
|
|
|
free (yp->error_msg);
|
|
|
|
yp->error_msg = calloc (1, len);
|
|
|
|
if (yp->error_msg)
|
2013-02-23 19:53:09 -05:00
|
|
|
sscanf (ptr + 11, "%[^\r\n]", yp->error_msg);
|
2004-05-10 12:17:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (yp->process == do_yp_add)
|
|
|
|
{
|
2013-02-23 19:53:09 -05:00
|
|
|
if (strncasecmp (ptr, "SID: ", 5) == 0)
|
2004-05-10 12:17:56 -04:00
|
|
|
{
|
2014-12-14 14:18:22 -05:00
|
|
|
size_t len = bytes - 5;
|
2004-05-10 12:17:56 -04:00
|
|
|
free (yp->sid);
|
|
|
|
yp->sid = calloc (1, len);
|
|
|
|
if (yp->sid)
|
2013-02-23 19:53:09 -05:00
|
|
|
sscanf (ptr + 5, "%[^\r\n]", yp->sid);
|
2004-05-10 12:17:56 -04:00
|
|
|
}
|
2005-06-19 09:50:39 -04:00
|
|
|
}
|
2013-02-23 19:53:09 -05:00
|
|
|
if (strncasecmp (ptr, "TouchFreq: ", 11) == 0)
|
2005-06-19 09:50:39 -04:00
|
|
|
{
|
|
|
|
unsigned secs;
|
2013-02-23 19:53:09 -05:00
|
|
|
if ( sscanf (ptr + 11, "%u", &secs) != 1 )
|
|
|
|
secs = 0;
|
2005-06-19 09:50:39 -04:00
|
|
|
if (secs < 30)
|
|
|
|
secs = 30;
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_DEBUG("server touch interval is %u", secs);
|
2005-06-19 09:50:39 -04:00
|
|
|
yp->touch_interval = secs;
|
2004-05-10 12:17:56 -04:00
|
|
|
}
|
2014-12-14 14:18:22 -05:00
|
|
|
return (size_t)bytes;
|
2004-05-10 12:17:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* search the active and pending YP server lists */
|
|
|
|
static struct yp_server *find_yp_server (const char *url)
|
|
|
|
{
|
|
|
|
struct yp_server *server;
|
|
|
|
|
2004-10-25 17:10:10 -04:00
|
|
|
server = (struct yp_server *)active_yps;
|
2004-05-10 12:17:56 -04:00
|
|
|
while (server)
|
|
|
|
{
|
|
|
|
if (strcmp (server->url, url) == 0)
|
|
|
|
return server;
|
|
|
|
server = server->next;
|
|
|
|
}
|
2004-10-25 17:10:10 -04:00
|
|
|
server = (struct yp_server *)pending_yps;
|
2004-05-10 12:17:56 -04:00
|
|
|
while (server)
|
|
|
|
{
|
|
|
|
if (strcmp (server->url, url) == 0)
|
|
|
|
break;
|
|
|
|
server = server->next;
|
|
|
|
}
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void destroy_yp_server (struct yp_server *server)
|
|
|
|
{
|
2015-01-18 12:51:58 -05:00
|
|
|
ypdata_t *yp;
|
|
|
|
|
2004-05-10 12:17:56 -04:00
|
|
|
if (server == NULL)
|
|
|
|
return;
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_DEBUG("Removing YP server entry for %s", server->url);
|
2015-01-18 12:51:58 -05:00
|
|
|
|
|
|
|
/* delete yps:
|
|
|
|
* first move all pendings into main queue.
|
|
|
|
* then mark all main queue entries for deleting.
|
|
|
|
* then remove all marked entries.
|
|
|
|
*/
|
|
|
|
add_pending_yp(server);
|
|
|
|
yp = server->mounts;
|
|
|
|
while (yp) {
|
|
|
|
yp->remove = 1;
|
|
|
|
yp = yp->next;
|
|
|
|
}
|
|
|
|
delete_marked_yp(server);
|
|
|
|
|
2015-04-08 03:44:36 -04:00
|
|
|
icecast_curl_free(server->curl);
|
2015-03-01 11:51:09 -05:00
|
|
|
if (server->mounts) ICECAST_LOG_WARN("active ypdata not freed");
|
|
|
|
if (server->pending_mounts) ICECAST_LOG_WARN("pending ypdata not freed");
|
2004-05-10 12:17:56 -04:00
|
|
|
free (server->url);
|
2007-11-07 22:21:33 -05:00
|
|
|
free (server->server_id);
|
2004-05-10 12:17:56 -04:00
|
|
|
free (server);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* search for a ypdata entry corresponding to a specific mountpoint */
|
2004-09-15 10:21:04 -04:00
|
|
|
static ypdata_t *find_yp_mount (ypdata_t *mounts, const char *mount)
|
2004-05-10 12:17:56 -04:00
|
|
|
{
|
2004-09-15 10:21:04 -04:00
|
|
|
ypdata_t *yp = mounts;
|
2004-05-10 12:17:56 -04:00
|
|
|
while (yp)
|
|
|
|
{
|
|
|
|
if (strcmp (yp->mount, mount) == 0)
|
|
|
|
break;
|
|
|
|
yp = yp->next;
|
|
|
|
}
|
|
|
|
return yp;
|
|
|
|
}
|
|
|
|
|
2004-01-26 17:42:22 -05:00
|
|
|
|
|
|
|
void yp_recheck_config (ice_config_t *config)
|
|
|
|
{
|
2004-05-10 12:17:56 -04:00
|
|
|
int i;
|
|
|
|
struct yp_server *server;
|
|
|
|
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_DEBUG("Updating YP configuration");
|
2004-05-10 12:17:56 -04:00
|
|
|
thread_rwlock_rlock (&yp_lock);
|
|
|
|
|
2004-10-25 17:10:10 -04:00
|
|
|
server = (struct yp_server *)active_yps;
|
2004-05-10 12:17:56 -04:00
|
|
|
while (server)
|
|
|
|
{
|
|
|
|
server->remove = 1;
|
|
|
|
server = server->next;
|
|
|
|
}
|
2005-08-08 22:14:20 -04:00
|
|
|
client_limit = config->client_limit;
|
2007-09-02 20:55:27 -04:00
|
|
|
free ((char*)server_version);
|
|
|
|
server_version = strdup (config->server_id);
|
2015-01-10 13:53:44 -05:00
|
|
|
/* for each yp url in config, check to see if one exists
|
2004-05-10 12:17:56 -04:00
|
|
|
if not, then add it. */
|
|
|
|
for (i=0 ; i < config->num_yp_directories; i++)
|
|
|
|
{
|
|
|
|
server = find_yp_server (config->yp_url[i]);
|
|
|
|
if (server == NULL)
|
|
|
|
{
|
|
|
|
server = calloc (1, sizeof (struct yp_server));
|
|
|
|
|
|
|
|
if (server == NULL)
|
|
|
|
{
|
|
|
|
destroy_yp_server (server);
|
|
|
|
break;
|
|
|
|
}
|
2007-11-07 22:21:33 -05:00
|
|
|
server->server_id = strdup ((char *)server_version);
|
2004-05-10 12:17:56 -04:00
|
|
|
server->url = strdup (config->yp_url[i]);
|
|
|
|
server->url_timeout = config->yp_url_timeout[i];
|
2004-10-25 17:10:10 -04:00
|
|
|
server->touch_interval = config->yp_touch_interval[i];
|
2015-04-08 03:44:36 -04:00
|
|
|
server->curl = icecast_curl_new(server->url, &(server->curl_error[0]));
|
2004-05-10 12:17:56 -04:00
|
|
|
if (server->curl == NULL)
|
|
|
|
{
|
|
|
|
destroy_yp_server (server);
|
|
|
|
break;
|
|
|
|
}
|
2008-05-20 22:19:47 -04:00
|
|
|
if (server->url_timeout > 10 || server->url_timeout < 1)
|
|
|
|
server->url_timeout = 6;
|
2004-05-10 12:17:56 -04:00
|
|
|
if (server->touch_interval < 30)
|
|
|
|
server->touch_interval = 30;
|
|
|
|
curl_easy_setopt (server->curl, CURLOPT_HEADERFUNCTION, handle_returned_header);
|
2004-10-25 17:10:10 -04:00
|
|
|
server->next = (struct yp_server *)pending_yps;
|
2004-05-10 12:17:56 -04:00
|
|
|
pending_yps = server;
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_INFO("Adding new YP server \"%s\" (timeout %ds, default interval %ds)",
|
2004-05-10 12:17:56 -04:00
|
|
|
server->url, server->url_timeout, server->touch_interval);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
server->remove = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
thread_rwlock_unlock (&yp_lock);
|
|
|
|
yp_update = 1;
|
2004-01-26 17:42:22 -05:00
|
|
|
}
|
|
|
|
|
2004-05-10 12:17:56 -04:00
|
|
|
|
2005-12-17 07:23:09 -05:00
|
|
|
void yp_initialize(void)
|
2003-06-26 09:33:55 -04:00
|
|
|
{
|
2004-01-26 17:42:22 -05:00
|
|
|
ice_config_t *config = config_get_config();
|
2004-05-10 12:17:56 -04:00
|
|
|
thread_rwlock_create (&yp_lock);
|
|
|
|
thread_mutex_create (&yp_pending_lock);
|
2004-01-26 17:42:22 -05:00
|
|
|
yp_recheck_config (config);
|
|
|
|
config_release_config ();
|
2004-05-10 12:17:56 -04:00
|
|
|
yp_thread = thread_create("YP Touch Thread", yp_update_thread,
|
|
|
|
(void *)NULL, THREAD_ATTACHED);
|
2003-06-26 09:33:55 -04:00
|
|
|
}
|
2004-05-10 12:17:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2008-02-03 00:43:32 -05:00
|
|
|
/* handler for curl, checks if successful handling occurred
|
2008-04-17 21:12:45 -04:00
|
|
|
* return 0 for ok, -1 for this entry failed, -2 for server fail.
|
|
|
|
* On failure case, update and process are modified
|
2008-02-03 00:43:32 -05:00
|
|
|
*/
|
2004-05-10 12:17:56 -04:00
|
|
|
static int send_to_yp (const char *cmd, ypdata_t *yp, char *post)
|
2003-02-02 09:24:13 -05:00
|
|
|
{
|
2004-05-10 12:17:56 -04:00
|
|
|
int curlcode;
|
|
|
|
struct yp_server *server = yp->server;
|
|
|
|
|
2014-10-31 04:46:58 -04:00
|
|
|
/* ICECAST_LOG_DEBUG("send YP (%s):%s", cmd, post); */
|
2004-05-10 12:17:56 -04:00
|
|
|
yp->cmd_ok = 0;
|
|
|
|
curl_easy_setopt (server->curl, CURLOPT_POSTFIELDS, post);
|
|
|
|
curl_easy_setopt (server->curl, CURLOPT_WRITEHEADER, yp);
|
|
|
|
curlcode = curl_easy_perform (server->curl);
|
|
|
|
if (curlcode)
|
|
|
|
{
|
|
|
|
yp->process = do_yp_add;
|
2008-05-02 12:10:22 -04:00
|
|
|
yp->next_update = now + 1200;
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_ERROR("connection to %s failed with \"%s\"", server->url, server->curl_error);
|
2008-02-03 00:43:32 -05:00
|
|
|
return -2;
|
2003-04-10 23:00:24 -04:00
|
|
|
}
|
2004-05-10 12:17:56 -04:00
|
|
|
if (yp->cmd_ok == 0)
|
|
|
|
{
|
2004-06-25 14:25:10 -04:00
|
|
|
if (yp->error_msg == NULL)
|
|
|
|
yp->error_msg = strdup ("no response from server");
|
2008-04-17 21:12:45 -04:00
|
|
|
if (yp->process == do_yp_add)
|
|
|
|
{
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_ERROR("YP %s on %s failed: %s", cmd, server->url, yp->error_msg);
|
2008-05-02 12:10:22 -04:00
|
|
|
yp->next_update = now + 7200;
|
2008-04-17 21:12:45 -04:00
|
|
|
}
|
2008-04-22 21:45:38 -04:00
|
|
|
if (yp->process == do_yp_touch)
|
2008-04-17 21:12:45 -04:00
|
|
|
{
|
2008-04-22 21:45:38 -04:00
|
|
|
/* At this point the touch request failed, either because they rejected our session
|
|
|
|
* or the server isn't accessible. This means we have to wait before doing another
|
|
|
|
* add request. We have a minimum delay but we could allow the directory server to
|
|
|
|
* give us a wait time using the TouchFreq header. This time could be given in such
|
|
|
|
* cases as a firewall block or incorrect listenurl.
|
|
|
|
*/
|
|
|
|
if (yp->touch_interval < 1200)
|
2008-05-02 12:10:22 -04:00
|
|
|
yp->next_update = now + 1200;
|
2008-04-22 21:45:38 -04:00
|
|
|
else
|
2008-05-02 12:10:22 -04:00
|
|
|
yp->next_update = now + yp->touch_interval;
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_INFO("YP %s on %s failed: %s", cmd, server->url, yp->error_msg);
|
2008-04-17 21:12:45 -04:00
|
|
|
}
|
2004-05-10 12:17:56 -04:00
|
|
|
yp->process = do_yp_add;
|
2014-11-30 15:32:30 -05:00
|
|
|
free(yp->sid);
|
2008-04-17 21:12:45 -04:00
|
|
|
yp->sid = NULL;
|
2004-05-10 12:17:56 -04:00
|
|
|
return -1;
|
2003-02-02 11:48:15 -05:00
|
|
|
}
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_DEBUG("YP %s at %s succeeded", cmd, server->url);
|
2004-05-10 12:17:56 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* routines for building and issues requests to the YP server */
|
2008-02-03 00:43:32 -05:00
|
|
|
static int do_yp_remove (ypdata_t *yp, char *s, unsigned len)
|
2004-05-10 12:17:56 -04:00
|
|
|
{
|
2008-02-03 00:43:32 -05:00
|
|
|
int ret = 0;
|
|
|
|
|
2004-05-10 12:17:56 -04:00
|
|
|
if (yp->sid)
|
|
|
|
{
|
2008-02-03 00:43:32 -05:00
|
|
|
ret = snprintf (s, len, "action=remove&sid=%s", yp->sid);
|
2004-05-10 12:17:56 -04:00
|
|
|
if (ret >= (signed)len)
|
|
|
|
return ret+1;
|
|
|
|
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_INFO("clearing up YP entry for %s", yp->mount);
|
2008-02-03 00:43:32 -05:00
|
|
|
ret = send_to_yp ("remove", yp, s);
|
2004-05-10 12:17:56 -04:00
|
|
|
free (yp->sid);
|
|
|
|
yp->sid = NULL;
|
2003-02-02 11:48:15 -05:00
|
|
|
}
|
2004-06-02 15:34:14 -04:00
|
|
|
yp->remove = 1;
|
|
|
|
yp->process = do_yp_add;
|
2008-04-17 21:12:45 -04:00
|
|
|
yp_update = 1;
|
2004-09-15 10:21:04 -04:00
|
|
|
|
2008-02-03 00:43:32 -05:00
|
|
|
return ret;
|
2003-02-02 09:24:13 -05:00
|
|
|
}
|
|
|
|
|
2004-05-10 12:17:56 -04:00
|
|
|
|
2008-02-03 00:43:32 -05:00
|
|
|
static int do_yp_add (ypdata_t *yp, char *s, unsigned len)
|
2003-02-02 09:24:13 -05:00
|
|
|
{
|
2004-12-09 19:11:16 -05:00
|
|
|
int ret;
|
|
|
|
char *value;
|
2015-01-18 13:47:24 -05:00
|
|
|
ice_config_t *config;
|
|
|
|
char *admin;
|
|
|
|
|
|
|
|
config = config_get_config();
|
|
|
|
admin = util_url_escape(config->admin);
|
|
|
|
config_release_config();
|
2004-12-09 19:11:16 -05:00
|
|
|
|
2005-05-30 10:50:57 -04:00
|
|
|
value = stats_get_value (yp->mount, "server_type");
|
|
|
|
add_yp_info (yp, value, YP_SERVER_TYPE);
|
|
|
|
free (value);
|
|
|
|
|
|
|
|
value = stats_get_value (yp->mount, "server_name");
|
|
|
|
add_yp_info (yp, value, YP_SERVER_NAME);
|
|
|
|
free (value);
|
|
|
|
|
|
|
|
value = stats_get_value (yp->mount, "server_url");
|
|
|
|
add_yp_info (yp, value, YP_SERVER_URL);
|
|
|
|
free (value);
|
|
|
|
|
|
|
|
value = stats_get_value (yp->mount, "genre");
|
|
|
|
add_yp_info (yp, value, YP_SERVER_GENRE);
|
|
|
|
free (value);
|
|
|
|
|
|
|
|
value = stats_get_value (yp->mount, "bitrate");
|
2008-05-20 22:19:47 -04:00
|
|
|
if (value == NULL)
|
|
|
|
value = stats_get_value (yp->mount, "ice-bitrate");
|
2005-05-30 10:50:57 -04:00
|
|
|
add_yp_info (yp, value, YP_BITRATE);
|
|
|
|
free (value);
|
|
|
|
|
|
|
|
value = stats_get_value (yp->mount, "server_description");
|
|
|
|
add_yp_info (yp, value, YP_SERVER_DESC);
|
|
|
|
free (value);
|
|
|
|
|
2004-12-09 19:11:16 -05:00
|
|
|
value = stats_get_value (yp->mount, "subtype");
|
2005-05-30 10:50:57 -04:00
|
|
|
add_yp_info (yp, value, YP_SUBTYPE);
|
|
|
|
free (value);
|
|
|
|
|
|
|
|
value = stats_get_value (yp->mount, "audio_info");
|
|
|
|
add_yp_info (yp, value, YP_AUDIO_INFO);
|
|
|
|
free (value);
|
2004-12-09 19:11:16 -05:00
|
|
|
|
2015-01-18 13:47:24 -05:00
|
|
|
ret = snprintf (s, len, "action=add&admin=%s&sn=%s&genre=%s&cpswd=%s&desc="
|
2004-12-09 19:11:16 -05:00
|
|
|
"%s&url=%s&listenurl=%s&type=%s&stype=%s&b=%s&%s\r\n",
|
2015-01-18 13:47:24 -05:00
|
|
|
admin,
|
2004-05-10 12:17:56 -04:00
|
|
|
yp->server_name, yp->server_genre, yp->cluster_password,
|
|
|
|
yp->server_desc, yp->url, yp->listen_url,
|
2004-12-09 19:11:16 -05:00
|
|
|
yp->server_type, yp->subtype, yp->bitrate, yp->audio_info);
|
2015-01-18 13:47:24 -05:00
|
|
|
free(admin);
|
|
|
|
|
2004-05-10 12:17:56 -04:00
|
|
|
if (ret >= (signed)len)
|
|
|
|
return ret+1;
|
2008-02-03 00:43:32 -05:00
|
|
|
ret = send_to_yp ("add", yp, s);
|
|
|
|
if (ret == 0)
|
2004-05-10 12:17:56 -04:00
|
|
|
{
|
|
|
|
yp->process = do_yp_touch;
|
|
|
|
/* force first touch in 5 secs */
|
|
|
|
yp->next_update = time(NULL) + 5;
|
|
|
|
}
|
2008-02-03 00:43:32 -05:00
|
|
|
return ret;
|
2004-05-10 12:17:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-03 00:43:32 -05:00
|
|
|
static int do_yp_touch (ypdata_t *yp, char *s, unsigned len)
|
2004-05-10 12:17:56 -04:00
|
|
|
{
|
2005-08-08 22:14:20 -04:00
|
|
|
unsigned listeners = 0, max_listeners = 1;
|
2004-05-10 12:17:56 -04:00
|
|
|
char *val, *artist, *title;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
artist = (char *)stats_get_value (yp->mount, "artist");
|
|
|
|
title = (char *)stats_get_value (yp->mount, "title");
|
|
|
|
if (artist || title)
|
|
|
|
{
|
|
|
|
char *song;
|
2014-11-30 13:18:05 -05:00
|
|
|
const char *separator = " - ";
|
2004-05-10 12:17:56 -04:00
|
|
|
if (artist == NULL)
|
|
|
|
{
|
|
|
|
artist = strdup("");
|
|
|
|
separator = "";
|
|
|
|
}
|
|
|
|
if (title == NULL) title = strdup("");
|
|
|
|
song = malloc (strlen (artist) + strlen (title) + strlen (separator) +1);
|
|
|
|
if (song)
|
|
|
|
{
|
|
|
|
sprintf (song, "%s%s%s", artist, separator, title);
|
2005-05-30 10:50:57 -04:00
|
|
|
add_yp_info(yp, song, YP_CURRENT_SONG);
|
|
|
|
stats_event (yp->mount, "yp_currently_playing", song);
|
2004-05-10 12:17:56 -04:00
|
|
|
free (song);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free (artist);
|
|
|
|
free (title);
|
|
|
|
|
|
|
|
val = (char *)stats_get_value (yp->mount, "listeners");
|
|
|
|
if (val)
|
|
|
|
{
|
|
|
|
listeners = atoi (val);
|
|
|
|
free (val);
|
|
|
|
}
|
2005-08-08 22:14:20 -04:00
|
|
|
val = stats_get_value (yp->mount, "max_listeners");
|
2007-11-07 22:21:33 -05:00
|
|
|
if (val == NULL || strcmp (val, "unlimited") == 0 || atoi(val) < 0)
|
2005-08-08 22:14:20 -04:00
|
|
|
max_listeners = client_limit;
|
|
|
|
else
|
|
|
|
max_listeners = atoi (val);
|
2007-11-07 22:21:33 -05:00
|
|
|
free (val);
|
2005-08-08 22:14:20 -04:00
|
|
|
|
2004-12-17 15:03:26 -05:00
|
|
|
val = stats_get_value (yp->mount, "subtype");
|
|
|
|
if (val)
|
|
|
|
{
|
2005-05-30 10:50:57 -04:00
|
|
|
add_yp_info (yp, val, YP_SUBTYPE);
|
2004-12-17 15:03:26 -05:00
|
|
|
free (val);
|
|
|
|
}
|
2004-12-07 21:28:07 -05:00
|
|
|
|
|
|
|
ret = snprintf (s, len, "action=touch&sid=%s&st=%s"
|
2005-08-08 22:14:20 -04:00
|
|
|
"&listeners=%u&max_listeners=%u&stype=%s\r\n",
|
2004-12-17 15:03:26 -05:00
|
|
|
yp->sid, yp->current_song, listeners, max_listeners, yp->subtype);
|
2004-05-10 12:17:56 -04:00
|
|
|
|
|
|
|
if (ret >= (signed)len)
|
|
|
|
return ret+1; /* space required for above text and nul*/
|
|
|
|
|
2008-04-17 21:12:45 -04:00
|
|
|
if (send_to_yp ("touch", yp, s) == 0)
|
|
|
|
{
|
|
|
|
yp->next_update = now + yp->touch_interval;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
2003-02-02 09:24:13 -05:00
|
|
|
}
|
2003-02-06 08:10:48 -05:00
|
|
|
|
2004-05-10 12:17:56 -04:00
|
|
|
|
|
|
|
|
2014-11-30 15:32:30 -05:00
|
|
|
static int process_ypdata(struct yp_server *server, ypdata_t *yp)
|
2003-02-02 09:24:13 -05:00
|
|
|
{
|
2008-04-18 22:04:17 -04:00
|
|
|
unsigned len = 1024;
|
2004-05-10 12:17:56 -04:00
|
|
|
char *s = NULL, *tmp;
|
2003-02-02 11:48:15 -05:00
|
|
|
|
2004-05-10 12:17:56 -04:00
|
|
|
if (now < yp->next_update)
|
2008-02-03 00:43:32 -05:00
|
|
|
return 0;
|
2003-02-02 11:48:15 -05:00
|
|
|
|
2004-05-10 12:17:56 -04:00
|
|
|
/* loop just in case the memory area isn't big enough */
|
|
|
|
while (1)
|
|
|
|
{
|
2008-02-03 00:43:32 -05:00
|
|
|
int ret;
|
2014-11-30 15:32:30 -05:00
|
|
|
if ((tmp = realloc(s, len)) == NULL)
|
2008-02-03 00:43:32 -05:00
|
|
|
return 0;
|
2004-05-10 12:17:56 -04:00
|
|
|
s = tmp;
|
2003-02-02 11:48:15 -05:00
|
|
|
|
2004-09-15 10:21:04 -04:00
|
|
|
if (yp->release)
|
|
|
|
{
|
|
|
|
yp->process = do_yp_remove;
|
|
|
|
yp->next_update = 0;
|
|
|
|
}
|
|
|
|
|
2004-05-10 12:17:56 -04:00
|
|
|
ret = yp->process (yp, s, len);
|
2008-04-18 22:04:17 -04:00
|
|
|
if (ret <= 0)
|
2004-05-10 12:17:56 -04:00
|
|
|
{
|
|
|
|
free (s);
|
2008-02-03 00:43:32 -05:00
|
|
|
return ret;
|
2003-02-02 11:48:15 -05:00
|
|
|
}
|
2004-05-10 12:17:56 -04:00
|
|
|
len = ret;
|
2003-02-02 11:48:15 -05:00
|
|
|
}
|
2008-02-03 00:43:32 -05:00
|
|
|
return 0;
|
2003-02-02 09:24:13 -05:00
|
|
|
}
|
2004-05-10 12:17:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
static void yp_process_server (struct yp_server *server)
|
2003-02-02 09:24:13 -05:00
|
|
|
{
|
2004-05-10 12:17:56 -04:00
|
|
|
ypdata_t *yp;
|
2008-02-03 00:43:32 -05:00
|
|
|
int state = 0;
|
2004-05-10 12:17:56 -04:00
|
|
|
|
2014-10-31 04:46:58 -04:00
|
|
|
/* ICECAST_LOG_DEBUG("processing yp server %s", server->url); */
|
2004-05-10 12:17:56 -04:00
|
|
|
yp = server->mounts;
|
|
|
|
while (yp)
|
|
|
|
{
|
|
|
|
now = time (NULL);
|
2008-02-03 00:43:32 -05:00
|
|
|
/* if one of the streams shows that the server cannot be contacted then mark the
|
|
|
|
* other entries for an update later. Assume YP server is dead and skip it for now
|
|
|
|
*/
|
|
|
|
if (state == -2)
|
|
|
|
{
|
2016-11-23 15:36:53 -05:00
|
|
|
ICECAST_LOG_DEBUG("skipping %s on %s", yp->mount, server->url);
|
2008-02-03 00:43:32 -05:00
|
|
|
yp->process = do_yp_add;
|
|
|
|
yp->next_update += 900;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
state = process_ypdata (server, yp);
|
2004-05-10 12:17:56 -04:00
|
|
|
yp = yp->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-05-30 10:50:57 -04:00
|
|
|
static ypdata_t *create_yp_entry (const char *mount)
|
2004-05-10 12:17:56 -04:00
|
|
|
{
|
|
|
|
ypdata_t *yp;
|
2003-06-26 09:33:55 -04:00
|
|
|
char *s;
|
2003-02-02 11:48:15 -05:00
|
|
|
|
2004-05-10 12:17:56 -04:00
|
|
|
yp = calloc (1, sizeof (ypdata_t));
|
|
|
|
do
|
|
|
|
{
|
|
|
|
unsigned len = 512;
|
|
|
|
int ret;
|
|
|
|
char *url;
|
2004-12-07 17:29:46 -05:00
|
|
|
mount_proxy *mountproxy = NULL;
|
2004-05-10 12:17:56 -04:00
|
|
|
ice_config_t *config;
|
|
|
|
|
|
|
|
if (yp == NULL)
|
|
|
|
break;
|
2005-05-30 10:50:57 -04:00
|
|
|
yp->mount = strdup (mount);
|
2004-05-10 12:17:56 -04:00
|
|
|
yp->server_name = strdup ("");
|
|
|
|
yp->server_desc = strdup ("");
|
|
|
|
yp->server_genre = strdup ("");
|
|
|
|
yp->bitrate = strdup ("");
|
|
|
|
yp->server_type = strdup ("");
|
|
|
|
yp->cluster_password = strdup ("");
|
|
|
|
yp->url = strdup ("");
|
|
|
|
yp->current_song = strdup ("");
|
|
|
|
yp->audio_info = strdup ("");
|
2004-12-09 19:11:16 -05:00
|
|
|
yp->subtype = strdup ("");
|
2004-05-10 12:17:56 -04:00
|
|
|
yp->process = do_yp_add;
|
|
|
|
|
|
|
|
url = malloc (len);
|
|
|
|
if (url == NULL)
|
|
|
|
break;
|
|
|
|
config = config_get_config();
|
2005-05-30 10:50:57 -04:00
|
|
|
ret = snprintf (url, len, "http://%s:%d%s", config->hostname, config->port, mount);
|
2004-05-10 12:17:56 -04:00
|
|
|
if (ret >= (signed)len)
|
2004-02-19 10:24:06 -05:00
|
|
|
{
|
2004-05-10 12:17:56 -04:00
|
|
|
s = realloc (url, ++ret);
|
|
|
|
if (s) url = s;
|
2005-05-30 10:50:57 -04:00
|
|
|
snprintf (url, ret, "http://%s:%d%s", config->hostname, config->port, mount);
|
2004-05-10 12:17:56 -04:00
|
|
|
}
|
2004-12-07 17:29:46 -05:00
|
|
|
|
2013-04-02 14:46:44 -04:00
|
|
|
mountproxy = config_find_mount (config, mount, MOUNT_TYPE_NORMAL);
|
2005-05-25 23:04:48 -04:00
|
|
|
if (mountproxy && mountproxy->cluster_password)
|
2005-05-30 10:50:57 -04:00
|
|
|
add_yp_info (yp, mountproxy->cluster_password, YP_CLUSTER_PASSWORD);
|
2004-05-10 12:17:56 -04:00
|
|
|
config_release_config();
|
2005-05-25 23:04:48 -04:00
|
|
|
|
2004-05-10 12:17:56 -04:00
|
|
|
yp->listen_url = util_url_escape (url);
|
|
|
|
free (url);
|
|
|
|
if (yp->listen_url == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
return yp;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
yp_destroy_ypdata (yp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Check for changes in the YP servers configured */
|
2005-12-17 07:23:09 -05:00
|
|
|
static void check_servers (void)
|
2004-05-10 12:17:56 -04:00
|
|
|
{
|
2004-10-25 17:10:10 -04:00
|
|
|
struct yp_server *server = (struct yp_server *)active_yps,
|
|
|
|
**server_p = (struct yp_server **)&active_yps;
|
2004-05-10 12:17:56 -04:00
|
|
|
|
|
|
|
while (server)
|
|
|
|
{
|
|
|
|
if (server->remove)
|
|
|
|
{
|
|
|
|
struct yp_server *to_go = server;
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_DEBUG("YP server \"%s\"removed", server->url);
|
2004-05-10 12:17:56 -04:00
|
|
|
*server_p = server->next;
|
|
|
|
server = server->next;
|
2014-11-30 15:32:30 -05:00
|
|
|
destroy_yp_server(to_go);
|
2003-06-26 09:33:55 -04:00
|
|
|
continue;
|
2003-02-02 11:48:15 -05:00
|
|
|
}
|
2004-05-10 12:17:56 -04:00
|
|
|
server_p = &server->next;
|
|
|
|
server = server->next;
|
|
|
|
}
|
|
|
|
/* add new server entries */
|
|
|
|
while (pending_yps)
|
|
|
|
{
|
|
|
|
avl_node *node;
|
|
|
|
|
2004-10-25 17:10:10 -04:00
|
|
|
server = (struct yp_server *)pending_yps;
|
2004-05-10 12:17:56 -04:00
|
|
|
pending_yps = server->next;
|
|
|
|
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_DEBUG("Add pending yps %s", server->url);
|
2004-10-25 17:10:10 -04:00
|
|
|
server->next = (struct yp_server *)active_yps;
|
2004-05-10 12:17:56 -04:00
|
|
|
active_yps = server;
|
|
|
|
|
|
|
|
/* new YP server configured, need to populate with existing sources */
|
|
|
|
avl_tree_rlock (global.source_tree);
|
|
|
|
node = avl_get_first (global.source_tree);
|
|
|
|
while (node)
|
|
|
|
{
|
|
|
|
ypdata_t *yp;
|
|
|
|
|
|
|
|
source_t *source = node->key;
|
2005-08-29 20:30:30 -04:00
|
|
|
if (source->yp_public && (yp = create_yp_entry (source->mount)) != NULL)
|
2004-05-10 12:17:56 -04:00
|
|
|
{
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_DEBUG("Adding existing mount %s", source->mount);
|
2004-05-10 12:17:56 -04:00
|
|
|
yp->server = server;
|
|
|
|
yp->touch_interval = server->touch_interval;
|
|
|
|
yp->next = server->mounts;
|
|
|
|
server->mounts = yp;
|
2003-02-02 11:48:15 -05:00
|
|
|
}
|
2004-05-10 12:17:56 -04:00
|
|
|
node = avl_get_next (node);
|
2003-02-02 11:48:15 -05:00
|
|
|
}
|
2004-05-10 12:17:56 -04:00
|
|
|
avl_tree_unlock (global.source_tree);
|
2003-06-26 09:33:55 -04:00
|
|
|
}
|
2004-05-10 12:17:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void add_pending_yp (struct yp_server *server)
|
|
|
|
{
|
|
|
|
ypdata_t *current, *yp;
|
|
|
|
unsigned count = 0;
|
|
|
|
|
|
|
|
if (server->pending_mounts == NULL)
|
|
|
|
return;
|
|
|
|
current = server->mounts;
|
|
|
|
server->mounts = server->pending_mounts;
|
|
|
|
server->pending_mounts = NULL;
|
|
|
|
yp = server->mounts;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
if (yp->next == NULL)
|
|
|
|
break;
|
|
|
|
yp = yp->next;
|
2003-02-02 11:48:15 -05:00
|
|
|
}
|
2004-05-10 12:17:56 -04:00
|
|
|
yp->next = current;
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_DEBUG("%u YP entries added to %s", count, server->url);
|
2004-05-10 12:17:56 -04:00
|
|
|
}
|
|
|
|
|
2003-06-26 09:33:55 -04:00
|
|
|
|
2014-11-30 15:32:30 -05:00
|
|
|
static void delete_marked_yp(struct yp_server *server)
|
2004-05-10 12:17:56 -04:00
|
|
|
{
|
|
|
|
ypdata_t *yp = server->mounts, **prev = &server->mounts;
|
2003-06-26 09:33:55 -04:00
|
|
|
|
2004-05-10 12:17:56 -04:00
|
|
|
while (yp)
|
|
|
|
{
|
|
|
|
if (yp->remove)
|
|
|
|
{
|
|
|
|
ypdata_t *to_go = yp;
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_DEBUG("removed %s from YP server %s", yp->mount, server->url);
|
2004-05-10 12:17:56 -04:00
|
|
|
*prev = yp->next;
|
|
|
|
yp = yp->next;
|
2014-11-30 15:32:30 -05:00
|
|
|
yp_destroy_ypdata(to_go);
|
2004-05-10 12:17:56 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
prev = &yp->next;
|
|
|
|
yp = yp->next;
|
|
|
|
}
|
2003-02-02 09:24:13 -05:00
|
|
|
}
|
2004-05-10 12:17:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
static void *yp_update_thread(void *arg)
|
2003-02-02 09:24:13 -05:00
|
|
|
{
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_INFO("YP update thread started");
|
2004-05-10 12:17:56 -04:00
|
|
|
|
|
|
|
yp_running = 1;
|
|
|
|
while (yp_running)
|
|
|
|
{
|
|
|
|
struct yp_server *server;
|
|
|
|
|
|
|
|
thread_sleep (200000);
|
|
|
|
|
|
|
|
/* do the YP communication */
|
|
|
|
thread_rwlock_rlock (&yp_lock);
|
2004-10-25 17:10:10 -04:00
|
|
|
server = (struct yp_server *)active_yps;
|
2004-05-10 12:17:56 -04:00
|
|
|
while (server)
|
|
|
|
{
|
2014-10-31 04:46:58 -04:00
|
|
|
/* ICECAST_LOG_DEBUG("trying %s", server->url); */
|
2004-05-10 12:17:56 -04:00
|
|
|
yp_process_server (server);
|
|
|
|
server = server->next;
|
2003-02-02 11:48:15 -05:00
|
|
|
}
|
2004-05-10 12:17:56 -04:00
|
|
|
thread_rwlock_unlock (&yp_lock);
|
|
|
|
|
|
|
|
/* update the local YP structure */
|
|
|
|
if (yp_update)
|
|
|
|
{
|
|
|
|
thread_rwlock_wlock (&yp_lock);
|
|
|
|
check_servers ();
|
2004-10-25 17:10:10 -04:00
|
|
|
server = (struct yp_server *)active_yps;
|
2004-05-10 12:17:56 -04:00
|
|
|
while (server)
|
|
|
|
{
|
2014-10-31 04:46:58 -04:00
|
|
|
/* ICECAST_LOG_DEBUG("Checking yps %s", server->url); */
|
2004-05-10 12:17:56 -04:00
|
|
|
add_pending_yp (server);
|
|
|
|
delete_marked_yp (server);
|
|
|
|
server = server->next;
|
2003-02-02 11:48:15 -05:00
|
|
|
}
|
2004-05-10 12:17:56 -04:00
|
|
|
yp_update = 0;
|
|
|
|
thread_rwlock_unlock (&yp_lock);
|
2003-02-02 11:48:15 -05:00
|
|
|
}
|
|
|
|
}
|
2004-05-10 12:17:56 -04:00
|
|
|
thread_rwlock_destroy (&yp_lock);
|
|
|
|
thread_mutex_destroy (&yp_pending_lock);
|
|
|
|
/* free server and ypdata left */
|
|
|
|
while (active_yps)
|
|
|
|
{
|
2004-10-25 17:10:10 -04:00
|
|
|
struct yp_server *server = (struct yp_server *)active_yps;
|
2004-05-10 12:17:56 -04:00
|
|
|
active_yps = server->next;
|
|
|
|
destroy_yp_server (server);
|
|
|
|
}
|
2003-02-02 09:24:13 -05:00
|
|
|
|
2004-05-10 12:17:56 -04:00
|
|
|
return NULL;
|
2003-02-02 09:24:13 -05:00
|
|
|
}
|
2003-02-06 08:10:48 -05:00
|
|
|
|
2004-05-10 12:17:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
static void yp_destroy_ypdata(ypdata_t *ypdata)
|
2003-02-02 09:24:13 -05:00
|
|
|
{
|
2003-02-02 11:48:15 -05:00
|
|
|
if (ypdata) {
|
2004-05-10 12:17:56 -04:00
|
|
|
if (ypdata->mount) {
|
2014-11-30 15:32:30 -05:00
|
|
|
free(ypdata->mount);
|
2004-05-10 12:17:56 -04:00
|
|
|
}
|
|
|
|
if (ypdata->url) {
|
2014-11-30 15:32:30 -05:00
|
|
|
free(ypdata->url);
|
2004-02-16 16:59:08 -05:00
|
|
|
}
|
2003-02-02 11:48:15 -05:00
|
|
|
if (ypdata->sid) {
|
|
|
|
free(ypdata->sid);
|
|
|
|
}
|
|
|
|
if (ypdata->server_name) {
|
|
|
|
free(ypdata->server_name);
|
|
|
|
}
|
|
|
|
if (ypdata->server_desc) {
|
|
|
|
free(ypdata->server_desc);
|
|
|
|
}
|
|
|
|
if (ypdata->server_genre) {
|
|
|
|
free(ypdata->server_genre);
|
|
|
|
}
|
|
|
|
if (ypdata->cluster_password) {
|
|
|
|
free(ypdata->cluster_password);
|
|
|
|
}
|
|
|
|
if (ypdata->listen_url) {
|
|
|
|
free(ypdata->listen_url);
|
|
|
|
}
|
|
|
|
if (ypdata->current_song) {
|
|
|
|
free(ypdata->current_song);
|
|
|
|
}
|
|
|
|
if (ypdata->bitrate) {
|
|
|
|
free(ypdata->bitrate);
|
|
|
|
}
|
|
|
|
if (ypdata->server_type) {
|
|
|
|
free(ypdata->server_type);
|
|
|
|
}
|
2003-02-26 18:52:23 -05:00
|
|
|
if (ypdata->audio_info) {
|
|
|
|
free(ypdata->audio_info);
|
|
|
|
}
|
2014-11-30 15:32:30 -05:00
|
|
|
free(ypdata->subtype);
|
|
|
|
free(ypdata->error_msg);
|
|
|
|
free(ypdata);
|
2003-02-02 11:48:15 -05:00
|
|
|
}
|
2003-02-02 09:24:13 -05:00
|
|
|
}
|
2003-02-06 08:10:48 -05:00
|
|
|
|
2014-11-30 15:32:30 -05:00
|
|
|
static void add_yp_info(ypdata_t *yp, void *info, int type)
|
2003-06-26 09:33:55 -04:00
|
|
|
{
|
|
|
|
char *escaped;
|
2004-05-10 12:17:56 -04:00
|
|
|
|
|
|
|
if (!info)
|
2003-06-26 09:33:55 -04:00
|
|
|
return;
|
2004-05-10 12:17:56 -04:00
|
|
|
|
2005-05-30 22:48:40 -04:00
|
|
|
escaped = util_url_escape(info);
|
|
|
|
if (escaped == NULL)
|
|
|
|
return;
|
|
|
|
|
2004-05-10 12:17:56 -04:00
|
|
|
switch (type)
|
|
|
|
{
|
2003-06-26 09:33:55 -04:00
|
|
|
case YP_SERVER_NAME:
|
2005-05-30 22:48:40 -04:00
|
|
|
free (yp->server_name);
|
|
|
|
yp->server_name = escaped;
|
2004-05-10 12:17:56 -04:00
|
|
|
break;
|
2003-06-26 09:33:55 -04:00
|
|
|
case YP_SERVER_DESC:
|
2005-05-30 22:48:40 -04:00
|
|
|
free (yp->server_desc);
|
|
|
|
yp->server_desc = escaped;
|
2004-05-10 12:17:56 -04:00
|
|
|
break;
|
2003-06-26 09:33:55 -04:00
|
|
|
case YP_SERVER_GENRE:
|
2005-05-30 22:48:40 -04:00
|
|
|
free (yp->server_genre);
|
|
|
|
yp->server_genre = escaped;
|
2004-05-10 12:17:56 -04:00
|
|
|
break;
|
2003-06-26 09:33:55 -04:00
|
|
|
case YP_SERVER_URL:
|
2005-05-30 22:48:40 -04:00
|
|
|
free (yp->url);
|
|
|
|
yp->url = escaped;
|
2004-05-10 12:17:56 -04:00
|
|
|
break;
|
2003-06-26 09:33:55 -04:00
|
|
|
case YP_BITRATE:
|
2005-05-30 22:48:40 -04:00
|
|
|
free (yp->bitrate);
|
|
|
|
yp->bitrate = escaped;
|
2004-05-10 12:17:56 -04:00
|
|
|
break;
|
2003-06-26 09:33:55 -04:00
|
|
|
case YP_AUDIO_INFO:
|
2005-05-30 22:48:40 -04:00
|
|
|
free (yp->audio_info);
|
|
|
|
yp->audio_info = escaped;
|
2004-05-10 12:17:56 -04:00
|
|
|
break;
|
2003-06-26 09:33:55 -04:00
|
|
|
case YP_SERVER_TYPE:
|
2005-05-30 22:48:40 -04:00
|
|
|
free (yp->server_type);
|
|
|
|
yp->server_type = escaped;
|
2004-05-10 12:17:56 -04:00
|
|
|
break;
|
2003-06-26 09:33:55 -04:00
|
|
|
case YP_CURRENT_SONG:
|
2005-05-30 22:48:40 -04:00
|
|
|
free (yp->current_song);
|
|
|
|
yp->current_song = escaped;
|
2004-05-10 12:17:56 -04:00
|
|
|
break;
|
2004-12-07 17:29:46 -05:00
|
|
|
case YP_CLUSTER_PASSWORD:
|
2005-05-30 22:48:40 -04:00
|
|
|
free (yp->cluster_password);
|
|
|
|
yp->cluster_password = escaped;
|
2004-12-07 17:29:46 -05:00
|
|
|
break;
|
2004-12-09 19:11:16 -05:00
|
|
|
case YP_SUBTYPE:
|
2005-05-30 22:48:40 -04:00
|
|
|
free (yp->subtype);
|
|
|
|
yp->subtype = escaped;
|
2004-12-09 19:11:16 -05:00
|
|
|
break;
|
2006-03-08 06:14:56 -05:00
|
|
|
default:
|
|
|
|
free (escaped);
|
2004-05-10 12:17:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Add YP entries to active servers */
|
2005-05-30 10:50:57 -04:00
|
|
|
void yp_add (const char *mount)
|
2004-05-10 12:17:56 -04:00
|
|
|
{
|
|
|
|
struct yp_server *server;
|
|
|
|
|
|
|
|
/* make sure YP thread is not modifying the lists */
|
|
|
|
thread_rwlock_rlock (&yp_lock);
|
|
|
|
|
|
|
|
/* make sure we don't race against another yp_add */
|
|
|
|
thread_mutex_lock (&yp_pending_lock);
|
2004-10-25 17:10:10 -04:00
|
|
|
server = (struct yp_server *)active_yps;
|
2004-05-10 12:17:56 -04:00
|
|
|
while (server)
|
|
|
|
{
|
|
|
|
ypdata_t *yp;
|
2005-08-29 20:30:30 -04:00
|
|
|
|
|
|
|
/* on-demand relays may already have a YP entry */
|
|
|
|
yp = find_yp_mount (server->mounts, mount);
|
|
|
|
if (yp == NULL)
|
2004-05-10 12:17:56 -04:00
|
|
|
{
|
2005-08-29 20:30:30 -04:00
|
|
|
/* add new ypdata to each servers pending yp */
|
|
|
|
yp = create_yp_entry (mount);
|
|
|
|
if (yp)
|
|
|
|
{
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_DEBUG("Adding %s to %s", mount, server->url);
|
2005-08-29 20:30:30 -04:00
|
|
|
yp->server = server;
|
|
|
|
yp->touch_interval = server->touch_interval;
|
|
|
|
yp->next = server->pending_mounts;
|
2008-04-18 22:04:17 -04:00
|
|
|
yp->next_update = time(NULL) + 60;
|
2005-08-29 20:30:30 -04:00
|
|
|
server->pending_mounts = yp;
|
|
|
|
yp_update = 1;
|
|
|
|
}
|
2004-05-10 12:17:56 -04:00
|
|
|
}
|
2005-08-29 20:30:30 -04:00
|
|
|
else
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_DEBUG("YP entry %s already exists", mount);
|
2004-05-10 12:17:56 -04:00
|
|
|
server = server->next;
|
|
|
|
}
|
|
|
|
thread_mutex_unlock (&yp_pending_lock);
|
|
|
|
thread_rwlock_unlock (&yp_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Mark an existing entry in the YP list as to be marked for deletion */
|
|
|
|
void yp_remove (const char *mount)
|
|
|
|
{
|
2004-10-25 17:10:10 -04:00
|
|
|
struct yp_server *server = (struct yp_server *)active_yps;
|
2004-05-10 12:17:56 -04:00
|
|
|
|
|
|
|
thread_rwlock_rlock (&yp_lock);
|
|
|
|
while (server)
|
|
|
|
{
|
2008-04-22 21:45:38 -04:00
|
|
|
ypdata_t *list = server->mounts;
|
|
|
|
|
|
|
|
while (1)
|
2004-05-10 12:17:56 -04:00
|
|
|
{
|
2008-04-22 21:45:38 -04:00
|
|
|
ypdata_t *yp = find_yp_mount (list, mount);
|
|
|
|
if (yp == NULL)
|
|
|
|
break;
|
|
|
|
if (yp->release || yp->remove)
|
|
|
|
{
|
|
|
|
list = yp->next;
|
|
|
|
continue; /* search again these are old entries */
|
|
|
|
}
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_DEBUG("release %s on YP %s", mount, server->url);
|
2004-09-15 10:21:04 -04:00
|
|
|
yp->release = 1;
|
2004-05-10 12:17:56 -04:00
|
|
|
yp->next_update = 0;
|
2003-06-26 09:33:55 -04:00
|
|
|
}
|
2004-05-10 12:17:56 -04:00
|
|
|
server = server->next;
|
2003-06-26 09:33:55 -04:00
|
|
|
}
|
2004-05-10 12:17:56 -04:00
|
|
|
thread_rwlock_unlock (&yp_lock);
|
2003-06-26 09:33:55 -04:00
|
|
|
}
|
2004-05-10 12:17:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
/* This is similar to yp_remove, but we force a touch
|
|
|
|
* attempt */
|
|
|
|
void yp_touch (const char *mount)
|
|
|
|
{
|
2004-10-25 17:10:10 -04:00
|
|
|
struct yp_server *server = (struct yp_server *)active_yps;
|
2004-09-15 10:21:04 -04:00
|
|
|
ypdata_t *search_list = NULL;
|
2004-05-10 12:17:56 -04:00
|
|
|
|
|
|
|
thread_rwlock_rlock (&yp_lock);
|
2004-09-15 10:21:04 -04:00
|
|
|
if (server)
|
|
|
|
search_list = server->mounts;
|
|
|
|
|
2004-05-10 12:17:56 -04:00
|
|
|
while (server)
|
|
|
|
{
|
2004-09-15 10:21:04 -04:00
|
|
|
ypdata_t *yp = find_yp_mount (search_list, mount);
|
2004-05-10 12:17:56 -04:00
|
|
|
if (yp)
|
|
|
|
{
|
2004-09-15 10:21:04 -04:00
|
|
|
/* we may of found old entries not purged yet, so skip them */
|
2004-12-07 21:28:07 -05:00
|
|
|
if (yp->release != 0 || yp->remove != 0)
|
2004-09-15 10:21:04 -04:00
|
|
|
{
|
|
|
|
search_list = yp->next;
|
|
|
|
continue;
|
|
|
|
}
|
2008-09-07 20:22:37 -04:00
|
|
|
/* don't update the directory if there is a touch scheduled soon */
|
|
|
|
if (yp->process == do_yp_touch && now + yp->touch_interval - yp->next_update > 60)
|
|
|
|
yp->next_update = now + 3;
|
2004-05-10 12:17:56 -04:00
|
|
|
}
|
|
|
|
server = server->next;
|
2004-09-15 10:21:04 -04:00
|
|
|
if (server)
|
|
|
|
search_list = server->mounts;
|
2004-05-10 12:17:56 -04:00
|
|
|
}
|
|
|
|
thread_rwlock_unlock (&yp_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-17 07:23:09 -05:00
|
|
|
void yp_shutdown (void)
|
2004-05-10 12:17:56 -04:00
|
|
|
{
|
|
|
|
yp_running = 0;
|
|
|
|
yp_update = 1;
|
2004-06-02 15:34:14 -04:00
|
|
|
if (yp_thread)
|
|
|
|
thread_join (yp_thread);
|
2007-09-02 20:55:27 -04:00
|
|
|
free ((char*)server_version);
|
|
|
|
server_version = NULL;
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_INFO("YP thread down");
|
2004-05-10 12:17:56 -04:00
|
|
|
}
|
|
|
|
|