119 lines
2.5 KiB
C
119 lines
2.5 KiB
C
/*
|
|
* Copyright (C) 2020-2021 Soleta Networks <info@soleta.eu>
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify it under
|
|
* the terms of the GNU Affero General Public License as published by the
|
|
* Free Software Foundation; either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*/
|
|
|
|
#include "core.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <syslog.h>
|
|
#include <sys/ioctl.h>
|
|
#include <netinet/in.h>
|
|
#include <ifaddrs.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <netinet/tcp.h>
|
|
#include <fcntl.h>
|
|
#include <time.h>
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
#include <errno.h>
|
|
#include <getopt.h>
|
|
|
|
int max_clients = DEFAULT_MAX_CLIENTS;
|
|
const char *root = ".";
|
|
int max_redirect = 0;
|
|
|
|
static struct option tip_repo_opts[] = {
|
|
{ "max-clients", required_argument, 0, 'n' },
|
|
{ "redirect", optional_argument, 0, 'r' },
|
|
{ "root", required_argument, 0, 't' },
|
|
{ "daemon", no_argument, 0, 'd' },
|
|
{ NULL },
|
|
};
|
|
|
|
struct ev_io ev_io_server_rest;
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
int socket_rest, val, ret;
|
|
bool daemon = false;
|
|
|
|
openlog("tiptorrent", LOG_PID, LOG_DAEMON);
|
|
|
|
tip_main_loop = ev_default_loop(0);
|
|
|
|
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
|
|
exit(EXIT_FAILURE);
|
|
|
|
while (1) {
|
|
val = getopt_long(argc, argv, "n:r::d", tip_repo_opts, NULL);
|
|
if (val < 0)
|
|
break;
|
|
|
|
switch (val) {
|
|
case 'n':
|
|
max_clients = atoi(optarg);
|
|
if (max_clients <= 0) {
|
|
syslog(LOG_ERR, "Invalid number for max_clients");
|
|
return EXIT_FAILURE;
|
|
}
|
|
break;
|
|
case 'r':
|
|
if (optarg) {
|
|
max_redirect = atoi(optarg);
|
|
if (max_redirect <= 0) {
|
|
syslog(LOG_ERR, "Invalid number for redirections");
|
|
return EXIT_FAILURE;
|
|
}
|
|
} else {
|
|
max_redirect = max_clients;
|
|
}
|
|
break;
|
|
case 't':
|
|
root = strdup(optarg);
|
|
break;
|
|
case 'd':
|
|
daemon = true;
|
|
break;
|
|
case '?':
|
|
return EXIT_FAILURE;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (daemon) {
|
|
ret = fork();
|
|
if (ret < 0) {
|
|
fprintf(stderr, "Failed to fork() daemon tiptorrent\n");
|
|
exit(EXIT_FAILURE);
|
|
} else if (ret > 0) {
|
|
exit(EXIT_SUCCESS);
|
|
}
|
|
}
|
|
|
|
socket_rest = tip_socket_server_init("9999");
|
|
if (socket_rest < 0) {
|
|
syslog(LOG_ERR, "Cannot open tiptorrent server socket\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
ev_io_init(&ev_io_server_rest, tip_server_accept_cb, socket_rest, EV_READ);
|
|
ev_io_start(tip_main_loop, &ev_io_server_rest);
|
|
|
|
syslog(LOG_INFO, "Waiting for connections\n");
|
|
|
|
while (1)
|
|
ev_loop(tip_main_loop, 0);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
}
|