Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

discovery: allow setting port explicitly #169

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/authentication/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use rand;
use std::collections::BTreeMap;
use std::io;
use std::sync::Arc;
use std::net::{SocketAddr,IpAddr,Ipv4Addr};
use tokio_core::net::TcpListener;
use tokio_core::reactor::Handle;
use url;
Expand Down Expand Up @@ -206,12 +207,13 @@ pub struct DiscoveryStream {
task: Box<Future<Item=(), Error=io::Error>>,
}

pub fn discovery(handle: &Handle, device_name: String, device_id: String)
pub fn discovery(handle: &Handle, device_name: String, device_id: String, discovery_port: Option<u16>)
-> io::Result<DiscoveryStream>
{
let (discovery, creds_rx) = Discovery::new(device_name.clone(), device_id);
let bind_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), discovery_port.unwrap_or(0));

let listener = TcpListener::bind(&"0.0.0.0:0".parse().unwrap(), handle)?;
let listener = TcpListener::bind(&bind_addr, handle)?;
let addr = listener.local_addr()?;

let http = Http::new();
Expand Down
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ fn setup(args: &[String]) -> Setup {
.optflag("v", "verbose", "Enable verbose output")
.optopt("u", "username", "Username to sign in with", "USERNAME")
.optopt("p", "password", "Password", "PASSWORD")
.optopt("", "discovery-port", "Explicitly set TCP port used for discovery", "PORT")
.optflag("", "disable-discovery", "Disable discovery mode")
.optopt("", "backend", "Audio backend to use. Use '?' to list options", "BACKEND")
.optopt("", "device", "Audio device to use. Use '?' to list options", "DEVICE")
Expand Down Expand Up @@ -145,13 +146,15 @@ fn setup(args: &[String]) -> Setup {
cached_credentials);

let enable_discovery = !matches.opt_present("disable-discovery");
let discovery_port = matches.opt_str("discovery-port").map(|x| x.parse::<u16>().expect("invalid port"));

let config = Config {
user_agent: version::version_string(),
device_id: device_id,
bitrate: bitrate,
onstart: matches.opt_str("onstart"),
onstop: matches.opt_str("onstop"),
discovery_port: discovery_port
};

let device = matches.opt_str("device");
Expand Down Expand Up @@ -217,8 +220,9 @@ impl Main {
fn discovery(&mut self) {
let device_id = self.config.device_id.clone();
let name = self.name.clone();
let discovery_port = self.config.discovery_port;

self.discovery = Some(discovery(&self.handle, name, device_id).unwrap());
self.discovery = Some(discovery(&self.handle, name, device_id, discovery_port).unwrap());
}

fn credentials(&mut self, credentials: Credentials) {
Expand Down
2 changes: 2 additions & 0 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub struct Config {
pub bitrate: Bitrate,
pub onstart: Option<String>,
pub onstop: Option<String>,
pub discovery_port: Option<u16>
}

impl Default for Config {
Expand All @@ -60,6 +61,7 @@ impl Default for Config {
bitrate: Bitrate::Bitrate160,
onstart: None,
onstop: None,
discovery_port: None,
}
}
}
Expand Down