summaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 15afaf72839da980eacccadcfe04bea34143bb76 (plain)
1
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * 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 (optind < argc &&
				    argv[optind][0] != '-') {
					max_redirect = atoi(argv[optind++]);
					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, "Starting tiptorrent server, max_clients=%u redirection=%u root=%s\n",
	       max_clients, max_redirect, root);

	while (1)
		ev_loop(tip_main_loop, 0);

	exit(EXIT_SUCCESS);
}