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
use std::io;
use std::net::{IpAddr, Ipv4Addr, SocketAddr, ToSocketAddrs, UdpSocket};
use std::option;
use std::time::Duration;

use super::{MAX_NAME_LENGTH, PORT_360, PORT_CLASSIC};

const RESOLVE_TIMEOUT_MILLIS: u64 = 300;
const MAX_PACKET_LENGTH: usize = MAX_NAME_LENGTH + 2;

/// Describes an Xbox Development Kit found by a discover or resolve operation.
#[derive(Debug)]
pub struct Xbox {
    ip: Ipv4Addr,
    port: u16,
    name: String,
}

impl Xbox {
    pub fn socket_addr(&self) -> SocketAddr {
        SocketAddr::new(IpAddr::V4(self.ip), self.port)
    }
    pub fn ip(&self) -> Ipv4Addr { self.ip }
    pub fn port(&self) -> u16 { self.port }
    pub fn name(&self) -> &str { &self.name }
    pub fn is_360(&self) -> bool { self.port == PORT_360 }
    pub fn is_classic(&self) -> bool { self.port == PORT_CLASSIC }
}

impl ToSocketAddrs for Xbox {
    type Iter = option::IntoIter<SocketAddr>;
    fn to_socket_addrs(&self) -> io::Result<Self::Iter> {
        Ok(Some(self.socket_addr()).into_iter())
    }
}

fn parse_reply(data: &[u8], src: SocketAddr) -> Option<Xbox> {
    if data.len() < 3 || data[0] != 2 || data[1] == 0 {
        return None
    }
    if src.port() != PORT_360 && src.port() != PORT_CLASSIC {
        return None
    }
    Some(Xbox {
        ip: match src.ip() {
            IpAddr::V4(ip) => ip,
            _ => return None,
        },
        port: src.port(),
        name: match String::from_utf8(data[2..(data[1] as usize) + 2].into()) {
            Ok(s) => s,
            Err(_) => return None,
        },
    })
}

/// An iterator over `Xbox` instances returned from a discover operation.
pub struct Discover {
    socket: UdpSocket,
}

impl Iterator for Discover {
    type Item = Xbox;
    fn next(&mut self) -> Option<Self::Item> {
        let mut buf = [0; MAX_PACKET_LENGTH];
        loop {
            let (n, src) = match self.socket.recv_from(&mut buf) {
                Ok(x) => x,
                Err(_) => break,
            };
            if let Some(xbox) = parse_reply(&buf[..n], src) {
                return Some(xbox)
            }
        }
        None
    }
}

/// Discover active Xbox Development Kits on the local network.
pub fn discover() -> io::Result<Discover> {
    let ip = Ipv4Addr::new(255, 255, 255, 255);
    let pkt = [3, 0];
    let timeout = Some(Duration::from_millis(300));
    let socket = UdpSocket::bind((Ipv4Addr::new(0, 0, 0, 0), 0))?;
    socket.set_broadcast(true)?;
    socket.set_read_timeout(timeout)?;
    socket.set_write_timeout(timeout)?;
    socket.send_to(&pkt, (ip, PORT_360))?;
    socket.send_to(&pkt, (ip, PORT_CLASSIC))?;
    Ok(Discover { socket: socket })
}

/// Resolve the Xbox debug name or IP address specified by `host`
/// as an `Xbox` instance.
pub fn resolve(host: &str) -> io::Result<Option<Xbox>> {
    match host.parse() {
        Ok(ip) => resolve_ip(ip),
        _ => resolve_name(host),
    }
}

fn is_timeout(e: &io::Error) -> bool {
    e.kind() == io::ErrorKind::WouldBlock || e.kind() == io::ErrorKind::TimedOut
}

/// Resolve the IP address specified by `ip` as an `Xbox` instance.
pub fn resolve_ip(ip: Ipv4Addr) -> io::Result<Option<Xbox>> {
    let mut buf = [0; MAX_PACKET_LENGTH];
    buf[0] = 3;
    buf[1] = 0;
    let timeout = Some(Duration::from_millis(RESOLVE_TIMEOUT_MILLIS));
    let socket = UdpSocket::bind((Ipv4Addr::new(0, 0, 0, 0), 0))?;
    socket.set_read_timeout(timeout)?;
    socket.set_write_timeout(timeout)?;
    socket.send_to(&buf[..2], (ip, PORT_360))?;
    socket.send_to(&buf[..2], (ip, PORT_CLASSIC))?;
    loop {
        let (n, src) = match socket.recv_from(&mut buf) {
            Ok(x) => x,
            Err(ref e) if is_timeout(e) => break,
            Err(e) => return Err(e),
        };
        if let Some(xbox) = parse_reply(&buf[..n], src) {
            if xbox.ip == ip {
                return Ok(Some(xbox));
            }
        }
    }
    Ok(None)
}

/// Resolve the Xbox debug name specified by `name` as an `Xbox` instance.
pub fn resolve_name(name: &str) -> io::Result<Option<Xbox>> {
    if name.len() == 0 {
        return Ok(None)
    } else if name.len() > MAX_NAME_LENGTH {
        return Err(io::Error::new(
            io::ErrorKind::InvalidInput, "name is too long"))
    }

    let timeout = Some(Duration::from_millis(RESOLVE_TIMEOUT_MILLIS));
    let socket = UdpSocket::bind((Ipv4Addr::new(0, 0, 0, 0), 0))?;
    socket.set_broadcast(true)?;
    socket.set_read_timeout(timeout)?;
    socket.set_write_timeout(timeout)?;

    let mut buf = &mut [0; MAX_PACKET_LENGTH][..name.len()+2];
    buf[0] = 1;
    buf[1] = name.len() as u8;
    buf[2..].copy_from_slice(name.as_bytes());

    let ip = Ipv4Addr::new(255, 255, 255, 255);
    socket.send_to(&buf, (ip, PORT_360))?;
    socket.send_to(&buf, (ip, PORT_CLASSIC))?;

    loop {
        let (n, src) = match socket.recv_from(&mut buf) {
            Ok(x) => x,
            Err(ref e) if is_timeout(e) => break,
            Err(e) => return Err(e),
        };
        if let Some(xbox) = parse_reply(&buf[..n], src) {
            if xbox.name == name {
                return Ok(Some(xbox));
            }
        }
    }

    Ok(None)
}