-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathftp_server.cpp
177 lines (162 loc) · 4.73 KB
/
ftp_server.cpp
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/// very simple ftp server by yasio, only support file retrive command
#include "ftp_server.hpp"
#define FTP_CONTROL_CHANNEL_INDEX 0
long long g_stats_hits = 0;
ftp_server::ftp_server(cxx17::string_view root, cxx17::string_view wanip)
{
ftp_session::register_handlers_once();
cxx17::assign(root_, root);
if (!wanip.empty())
{
std::vector<ip::endpoint> eps;
xxsocket::resolve(eps, wanip.data());
if (!eps.empty())
{
wanip_ = eps[0].ip();
YASIO_LOG("resolve host: %s succeed, ip: %s", wanip.data(), wanip_.c_str());
}
}
else
cxx17::assign(wanip_, wanip);
fstats_ = fopen((root_ + "/stats.txt").c_str(), "wb");
}
ftp_server::~ftp_server()
{
if (fstats_)
fclose(fstats_);
}
void ftp_server::run(int max_clients, u_short port)
{
// max support 10 clients
std::vector<io_hostent> hosts{{"0.0.0.0", 21}};
transfer_start_index_ = static_cast<int>(hosts.size());
for (auto i = transfer_start_index_; i < transfer_start_index_ + max_clients; ++i)
{
hosts.push_back({"0.0.0.0", 0});
this->avails_.push_back(i);
this->sessions_.push_back(ftp_session_ptr{});
}
service_.reset(new io_service(&hosts.front(), static_cast<int>(hosts.size())));
service_->set_option(YOPT_S_NO_NEW_THREAD, 1);
service_->set_option(YOPT_C_MOD_FLAGS, FTP_CONTROL_CHANNEL_INDEX, YCF_REUSEADDR, 0);
service_->schedule(std::chrono::microseconds(1), [](io_service& service) {
service.open(FTP_CONTROL_CHANNEL_INDEX, YCK_TCP_SERVER);
return true;
});
service_->start([=](event_ptr&& ev) {
if (!ev->passive())
{
switch (ev->kind())
{
case YEK_PACKET:
if (ev->cindex() == 0)
{
dispatch_packet(ev);
}
else
{
; // ignore
}
break;
case YEK_ON_OPEN:
if (ev->status() == 0)
{
if (ev->cindex() == 0)
on_open_session(ev); // port 21
else
on_open_transmit_session(ev); // data port
}
break;
case YEK_ON_CLOSE:
if (ev->cindex() == 0)
on_close_session(ev);
break;
}
}
else
{
switch (ev->kind())
{
case YEK_ON_OPEN:
if (ev->status() == 0)
printf("%s", "start ftp server sucess.\n");
else
printf("start ftp server failed, status=%d\n", ev->status());
break;
case YEK_ON_CLOSE:
break;
}
}
});
}
void ftp_server::on_open_session(event_ptr& ev)
{
auto thandle = ev->transport();
if (!this->avails_.empty())
{
auto transfer_cindex = this->avails_.back();
auto wrap = new (std::nothrow) ftp_session_ptr(std::make_shared<ftp_session>(*this, thandle, transfer_cindex));
if (wrap)
{
this->sessions_[to_session_index(transfer_cindex)] = *wrap;
ev->transport_ud(wrap); // store ftp_session_ptr wrap to as transport userdata
printf("a ftp session: %p income, transfer_cindex=%d\n", thandle, transfer_cindex);
this->avails_.pop_back();
(*wrap)->say_hello();
}
else
{
service_->close(thandle);
}
}
else
{ // close directly
service_->close(thandle);
}
}
void ftp_server::on_close_session(event_ptr& ev)
{
auto wrap = ev->transport_ud<ftp_session_ptr*>();
if (wrap)
{
auto transfer_cindex = (*wrap)->transfer_cindex_;
printf("the ftp session: %p is ended, transfer_cindex=%d\n", ev->transport(), transfer_cindex);
this->avails_.push_back(transfer_cindex); // recyle transfer channel
this->sessions_[to_session_index(transfer_cindex)].reset(); // release the session
delete wrap; // delete wrap session object
}
}
void ftp_server::on_open_transmit_session(event_ptr& ev)
{
auto cindex = ev->cindex();
auto thandle = ev->transport();
auto session = this->sessions_[to_session_index(cindex)];
if (session)
session->open_transimt_session(thandle);
else
{
printf("Error: no ftp session for file transfer channel: %d\n", cindex);
service_->close(thandle);
}
}
void ftp_server::dispatch_packet(event_ptr& ev)
{
auto wrap = ev->transport_ud<ftp_session_ptr*>();
if (wrap)
{
(*wrap)->handle_packet(ev->packet());
++g_stats_hits;
if (fstats_)
{
rewind(fstats_);
fprintf(fstats_, "The commands processed totals=%lld", g_stats_hits);
fflush(fstats_);
}
}
else
{
auto thandle = ev->transport();
printf("Error: cann't dispatch for a unregistered session: %p, will close it.", thandle);
service_->close(thandle);
}
}