slave.c 6.86 KB
Newer Older
1
/* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- */
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
/* 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"

Karl Heyes's avatar
Karl Heyes committed
28
29
30
31
#include "thread/thread.h"
#include "avl/avl.h"
#include "net/sock.h"
#include "httpp/httpp.h"
32
33
34
35
36
37
38
39
40
41

#include "config.h"
#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
42
#include "format.h"
43
44
45
46

#define CATMODULE "slave"

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

void slave_initialize(void) {
Michael Smith's avatar
Michael Smith committed
51
    ice_config_t *config;
52
    if (_initialized) return;
Michael Smith's avatar
Michael Smith committed
53
54

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

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

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

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

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

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

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

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

Michael Smith's avatar
Michael Smith committed
137
138
139
140
    return;
}

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

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

    if(password == NULL)
Michael Smith's avatar
Michael Smith committed
158
159
160
        password = config->source_password;

    config_release_config();
161

162

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

174
            interval = 0;
Michael Smith's avatar
Michael Smith committed
175
176
177
178
179
180
181
182
        }

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

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

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

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

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

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

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

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

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

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