slave.c 11.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/* Icecast
 *
 * This program is distributed under the GNU General Public License, version 2.
 * A copy of this license is included with this source.
 *
 * Copyright 2000-2004, Jack Moffitt <jack@xiph.org, 
 *                      Michael Smith <msmith@xiph.org>,
 *                      oddsock <oddsock@xiph.org>,
 *                      Karl Heyes <karl@xiph.org>
 *                      and others (see AUTHORS for details).
 */

13
/* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- */
14 15 16 17 18 19 20
/* slave.c
 * by Ciaran Anscomb <ciaran.anscomb@6809.org.uk>
 *
 * Periodically requests a list of streams from a master server
 * and creates source threads for any it doesn't already have.
 * */

21 22 23 24
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>

#ifndef _WIN32
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#else
#include <winsock2.h>
#define snprintf _snprintf
#define strcasecmp stricmp
#define strncasecmp strnicmp
#endif

#include "os.h"

Karl Heyes's avatar
Karl Heyes committed
44 45 46 47
#include "thread/thread.h"
#include "avl/avl.h"
#include "net/sock.h"
#include "httpp/httpp.h"
48

49
#include "cfgfile.h"
50 51 52 53 54 55 56 57
#include "global.h"
#include "util.h"
#include "connection.h"
#include "refbuf.h"
#include "client.h"
#include "stats.h"
#include "logging.h"
#include "source.h"
Michael Smith's avatar
Michael Smith committed
58
#include "format.h"
59 60 61 62

#define CATMODULE "slave"

static void *_slave_thread(void *arg);
63
thread_type *_slave_thread_id;
64 65
static int slave_running = 0;
static int max_interval = 0;
66

67
relay_server *relay_free (relay_server *relay)
68
{
69 70 71 72 73 74 75 76 77
    relay_server *next = relay->next;
    DEBUG1("freeing relay %s", relay->localmount);
    if (relay->source)
       source_free_source (relay->source);
    xmlFree (relay->server);
    xmlFree (relay->mount);
    xmlFree (relay->localmount);
    xmlFree (relay);
    return next;
78 79
}

80

81 82 83
relay_server *relay_copy (relay_server *r)
{
    relay_server *copy = calloc (1, sizeof (relay_server));
Michael Smith's avatar
Michael Smith committed
84

85
    if (copy)
Michael Smith's avatar
Michael Smith committed
86
    {
87 88 89 90 91
        copy->server = xmlStrdup (r->server);
        copy->mount = xmlStrdup (r->mount);
        copy->localmount = xmlStrdup (r->localmount);
        copy->port = r->port;
        copy->mp3metadata = r->mp3metadata;
Michael Smith's avatar
Michael Smith committed
92
    }
93 94
    return copy;
}
95

96 97 98 99 100 101 102 103 104 105 106 107 108 109

static void *_relay_thread (void *arg)
{
    relay_server *relay = arg;

    relay->running = 1;

    source_main (relay->source);

    relay->running = 0;
    if (relay->cleanup)
        relay_free (relay);

    return NULL;
110 111
}

112 113 114 115

void slave_recheck (void)
{
    max_interval = 0;
116 117
}

118 119

void slave_initialize(void)
Michael Smith's avatar
Michael Smith committed
120
{
121 122
    if (slave_running)
        return;
Michael Smith's avatar
Michael Smith committed
123

124 125 126
    slave_running = 1;
    _slave_thread_id = thread_create("Slave Thread", _slave_thread, NULL, THREAD_ATTACHED);
}
127

Michael Smith's avatar
Michael Smith committed
128

129 130 131 132 133
void slave_shutdown(void)
{
    relay_server *relay;

    if (!slave_running)
134
        return;
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    slave_running = 0;
    thread_join (_slave_thread_id);

    relay = global.relays;
    while (relay)
        relay = relay_free (relay);
    global.relays = NULL;

    relay = global.master_relays;
    while (relay)
        relay = relay_free (relay);
    global.master_relays = NULL;
}


/* This does the actual connection for a relay. A thread is
 * started off if a connection can be acquired
 */
