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
use error::EtherdreamError;
use net2::UdpBuilder;
use protocol::Broadcast;
use std::net::IpAddr;
pub const COMMUNICATION_PORT : u16 = 7765;
pub const BROADCAST_PORT : u16 = 7654;
#[derive(Clone, Copy, Debug)]
pub struct SearchResult {
pub ip_address : IpAddr,
pub broadcast : Broadcast,
}
pub fn find_first_dac() -> Result<SearchResult, EtherdreamError> {
let udp = UdpBuilder::new_v4()?;
udp.reuse_address(true)?;
let socket = udp.bind(("0.0.0.0", BROADCAST_PORT))?;
let mut buf = [0u8; 128];
let result = socket.recv_from(&mut buf)?;
let broadcast = Broadcast::parse(&buf[0..36])?;
Ok(SearchResult {
ip_address : result.1.ip(),
broadcast: broadcast,
})
}