slave.c 6.96 KB
Newer Older
1
/* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2 3 4 5 6 7 8
/* 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.
 * */

9 10 11 12
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#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
32 33 34 35
#include "thread/thread.h"
#include "avl/avl.h"
#include "net/sock.h"
#include "httpp/httpp.h"
36

37
#include "cfgfile.h"
38 39 40 41 42 43 44 45
#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
46
#include "format.h"
47 48 49 50

#define CATMODULE "slave"

static void *_slave_thread(void *arg);
51
thread_type *_slave_thread_id;
52 53 54
static int _initialized = 0;

void slave_initialize(void) {
Michael Smith's avatar
Michael Smith committed
55
    ice_config_t *config;
56
    if (_initialized) return;
Michael Smith's avatar
Michael Smith committed
57 58

    config = config_get_config();
59
    /* Don't create a slave thread if it isn't configured */
Michael Smith's avatar
Michael Smith committed
60 61 62 63
    if (config->master_server == NULL && 
            config->relay == NULL)
    {
        config_release_config();
64
        return;
Michael Smith's avatar
Michael Smith committed
65 66
    }
    config_release_config();
67

68 69
    _initialized = 1;
    _slave_thread_id = thread_create("Slave Thread", _slave_thread, NULL, THREAD_ATTACHED);
70 71 72
}

void slave_shutdown(void) {
73 74 75
    if (!_initialized) return;
    _initialized = 0;
    thread_join(_slave_thread_id);
76 77
}

78
static void create_relay_stream(char *server, int port, 
79
        char *remotemount, char *localmount, int mp3)
Michael Smith's avatar
Michael Smith committed
80 81
{
    sock_t streamsock;
82 83 84
    char header[4096];
    connection_t *con;
    http_parser_t *parser;
85
    client_t *client;
Michael Smith's avatar
Michael Smith committed
86

87 88 89 90
    if(!localmount)
        localmount = remotemount;

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

92 93
    streamsock = sock_connect_wto(server, port, 0);
    if (streamsock == SOCK_ERROR) {
94
        WARN2("Failed to relay stream from master server, couldn't connect to http://%s:%d", server, port);
Michael Smith's avatar
Michael Smith committed
95
        return;
96
    }
97
    con = create_connection(streamsock, -1, NULL);
98 99 100 101 102 103 104 105 106 107
    /* 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);
108 109
    memset(header, 0, sizeof(header));
    if (util_read_header(con->sock, header, 4096) == 0) {
110
        WARN0("Header read failed");
111 112 113 114 115 116
        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
117 118 119 120 121 122
        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");
123
        connection_close(con);
Michael Smith's avatar
Michael Smith committed
124 125 126 127 128
        httpp_destroy(parser);
        return;
    }

    client = client_create(con, parser);
129
    if (!connection_create_source(client, con, parser, 
Michael Smith's avatar
Michael Smith committed
130
                httpp_getvar(parser, HTTPP_VAR_URI))) {
131
        DEBUG0("Failed to create source");
Michael Smith's avatar
Michael Smith committed
132
        client_destroy(client);
133
    }
134

Michael Smith's avatar
Michael Smith committed
135 136 137 138
    return;
}

static void *_slave_thread(void *arg) {
139 140
    sock_t mastersock;
    char buf[256];
Michael Smith's avatar
Michael Smith committed
141
    int interval;
142 143 144
    char *authheader, *data;
    int len;
    char *username = "relay";
Michael Smith's avatar
Michael Smith committed
145
    int max_interval;
Michael Smith's avatar
Michael Smith committed
146
    relay_server *relay;
Michael Smith's avatar
Michael Smith committed
147 148 149 150 151
    ice_config_t *config;
    
    config = config_get_config();

    interval = max_interval = config->master_update_interval;
152

Michael Smith's avatar
Michael Smith committed
153
    config_release_config();
154

155
    while (_initialized) {
Michael Smith's avatar
Michael Smith committed
156
        if (max_interval > ++interval) {
157
            thread_sleep(1000000);
158 159
            continue;
        }
Michael Smith's avatar
Michael Smith committed
160 161 162 163 164
        else {
            /* In case it's been reconfigured */
            config = config_get_config();
            max_interval = config->master_update_interval;

165
            interval = 0;
Michael Smith's avatar
Michael Smith committed
166 167 168
        }

        if(config->master_server != NULL) {
169
            char *server = strdup (config->master_server);
Michael Smith's avatar
Michael Smith committed
170
            int port = config->master_server_port;
171 172 173 174 175
            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
176 177
            config_release_config();

178
            mastersock = sock_connect_wto(server, port, 0);
179

180
            if (mastersock == SOCK_ERROR) {
Michael Smith's avatar
Michael Smith committed
181
                WARN0("Relay slave failed to contact master server to fetch stream list");
182 183
                free (server);
                free (password);
184 185
                continue;
            }
Michael Smith's avatar
Michael Smith committed
186 187 188 189 190 191 192

            len = strlen(username) + strlen(password) + 1;
            authheader = malloc(len+1);
            strcpy(authheader, username);
            strcat(authheader, ":");
            strcat(authheader, password);
            data = util_base64_encode(authheader);
193
            sock_write(mastersock, 
194
                    "GET /admin/streamlist.txt HTTP/1.0\r\n"
Michael Smith's avatar
Michael Smith committed
195 196 197 198
                    "Authorization: Basic %s\r\n"
                    "\r\n", data);
            free(authheader);
            free(data);
199
            while (sock_read_line(mastersock, buf, sizeof(buf))) {
Michael Smith's avatar
Michael Smith committed
200 201 202
                if(!strlen(buf))
                    break;
            }
203

204 205 206 207
            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
208

Michael Smith's avatar
Michael Smith committed
209
                    create_relay_stream(server, port, buf, NULL, 0);
210
                } 
Michael Smith's avatar
Michael Smith committed
211
                else
212 213
                    avl_tree_unlock(global.source_tree);
            }
214 215
            free (server);
            free (password);
216
            sock_close(mastersock);
Michael Smith's avatar
Michael Smith committed
217
        }
Michael Smith's avatar
Michael Smith committed
218 219 220
        else {
            config_release_config();
        }
Michael Smith's avatar
Michael Smith committed
221 222

        /* And now, we process the individual mounts... */
Michael Smith's avatar
Michael Smith committed
223 224 225 226 227
        config = config_get_config();
        relay = config->relay;
        thread_mutex_lock(&(config_locks()->relay_lock));
        config_release_config();

Michael Smith's avatar
Michael Smith committed
228 229
        while(relay) {
            avl_tree_rlock(global.source_tree);
230
            if(!source_find_mount(relay->localmount)) {
Michael Smith's avatar
Michael Smith committed
231 232
                avl_tree_unlock(global.source_tree);

233
                create_relay_stream(relay->server, relay->port, relay->mount,
234
                        relay->localmount, relay->mp3metadata);
Michael Smith's avatar
Michael Smith committed
235 236 237
            }
            else
                avl_tree_unlock(global.source_tree);
Michael Smith's avatar
Michael Smith committed
238
            relay = relay->next;
Michael Smith's avatar
Michael Smith committed
239
        }
Michael Smith's avatar
Michael Smith committed
240 241

        thread_mutex_unlock(&(config_locks()->relay_lock));
242 243 244
    }
    thread_exit(0);
    return NULL;
245
}
Michael Smith's avatar
Michael Smith committed
246