static void start_relay_stream (relay_server *relay)
{
    sock_t streamsock = SOCK_ERROR;
    source_t *src = relay->source;
    http_parser_t *parser = NULL;
    connection_t *con=NULL;
    char header[4096];

    INFO1("Starting relayed source at mountpoint \"%s\"", relay->localmount);
    do
    {
        streamsock = sock_connect_wto (relay->server, relay->port, 30);
        if (streamsock == SOCK_ERROR)
        {
            WARN3("Failed to relay stream from master server, couldn't connect to http://%s:%d%s",
                    relay->server, relay->port, relay->mount);
            break;
Michael Smith's avatar
Michael Smith committed
170
        }
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
        con = create_connection (streamsock, -1, NULL);

        /* At this point we may not know if we are relaying an mp3 or vorbis
         * stream, but only send the icy-metadata header if the relay details
         * state so (the typical case).  It's harmless in the vorbis case. If
         * we don't send in this header then relay will not have mp3 metadata.
         */
        sock_write(streamsock, "GET %s HTTP/1.0\r\n"
                "User-Agent: " ICECAST_VERSION_STRING "\r\n"
                "%s"
                "\r\n",
                relay->mount, relay->mp3metadata?"Icy-MetaData: 1\r\n":"");
        memset (header, 0, sizeof(header));
        if (util_read_header (con->sock, header, 4096) == 0)
        {
            WARN0("Header read failed");
            break;
        }
        parser = httpp_create_parser();
        httpp_initialize (parser, NULL);
        if (! httpp_parse_response (parser, header, strlen(header), relay->localmount))
        {
Michael Smith's avatar
Michael Smith committed
193
            ERROR0("Error parsing relay request");
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
            break;
        }
        if (httpp_getvar (parser, HTTPP_VAR_ERROR_MESSAGE))
        {
            ERROR1("Error from relay request: %s", httpp_getvar(parser, HTTPP_VAR_ERROR_MESSAGE));
            break;
        }
        src->parser = parser;
        src->con = con;
        if (connection_complete_source (src) < 0)
        {
            DEBUG0("Failed to complete source initialisation");
            break;
        }
        thread_create ("Relay Thread", _relay_thread, relay, THREAD_DETACHED);

Michael Smith's avatar
Michael Smith committed
210
        return;
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    } while (0);

    if (con == NULL && streamsock != SOCK_ERROR)
        sock_close (streamsock);
    if (con)
        connection_close (con);
    src->con = NULL;
    if (parser)
        httpp_destroy (parser);
    src->parser = NULL;
}


/* wrapper for starting the provided relay stream */
static void check_relay_stream (relay_server *relay)
{
    if (relay->source == NULL)
    {
        /* new relay, reserve the name */
        DEBUG1("Adding relay source at mountpoint \"%s\"", relay->localmount);
        relay->source = source_reserve (relay->localmount);
    }
    if (relay->source && !relay->running)
    {
        start_relay_stream (relay);
Michael Smith's avatar
Michael Smith committed
236
    }
237 238
}

Michael Smith's avatar
Michael Smith committed
239

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
/* go through updated looking for relays that are different configured. The
 * returned list contains relays that should be kept running, current contains
 * the list of relays to shutdown
 */
static relay_server *
update_relay_set (relay_server **current, relay_server *updated)
{
    relay_server *relay = updated;
    relay_server *existing_relay, **existing_p;
    relay_server *new_list = NULL;

    while (relay)
    {
         existing_relay = *current;
         existing_p = current;

         while (existing_relay)
         {
             if (strcmp (relay->localmount, existing_relay->localmount) == 0)
                 break;
             existing_p = &existing_relay->next;
             existing_relay = existing_relay->next;
         }
         if (existing_relay == NULL)
         {
             /* new one, copy and insert */
             existing_relay = relay_copy (relay);
         }
         else
         {
             *existing_p = existing_relay->next;
         }
         existing_relay->next = new_list;
         new_list = existing_relay;
         relay = relay->next;
275
    }
276 277 278 279 280 281 282 283 284 285 286 287 288
    return new_list;
}


/* update the relay_list with entries from new_relay_list. Any new relays
 * are added to the list, and any not listed in the provided new_relay_list
 * get marked for shutting down, just in case they are not shutting down by
 * themselves
 */
static void
update_relays (relay_server **relay_list, relay_server *new_relay_list)
{
    relay_server *relay, *current;
289

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
    current = update_relay_set (relay_list, new_relay_list);

    /* ok whats left, lets make sure they shut down */
    relay = *relay_list;
    while (relay)
    {
        relay->cleanup = 1;
        if (relay->source)
        {
            if (relay->source->running)
                DEBUG1 ("requested %s to shut down", relay->source->mount);
            relay->source->running = 0;
            relay = relay->next;
        }
        else
            relay = relay_free (relay);
    }
    /* re-assign new set */
    *relay_list = current;
Michael Smith's avatar
Michael Smith committed
309 310
}

