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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//! Fresh (non-pooled) FTP connection negotiation helpers.
//!
//! Contains the `FtpNegotiator` impl methods for establishing a new FTP
//! control connection, authenticating, and querying server capabilities
//! (FEAT, OPTS UTF8 ON, SYST).
use tokio::io::BufReader;
use tokio::net::TcpStream;
use tokio::time::{Duration, timeout};
use tracing::{debug, info, warn};
use crate::error::{Aria2Error, RecoverableError, Result};
use crate::ftp::connection::negotiation::capabilities;
use crate::ftp::connection::negotiation::capabilities::ServerCapabilities;
use crate::ftp::connection::negotiation::control::FreshControl;
use crate::ftp::connection::negotiation::parsing::{parse_epsv_response, parse_pasv_response};
use crate::ftp::connection::negotiation::{FtpNegotiator, PasvResult};
impl FtpNegotiator {
/// Connect to FTP server, read greeting, authenticate, and detect capabilities.
///
/// After successful authentication, sends FEAT to detect server features
/// and OPTS UTF8 ON if the server advertises UTF8 support (RFC 2640).
/// This matches the C++ aria2 flow where FEAT/OPTS are sent right after
/// login on every fresh connection.
pub(super) async fn connect_and_authenticate(
host: &str,
port: u16,
username: &str,
password: &str,
connect_timeout: Duration,
command_timeout: Duration,
) -> Result<(FreshControl, ServerCapabilities)> {
debug!("Connecting to FTP server at {}:{}", host, port);
let stream = timeout(connect_timeout, TcpStream::connect((host, port)))
.await
.map_err(|_| Aria2Error::Recoverable(RecoverableError::Timeout))?
.map_err(|e| {
Aria2Error::Recoverable(RecoverableError::TemporaryNetworkFailure {
message: format!("FTP connect failed to {}:{}: {}", host, port, e),
})
})?;
stream.set_nodelay(true).map_err(|e| {
Aria2Error::Recoverable(RecoverableError::TemporaryNetworkFailure {
message: format!("set_nodelay failed: {}", e),
})
})?;
let mut ctrl = FreshControl {
reader: BufReader::new(stream),
command_timeout,
};
// Read welcome message
let welcome = ctrl.read_response(command_timeout).await?;
if welcome.0 != 220 {
// C++ aria2: EX_CONNECTION_FAILED -> FTP_PROTOCOL_ERROR for non-220 greeting
return Err(Aria2Error::Recoverable(
RecoverableError::FtpProtocolError {
message: format!(
"FTP server rejected connection (expected 220): {} {}",
welcome.0, welcome.1
),
},
));
}
info!("Connected to FTP server {}:{}", host, port);
// Authenticate using shared helper
super::parsing::authenticate(&mut ctrl, username, password).await?;
// Query server capabilities via FEAT command
let capabilities = capabilities::query_feat(&mut ctrl).await?;
// If FEAT reports UTF8 support, send OPTS UTF8 ON (RFC 2640)
if capabilities.utf8 {
capabilities::send_opts_utf8_on(&mut ctrl).await?;
}
Ok((ctrl, capabilities))
}
/// Enter passive mode and return the resolved port + optional direct data stream.
///
/// This method separates port resolution from stream creation, enabling the
/// C++ `SEQ_RESOLVE_PROXY` flow where the PASV data connection may be
/// tunneled through an HTTP proxy instead of connected directly.
///
/// When no proxy is configured, the direct `TcpStream` is included in the
/// result. When a proxy is configured, only the port is returned and the
/// caller must establish the tunnel separately.
pub(super) async fn enter_passive_mode_get_port(
ctrl: &mut FreshControl,
_host: &str,
connect_timeout: Duration,
caps: &ServerCapabilities,
) -> Result<PasvResult> {
// Try EPSV first (IPv6-friendly, RFC 2428)
if caps.epsv || !caps.mlst_mlsd {
debug!("Attempting extended passive mode (EPSV)");
let epsv_resp = ctrl.command("EPSV").await;
match epsv_resp {
Ok(resp) if resp.0 == 229 => {
if let Some(port) = parse_epsv_response(&resp.1) {
debug!("EPSV successful, using port: {}", port);
let data_stream = timeout(
connect_timeout,
TcpStream::connect(std::net::SocketAddr::new(ctrl.peer_ip()?, port)),
)
.await
.map_err(|_| Aria2Error::Recoverable(RecoverableError::Timeout))?
.map_err(|e| {
Aria2Error::Recoverable(RecoverableError::TemporaryNetworkFailure {
message: format!("EPSV data connection failed: {}", e),
})
})?;
let _ = data_stream.set_nodelay(true);
return Ok(PasvResult {
port,
stream: Some(data_stream),
});
}
warn!("Failed to parse EPSV response, falling back to PASV");
}
Ok(resp) if (500..600).contains(&resp.0) => {
debug!(
"EPSV rejected with {} (server does not support EPSV), falling back to PASV",
resp.0
);
}
Ok(resp) => {
debug!(
"EPSV unexpected response: {} {}, trying PASV",
resp.0, resp.1
);
}
_ => {
debug!("EPSV not supported (I/O error), trying PASV");
}
}
}
// Fallback to PASV
debug!("Entering passive mode (PASV)");
let pasv_resp = ctrl.command("PASV").await?;
if pasv_resp.0 != 227 {
return Err(Aria2Error::Recoverable(
RecoverableError::FtpProtocolError {
message: format!("PASV failed: {} {}", pasv_resp.0, pasv_resp.1),
},
));
}
match parse_pasv_response(&pasv_resp.1) {
Some((data_host, data_port)) => {
debug!(
advertised_host = %data_host,
control_peer = %ctrl.peer_ip()?,
port = data_port,
"PASV successful; using control peer address"
);
let data_stream = timeout(
connect_timeout,
TcpStream::connect(std::net::SocketAddr::new(ctrl.peer_ip()?, data_port)),
)
.await
.map_err(|_| Aria2Error::Recoverable(RecoverableError::Timeout))?
.map_err(|e| {
Aria2Error::Recoverable(RecoverableError::TemporaryNetworkFailure {
message: format!("PASV data connection failed: {}", e),
})
})?;
let _ = data_stream.set_nodelay(true);
Ok(PasvResult {
port: data_port,
stream: Some(data_stream),
})
}
None => Err(Aria2Error::Recoverable(
RecoverableError::FtpProtocolError {
message: "Cannot parse PASV response".into(),
},
)),
}
}
/// Enter passive mode and connect to the data port on a fresh connection.
///
/// Convenience wrapper for `enter_passive_mode_get_port` that always
/// returns the direct stream (no proxy tunnel).
#[allow(dead_code)] // Kept for backward compatibility with tests
pub(super) async fn enter_passive_mode(
ctrl: &mut FreshControl,
host: &str,
connect_timeout: Duration,
caps: &ServerCapabilities,
) -> Result<TcpStream> {
let result = Self::enter_passive_mode_get_port(ctrl, host, connect_timeout, caps).await?;
Ok(result.stream.unwrap())
}
/// Enter active mode and accept the server's data connection on a fresh connection.
///
/// Uses server capabilities to decide EPRT vs PORT:
/// - If FEAT reported EPRT support, try EPRT first
/// - Fallback to PORT if EPRT fails
///
/// Matches C++ `FtpNegotiationCommand`:
/// - `SEQ_PREPARE_PORT` -> `SEQ_PREPARE_SERVER_SOCKET_EPRT` or `SEQ_PREPARE_SERVER_SOCKET`
/// - `SEQ_SEND_EPRT`/`SEQ_RECV_EPRT` -> `SEQ_SEND_PORT`/`SEQ_RECV_PORT` fallback
/// - `SEQ_WAIT_CONNECTION` -> accept
pub(super) async fn enter_active_mode(
ctrl: &mut FreshControl,
connect_timeout: Duration,
_caps: &ServerCapabilities,
) -> Result<TcpStream> {
// Get local address from the control stream
let local_addr = ctrl.reader.get_ref().local_addr().map_err(|e| {
Aria2Error::Recoverable(RecoverableError::TemporaryNetworkFailure {
message: format!("Failed to get local address: {}", e),
})
})?;
let local_ip = local_addr.ip();
// Bind listener on the appropriate wildcard address for the address family
// C++ uses socket->getAddressFamily() to decide; we match the control socket.
let bind_addr = match local_ip {
std::net::IpAddr::V4(_) => "0.0.0.0:0",
std::net::IpAddr::V6(_) => "[::]:0",
};
let listener = tokio::net::TcpListener::bind(bind_addr)
.await
.map_err(|e| {
Aria2Error::Recoverable(RecoverableError::TemporaryNetworkFailure {
message: format!("Failed to bind data port: {}", e),
})
})?;
let data_port = listener
.local_addr()
.map_err(|e| {
Aria2Error::Recoverable(RecoverableError::TemporaryNetworkFailure {
message: format!("Failed to get listen port: {}", e),
})
})?
.port();
// C++ SEQ_PREPARE_SERVER_SOCKET_EPRT: try EPRT first
// EPRT protocol number: |1| for IPv4, |2| for IPv6 (RFC 2428)
let proto_num = match local_ip {
std::net::IpAddr::V4(_) => 1,
std::net::IpAddr::V6(_) => 2,
};
let eprt_cmd = format!("EPRT |{}|{}|{}|", proto_num, local_ip, data_port);
debug!("Sending EPRT command");
let eprt_resp = ctrl.command(&eprt_cmd).await?;
// C++ recvEprt: 200 -> SEQ_SEND_REST; else -> SEQ_PREPARE_SERVER_SOCKET (PORT fallback)
if eprt_resp.0 != 200 && eprt_resp.0 != 500 && eprt_resp.0 != 501 && eprt_resp.0 != 502 {
return Err(Aria2Error::Recoverable(
RecoverableError::FtpProtocolError {
message: format!("EPRT command failed: {} {}", eprt_resp.0, eprt_resp.1),
},
));
}
if !(200..300).contains(&eprt_resp.0) {
// EPRT failed, try PORT (C++ SEQ_PREPARE_SERVER_SOCKET + SEQ_SEND_PORT)
warn!("EPRT unavailable, falling back to PORT mode");
let ipv4_addr = match local_ip {
std::net::IpAddr::V4(v4) => v4,
std::net::IpAddr::V6(_) => {
return Err(Aria2Error::Recoverable(
RecoverableError::FtpProtocolError {
message: "IPv6 does not support PORT command, use passive mode".into(),
},
));
}
};
let ip_bytes = ipv4_addr.octets();
let p1 = data_port / 256;
let p2 = data_port % 256;
let port_cmd = format!(
"PORT {},{},{},{},{},{}",
ip_bytes[0], ip_bytes[1], ip_bytes[2], ip_bytes[3], p1, p2
);
debug!("Sending PORT command");
let port_resp = ctrl.command(&port_cmd).await?;
// C++ recvPort: non-200 -> FTP_PROTOCOL_ERROR
if !(200..300).contains(&port_resp.0) {
return Err(Aria2Error::Recoverable(
RecoverableError::FtpProtocolError {
message: format!("PORT command failed: {} {}", port_resp.0, port_resp.1),
},
));
}
}
// C++ SEQ_WAIT_CONNECTION: wait for server to connect
debug!("Waiting for server data connection on port: {}", data_port);
let (data_stream, _addr) = timeout(connect_timeout, listener.accept())
.await
.map_err(|_| Aria2Error::Recoverable(RecoverableError::Timeout))?
.map_err(|e| {
Aria2Error::Recoverable(RecoverableError::TemporaryNetworkFailure {
message: format!("Failed to accept data connection: {}", e),
})
})?;
let _ = data_stream.set_nodelay(true);
debug!("Active mode data connection established");
Ok(data_stream)
}
}