-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
151 lines (124 loc) · 4.29 KB
/
server.cpp
File metadata and controls
151 lines (124 loc) · 4.29 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <thread>
#include <vector>
#include <csignal>
#include <atomic>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cerrno>
#include "server_utils.h"
#include "ftp_session.h"
#include "peer_manager.h"
// graceful shutdown state
static std::atomic<bool> g_running{true};
static int g_listen_fd_main = -1; // client listener (base port)
static int g_listen_fd_peer = -1; // peer listener (port + 200)
static void close_fd_safe(int& fd) {
if (fd >= 0) {
::shutdown(fd, SHUT_RDWR);
::close(fd);
fd = -1;
}
}
static void sigint_handler(int) {
g_running = false;
// Wake up accept() immediately by closing the listening socket
close_fd_safe(g_listen_fd_main);
close_fd_safe(g_listen_fd_peer);
}
static int make_listener(uint16_t port) {
int lfd = ::socket(AF_INET, SOCK_STREAM, 0);
if (lfd < 0) {
std::perror("socket");
return -1;
}
int opt = 1;
::setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(port);
if (::bind(lfd, (sockaddr*)&addr, sizeof(addr)) < 0) {
std::perror("bind");
::close(lfd);
return -1;
}
if (::listen(lfd, 16) < 0) {
std::perror("listen");
::close(lfd);
return -1;
}
return lfd;
}
static void accept_loop(int lfd, const std::string& jail_root, PeerManager* manager,
const std::string& our_ip, uint16_t base_port)
{
while (g_running.load()) {
sockaddr_in peer{};
socklen_t plen = sizeof(peer);
int cfd = ::accept(lfd, (sockaddr*)&peer, &plen);
if (cfd < 0) {
if (!g_running.load() || errno == EBADF)
break; // listener was closed
if (errno == EINTR)
continue;
std::perror("accept");
continue;
}
// Spawn detached worker for this control connection
std::thread([cfd, jail_root, manager, our_ip, base_port](){
FTPSession sess(cfd, jail_root, manager, our_ip, base_port);
sess.run();
}).detach();
}
}
int main() {
// Setup signal handlers
struct sigaction sa{};
sa.sa_handler = sigint_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGINT, &sa, nullptr);
sigaction(SIGTERM, &sa, nullptr);
std::signal(SIGPIPE, SIG_IGN);
// .conf reading
const std::string conf_path = "server.conf";
uint16_t port = srv::read_port_from_config(conf_path, 2121);
std::string peer_cidr = srv::read_string_from_config(conf_path, "PEER_SUBNET", "");
int peer_interval = srv::read_int_from_config(conf_path, "PEER_INTERVAL", 10);
uint16_t peer_port = static_cast<uint16_t>(port + 200); // Peer port offset
// Create listeners
g_listen_fd_main = make_listener(port);
if (g_listen_fd_main < 0)
return 1;
g_listen_fd_peer = make_listener(peer_port);
if (g_listen_fd_peer < 0) {
std::cerr << "Failed to start peer listener on port " << peer_port << std::endl;
close_fd_safe(g_listen_fd_main);
return 1;
}
// Pick an IP to advertise to peers
std::string bl = "";
std::string our_ip = srv::get_primary_ipv4_guess_from_interfaces(bl);
if (!peer_cidr.empty()) {
our_ip = srv::get_primary_ipv4_guess_from_interfaces(peer_cidr);
}
std::cout << "FTP server listening on port " << port << " (ip " << our_ip << ")" << " and peer port " << peer_port << std::endl;
PeerManager* manager = nullptr;
PeerManager pm(peer_cidr, port, peer_interval, our_ip); // pm will add 200
if (!peer_cidr.empty())
manager = ±
const std::string jail_root = "./db";
// Run accept loops for client and peer on separate threads
std::thread t_client(accept_loop, g_listen_fd_main, jail_root, manager, our_ip, port);
std::thread t_peer(accept_loop, g_listen_fd_peer, jail_root, manager, our_ip, peer_port);
// Wait for Ctrl C
t_client.join();
t_peer.join();
// Cleanup (safe even if closed by signal handler)
close_fd_safe(g_listen_fd_main);
close_fd_safe(g_listen_fd_peer);
g_running = false;
return 0;
}