From d923f3bad377fc17add533e8de294fdf8bfcee3d Mon Sep 17 00:00:00 2001 From: Sasha Hilton Date: Wed, 31 Jan 2018 12:00:53 +0100 Subject: [PATCH] Add with-dns-sd feature flag --- Cargo.lock | 11 +++++++++++ Cargo.toml | 7 +++++-- src/discovery.rs | 30 +++++++++++++++++++++++++++++- src/lib.rs | 7 ++++++- 4 files changed, 51 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1bee8116..d01def63 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -106,6 +106,15 @@ dependencies = [ "quick-error 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "dns-sd" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "dtoa" version = "0.4.2" @@ -272,6 +281,7 @@ version = "0.1.0" dependencies = [ "alsa 0.0.1 (git+https://github.com/plietar/rust-alsa)", "base64 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", + "dns-sd 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "getopts 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1108,6 +1118,7 @@ dependencies = [ "checksum bytes 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "1b7db437d718977f6dc9b2e3fd6fc343c02ac6b899b73fdd2179163447bd9ce9" "checksum cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c819a1287eb618df47cc647173c5c4c66ba19d888a6e50d605672aed3140de" "checksum dns-parser 0.3.2 (git+https://github.com/plietar/dns-parser)" = "" +"checksum dns-sd 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d748509dea20228f63ba519bf142ce2593396386125b01f5b0d6412dab972087" "checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab" "checksum env_logger 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3ddf21e73e016298f5cb37d6ef8e8da8e39f91f9ec8b0df44b7deb16a9f8cd5b" "checksum error-chain 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e92ecf0a508c8e074c0e6fa8fe0fa38414848ad4dfc4db6f74c5e9753330b248" diff --git a/Cargo.toml b/Cargo.toml index 277e149f..dfe27524 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -54,6 +54,7 @@ alsa = { git = "https://github.com/plietar/rust-alsa", optional = tru portaudio-rs = { version = "0.3.0", optional = true } libpulse-sys = { version = "0.0.0", optional = true } libc = { version = "0.2", optional = true } +dns-sd = { version = "0.1.3", optional = true } [build-dependencies] rand = "0.3.13" @@ -68,11 +69,13 @@ pulseaudio-backend = ["libpulse-sys", "libc"] with-tremor = ["librespot-audio/with-tremor"] with-lewton = ["librespot-audio/with-lewton"] +with-dns-sd = ["dns-sd"] + default = ["portaudio-backend"] [package.metadata.deb] -maintainer = "nobody" -copyright = "2016 Paul Liétar" +maintainer = "librespot-org" +copyright = "2018 Paul Liétar" license_file = ["LICENSE", "4"] depends = "$auto" extended_description = """\ diff --git a/src/discovery.rs b/src/discovery.rs index fc168d5c..1f4617b4 100644 --- a/src/discovery.rs +++ b/src/discovery.rs @@ -6,7 +6,13 @@ use futures::sync::mpsc; use futures::{Future, Stream, Poll}; use hyper::server::{Service, Request, Response, Http}; use hyper::{self, Get, Post, StatusCode}; + +#[cfg(feature = "with-dns-sd")] +use dns_sd::DNSService; + +#[cfg(not(feature = "with-dns-sd"))] use mdns; + use num_bigint::BigUint; use rand; use std::collections::BTreeMap; @@ -189,6 +195,13 @@ impl Service for Discovery { } } +#[cfg(feature = "with-dns-sd")] +pub struct DiscoveryStream { + credentials: mpsc::UnboundedReceiver, + _svc: DNSService, +} + +#[cfg(not(feature = "with-dns-sd"))] pub struct DiscoveryStream { credentials: mpsc::UnboundedReceiver, _svc: mdns::Service, @@ -203,7 +216,10 @@ pub fn discovery(handle: &Handle, config: ConnectConfig, device_id: String) let http = Http::new(); http.serve_addr_handle(&"0.0.0.0:0".parse().unwrap(), &handle, move || Ok(discovery.clone())).unwrap() }; - let addr = serve.incoming_ref().local_addr(); + + #[cfg(feature = "with-dns-sd")] + let port = serve.incoming_ref().local_addr().port(); + let server_future = { let handle = handle.clone(); serve.for_each(move |connection| { @@ -214,7 +230,19 @@ pub fn discovery(handle: &Handle, config: ConnectConfig, device_id: String) }; handle.spawn(server_future); + #[cfg(feature = "with-dns-sd")] + let svc = DNSService::register(Some(&*config.name), + "_spotify-connect._tcp", + None, + None, + port, + &["VERSION=1.0", "CPath=/"]).unwrap(); + + #[cfg(not(feature = "with-dns-sd"))] + let addr = serve.incoming_ref().local_addr(); + #[cfg(not(feature = "with-dns-sd"))] let responder = mdns::Responder::spawn(&handle)?; + #[cfg(not(feature = "with-dns-sd"))] let svc = responder.register( "_spotify-connect._tcp".to_owned(), config.name, diff --git a/src/lib.rs b/src/lib.rs index 53257a8a..a08f2f5c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,7 +10,6 @@ extern crate base64; extern crate crypto; extern crate futures; extern crate hyper; -extern crate mdns; extern crate num_bigint; extern crate protobuf; extern crate rand; @@ -34,6 +33,12 @@ extern crate libpulse_sys; #[cfg(feature = "libc")] extern crate libc; +#[cfg(feature = "with-dns-sd")] +extern crate dns_sd; + +#[cfg(not(feature = "with-dns-sd"))] +extern crate mdns; + pub mod audio_backend; pub mod discovery; pub mod keymaster;