2007-07-14 15:31:27 +02:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 07/2007 by Olaf Rempel *
|
|
|
|
* razzor@kopf-tisch.de *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; version 2 of the License *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
***************************************************************************/
|
2007-05-15 17:39:34 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2008-11-02 13:14:53 +01:00
|
|
|
#include <getopt.h>
|
|
|
|
|
2007-05-15 17:39:34 +02:00
|
|
|
#include "configfile.h"
|
2007-06-18 15:44:05 +02:00
|
|
|
#include "connection.h"
|
2007-05-15 17:39:34 +02:00
|
|
|
#include "event.h"
|
2007-06-18 15:44:05 +02:00
|
|
|
#include "httpd.h"
|
2007-05-15 17:39:34 +02:00
|
|
|
#include "list.h"
|
|
|
|
#include "logging.h"
|
|
|
|
#include "sockaddr.h"
|
|
|
|
#include "tcpsocket.h"
|
|
|
|
|
2008-11-02 13:14:53 +01:00
|
|
|
#define DEFAULT_CONFIG "torrent-stats.conf"
|
|
|
|
|
|
|
|
static struct option opts[] = {
|
|
|
|
{"config", 1, 0, 'c'},
|
|
|
|
{"help", 0, 0, 'h'},
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
2007-05-15 17:39:34 +02:00
|
|
|
static int listen_cb(const char *parameter, void *privdata)
|
|
|
|
{
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
|
|
|
|
if (parse_sockaddr(parameter, &addr) < 0) {
|
|
|
|
log_print(LOG_WARN, "listen_cb(): invalid address");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sockfd = tcp_listen(&addr);
|
|
|
|
if (sockfd < 0) {
|
|
|
|
log_print(LOG_WARN, "listen_cb(): tcp_listen()");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_print(LOG_INFO, "listen on %s", get_sockaddr_buf(&addr));
|
2007-06-18 15:44:05 +02:00
|
|
|
event_add_readfd(NULL, sockfd, privdata, NULL);
|
2007-05-15 17:39:34 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2008-11-02 13:14:53 +01:00
|
|
|
char *config = DEFAULT_CONFIG;
|
|
|
|
int code, arg = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
code = getopt_long(argc, argv, "c:h", opts, &arg);
|
|
|
|
|
|
|
|
switch (code) {
|
|
|
|
case 'c': /* config */
|
|
|
|
config = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'h': /* help */
|
|
|
|
printf("Usage: torrent-stats [options]\n"
|
|
|
|
"Options: \n"
|
|
|
|
" --config -c configfile use this configfile\n"
|
|
|
|
" --help -h this help\n"
|
|
|
|
"\n");
|
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?': /* error */
|
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* unknown / all options parsed */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (code != -1);
|
|
|
|
|
|
|
|
if (config_parse(config) < 0)
|
|
|
|
exit(1);
|
2007-05-15 17:39:34 +02:00
|
|
|
|
2007-06-18 15:44:05 +02:00
|
|
|
config_get_strings("global", "listen", listen_cb, ctcs_accept_handler);
|
|
|
|
config_get_strings("global", "listen-http", listen_cb, httpd_accept_handler);
|
|
|
|
|
2007-07-07 13:53:36 +02:00
|
|
|
httpd_add_cb("/quit", 0, ctcs_httpd_quit, NULL);
|
|
|
|
httpd_add_cb("/", 1, ctcs_httpd_show, NULL);
|
2007-05-15 17:39:34 +02:00
|
|
|
|
2007-06-19 15:44:57 +02:00
|
|
|
struct timeval tv = { .tv_sec = 10, .tv_usec = 0 };
|
|
|
|
event_add_timeout(&tv, ctcs_trigger_status, NULL);
|
|
|
|
|
2007-05-15 17:39:34 +02:00
|
|
|
event_loop();
|
|
|
|
return 0;
|
|
|
|
}
|