slave.c 6.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/* 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.
 * */

#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"

#include "thread.h"
#include "avl.h"
#include "sock.h"
#include "log.h"
#include "httpp.h"

#include "config.h"
#include "global.h"
#include "util.h"
#include "connection.h"
#include "refbuf.h"
#include "client.h"
#include "stats.h"
#include "logging.h"
41
#include "geturl.h"
42
#include "source.h"
43
#include "format.h"
44 45 46 47

#define CATMODULE "slave"

static void *_slave_thread(void *arg);
48
thread_type *_slave_thread_id;
49 50 51
static int _initialized = 0;

void slave_initialize(void) {
52
    ice_config_t *config;
53
    if (_initialized) return;
54 55

    config = config_get_config();
56
    /* Don't create a slave thread if it isn't configured */
57 58 59 60
    if (config->master_server == NULL && 
            config->relay == NULL)
    {
        config_release_config();
61
        return;
62 63
    }
    config_release_config();
64

65 66
    _initialized = 1;
    _slave_thread_id = thread_create("Slave Thread", _slave_thread, NULL, THREAD_ATTACHED);
67 68 69
}

void slave_shutdown(void) {
70 71 72
    if (!_initialized) return;
    _initialized = 0;
    thread_join(_slave_thread_id);
73 74
}

75
static void create_relay_stream(char *server, int port, 
76
        char *remotemount, char *localmount, int mp3)
77 78
{
    sock_t streamsock;
79 80 81
    char header[4096];
    connection_t *con;
    http_parser_t *parser;
82
    client_t *client;
83

84 85 86 87
    if(!localmount)
        localmount = remotemount;

    DEBUG1("Adding source at mountpoint \"%s\"", localmount);
88

89 90
    streamsock = sock_connect_wto(server, port, 0);
    if (streamsock == SOCK_ERROR) {
91
        WARN2("Failed to relay stream from master server, couldn't connect to http://%s:%d", server, port);
92
        return;
93 94
    }
    con = create_connection(streamsock, NULL);
95
    if(mp3) {
96 97 98
        /* Some mp3 servers are bitchy, send a user-agent string to make them
         * send the right response.
         */
99
        sock_write(streamsock, "GET %s HTTP/1.0\r\n"
100 101 102
                               "User-Agent: " ICECAST_VERSION_STRING "\r\n"
                               "Icy-MetaData: 1\r\n"
                               "\r\n", 
103 104 105
                remotemount);
    }
    else {
106
        sock_write(streamsock, "GET %s HTTP/1.0\r\n"
107 108 109
                               "User-Agent: " ICECAST_VERSION_STRING "\r\n"
                               "\r\n",
                remotemount);
110
    }
111 112
    memset(header, 0, sizeof(header));
    if (util_read_header(con->sock, header, 4096) == 0) {
113
        WARN0("Header read failed");
114 115 116 117 118 119
        connection_close(con);
        return;
    }
    parser = httpp_create_parser();
    httpp_initialize(parser, NULL);
    if(!httpp_parse_response(parser, header, strlen(header), localmount)) {
120 121 122 123 124 125
        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");
126
        connection_close(con);
127 128 129 130 131
        httpp_destroy(parser);
        return;
    }

    client = client_create(con, parser);
132
    if (!connection_create_source(client, con, parser, 
133
                httpp_getvar(parser, HTTPP_VAR_URI))) {
134
        DEBUG0("Failed to create source");
135
        client_destroy(client);
136
    }
137

138 139 140 141
    return;
}

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

    password = config->master_password;
    interval = max_interval = config->master_update_interval;
157 158

    if(password == NULL)
159 160 161
        password = config->source_password;

    config_release_config();
162

163

164
    while (_initialized) {
165
        if (max_interval > ++interval) {
166
            thread_sleep(1000000);
167 168
            continue;
        }
169 170 171 172 173 174
        else {
            /* In case it's been reconfigured */
            config = config_get_config();
            max_interval = config->master_update_interval;
            config_release_config();

175
            interval = 0;
176 177 178 179 180 181 182 183
        }

        config = config_get_config();
        if(config->master_server != NULL) {
            char *server = config->master_server;
            int port = config->master_server_port;
            config_release_config();

184
            mastersock = sock_connect_wto(server, port, 0);
185

186
            if (mastersock == SOCK_ERROR) {
Michael Smith's avatar
Michael Smith committed
187
                WARN0("Relay slave failed to contact master server to fetch stream list");
188 189
                continue;
            }
Michael Smith's avatar
Michael Smith committed
190 191 192 193 194 195 196

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

208 209 210 211
            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
212

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

        /* And now, we process the individual mounts... */
225 226 227 228 229
        config = config_get_config();
        relay = config->relay;
        thread_mutex_lock(&(config_locks()->relay_lock));
        config_release_config();

230 231
        while(relay) {
            avl_tree_rlock(global.source_tree);
232
            if(!source_find_mount(relay->localmount)) {
233 234
                avl_tree_unlock(global.source_tree);

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

        thread_mutex_unlock(&(config_locks()->relay_lock));
244 245 246
    }
    thread_exit(0);
    return NULL;
247
}
248