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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
use crate::error::UdpError;
use crate::network::ConnectionInfo;
use crate::state::{State, StatePhase};
use futures_util::future::join4;
use futures_util::stream::{SplitSink, SplitStream, Stream};
use futures_util::{FutureExt, SinkExt, StreamExt};
use log::*;
use mumble_protocol::crypt::ClientCryptState;
use mumble_protocol::ping::{PingPacket, PongPacket};
use mumble_protocol::voice::VoicePacket;
use mumble_protocol::Serverbound;
use std::collections::{hash_map::Entry, HashMap};
use std::convert::TryFrom;
use std::net::{Ipv6Addr, SocketAddr};
use std::sync::{
atomic::{AtomicU64, Ordering},
Arc, RwLock,
};
use tokio::sync::{mpsc, oneshot, watch, Mutex};
use tokio::time::{interval, timeout, Duration};
use tokio::{join, net::UdpSocket};
use tokio_util::udp::UdpFramed;
use super::{run_until, VoiceStreamType};
pub type PingRequest = (u64, SocketAddr, Box<dyn FnOnce(Option<PongPacket>) + Send>);
type UdpSender = SplitSink<UdpFramed<ClientCryptState>, (VoicePacket<Serverbound>, SocketAddr)>;
type UdpReceiver = SplitStream<UdpFramed<ClientCryptState>>;
pub async fn handle(
state: Arc<RwLock<State>>,
mut connection_info_receiver: watch::Receiver<Option<ConnectionInfo>>,
mut crypt_state_receiver: mpsc::Receiver<ClientCryptState>,
) -> Result<(), UdpError> {
let receiver = state.read().unwrap().audio_input().receiver();
loop {
let connection_info = 'data: loop {
while connection_info_receiver.changed().await.is_ok() {
if let Some(data) = connection_info_receiver.borrow().clone() {
break 'data data;
}
}
return Err(UdpError::NoConnectionInfoReceived);
};
let (sink, source) = connect(&mut crypt_state_receiver).await?;
let sink = Arc::new(Mutex::new(sink));
let source = Arc::new(Mutex::new(source));
let phase_watcher = state.read().unwrap().phase_receiver();
let last_ping_recv = AtomicU64::new(0);
run_until(
|phase| matches!(phase, StatePhase::Disconnected),
join4(
listen(Arc::clone(&state), Arc::clone(&source), &last_ping_recv),
send_voice(
Arc::clone(&sink),
connection_info.socket_addr,
phase_watcher.clone(),
Arc::clone(&receiver),
),
send_pings(
Arc::clone(&state),
Arc::clone(&sink),
connection_info.socket_addr,
&last_ping_recv,
),
new_crypt_state(&mut crypt_state_receiver, sink, source),
)
.map(|_| ()),
phase_watcher,
)
.await;
debug!("Fully disconnected UDP stream, waiting for new connection info");
}
}
async fn connect(
crypt_state: &mut mpsc::Receiver<ClientCryptState>,
) -> Result<(UdpSender, UdpReceiver), UdpError> {
// Bind UDP socket
let udp_socket = UdpSocket::bind((Ipv6Addr::from(0u128), 0u16)).await?;
// Wait for initial CryptState
let crypt_state = match crypt_state.recv().await {
Some(crypt_state) => crypt_state,
// disconnected before we received the CryptSetup packet, oh well
None => return Err(UdpError::DisconnectBeforeCryptSetup),
};
debug!("UDP connected");
// Wrap the raw UDP packets in Mumble's crypto and voice codec (CryptState does both)
Ok(UdpFramed::new(udp_socket, crypt_state).split())
}
async fn new_crypt_state(
crypt_state: &mut mpsc::Receiver<ClientCryptState>,
sink: Arc<Mutex<UdpSender>>,
source: Arc<Mutex<UdpReceiver>>,
) {
loop {
if let Some(crypt_state) = crypt_state.recv().await {
info!("Received new crypt state");
let udp_socket = UdpSocket::bind((Ipv6Addr::from(0u128), 0u16))
.await
.expect("Failed to bind UDP socket");
let (new_sink, new_source) = UdpFramed::new(udp_socket, crypt_state).split();
*sink.lock().await = new_sink;
*source.lock().await = new_source;
}
}
}
async fn listen(
state: Arc<RwLock<State>>,
source: Arc<Mutex<UdpReceiver>>,
last_ping_recv: &AtomicU64,
) {
loop {
let packet = source.lock().await.next().await.unwrap();
let (packet, _src_addr) = match packet {
Ok(packet) => packet,
Err(err) => {
warn!("Got an invalid UDP packet: {}", err);
// To be expected, considering this is the internet, just ignore it
continue;
}
};
match packet {
VoicePacket::Ping { timestamp } => {
state
.read()
.unwrap()
.broadcast_phase(StatePhase::Connected(VoiceStreamType::UDP));
last_ping_recv.store(timestamp, Ordering::Relaxed);
}
VoicePacket::Audio {
session_id,
// seq_num,
payload,
// position_info,
..
} => {
state.read().unwrap().audio_output().decode_packet_payload(
VoiceStreamType::UDP,
session_id,
payload,
);
}
}
}
}
async fn send_pings(
state: Arc<RwLock<State>>,
sink: Arc<Mutex<UdpSender>>,
server_addr: SocketAddr,
last_ping_recv: &AtomicU64,
) {
let mut last_send = None;
let mut interval = interval(Duration::from_millis(1000));
loop {
interval.tick().await;
let last_recv = last_ping_recv.load(Ordering::Relaxed);
if last_send.is_some() && last_send.unwrap() != last_recv {
debug!("Sending TCP voice");
state
.read()
.unwrap()
.broadcast_phase(StatePhase::Connected(VoiceStreamType::TCP));
}
match sink
.lock()
.await
.send((
VoicePacket::Ping {
timestamp: last_recv + 1,
},
server_addr,
))
.await
{
Ok(_) => {
last_send = Some(last_recv + 1);
}
Err(e) => {
debug!("Error sending UDP ping: {}", e);
}
}
}
}
async fn send_voice(
sink: Arc<Mutex<UdpSender>>,
server_addr: SocketAddr,
phase_watcher: watch::Receiver<StatePhase>,
receiver: Arc<Mutex<Box<(dyn Stream<Item = VoicePacket<Serverbound>> + Unpin)>>>,
) {
loop {
let mut inner_phase_watcher = phase_watcher.clone();
loop {
inner_phase_watcher.changed().await.unwrap();
if matches!(
*inner_phase_watcher.borrow(),
StatePhase::Connected(VoiceStreamType::UDP)
) {
break;
}
}
run_until(
|phase| !matches!(phase, StatePhase::Connected(VoiceStreamType::UDP)),
async {
let mut receiver = receiver.lock().await;
loop {
let sending = (receiver.next().await.unwrap(), server_addr);
sink.lock().await.send(sending).await.unwrap();
}
},
phase_watcher.clone(),
)
.await;
}
}
pub async fn handle_pings(mut ping_request_receiver: mpsc::UnboundedReceiver<PingRequest>) {
let udp_socket = UdpSocket::bind((Ipv6Addr::from(0u128), 0u16))
.await
.expect("Failed to bind UDP socket");
let pending = Mutex::new(HashMap::new());
let sender = async {
while let Some((id, socket_addr, handle)) = ping_request_receiver.recv().await {
debug!("Sending ping with id {} to {}", id, socket_addr);
let packet = PingPacket { id };
let packet: [u8; 12] = packet.into();
udp_socket.send_to(&packet, &socket_addr).await.unwrap();
let (tx, rx) = oneshot::channel();
match pending.lock().await.entry(id) {
Entry::Occupied(_) => {
warn!("Tried to send duplicate ping with id {}", id);
continue;
}
Entry::Vacant(v) => {
v.insert(tx);
}
}
tokio::spawn(async move {
handle(match timeout(Duration::from_secs(1), rx).await {
Ok(Ok(r)) => Some(r),
Ok(Err(_)) => {
warn!(
"Ping response sender for server {}, ping id {} dropped",
socket_addr, id
);
None
}
Err(_) => {
debug!(
"Server {} timed out when sending ping id {}",
socket_addr, id
);
None
}
});
});
}
};
let receiver = async {
let mut buf = vec![0; 24];
while let Ok(read) = udp_socket.recv(&mut buf).await {
if read != 24 {
warn!("Ping response had length {}, expected 24", read);
continue;
}
let packet = PongPacket::try_from(buf.as_slice()).unwrap();
match pending.lock().await.entry(packet.id) {
Entry::Occupied(o) => {
let id = *o.key();
if o.remove().send(packet).is_err() {
debug!("Received response to ping with id {} too late", id);
}
}
Entry::Vacant(v) => {
warn!("Received ping with id {} that we didn't send", v.key());
}
}
}
};
debug!("Waiting for ping requests");
join!(sender, receiver);
}
|