slave.c 7.36 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
static int _initialized = 0;
65 66 67 68 69 70 71 72
static unsigned max_interval = 0;


void slave_recheck (void)
{
    max_interval = 0;
}

73 74

void slave_initialize(void) {
Michael Smith's avatar
Michael Smith committed
75
    ice_config_t *config;
76
    if (_initialized) return;
Michael Smith's avatar
Michael Smith committed
77 78

    config = config_get_config();
79
    /* Don't create a slave thread if it isn't configured */
Michael Smith's avatar
Michael Smith committed
80 81 82 83
    if (config->master_server == NULL && 
            config->relay == NULL)
    {
        config_release_config();
84
        return;
Michael Smith's avatar
Michael Smith committed
85 86
    }
    config_release_config();
87

88 89
    _initialized = 1;
    _slave_thread_id = thread_create("Slave Thread", _slave_thread, NULL, THREAD_ATTACHED);
90 91 92
}

void slave_shutdown(void) {
93 94 95
    if (!_initialized) return;
    _initialized = 0;
    thread_join(_slave_thread_id);
96 97
}

98
static void create_relay_stream(char *server, int port, 
99
        char *remotemount, char *localmount, int mp3)
Michael Smith's avatar
Michael Smith committed
100 101
{
    sock_t streamsock;
102 103 104
    char header[4096];
    connection_t *con;
    http_parser_t *parser;
105
    client_t *client;
Michael Smith's avatar
Michael Smith committed
106

107 108 109 110
    if(!localmount)
        localmount = remotemount;

    DEBUG1("Adding source at mountpoint \"%s\"", localmount);
Michael Smith's avatar
Michael Smith committed
111

112 113
    streamsock = sock_connect_wto(server, port, 0);
    if (streamsock == SOCK_ERROR) {
114
        WARN2("Failed to relay stream from master server, couldn't connect to http://%s:%d", server, port);
Michael Smith's avatar
Michael Smith committed
115
        return;
116
    }
117
    con = create_connection(streamsock, -1, NULL);
118 119 120 121 122 123 124 125 126 127
    /* At this point we may not know if we are relaying a mp3 or vorbis stream,
     * so lets send in the icy-metadata header just in 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"
                           "Icy-MetaData: 1\r\n"
                           "\r\n", 
                           remotemount);
128 129
    memset(header, 0, sizeof(header));
    if (util_read_header(con->sock, header, 4096) == 0) {
130
        WARN0("Header read failed");
131 132 133 134 135 136
        connection_close(con);
        return;
    }
    parser = httpp_create_parser();
    httpp_initialize(parser, NULL);
    if(!httpp_parse_response(parser, header, strlen(header), localmount)) {
Michael Smith's avatar
Michael Smith committed
137 138 139 140 141 142
        if(httpp_getvar(parser, HTTPP_VAR_ERROR_MESSAGE)) {
            ERROR1("Error parsing relay request: %s", 
                    httpp_getvar(parser, HTTPP_VAR_ERROR_MESSAGE));
        }
        else
            ERROR0("Error parsing relay request");
143
        connection_close(con);
Michael Smith's avatar
Michael Smith committed
144 145 146 147 148
        httpp_destroy(parser);
        return;
    }

    client = client_create(con, parser);
149
    if (!connection_create_source(client, con, parser, 
Michael Smith's avatar
Michael Smith committed
150
                httpp_getvar(parser, HTTPP_VAR_URI))) {
151
        DEBUG0("Failed to create source");
Michael Smith's avatar
Michael Smith committed
152
        client_destroy(client);
153
    }
154

Michael Smith's avatar
Michael Smith committed
155 156 157 158
    return;
}

static void *_slave_thread(void *arg) {
159 160
    sock_t mastersock;
    char buf[256];
161
    unsigned interval = 0;
162 163 164
    char *authheader, *data;
    int len;
    char *username = "relay";
Michael Smith's avatar
Michael Smith committed
165
    relay_server *relay;
Michael Smith's avatar
Michael Smith committed
166
    ice_config_t *config;
167

168
    while (_initialized) {
Michael Smith's avatar
Michael Smith committed
169
        if (max_interval > ++interval) {
170
            thread_sleep(1000000);
171 172
            continue;
        }
Michael Smith's avatar
Michael Smith committed
173 174 175 176 177
        else {
            /* In case it's been reconfigured */
            config = config_get_config();
            max_interval = config->master_update_interval;

178
            interval = 0;
Michael Smith's avatar
Michael Smith committed
179 180 181
        }

        if(config->master_server != NULL) {
182
            char *server = strdup (config->master_server);
Michael Smith's avatar
Michael Smith committed
183
            int port = config->master_server_port;
184 185 186 187 188
            char *password = NULL;
            if (config->master_password != NULL)
                password = strdup (config->master_password);
            else
                password = strdup (config->source_password);
Michael Smith's avatar
Michael Smith committed
189 190
            config_release_config();

191
            mastersock = sock_connect_wto(server, port, 0);
192

193
            if (mastersock == SOCK_ERROR) {
Michael Smith's avatar
Michael Smith committed
194
                WARN0("Relay slave failed to contact master server to fetch stream list");
195 196
                free (server);
                free (password);
197 198
                continue;
            }
Michael Smith's avatar
Michael Smith committed
199 200 201 202 203 204 205

            len = strlen(username) + strlen(password) + 1;
            authheader = malloc(len+1);
            strcpy(authheader, username);
            strcat(authheader, ":");
            strcat(authheader, password);
            data = util_base64_encode(authheader);
206
            sock_write(mastersock, 
207
                    "GET /admin/streamlist.txt HTTP/1.0\r\n"
Michael Smith's avatar
Michael Smith committed
208 209 210 211
                    "Authorization: Basic %s\r\n"
                    "\r\n", data);
            free(authheader);
            free(data);
212
            while (sock_read_line(mastersock, buf, sizeof(buf))) {
Michael Smith's avatar
Michael Smith committed
213 214 215
                if(!strlen(buf))
                    break;
            }
216

217 218 219 220
            while (sock_read_line(mastersock, buf, sizeof(buf))) {
                avl_tree_rlock(global.source_tree);
                if (!source_find_mount(buf)) {
                    avl_tree_unlock(global.source_tree);
Michael Smith's avatar
Michael Smith committed
221

Michael Smith's avatar
Michael Smith committed
222
                    create_relay_stream(server, port, buf, NULL, 0);
223
                } 
Michael Smith's avatar
Michael Smith committed
224
                else
225 226
                    avl_tree_unlock(global.source_tree);
            }
227 228
            free (server);
            free (password);
229
            sock_close(mastersock);
Michael Smith's avatar
Michael Smith committed
230
        }
