2002-08-05 10:48:04 -04:00
|
|
|
/* 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"
|
2003-02-02 09:33:47 -05:00
|
|
|
#include "geturl.h"
|
2002-08-05 10:48:04 -04:00
|
|
|
#include "source.h"
|
2002-12-30 02:55:56 -05:00
|
|
|
#include "format.h"
|
2002-08-05 10:48:04 -04:00
|
|
|
|
|
|
|
#define CATMODULE "slave"
|
|
|
|
|
|
|
|
static void *_slave_thread(void *arg);
|
2002-12-29 10:46:32 -05:00
|
|
|
thread_type *_slave_thread_id;
|
2002-08-05 10:48:04 -04:00
|
|
|
static int _initialized = 0;
|
|
|
|
|
|
|
|
void slave_initialize(void) {
|
|
|
|
if (_initialized) return;
|
|
|
|
/* Don't create a slave thread if it isn't configured */
|
|
|
|
if (config_get_config()->master_server == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_initialized = 1;
|
|
|
|
_slave_thread_id = thread_create("Slave Thread", _slave_thread, NULL, THREAD_ATTACHED);
|
|
|
|
}
|
|
|
|
|
|
|
|
void slave_shutdown(void) {
|
|
|
|
if (!_initialized) return;
|
|
|
|
_initialized = 0;
|
|
|
|
thread_join(_slave_thread_id);
|
|
|
|
}
|
|
|
|
|
2003-02-07 05:53:38 -05:00
|
|
|
static void create_relay_stream(char *server, int port, char *mount)
|
|
|
|
{
|
|
|
|
sock_t streamsock;
|
2002-08-05 10:48:04 -04:00
|
|
|
char header[4096];
|
|
|
|
connection_t *con;
|
|
|
|
http_parser_t *parser;
|
2002-08-16 10:26:48 -04:00
|
|
|
client_t *client;
|
2003-02-07 05:53:38 -05:00
|
|
|
|
|
|
|
DEBUG1("Adding source at mountpoint \"%s\"", mount);
|
|
|
|
|
|
|
|
streamsock = sock_connect_wto(server, port, 0);
|
|
|
|
if (streamsock == SOCK_ERROR) {
|
|
|
|
WARN0("Failed to relay stream from master server");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
con = create_connection(streamsock, NULL);
|
|
|
|
sock_write(streamsock, "GET %s HTTP/1.0\r\n\r\n", mount);
|
|
|
|
memset(header, 0, sizeof(header));
|
|
|
|
if (util_read_header(con->sock, header, 4096) == 0) {
|
|
|
|
connection_close(con);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
parser = httpp_create_parser();
|
|
|
|
httpp_initialize(parser, NULL);
|
|
|
|
if(!httpp_parse_response(parser, header, strlen(header), mount)) {
|
|
|
|
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");
|
|
|
|
connection_close(con);
|
|
|
|
httpp_destroy(parser);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
client = client_create(con, parser);
|
|
|
|
if (!connection_create_source(client, con, parser,
|
|
|
|
httpp_getvar(parser, HTTPP_VAR_URI))) {
|
|
|
|
client_destroy(client);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *_slave_thread(void *arg) {
|
|
|
|
sock_t mastersock;
|
|
|
|
char buf[256];
|
2002-08-05 10:48:04 -04:00
|
|
|
int interval = config_get_config()->master_update_interval;
|
2002-08-16 10:55:56 -04:00
|
|
|
char *authheader, *data;
|
|
|
|
int len;
|
|
|
|
char *username = "relay";
|
2002-08-17 02:25:38 -04:00
|
|
|
char *password = config_get_config()->master_password;
|
|
|
|
|
|
|
|
if(password == NULL)
|
|
|
|
password = config_get_config()->source_password;
|
|
|
|
|
2002-08-05 10:48:04 -04:00
|
|
|
|
|
|
|
while (_initialized) {
|
|
|
|
if (config_get_config()->master_update_interval > ++interval) {
|
|
|
|
thread_sleep(1000000);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
interval = 0;
|
|
|
|
|
|
|
|
mastersock = sock_connect_wto(config_get_config()->master_server, config_get_config()->master_server_port, 0);
|
|
|
|
if (mastersock == SOCK_ERROR) {
|
2002-08-09 04:06:00 -04:00
|
|
|
WARN0("Relay slave failed to contact master server to fetch stream list");
|
2002-08-05 10:48:04 -04:00
|
|
|
continue;
|
|
|
|
}
|
2002-08-16 10:55:56 -04:00
|
|
|
|
2002-08-17 02:25:38 -04:00
|
|
|
len = strlen(username) + strlen(password) + 1;
|
2002-08-16 10:55:56 -04:00
|
|
|
authheader = malloc(len+1);
|
|
|
|
strcpy(authheader, username);
|
|
|
|
strcat(authheader, ":");
|
2002-08-17 02:25:38 -04:00
|
|
|
strcat(authheader, password);
|
2002-08-16 10:55:56 -04:00
|
|
|
data = util_base64_encode(authheader);
|
2002-12-30 10:42:38 -05:00
|
|
|
sock_write(mastersock,
|
|
|
|
"GET /admin/streamlist HTTP/1.0\r\n"
|
|
|
|
"Authorization: Basic %s\r\n"
|
|
|
|
"\r\n", data);
|
2002-12-29 09:06:20 -05:00
|
|
|
free(authheader);
|
2002-08-16 10:55:56 -04:00
|
|
|
free(data);
|
2002-08-05 10:48:04 -04:00
|
|
|
while (sock_read_line(mastersock, buf, sizeof(buf))) {
|
2002-12-29 04:21:32 -05:00
|
|
|
if(!strlen(buf))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (sock_read_line(mastersock, buf, sizeof(buf))) {
|
2002-08-05 10:48:04 -04:00
|
|
|
avl_tree_rlock(global.source_tree);
|
|
|
|
if (!source_find_mount(buf)) {
|
|
|
|
avl_tree_unlock(global.source_tree);
|
2002-08-09 04:06:00 -04:00
|
|
|
|
2003-02-07 05:53:38 -05:00
|
|
|
create_relay_stream(
|
|
|
|
config_get_config()->master_server,
|
|
|
|
config_get_config()->master_server_port,
|
|
|
|
buf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
avl_tree_unlock(global.source_tree);
|
2002-08-05 10:48:04 -04:00
|
|
|
}
|
|
|
|
sock_close(mastersock);
|
2003-02-07 05:53:38 -05:00
|
|
|
|
|
|
|
/* And now, we process the individual mounts... */
|
|
|
|
relay_server *relay = config_get_config()->relay;
|
|
|
|
while(relay) {
|
|
|
|
avl_tree_rlock(global.source_tree);
|
|
|
|
if(!source_find_mount(relay->mount)) {
|
|
|
|
avl_tree_unlock(global.source_tree);
|
|
|
|
|
|
|
|
create_relay_stream(relay->server, relay->port, relay->mount);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
avl_tree_unlock(global.source_tree);
|
|
|
|
}
|
2002-08-05 10:48:04 -04:00
|
|
|
}
|
|
|
|
thread_exit(0);
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-02-07 05:53:38 -05:00
|
|
|
|