p2p 0.6.0

NAT Traversal for P2P communication
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use self::listener::Listener;
use self::puncher::{Puncher, Via};
use self::rendezvous_client::TcpRendezvousClient;
use mio::tcp::{TcpListener, TcpStream};
use mio::Poll;
use mio::Token;
use rand::{self, Rng};
use socket_collection::TcpSock;
use sodium::crypto::box_;
use std::any::Any;
use std::cell::RefCell;
use std::collections::HashSet;
use std::fmt::{self, Debug, Formatter};
use std::mem;
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
use std::rc::{Rc, Weak};
use std::time::Duration;
use tcp::new_reusably_bound_tcp_sockets;
use {Interface, NatError, NatState, NatType, TcpHolePunchInfo};

mod listener;
mod puncher;
mod rendezvous_client;

pub type RendezvousFinsih = Box<FnMut(&mut Interface, &Poll, NatType, ::Res<SocketAddr>)>;
pub type HolePunchFinsih = Box<FnMut(&mut Interface, &Poll, ::Res<TcpHolePunchInfo>)>;

const LISTENER_BACKLOG: i32 = 100;

enum State {
    None,
    Rendezvous {
        children: HashSet<Token>,
        info: (SocketAddr, Vec<SocketAddr>),
        f: RendezvousFinsih,
    },
    ReadyToHolePunch(SocketAddr),
    HolePunching {
        children: HashSet<Token>,
        f: HolePunchFinsih,
    },
}
impl Debug for State {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match *self {
            State::None => write!(f, "State::None"),
            State::Rendezvous { .. } => write!(f, "State::Rendezvous"),
            State::ReadyToHolePunch(..) => write!(f, "State::ReadyToHolePunch"),
            State::HolePunching { .. } => write!(f, "State::HolePunching"),
        }
    }
}

pub struct TcpHolePunchMediator {
    state: State,
    self_weak: Weak<RefCell<TcpHolePunchMediator>>,
}