Michael Smith's avatar
Michael Smith committed
231 232 233
        else {
            config_release_config();
        }
Michael Smith's avatar
Michael Smith committed
234 235

        /* And now, we process the individual mounts... */
Michael Smith's avatar
Michael Smith committed
236 237 238 239 240
        config = config_get_config();
        relay = config->relay;
        thread_mutex_lock(&(config_locks()->relay_lock));
        config_release_config();

Michael Smith's avatar
Michael Smith committed
241 242
        while(relay) {
            avl_tree_rlock(global.source_tree);
Michael Smith's avatar
Michael Smith committed
243
            if(!source_find_mount_raw(relay->localmount)) {
Michael Smith's avatar
Michael Smith committed
244 245
                avl_tree_unlock(global.source_tree);

246
                create_relay_stream(relay->server, relay->port, relay->mount,
247
                        relay->localmount, relay->mp3metadata);
Michael Smith's avatar
Michael Smith committed
248 249 250
            }
            else
                avl_tree_unlock(global.source_tree);
Michael Smith's avatar
Michael Smith committed
251
            relay = relay->next;
Michael Smith's avatar
Michael Smith committed
252
        }
Michael Smith's avatar
Michael Smith committed
253 254

        thread_mutex_unlock(&(config_locks()->relay_lock));
255
    }
256
    INFO0 ("Slave thread shutting down");
257
    return NULL;
258
}
Michael Smith's avatar
Michael Smith committed
259