Skip to content

Commit 65c379e

Browse files
committed
bind argument
1 parent bfbbd1d commit 65c379e

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/main.rs

+11
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ fn main() {
5454
.multiple(true)
5555
.default_value("")
5656
)
57+
.arg(
58+
Arg::with_name("bind")
59+
.help("bind to the interface associated with the address <host>")
60+
.takes_value(true)
61+
.long("bind")
62+
.short("B")
63+
.value_name("host")
64+
.required(true)
65+
.multiple(true)
66+
.default_value("0.0.0.0")
67+
)
5768
.arg(
5869
Arg::with_name("debug")
5970
.help("emit debug-level logging on stderr; default is info and above")

src/server.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
use std::error::Error;
2222
use std::io;
23-
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, Shutdown, SocketAddr};
23+
use std::net::{IpAddr, Ipv4Addr, Shutdown, SocketAddr};
2424
use std::sync::atomic::{AtomicBool, AtomicU16, Ordering};
2525
use std::sync::mpsc::channel;
2626
use std::sync::{Arc, Mutex};
@@ -401,13 +401,19 @@ pub fn serve(args: ArgMatches) -> BoxResult<()> {
401401
log::debug!("limiting service to {} concurrent clients", client_limit);
402402
}
403403

404+
let addr = args
405+
.value_of("bind")
406+
.unwrap_or("0.0.0.0")
407+
.parse::<IpAddr>()
408+
.unwrap_or(IpAddr::V4(Ipv4Addr::UNSPECIFIED));
409+
404410
//start listening for connections
405411
let port: u16 = args.value_of("port").unwrap().parse()?;
406412
let listener: TcpListener = if args.is_present("version6") {
407-
TcpListener::bind(&SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), port))
413+
TcpListener::bind(&SocketAddr::new(addr, port))
408414
.unwrap_or_else(|_| panic!("failed to bind TCP socket, port {}", port))
409415
} else {
410-
TcpListener::bind(&SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), port))
416+
TcpListener::bind(&SocketAddr::new(addr, port))
411417
.unwrap_or_else(|_| panic!("failed to bind TCP socket, port {}", port))
412418
};
413419
log::info!("server listening on {}", listener.local_addr()?);

0 commit comments

Comments
 (0)