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
|
/*
* 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 = ".";
bool redirect;
static struct option tip_repo_opts[] = {
{ "max-clients", 1, 0, 'n' },
{ "redirect", 0, 0, 'r' },
{ "root", 1, 0, 't' },
{ "daemon", 0, 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:rd", 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':
redirect = true;
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);
}
|