impl TcpHolePunchMediator {
    pub fn start(
        ifc: &mut Interface,
        poll: &Poll,
        f: RendezvousFinsih,
    ) -> ::Res<Rc<RefCell<Self>>> {
        let mut servers = ifc.config().remote_tcp_rendezvous_servers.clone();
        let num_servers = servers.len();

        if num_servers == 0 {
            return Err(NatError::TcpHolePunchMediatorFailedToStart);
        } else if num_servers < 2 {
            info!(
                "Tcp: Symmetric NAT detection and port prediction will not be possible using \
                 less than 2 Rendezvous Servers. Use at-least 2. Recommended is 3."
            );
        } else if num_servers > 3 {
            let mut rng = rand::thread_rng();
            rng.shuffle(&mut servers);
            servers = servers[..3].to_owned();
        }

        let addr_any = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 0), 0));
        let (builders, addr) = new_reusably_bound_tcp_sockets(&addr_any, num_servers)?;

        let mediator = Rc::new(RefCell::new(TcpHolePunchMediator {
            state: State::None,
            self_weak: Weak::new(),
        }));
        mediator.borrow_mut().self_weak = Rc::downgrade(&mediator);
        let weak = mediator.borrow().self_weak.clone();

        let mut rendezvous_children = HashSet::with_capacity(builders.len());

        for (builder, server) in builders.iter().zip(servers.iter()) {
            let sock = {
                let s = builder.to_tcp_stream()?;
                TcpSock::wrap(TcpStream::connect_stream(s, server)?)
            };

            let weak_cloned = weak.clone();
            let handler = move |ifc: &mut Interface, poll: &Poll, child, res| {
                if let Some(mediator) = weak_cloned.upgrade() {
                    mediator
                        .borrow_mut()
                        .handle_rendezvous(ifc, poll, child, res);
                }
            };

            if let Ok(child) = TcpRendezvousClient::start(ifc, poll, sock, Box::new(handler)) {
                let _ = rendezvous_children.insert(child);
            }
        }

        if rendezvous_children.is_empty() {
            Err(NatError::TcpHolePunchMediatorFailedToStart)
        } else {
            let n = rendezvous_children.len();
            mediator.borrow_mut().state = State::Rendezvous {
                children: rendezvous_children,
                info: (addr, Vec::with_capacity(n)),
                f: f,
            };

            Ok(mediator)
        }
    }

    fn handle_rendezvous(
        &mut self,
        ifc: &mut Interface,
        poll: &Poll,
        child: Token,
        res: ::Res<SocketAddr>,
    ) {
        let r = match self.state {
            State::Rendezvous {
                ref mut children,
                ref mut info,
                ref mut f,
            } => {
                let _ = children.remove(&child);
                if let Ok(ext_addr) = res {
                    info.1.push(ext_addr);
                }
                if children.is_empty() {
                    let ext_addrs = mem::replace(&mut info.1, vec![]);

                    if ext_addrs.is_empty() {
                        f(
                            ifc,
                            poll,
                            NatType::Unknown,
                            Err(NatError::TcpRendezvousFailed),
                        );
                        Err(NatError::TcpRendezvousFailed)
                    } else {
                        let mut nat_type = NatType::Unknown;
                        match TcpHolePunchMediator::port_prediction(ext_addrs, &mut nat_type) {
                            Ok(ext_addr) => {
                                f(ifc, poll, nat_type, Ok(ext_addr));
                                Ok(Some(info.0))
                            }
                            _ => {
                                f(ifc, poll, nat_type, Err(NatError::TcpRendezvousFailed));
                                Err(NatError::TcpRendezvousFailed)
                            }
                        }
                    }
                } else {
                    Ok(None)
                }
            }
            ref x => {
                warn!(
                    "Logic Error in state book-keeping - Pls report this as a bug. Expected \
                     state: State::Rendezvous ;; Found: {:?}",
                    x
                );
                Err(NatError::InvalidState)
            }
        };

        match r {
            Ok(Some(our_addr)) => self.state = State::ReadyToHolePunch(our_addr),
            Ok(None) => (),
            Err(e @ NatError::TcpRendezvousFailed) => {
                // This is reached only if children is empty. So no chance of borrow violation for
                // children in terminate()
                debug!("Terminating due to: {:?}", e);
                self.terminate(ifc, poll);
            }
            // Don't call terminate as that can lead to child being borrowed twice
            Err(e) => debug!("Ignoring error in handle rendezvous: {:?}", e),
        }
    }

    fn port_prediction(
        mut ext_addrs: Vec<SocketAddr>,
        nat_type: &mut NatType,
    ) -> ::Res<SocketAddr> {
        let mut ext_addr = match ext_addrs.pop() {
            Some(addr) => addr,
            None => return Err(NatError::TcpRendezvousFailed),
        };

        let mut addrs = vec![ext_addr];
        let mut port_prediction_offset = 0i32;
        let mut is_err = false;
        for addr in ext_addrs {
            addrs.push(addr);

            if ext_addr.ip() != addr.ip() {
                info!(
                    "Symmetric NAT with variable IP mapping detected. No logic for Tcp \
                     external address prediction for these circumstances!"
                );
                *nat_type = NatType::EDMRandomIp(addrs.into_iter().map(|s| s.ip()).collect());
                is_err = true;
                break;
            } else if port_prediction_offset == 0 {
                port_prediction_offset = addr.port() as i32 - ext_addr.port() as i32;
            } else if port_prediction_offset != addr.port() as i32 - ext_addr.port() as i32 {
                info!(
                    "Symmetric NAT with non-uniformly changing port mapping detected. No logic \
                     for Tcp external address prediction for these circumstances!"
                );
                *nat_type = NatType::EDMRandomPort(addrs.into_iter().map(|s| s.port()).collect());
                is_err = true;
                break;
            }

            ext_addr = addr;
        }

        if is_err {
            return Err(NatError::TcpRendezvousFailed);
        }

        let port = ext_addr.port();
        ext_addr.set_port((port as i32 + port_prediction_offset) as u16);
        trace!("Our ext addr by Tcp Rendezvous Client: {}", ext_addr);

        *nat_type = if port_prediction_offset == 0 {
            NatType::EIM
        } else {
            NatType::EDM(port_prediction_offset)
        };

        Ok(ext_addr)
    }

    pub fn rendezvous_timeout(&mut self, ifc: &mut Interface, poll: &Poll) -> NatError {
        debug!("Timeout for TCP Rendezvous");
        let e = match self.state {
            State::Rendezvous { .. } => NatError::TcpRendezvousFailed,
            _ => NatError::InvalidState,
        };

        match e {
            NatError::InvalidState => (),
            ref x => {
                debug!("Terminating due to: {:?}", x);
                self.terminate(ifc, poll);
            }
        }

        e
    }

    pub fn punch_hole(
        &mut self,
        ifc: &mut Interface,
        poll: &Poll,
        peer: SocketAddr,
        peer_enc_pk: &box_::PublicKey,
        f: HolePunchFinsih,
    ) -> ::Res<()> {
        let our_addr = match self.state {
            State::ReadyToHolePunch(our_addr) => our_addr,
            ref x => {
                debug!("Improper state for this operation: {:?}", x);
                return Err(NatError::InvalidState);
            }
        };

        let l = new_reusably_bound_tcp_sockets(&our_addr, 1)?.0[0].listen(LISTENER_BACKLOG)?;
        let listener = TcpListener::from_listener(l, &our_addr)?;

        let mut children = HashSet::with_capacity(2);

        let weak = self.self_weak.clone();
        let handler = move |ifc: &mut Interface, poll: &Poll, token, res| {
            if let Some(mediator) = weak.upgrade() {
                mediator
                    .borrow_mut()
                    .handle_hole_punch(ifc, poll, token, res);
            }
        };
        let via = Via::Connect {
            our_addr: our_addr,
            peer_addr: peer,
        };
        if let Ok(child) = Puncher::start(ifc, poll, via, peer_enc_pk, Box::new(handler)) {
            let _ = children.insert(child);
        }

        let weak = self.self_weak.clone();
        let handler = move |ifc: &mut Interface, poll: &Poll, token, res| {
            if let Some(mediator) = weak.upgrade() {
                mediator
                    .borrow_mut()
                    .handle_hole_punch(ifc, poll, token, res);
            }
        };
        if let Ok(child) = Listener::start(ifc, poll, listener, peer_enc_pk, Box::new(handler)) {
            let _ = children.insert(child);
        }

        if children.is_empty() {
            debug!("Failure: Not even one valid child even managed to start hole punching");
            self.terminate(ifc, poll);
            return Err(NatError::TcpHolePunchFailed);
        }

        self.state = State::HolePunching {
            children: children,
            f: f,
        };

        Ok(())
    }

    fn handle_hole_punch(
        &mut self,
        ifc: &mut Interface,
        poll: &Poll,
        child: Token,
        res: ::Res<(TcpSock, Duration)>,
    ) {
        let r = match self.state {
            State::HolePunching {
                ref mut children,
                ref mut f,
            } => {
                let _ = children.remove(&child);
                if let Ok((sock, dur)) = res {
                    f(ifc, poll, Ok(TcpHolePunchInfo::new(sock, child, dur)));
                    Ok(true)
                } else if children.is_empty() {
                    f(ifc, poll, Err(NatError::TcpHolePunchFailed));
                    Err(NatError::TcpHolePunchFailed)
                } else {
                    Ok(false)
                }
            }
            ref x => {
                warn!(
                    "Logic Error in state book-keeping - Pls report this as a bug. Expected \
                     state: State::HolePunching ;; Found: {:?}",
                    x
                );
                Err(NatError::InvalidState)
            }
        };

        match r {
            Ok(true) => self.terminate(ifc, poll),
            Ok(false) => (),
            Err(e @ NatError::TcpHolePunchFailed) => {
                // This is reached only if children is empty, or we have removed the child that
                // called us. So no chance of borrow violation for children in terminate()
                debug!("Terminating due to: {:?}", e);
                self.terminate(ifc, poll);
            }
            // Don't call terminate as that can lead to child being borrowed twice
            Err(e) => debug!("Ignoring error in handle hole-punch: {:?}", e),
        }
    }

    fn terminate_children(ifc: &mut Interface, poll: &Poll, children: &mut HashSet<Token>) {
        for child in children.drain() {
            let child = match ifc.state(child) {
                Some(state) => state,
                None => continue,
            };

            child.borrow_mut().terminate(ifc, poll);
        }
    }
}

impl NatState for TcpHolePunchMediator {
    fn terminate(&mut self, ifc: &mut Interface, poll: &Poll) {
        match self.state {
            State::Rendezvous {
                ref mut children, ..
            }
            | State::HolePunching {
                ref mut children, ..
            } => {
                TcpHolePunchMediator::terminate_children(ifc, poll, children);
            }
            State::None | State::ReadyToHolePunch(_) => (),
        }
    }

    fn as_any(&mut self) -> &mut Any {
        self
    }
}