Skip to content

Commit 3da883d

Browse files
committed
Fix #1 '"bind_any" doesn't respect '--addr' argument
1 parent 4bff4be commit 3da883d

File tree

4 files changed

+19
-16
lines changed

4 files changed

+19
-16
lines changed

src/handler.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ fn init_txc(dll_path: PathBuf, log_dir: PathBuf) -> ah::Result<libtxc::Module> {
2727
Ok(lib)
2828
}
2929

30-
fn init_upstream(con: &mut net::TcpStream) -> ah::Result<()> {
30+
fn init_upstream(con: &mut net::TcpStream, local_addr: net::IpAddr) -> ah::Result<()> {
3131
// open 'data' server
32-
let (data_server, data_port) =
33-
ws2::bind_any().context("Не удалось зарегистрировать сервер трансляции данных")?;
32+
let (data_server, data_port) = ws2::bind_any(local_addr)
33+
.context("Не удалось зарегистрировать сервер трансляции данных")?;
3434

3535
// send 'data' server port to client and await for connection
3636
con.write_all(&data_port.to_ne_bytes())?;
@@ -42,8 +42,10 @@ fn init_upstream(con: &mut net::TcpStream) -> ah::Result<()> {
4242
Ok(())
4343
}
4444

45-
pub fn handler(mut con: net::TcpStream, dll_path: PathBuf, log_dir: PathBuf) -> ah::Result<()> {
46-
init_upstream(&mut con)?;
45+
pub fn handler(
46+
(mut con, dll_path, log_dir, local_addr): (net::TcpStream, PathBuf, PathBuf, net::IpAddr),
47+
) -> ah::Result<()> {
48+
init_upstream(&mut con, local_addr)?;
4749
let lib = init_txc(dll_path, log_dir)?;
4850

4951
// loop

src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub use handler::handler;
66

77
use std::{
88
env, io,
9-
net::{SocketAddr, TcpListener, TcpStream},
9+
net::{IpAddr, SocketAddr, TcpListener, TcpStream},
1010
path::PathBuf,
1111
process::{Command, Stdio},
1212
};
@@ -64,8 +64,8 @@ pub fn txc_log_level() -> i32 {
6464
}
6565
}
6666

67-
pub fn read_handler_params() -> ah::Result<(TcpStream, PathBuf, PathBuf)> {
68-
// 'master' process ensures that the handler 'fork' is passed the 'dll_path' and 'log_dir'
67+
pub fn read_handler_params() -> ah::Result<(TcpStream, PathBuf, PathBuf, IpAddr)> {
68+
// 'master' process ensures that the handler 'fork' is run with the 'dll_path', 'log_dir' and 'addr'
6969
// as positional arguments, and client socket handle is written to 'stdin'
7070

7171
// init winsock2
@@ -75,7 +75,9 @@ pub fn read_handler_params() -> ah::Result<(TcpStream, PathBuf, PathBuf)> {
7575
.context("Не удалось получить дескриптор подключения через 'stdin'")?;
7676

7777
match env::args().collect::<Vec<String>>().as_slice() {
78-
[.., log_dir, dll_path] => Ok((con, log_dir.into(), dll_path.into())),
78+
[.., log_dir, dll_path, addr] => {
79+
Ok((con, log_dir.into(), dll_path.into(), addr.parse().expect("infallible")))
80+
}
7981
_ => unreachable!("unexpected number of arguments"),
8082
}
8183
}
@@ -100,6 +102,7 @@ pub fn master<A: Into<SocketAddr>>(addr: A, log_dir: PathBuf, dll_path: PathBuf)
100102
.stdout(Stdio::null())
101103
.arg(dll_path.to_string_lossy().to_string())
102104
.arg(log_dir.to_string_lossy().to_string())
105+
.arg(addr.ip().to_string())
103106
.spawn()
104107
.context("Ошибка запуска обработчика клиентcкого подключения")?;
105108

src/main.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ fn main() -> ah::Result<()> {
4444
}
4545
Role::Handler => read_handler_params()
4646
.context("Ошибка инициализации обработчика подключения")
47-
.and_then(|(con, dll_path, log_dir)| {
48-
handler(con, dll_path, log_dir).context("Ошибка обработки подключения")
49-
}),
47+
.and_then(handler)
48+
.context("Ошибка обработки подключения"),
5049
}
5150
}

src/ws2.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,10 @@ pub fn write_pinfo<W: Write>(mut writer: W, info: WSAPROTOCOL_INFOW) -> ah::Resu
5353
}
5454
}
5555

56-
pub fn bind_any() -> io::Result<(net::TcpListener, u16)> {
56+
pub fn bind_any<A: Into<Option<net::IpAddr>>>(addr: A) -> io::Result<(net::TcpListener, u16)> {
57+
let addr = addr.into().unwrap_or([127, 0, 0, 1].into());
5758
for port in 4243..65535 {
58-
if let Ok(listener) =
59-
net::TcpListener::bind(net::SocketAddrV4::new([127, 0, 0, 1].into(), port))
60-
{
59+
if let Ok(listener) = net::TcpListener::bind(net::SocketAddr::new(addr, port)) {
6160
return Ok((listener, port));
6261
}
6362
}

0 commit comments

Comments
 (0)