311 312 313 314 315

static int update_from_master(ice_config_t *config)
{
    char *master = NULL, *password = NULL, *username= NULL;
    int port;
316
    sock_t mastersock;
317
    int ret = 0;
318
    char buf[256];
319 320 321 322 323
    do
    {
        char *authheader, *data;
        relay_server *relays = NULL, *relay;
        int len, count = 1;
324

325 326 327
        username = strdup ("relay");
        if (config->master_password)
            password = strdup (config->master_password);
Michael Smith's avatar
Michael Smith committed
328

329 330
        if (config->master_server)
            master = strdup (config->master_server);
Michael Smith's avatar
Michael Smith committed
331

332
        port = config->master_server_port;
Michael Smith's avatar
Michael Smith committed
333

334 335 336 337 338
        if (password == NULL || master == NULL || port == 0)
            break;
        ret = 1;
        config_release_config();
        mastersock = sock_connect_wto (master, port, 0);
339

340 341 342 343 344
        if (mastersock == SOCK_ERROR)
        {
            WARN0("Relay slave failed to contact master server to fetch stream list");
            break;
        }
Michael Smith's avatar
Michael Smith committed
345

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
        len = strlen(username) + strlen(password) + 1;
        authheader = malloc(len+1);
        strcpy(authheader, username);
        strcat(authheader, ":");
        strcat(authheader, password);
        data = util_base64_encode(authheader);
        sock_write (mastersock,
                "GET /admin/streamlist.txt HTTP/1.0\r\n"
                "Authorization: Basic %s\r\n"
                "\r\n", data);
        free(authheader);
        free(data);

        while (sock_read_line(mastersock, buf, sizeof(buf)))
        {
            if (!strlen(buf))
                break;
        }
        while (sock_read_line(mastersock, buf, sizeof(buf)))
        {
            relay_server *r;
            if (!strlen(buf))
                continue;
            DEBUG2 ("read %d from master \"%s\"", count++, buf);
            r = calloc (1, sizeof (relay_server));
            if (r)
            {
                r->server = xmlStrdup (master);
                r->port = port;
                r->mount = xmlStrdup (buf);
                r->localmount = xmlStrdup (buf);
                r->mp3metadata = 1;
                r->next = relays;
                relays = r;
380
            }
Michael Smith's avatar
Michael Smith committed
381
        }
382 383 384 385 386 387 388 389 390
        sock_close (mastersock);

        update_relays (&global.master_relays, relays);
        /* start any inactive relays */
        relay = global.master_relays;
        while (relay)
        {
            check_relay_stream (relay);
            relay = relay->next;
Michael Smith's avatar
Michael Smith committed
391
        }
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
        relay = relays;
        while (relay)
            relay = relay_free (relay);
    } while(0);

    if (master)
        free (master);
    if (username)
        free (username);
    if (password)
        free (password);

    return ret;
}


static void *_slave_thread(void *arg)
{
    ice_config_t *config;
    relay_server *relay;
    unsigned interval = 0;
Michael Smith's avatar
Michael Smith committed
413

414 415 416 417 418 419 420
    while (slave_running)
    {
        thread_sleep (1000000);
        if (max_interval > ++interval)
            continue;

        interval = 0;
Michael Smith's avatar
Michael Smith committed
421 422
        config = config_get_config();

423
        max_interval = config->master_update_interval;
Michael Smith's avatar
Michael Smith committed
424

425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
        /* the connection could time some time, so the lock can drop */
        if (update_from_master (config))
            config = config_get_config();

        thread_mutex_lock (&(config_locks()->relay_lock));

        update_relays (&global.relays, config->relay);

        config_release_config();

        /* start any inactive relays */
        relay = global.relays;
        while (relay)
        {
            check_relay_stream (relay);
Michael Smith's avatar
Michael Smith committed
440
            relay = relay->next;
Michael Smith's avatar
Michael Smith committed
441
        }
442
        thread_mutex_unlock (&(config_locks()->relay_lock));
443
    }
444 445
    INFO0 ("Slave thread shutdown complete");

446
    return NULL;
447
}
Michael Smith's avatar
Michael Smith committed
448