You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
opentracker/opentracker.c

809 lines
22 KiB
C

/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
18 years ago
It is considered beerware. Prost. Skol. Cheers or whatever.
Some of the stuff below is stolen from Fefes example libowfat httpd.
$Id$ */
18 years ago
/* System */
#include <arpa/inet.h>
9 months ago
#include <ctype.h>
18 years ago
#include <errno.h>
9 months ago
#include <pthread.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
9 months ago
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#ifdef WANT_SYSLOGS
#include <syslog.h>
#endif
18 years ago
/* Libowfat */
9 months ago
#include "byte.h"
#include "io.h"
#include "iob.h"
16 years ago
#include "ip6.h"
9 months ago
#include "scan.h"
#include "socket.h"
/* Opentracker */
#include "ot_accesslist.h"
9 months ago
#include "ot_http.h"
#include "ot_livesync.h"
9 months ago
#include "ot_mutex.h"
#include "ot_stats.h"
#include "ot_udp.h"
#include "trackerlogic.h"
/* Globals */
9 months ago
time_t g_now_seconds;
char *g_redirecturl;
uint32_t g_tracker_id;
volatile int g_opentracker_running = 1;
int g_self_pipe[2];
static char *g_serverdir;
static char *g_serveruser;
static unsigned int g_udp_workers;
9 months ago
static void panic(const char *routine) __attribute__((noreturn));
static void panic(const char *routine) {
fprintf(stderr, "%s: %s\n", routine, strerror(errno));
exit(111);
18 years ago
}
9 months ago
static void signal_handler(int s) {
if (s == SIGINT) {
/* Any new interrupt signal quits the application */
9 months ago
signal(SIGINT, SIG_DFL);
/* Tell all other threads to not acquire any new lock on a bucket
but cancel their operations and return */
g_opentracker_running = 0;
trackerlogic_deinit();
#ifdef WANT_SYSLOGS
closelog();
#endif
9 months ago
exit(0);
}
}
9 months ago
static void defaul_signal_handlers(void) {
sigset_t signal_mask;
sigemptyset(&signal_mask);
9 months ago
sigaddset(&signal_mask, SIGPIPE);
sigaddset(&signal_mask, SIGHUP);
sigaddset(&signal_mask, SIGINT);
sigaddset(&signal_mask, SIGALRM);
pthread_sigmask(SIG_BLOCK, &signal_mask, NULL);
}
9 months ago
static void install_signal_handlers(void) {
struct sigaction sa;
sigset_t signal_mask;
sigemptyset(&signal_mask);
sa.sa_handler = signal_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
9 months ago
if ((sigaction(SIGINT, &sa, NULL) == -1) || (sigaction(SIGALRM, &sa, NULL) == -1))
panic("install_signal_handlers");
9 months ago
sigaddset(&signal_mask, SIGINT);
pthread_sigmask(SIG_UNBLOCK, &signal_mask, NULL);
}
9 months ago
static void usage(char *name) {
fprintf(stderr,
"Usage: %s [-i ip] [-p port] [-P port] [-r redirect] [-d dir] [-u user] [-A ip[/bits]] [-f config] [-s livesyncport]"
#ifdef WANT_ACCESSLIST_BLACK
9 months ago
" [-b blacklistfile]"
#elif defined(WANT_ACCESSLIST_WHITE)
" [-w whitelistfile]"
#endif
9 months ago
"\n",
name);
18 years ago
}
9 months ago
#define HELPLINE(opt, desc) fprintf(stderr, "\t%-10s%s\n", opt, desc)
static void help(char *name) {
usage(name);
HELPLINE("-f config", "include and execute the config file");
HELPLINE("-i ip", "specify ip to bind to with next -[pP] (default: any, overrides preceeding ones)");
HELPLINE("-p port", "do bind to tcp port (default: 6969, you may specify more than one)");
HELPLINE("-P port", "do bind to udp port (default: 6969, you may specify more than one)");
HELPLINE("-r redirecturl", "specify url where / should be redirected to (default none)");
HELPLINE("-d dir", "specify directory to try to chroot to (default: \".\")");
HELPLINE("-u user", "specify user under whose privileges opentracker should run (default: \"nobody\")");
HELPLINE("-A ip[/bits]", "bless an ip address or net as admin address (e.g. to allow syncs from this address)");
#ifdef WANT_ACCESSLIST_BLACK
9 months ago
HELPLINE("-b file", "specify blacklist file.");
#elif defined(WANT_ACCESSLIST_WHITE)
HELPLINE("-w file", "specify whitelist file.");
#endif
fprintf(stderr, "\nExample: ./opentracker -i 127.0.0.1 -p 6969 -P 6969 -f ./opentracker.conf -i 10.1.1.23 -p 2710 -p 80\n");
fprintf(stderr, " Here -i 127.0.0.1 selects the ip address for the next -p 6969 and -P 6969.\n");
fprintf(stderr, " If no port is bound from config file or command line, the last address given\n");
fprintf(stderr, " (or ::1 if none is set) will be used on port 6969.\n");
}
#undef HELPLINE
9 months ago
static ssize_t header_complete(char *request, ssize_t byte_count) {
ssize_t i = 0, state = 0;
9 months ago
for (i = 1; i < byte_count; i += 2)
if (request[i] <= 13) {
i--;
9 months ago
for (state = 0; i < byte_count; ++i) {
char c = request[i];
9 months ago
if (c == '\r' || c == '\n')
state = (state >> 2) | ((c << 6) & 0xc0);
else
break;
9 months ago
if (state >= 0xa0 || state == 0x99)
return i + 1;
}
9 months ago
}
return 0;
}
9 months ago
static void handle_dead(const int64 sock) {
struct http_data *cookie = io_getcookie(sock);
if (cookie) {
size_t i;
9 months ago
for (i = 0; i < cookie->batches; ++i)
iob_reset(cookie->batch + i);
free(cookie->batch);
array_reset(&cookie->request);
if (cookie->flag & (STRUCT_HTTP_FLAG_WAITINGFORTASK | STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER))
mutex_workqueue_canceltask(sock);
free(cookie);
}
9 months ago
io_close(sock);
}
9 months ago
static void handle_read(const int64 sock, struct ot_workstruct *ws) {
struct http_data *cookie = io_getcookie(sock);
ssize_t byte_count = io_tryread(sock, ws->inbuf, G_INBUF_SIZE);
9 months ago
if (byte_count == 0 || byte_count == -3) {
handle_dead(sock);
return;
}
9 months ago
if (byte_count == -1)
return;
/* If we get the whole request in one packet, handle it without copying */
9 months ago
if (!array_start(&cookie->request)) {
if ((ws->header_size = header_complete(ws->inbuf, byte_count))) {
ws->request = ws->inbuf;
ws->request_size = byte_count;
9 months ago
http_handle_request(sock, ws);
} else
9 months ago
array_catb(&cookie->request, ws->inbuf, (size_t)byte_count);
return;
}
9 months ago
array_catb(&cookie->request, ws->inbuf, byte_count);
if (array_failed(&cookie->request) || array_bytes(&cookie->request) > 8192) {
http_issue_error(sock, ws, CODE_HTTPERROR_500);
return;
}
9 months ago
while ((ws->header_size = header_complete(array_start(&cookie->request), array_bytes(&cookie->request)))) {
ws->request = array_start(&cookie->request);
ws->request_size = array_bytes(&cookie->request);
http_handle_request(sock, ws);
#ifdef WANT_KEEPALIVE
9 months ago
if (!ws->keep_alive)
#endif
return;
}
}
9 months ago
static void handle_write(const int64 sock) {
struct http_data *cookie = io_getcookie(sock);
size_t i;
int chunked = 0;
/* Look for the first io_batch still containing bytes to write */
9 months ago
if (cookie) {
if (cookie->flag & STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER)
chunked = 1;
9 months ago
for (i = 0; i < cookie->batches; ++i) {
if (cookie->batch[i].bytesleft) {
int64 res = iob_send(sock, cookie->batch + i);
9 months ago
if (res == -3) {
handle_dead(sock);
return;
}
9 months ago
if (!cookie->batch[i].bytesleft)
continue;
9 months ago
if (res == -1 || res > 0 || i < cookie->batches - 1)
return;
}
}
}
/* In a chunked transfer after all batches accumulated have been sent, wait for the next one */
9 months ago
if (chunked)
io_dontwantwrite(sock);
else
9 months ago
handle_dead(sock);
}
9 months ago
static void handle_accept(const int64 serversocket) {
struct http_data *cookie;
9 months ago
int64 sock;
ot_ip6 ip;
uint16 port;
tai6464 t;
9 months ago
while ((sock = socket_accept6(serversocket, ip, &port, NULL)) != -1) {
/* Put fd into a non-blocking mode */
9 months ago
io_nonblock(sock);
9 months ago
if (!io_fd(sock) || !(cookie = (struct http_data *)malloc(sizeof(struct http_data)))) {
io_close(sock);
continue;
}
9 months ago
memset(cookie, 0, sizeof(struct http_data));
memcpy(cookie->ip, ip, sizeof(ot_ip6));
9 months ago
io_setcookie(sock, cookie);
io_wantread(sock);
9 months ago
stats_issue_event(EVENT_ACCEPT, FLAG_TCP, (uintptr_t)ip);
/* That breaks taia encapsulation. But there is no way to take system
time this often in FreeBSD and libowfat does not allow to set unix time */
9 months ago
taia_uint(&t, 0); /* Clear t */
tai_unix(&(t.sec), (g_now_seconds + OT_CLIENT_TIMEOUT));
io_timeout(sock, t);
}
io_eagain(serversocket);
}
9 months ago
static void *server_mainloop(void *args) {
struct ot_workstruct ws;
9 months ago
time_t next_timeout_check = g_now_seconds + OT_CLIENT_TIMEOUT_CHECKINTERVAL;
struct iovec *iovector;
int iovec_entries, is_partial;
(void)args;
/* Initialize our "thread local storage" */
9 months ago
ws.inbuf = malloc(G_INBUF_SIZE);
ws.outbuf = malloc(G_OUTBUF_SIZE);
#ifdef _DEBUG_HTTPERROR
9 months ago
ws.debugbuf = malloc(G_DEBUGBUF_SIZE);
#endif
9 months ago
if (!ws.inbuf || !ws.outbuf)
panic("Initializing worker failed");
#ifdef WANT_ARC4RANDOM
arc4random_buf(&ws.rand48_state[0], 3 * sizeof(uint16_t));
#else
ws.rand48_state[0] = (uint16_t)random();
ws.rand48_state[1] = (uint16_t)random();
ws.rand48_state[2] = (uint16_t)random();
#endif
9 months ago
for (;;) {
int64 sock;
io_wait();
9 months ago
while ((sock = io_canread()) != -1) {
const void *cookie = io_getcookie(sock);
if ((intptr_t)cookie == FLAG_TCP)
handle_accept(sock);
else if ((intptr_t)cookie == FLAG_UDP)
handle_udp6(sock, &ws);
else if ((intptr_t)cookie == FLAG_SELFPIPE)
io_tryread(sock, ws.inbuf, G_INBUF_SIZE);
else
9 months ago
handle_read(sock, &ws);
}
9 months ago
while ((sock = mutex_workqueue_popresult(&iovec_entries, &iovector, &is_partial)) != -1)
http_sendiovecdata(sock, &ws, iovec_entries, iovector, is_partial);
9 months ago
while ((sock = io_canwrite()) != -1)
handle_write(sock);
9 months ago
if (g_now_seconds > next_timeout_check) {
while ((sock = io_timeouted()) != -1)
handle_dead(sock);
next_timeout_check = g_now_seconds + OT_CLIENT_TIMEOUT_CHECKINTERVAL;
}
livesync_ticker();
}
return 0;
}
9 months ago
static int64_t ot_try_bind(ot_ip6 ip, uint16_t port, PROTO_FLAG proto) {
int64 sock = proto == FLAG_TCP ? socket_tcp6() : socket_udp6();
#ifdef _DEBUG
{
9 months ago
char *protos[] = {"TCP", "UDP", "UDP mcast"};
char _debug[512];
int off = snprintf(_debug, sizeof(_debug), "Binding socket type %s to address [", protos[proto]);
off += fmt_ip6c(_debug + off, ip);
snprintf(_debug + off, sizeof(_debug) - off, "]:%d...", port);
fputs(_debug, stderr);
}
#endif
9 months ago
if (socket_bind6_reuse(sock, ip, port, 0) == -1)
panic("socket_bind6_reuse");
9 months ago
if ((proto == FLAG_TCP) && (socket_listen(sock, SOMAXCONN) == -1))
panic("socket_listen");
9 months ago
if (!io_fd(sock))
panic("io_fd");
9 months ago
io_setcookie(sock, (void *)proto);
9 months ago
if ((proto == FLAG_UDP) && g_udp_workers) {
io_block(sock);
udp_init(sock, g_udp_workers);
} else
9 months ago
io_wantread(sock);
16 years ago
#ifdef _DEBUG
9 months ago
fputs(" success.\n", stderr);
#endif
16 years ago
return sock;
}
9 months ago
char *set_config_option(char **option, char *value) {
#ifdef _DEBUG
9 months ago
fprintf(stderr, "Setting config option: %s\n", value);
#endif
9 months ago
while (isspace(*value))
++value;
free(*option);
return *option = strdup(value);
}
9 months ago
static int scan_ip6_port(const char *src, ot_ip6 ip, uint16 *port) {
const char *s = src;
9 months ago
int off, bracket = 0;
while (isspace(*s))
++s;
if (*s == '[')
++s, ++bracket; /* for v6 style notation */
if (!(off = scan_ip6(s, ip)))
return 0;
s += off;
9 months ago
if (bracket && *s == ']')
++s;
if (*s == 0 || isspace(*s))
return s - src;
if (!ip6_isv4mapped(ip)) {
if (*s != ':' && *s != '.')
return 0;
if (!bracket && *(s) == ':')
return 0;
16 years ago
s++;
} else {
9 months ago
if (*(s++) != ':')
return 0;
16 years ago
}
9 months ago
if (!(off = scan_ushort(s, port)))
return 0;
return off + s - src;
}
9 months ago
static int scan_ip6_net(const char *src, ot_net *net) {
const char *s = src;
9 months ago
int off;
while (isspace(*s))
++s;
if (!(off = scan_ip6(s, net->address)))
return 0;
s += off;
9 months ago
if (*s != '/')
net->bits = 128;
else {
s++;
9 months ago
if (!(off = scan_int(s, &net->bits)))
return 0;
9 months ago
if (ip6_isv4mapped(net->address))
net->bits += 96;
9 months ago
if (net->bits > 128)
return 0;
s += off;
}
9 months ago
return off + s - src;
}
9 months ago
int parse_configfile(char *config_filename) {
FILE *accesslist_filehandle;
char inbuf[512];
ot_ip6 tmpip;
#if defined(WANT_RESTRICT_STATS) || defined(WANT_IP_FROM_PROXY) || defined(WANT_SYNC_LIVE)
9 months ago
ot_net tmpnet;
#endif
9 months ago
int bound = 0;
9 months ago
accesslist_filehandle = fopen(config_filename, "r");
16 years ago
9 months ago
if (accesslist_filehandle == NULL) {
fprintf(stderr, "Warning: Can't open config file: %s.", config_filename);
return 0;
}
16 years ago
9 months ago
while (fgets(inbuf, sizeof(inbuf), accesslist_filehandle)) {
char *p = inbuf;
size_t strl;
/* Skip white spaces */
9 months ago
while (isspace(*p))
++p;
16 years ago
/* Ignore comments and empty lines */
9 months ago
if ((*p == '#') || (*p == '\n') || (*p == 0))
continue;
/* consume trailing new lines and spaces */
strl = strlen(p);
9 months ago
while (strl && isspace(p[strl - 1]))
p[--strl] = 0;
/* Scan for commands */
9 months ago
if (!byte_diff(p, 15, "tracker.rootdir") && isspace(p[15])) {
set_config_option(&g_serverdir, p + 16);
} else if (!byte_diff(p, 12, "tracker.user") && isspace(p[12])) {
set_config_option(&g_serveruser, p + 13);
} else if (!byte_diff(p, 14, "listen.tcp_udp") && isspace(p[14])) {
uint16_t tmpport = 6969;
9 months ago
if (!scan_ip6_port(p + 15, tmpip, &tmpport))
goto parse_error;
ot_try_bind(tmpip, tmpport, FLAG_TCP);
++bound;
ot_try_bind(tmpip, tmpport, FLAG_UDP);
++bound;
} else if (!byte_diff(p, 10, "listen.tcp") && isspace(p[10])) {
uint16_t tmpport = 6969;
9 months ago
if (!scan_ip6_port(p + 11, tmpip, &tmpport))
goto parse_error;
ot_try_bind(tmpip, tmpport, FLAG_TCP);
++bound;
9 months ago
} else if (!byte_diff(p, 10, "listen.udp") && isspace(p[10])) {
uint16_t tmpport = 6969;
9 months ago
if (!scan_ip6_port(p + 11, tmpip, &tmpport))
goto parse_error;
ot_try_bind(tmpip, tmpport, FLAG_UDP);
++bound;
9 months ago
} else if (!byte_diff(p, 18, "listen.udp.workers") && isspace(p[18])) {
char *value = p + 18;
9 months ago
while (isspace(*value))
++value;
scan_uint(value, &g_udp_workers);
#ifdef WANT_ACCESSLIST_WHITE
9 months ago
} else if (!byte_diff(p, 16, "access.whitelist") && isspace(p[16])) {
set_config_option(&g_accesslist_filename, p + 17);
#elif defined(WANT_ACCESSLIST_BLACK)
} else if (!byte_diff(p, 16, "access.blacklist") && isspace(p[16])) {
set_config_option(&g_accesslist_filename, p + 17);
#endif
#ifdef WANT_DYNAMIC_ACCESSLIST
9 months ago
} else if (!byte_diff(p, 15, "access.fifo_add") && isspace(p[15])) {
set_config_option(&g_accesslist_pipe_add, p + 16);
} else if (!byte_diff(p, 18, "access.fifo_delete") && isspace(p[18])) {
set_config_option(&g_accesslist_pipe_delete, p + 19);
#endif
#ifdef WANT_RESTRICT_STATS
9 months ago
} else if (!byte_diff(p, 12, "access.stats") && isspace(p[12])) {
if (!scan_ip6_net(p + 13, &tmpnet))
goto parse_error;
accesslist_bless_net(&tmpnet, OT_PERMISSION_MAY_STAT);
#endif
9 months ago
} else if (!byte_diff(p, 17, "access.stats_path") && isspace(p[17])) {
set_config_option(&g_stats_path, p + 18);
#ifdef WANT_IP_FROM_PROXY
9 months ago
} else if (!byte_diff(p, 12, "access.proxy") && isspace(p[12])) {
if (!scan_ip6_net(p + 13, &tmpnet))
goto parse_error;
accesslist_bless_net(&tmpnet, OT_PERMISSION_MAY_PROXY);
#endif
9 months ago
} else if (!byte_diff(p, 20, "tracker.redirect_url") && isspace(p[20])) {
set_config_option(&g_redirecturl, p + 21);
#ifdef WANT_SYNC_LIVE
9 months ago
} else if (!byte_diff(p, 24, "livesync.cluster.node_ip") && isspace(p[24])) {
if (!scan_ip6_net(p + 25, &tmpnet))
goto parse_error;
accesslist_bless_net(&tmpnet, OT_PERMISSION_MAY_LIVESYNC);
} else if (!byte_diff(p, 23, "livesync.cluster.listen") && isspace(p[23])) {
uint16_t tmpport = LIVESYNC_PORT;
9 months ago
if (!scan_ip6_port(p + 24, tmpip, &tmpport))
goto parse_error;
livesync_bind_mcast(tmpip, tmpport);
#endif
} else
9 months ago
fprintf(stderr, "Unhandled line in config file: %s\n", inbuf);
continue;
parse_error:
9 months ago
fprintf(stderr, "Parse error in config file: %s\n", inbuf);
}
9 months ago
fclose(accesslist_filehandle);
return bound;
}
9 months ago
void load_state(const char *const state_filename) {
FILE *state_filehandle;
char inbuf[512];
ot_hash infohash;
unsigned long long base, downcount;
9 months ago
int consumed;
9 months ago
state_filehandle = fopen(state_filename, "r");
9 months ago
if (state_filehandle == NULL) {
fprintf(stderr, "Warning: Can't open config file: %s.", state_filename);
return;
}
/* We do ignore anything that is not of the form "^[:xdigit:]:\d+:\d+" */
9 months ago
while (fgets(inbuf, sizeof(inbuf), state_filehandle)) {
int i;
9 months ago
for (i = 0; i < (int)sizeof(ot_hash); ++i) {
int eger = 16 * scan_fromhex(inbuf[2 * i]) + scan_fromhex(inbuf[1 + 2 * i]);
if (eger < 0)
continue;
infohash[i] = eger;
}
9 months ago
if (i != (int)sizeof(ot_hash))
continue;
i *= 2;
9 months ago
if (inbuf[i++] != ':' || !(consumed = scan_ulonglong(inbuf + i, &base)))
continue;
i += consumed;
9 months ago
if (inbuf[i++] != ':' || !(consumed = scan_ulonglong(inbuf + i, &downcount)))
continue;
add_torrent_from_saved_state(infohash, base, downcount);
}
9 months ago
fclose(state_filehandle);
}
9 months ago
int drop_privileges(const char *const serveruser, const char *const serverdir) {
struct passwd *pws = NULL;
#ifdef _DEBUG
9 months ago
if (!geteuid())
fprintf(stderr, "Dropping to user %s.\n", serveruser);
if (serverdir)
fprintf(stderr, "ch%s'ing to directory %s.\n", geteuid() ? "dir" : "root", serverdir);
#endif
/* Grab pws entry before chrooting */
9 months ago
pws = getpwnam(serveruser);
endpwent();
9 months ago
if (geteuid() == 0) {
/* Running as root: chroot and drop privileges */
9 months ago
if (serverdir && chroot(serverdir)) {
fprintf(stderr, "Could not chroot to %s, because: %s\n", serverdir, strerror(errno));
return -1;
}
9 months ago
if (chdir("/"))
panic("chdir() failed after chrooting: ");
/* If we can't find server user, revert to nobody's default uid */
9 months ago
if (!pws) {
fprintf(stderr, "Warning: Could not get password entry for %s. Reverting to uid -2.\n", serveruser);
if (setegid((gid_t)-2) || setgid((gid_t)-2) || setuid((uid_t)-2) || seteuid((uid_t)-2))
panic("Could not set uid to value -2");
9 months ago
} else {
if (setegid(pws->pw_gid) || setgid(pws->pw_gid) || setuid(pws->pw_uid) || seteuid(pws->pw_uid))
panic("Could not set uid to specified value");
}
9 months ago
if (geteuid() == 0 || getegid() == 0)
panic("Still running with root privileges?!");
9 months ago
} else {
/* Normal user, just chdir() */
9 months ago
if (serverdir && chdir(serverdir)) {
fprintf(stderr, "Could not chroot to %s, because: %s\n", serverdir, strerror(errno));
return -1;
}
}
return 0;
}
/* Maintain our copy of the clock. time() on BSDs is very expensive. */
9 months ago
static void *time_caching_worker(void *args) {
(void)args;
while (1) {
g_now_seconds = time(NULL);
sleep(5);
}
return NULL;
}
9 months ago
int main(int argc, char **argv) {
ot_ip6 serverip;
ot_net tmpnet;
int bound = 0, scanon = 1;
uint16_t tmpport;
char *statefile = 0;
pthread_t thread_id; /* time cacher */
16 years ago
9 months ago
memset(serverip, 0, sizeof(ot_ip6));
#ifdef WANT_V4_ONLY
9 months ago
serverip[10] = serverip[11] = -1;
#endif
16 years ago
#ifdef WANT_DEV_RANDOM
srandomdev();
#else
9 months ago
srandom(time(NULL));
#endif
9 months ago
while (scanon) {
switch (getopt(argc, argv,
":i:p:A:P:d:u:r:s:f:l:v"
#ifdef WANT_ACCESSLIST_BLACK
9 months ago
"b:"
#elif defined(WANT_ACCESSLIST_WHITE)
"w:"
#endif
9 months ago
"h")) {
case -1:
scanon = 0;
break;
case 'i':
if (!scan_ip6(optarg, serverip)) {
usage(argv[0]);
exit(1);
}
break;
#ifdef WANT_ACCESSLIST_BLACK
9 months ago
case 'b':
set_config_option(&g_accesslist_filename, optarg);
break;
#elif defined(WANT_ACCESSLIST_WHITE)
case 'w':
set_config_option(&g_accesslist_filename, optarg);
break;
#endif
9 months ago
case 'p':
if (!scan_ushort(optarg, &tmpport)) {
usage(argv[0]);
exit(1);
}
ot_try_bind(serverip, tmpport, FLAG_TCP);
bound++;
break;
case 'P':
if (!scan_ushort(optarg, &tmpport)) {
usage(argv[0]);
exit(1);
}
ot_try_bind(serverip, tmpport, FLAG_UDP);
bound++;
break;
#ifdef WANT_SYNC_LIVE
9 months ago
case 's':
if (!scan_ushort(optarg, &tmpport)) {
usage(argv[0]);
exit(1);
}
livesync_bind_mcast(serverip, tmpport);
break;
#endif
9 months ago
case 'd':
set_config_option(&g_serverdir, optarg);
break;
case 'u':
set_config_option(&g_serveruser, optarg);
break;
case 'r':
set_config_option(&g_redirecturl, optarg);
break;
case 'l':
statefile = optarg;
break;
case 'A':
if (!scan_ip6_net(optarg, &tmpnet)) {
usage(argv[0]);
exit(1);
}
9 months ago
accesslist_bless_net(&tmpnet, 0xffff); /* Allow everything for now */
break;
case 'f':
bound += parse_configfile(optarg);
break;
case 'h':
help(argv[0]);
exit(0);
case 'v': {
char buffer[8192];
stats_return_tracker_version(buffer);
fputs(buffer, stderr);
exit(0);
}
default:
case '?':
usage(argv[0]);
exit(1);
}
}
/* Bind to our default tcp/udp ports */
9 months ago
if (!bound) {
ot_try_bind(serverip, 6969, FLAG_TCP);
ot_try_bind(serverip, 6969, FLAG_UDP);
}
defaul_signal_handlers();
#ifdef WANT_SYSLOGS
9 months ago
openlog("opentracker", 0, LOG_USER);
setlogmask(LOG_UPTO(LOG_INFO));
#endif
9 months ago
if (drop_privileges(g_serveruser ? g_serveruser : "nobody", g_serverdir) == -1)
panic("drop_privileges failed, exiting. Last error");
9 months ago
g_now_seconds = time(NULL);
pthread_create(&thread_id, NULL, time_caching_worker, NULL);
/* Create our self pipe which allows us to interrupt mainloops
io_wait in case some data is available to send out */
9 months ago
if (pipe(g_self_pipe) == -1)
panic("selfpipe failed: ");
if (!io_fd(g_self_pipe[0]))
panic("selfpipe io_fd failed: ");
if (!io_fd(g_self_pipe[1]))
panic("selfpipe io_fd failed: ");
io_setcookie(g_self_pipe[0], (void *)FLAG_SELFPIPE);
io_wantread(g_self_pipe[0]);
/* Init all sub systems. This call may fail with an exit() */
9 months ago
trackerlogic_init();
#ifdef _DEBUG_RANDOMTORRENTS
9 months ago
fprintf(stderr, "DEBUG: Generating %d random peers on random torrents. This may take a while. (Setting RANDOMTORRENTS in trackerlogic.h)\n", RANDOMTORRENTS);
trackerlogic_add_random_torrents(RANDOMTORRENTS);
fprintf(stderr, "... done.\n");
#endif
9 months ago
if (statefile)
load_state(statefile);
9 months ago
install_signal_handlers();
9 months ago
if (!g_udp_workers)
udp_init(-1, 0);
9 months ago
server_mainloop(0);
return 0;
18 years ago
}