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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
extern crate quick_xml;
use quick_xml::events::Event;
use quick_xml::Reader;
use std::io;
use std::net::{SocketAddr, UdpSocket};
use std::sync::mpsc::{self, TryRecvError};
use std::thread;
use std::time::Duration;
#[derive(Debug, Default)]
struct ProbeMatch {
urn: String,
name: String,
hardware: String,
location: String,
types: Vec<String>,
xaddrs: Vec<String>,
scopes: Vec<String>,
}
fn read_message(socket: &UdpSocket) -> Result<String, io::Error> {
let mut buf: [u8; 65_535] = [0; 65_535];
let result = socket.recv(&mut buf);
result.map(move |_| std::str::from_utf8(&buf).unwrap().to_string())
}
fn parse_probe_match(xml: String) -> Result<Option<ProbeMatch>, String> {
let mut reader = Reader::from_str(&xml);
reader.trim_text(true);
let mut buf = Vec::new();
loop {
match reader.read_event(&mut buf) {
Ok(Event::End(ref e)) => {
match e.name() {
b"SOAP-ENV:Header" => break,
_ => (),
};
}
Ok(Event::Eof) => return Err("Finished before header".to_string()),
Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
_ => (),
}
}
buf.clear();
let mut buf2 = Vec::new();
let mut stack = Vec::<String>::new();
let mut probe_match = ProbeMatch::default();
loop {
match reader.read_event(&mut buf2) {
Ok(Event::Start(ref e)) => {
let name = std::str::from_utf8(e.name()).unwrap().to_string();
println!("Pushing {}", name);
stack.push(name);
}
Ok(Event::End(ref _e)) => {
let ended_tag = stack.pop().expect("Stack can't be empty, but it was.");
if ended_tag == "SOAP-ENV:Body" {
break;
}
println!("Popped: {}", ended_tag);
}
Ok(Event::Text(e)) => {
let text = std::str::from_utf8(e.escaped())
.expect("UTF decode error")
.to_string();
let tag = stack.get(stack.len() - 1).expect("Stack can't be empty");
match tag.as_str() {
"wsa:Address" => probe_match.urn = text,
"d:Types" => probe_match.types = text.split(' ').map(|s| s.to_string()).collect(),
"d:Scopes" => probe_match.scopes = text.split(' ').map(|s| s.to_string()).collect(),
"d:XAddrs" => probe_match.xaddrs = text.split(' ').map(|s| s.to_string()).collect(),
_ => {
println!("Ignoring text {}", text);
}
}
}
Ok(Event::Eof) => break,
Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
_ => (),
}
}
for scope in &probe_match.scopes {
if scope.starts_with("onvif://www.onvif.org/hardware/") {
let parts: Vec<&str> = scope.split('/').collect();
probe_match.hardware = (parts[parts.len() - 1]).to_string();
} else if scope.starts_with("onvif://www.onvif.org/location/") {
let parts: Vec<&str> = scope.split('/').collect();
probe_match.location = (parts[parts.len() - 1]).to_string();
} else if scope.starts_with("onvif://www.onvif.org/name/") {
let parts: Vec<&str> = scope.split('/').collect();
probe_match.name = (parts[parts.len() - 1]).to_string();
}
}
Ok(Some(probe_match))
}
pub fn start_probe(probe_duration: &Duration) -> Result<(), io::Error> {
let MULTICAST_ADDR: SocketAddr = "239.255.255.250:3702".parse().unwrap();
println!("Started probe");
let soap_tmpl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:a=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\">
<s:Header>
<a:Action s:mustUnderstand=\"1\">
http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe</a:Action>
<a:MessageID>uuid:__uuid__</a:MessageID>
<a:ReplyTo><a:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>
</a:ReplyTo><a:To s:mustUnderstand=\"1\">urn:schemas-xmlsoap-org:ws:2005:04:discovery</a:To>
</s:Header><s:Body><Probe xmlns=\"http://schemas.xmlsoap.org/ws/2005/04/discovery\">
<d:Types xmlns:d=\"http://schemas.xmlsoap.org/ws/2005/04/discovery\" xmlns:dp0=\"http://www.onvif.org/ver10/network/wsdl\">
dp0:__type__</d:Types></Probe></s:Body>
</s:Envelope>";
let types = vec!["NetworkVideoTransmitter", "Device", "NetworkVideoDisplay"];
let soap_reqs: Vec<String> = types
.iter()
.map(|device_type| {
let s: &str = soap_tmpl.clone();
let s = s.replace("__type__", device_type);
let s = s.replace("__uuid__", "7c208633-8086-4a83-a9d4-b3fd8673b8f7");
s
})
.collect();
let all_interfaces = SocketAddr::from(([0, 0, 0, 0], 0));
let socket = UdpSocket::bind(&all_interfaces).expect("Could not bind to udp socket");
let read_socket = socket.try_clone().unwrap();
read_socket
.set_read_timeout(Some(Duration::from_secs(1)))
.expect("set_read_timeout call failed");
let (thread_stop_tx, thread_stop_rx) = mpsc::channel();
let (devices_tx, devices_rx) = mpsc::channel();
let _read_thread_handle = thread::spawn(move || {
loop {
if let Ok(message) = read_message(&read_socket) {
println!("message: {}", message);
devices_tx
.send(parse_probe_match(message))
.expect("Could not send found device over channel");
}
match thread_stop_rx.try_recv() {
Ok(_) | Err(TryRecvError::Disconnected) => {
println!("Stopping receive thread");
break;
}
Err(TryRecvError::Empty) => {}
}
}
});
let _broadcast_thread = thread::spawn(move || {
for _ in 1..5 {
for soap_req in &soap_reqs {
socket
.send_to(soap_req.as_bytes(), &MULTICAST_ADDR)
.expect("Could not send req");
thread::sleep(Duration::from_millis(100));
}
}
});
for _ in 1..10 {
let dev = devices_rx.recv().unwrap();
println!("{:?}", dev);
}
thread::sleep(*probe_duration);
let _ = thread_stop_tx.send(());
Ok(())
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
#[test]
fn probe_xml_parse() {
use super::*;
use std::fs;
use std::io::prelude::*;
let mut fl = fs::File::open("src/resources/probe-discovery-response.xml").unwrap();
let mut probe_discovery_response = String::new();
fl.read_to_string(&mut probe_discovery_response)
.expect("something went wrong reading the file");
let probe_match = parse_probe_match(probe_discovery_response)
.unwrap()
.unwrap();
println!("{:?}", probe_match);
}
}