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
use std::io;
use std::net::{SocketAddr, ToSocketAddrs};
use std::path::{Path, PathBuf};
use std::string;
use std::str::FromStr;
use std::str::Split;
use std::vec;

use rustc_serialize::hex::{FromHex,FromHexError};

#[derive(Debug, PartialEq)]
pub enum UnescapeError {
    ShortEscapeSequence,
    EscapeNotUtf8,
    HexConversionError
}

impl From<FromHexError> for UnescapeError {
    fn from(_: FromHexError) -> Self {
        UnescapeError::HexConversionError
    }
}

impl From<string::FromUtf8Error> for UnescapeError {
    fn from(_: string::FromUtf8Error) -> Self {
        UnescapeError::EscapeNotUtf8
    }
}

fn dbus_unescape(buf: &[u8]) -> Result<Vec<u8>, UnescapeError> {
    let mut out = Vec::with_capacity(buf.len());
    let mut i = buf.iter();
    while let Some(c) = i.next() {
        if *c == b'%' {
            let c1 = *try!(i.next().ok_or(UnescapeError::ShortEscapeSequence));
            let c2 = *try!(i.next().ok_or(UnescapeError::ShortEscapeSequence));
            let x = try!(String::from_utf8(vec!(c1, c2)));
            out.push(*try!(x.from_hex()).get(0).unwrap());
        } else {
            out.push(*c);
        }
    }
    Ok(out)
}

fn dbus_unescape_str(s: &str) -> Result<String, UnescapeError> {
    let vec = try!(dbus_unescape(s.as_bytes()));
    String::from_utf8(vec).map_err(From::from)
}

#[derive(Debug, PartialEq)]
pub enum Error {
    UnescapeError(UnescapeError),
    BadTransportSeparator,
    MalformedKeyValue,
    UnknownTransport,
    UnknownOption,
    MissingOption,
    ConflictingOptions,
}

pub type ServerAddressError = (Error, String);

impl From<UnescapeError> for ServerAddressError {
    fn from(e: UnescapeError) -> Self {
        (Error::UnescapeError(e), "".to_string())
    }
}


/// Iterator over key value pairs of the form "key=val,key=val"
struct AddrKeyVals<'a> {
    str: Split<'a, char>,
}

impl<'a> AddrKeyVals<'a> {
    fn new(s: &'a str) -> Self {
        AddrKeyVals { str: s.split(',') }
    }

    fn get_next(&mut self) -> Option<&'a str> {
        loop {
            let kvs = self.str.next();
            if kvs.is_none() || kvs.unwrap() != "" {
                return kvs;
            }
        }
    }
}

impl<'a> Iterator for AddrKeyVals<'a> {
    type Item = Result<(String, String), ServerAddressError>;

    fn next(&mut self) -> Option<Self::Item> {
        let kvs = self.get_next();
        if kvs.is_none() {
            return None;
        }
        let mut keyval = kvs.unwrap().split('=');
        if keyval.clone().count() != 2 {
            return Some(Err((Error::MalformedKeyValue, kvs.unwrap().to_string())));
        }

        let key = dbus_unescape_str(keyval.next().unwrap());
        if let Err(e) = key {
            return Some(Err(From::from(e)));
        }
        let val = dbus_unescape_str(keyval.next().unwrap());
        if let Err(e) = val {
            return Some(Err(From::from(e)));
        }
        // Unwrap is ok because we just checked that there are two elements
        Some(Ok((key.unwrap(), val.unwrap())))
    }
}

/// A DBus Unix address
#[derive(Debug)]
pub struct UnixAddress {
    path: PathBuf,
}

impl<'a> UnixAddress {
    /// Returns the Unix path
    pub fn path(&'a self) -> &'a Path {
        self.path.as_path()
    }
}

impl FromStr for UnixAddress {
    type Err = ServerAddressError;

    /// Constructs a UnixAddress from a key=value option string
    fn from_str(opts: &str) -> Result<Self, ServerAddressError> {
        let keyvals = AddrKeyVals::new(opts);
        let mut path = None;
        let mut abs = false;
        for kv in keyvals {
            let kv = try!(kv);

            match kv.0.as_ref() {
                "path" | "abstract" => {
                    if path.is_none() {
                        path = Some(kv.1);
                    } else {
                        return Err((Error::ConflictingOptions,
                                    "Duplicate path/abstract specified".to_string()));
                    }
                },
                "guid" => {}, // Ignore for now
                _ => return Err((Error::UnknownOption, kv.0))
            }
            if kv.0 == "abstract" {
                abs = true;
            }
        }
        if path == None {
            Err((Error::MissingOption, "No path for unix socket".to_string()))
        } else {
            let mut path = path.unwrap();
            if abs {
                path = "\0".to_string() + &path;
            }
            Ok(UnixAddress { path: PathBuf::from(path) })
        }
    }
}

/// A DBus Tcp address
#[derive(Debug)]
pub struct TcpAddress {
    host: String,
    port: String,
}

impl ToSocketAddrs for TcpAddress {
    type Iter = vec::IntoIter<SocketAddr>;
    /// Returns the Tcp path
    fn to_socket_addrs(&self) -> io::Result<Self::Iter> {
        (self.host.clone() + ":" + &self.port).to_socket_addrs()
    }
}

impl FromStr for TcpAddress {
    type Err = ServerAddressError;

    /// Constructs a TcpAddress from a key=value option string
    fn from_str(opts: &str) -> Result<Self, ServerAddressError> {
        let mut host = None;
        let mut port = None;
        for kv in AddrKeyVals::new(opts) {
            let kv = try!(kv);

            match kv.0.as_ref() {
                "host" => {
                    if host.is_none() {
                        host = Some(kv.1);
                    } else {
                        return Err((Error::ConflictingOptions,
                                    "Duplicate host specified".to_string()));
                    }
                },
                "port" => {
                    if port.is_none() {
                        port = Some(kv.1);
                    } else {
                        return Err((Error::ConflictingOptions,
                                    "Duplicate port specified".to_string()));
                    }
                },
                "guid" => {}, // Ignore for now
                _ => return Err((Error::UnknownOption, kv.0))
            }
        }
        if host == None {
            Err((Error::MissingOption, "No host for tcp socket".to_string()))
        } else if port == None {
            Err((Error::MissingOption, "No port for tcp socket".to_string()))
        } else {
            Ok(TcpAddress { host: host.unwrap(), port: port.unwrap() })
        }
    }
}

#[derive(Debug)]
pub enum ServerAddress {
    Unix(UnixAddress),
    Tcp(TcpAddress),
}

impl FromStr for ServerAddress {
    type Err = ServerAddressError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut sp = s.split(':');
        if sp.clone().count() != 2 {
            return Err((Error::BadTransportSeparator, s.to_string()));
        }
        // Unwrap is ok because we just checked that there are two elements
        let transport = sp.next().unwrap();
        let opts = sp.next().unwrap();

        match transport {
            "unix" => Ok(ServerAddress::Unix(try!(UnixAddress::from_str(opts)))),
            "tcp" => Ok(ServerAddress::Tcp(try!(TcpAddress::from_str(opts)))),
            _ => Err((Error::UnknownTransport, transport.to_string())),
        }
    }
}

#[test]
fn test_unescape() {
    assert_eq!(dbus_unescape(b"hello").unwrap(), b"hello");
    assert_eq!(dbus_unescape(b"\\").unwrap(), b"\\");
    assert_eq!(dbus_unescape(b"%61").unwrap(), b"a");
    assert_eq!(dbus_unescape(b"%5c").unwrap(), b"\\");
    assert_eq!(dbus_unescape(b"%").unwrap_err(), UnescapeError::ShortEscapeSequence);
    assert_eq!(dbus_unescape(b"%1").unwrap_err(), UnescapeError::ShortEscapeSequence);
}

#[test]
fn test_key_vals() {
    let mut a = AddrKeyVals::new("one=two").map(Result::unwrap);
    assert_eq!(a.next().unwrap(), ("one".to_string(), "two".to_string()));
    assert_eq!(a.next(), None);

    let mut a = AddrKeyVals::new("foo=bar,").map(Result::unwrap);
    assert_eq!(a.next().unwrap(), ("foo".to_string(), "bar".to_string()));
    assert_eq!(a.next(), None);

    let mut a = AddrKeyVals::new("foo=bar,a=b").map(Result::unwrap);
    assert_eq!(a.next().unwrap(), ("foo".to_string(), "bar".to_string()));
    assert_eq!(a.next().unwrap(), ("a".to_string(), "b".to_string()));
    assert_eq!(a.next(), None);

    let mut a = AddrKeyVals::new("foobar,a=b");
    assert_eq!(a.next().unwrap().unwrap_err().0, Error::MalformedKeyValue);
}

#[test]
fn test_server_address() {
    assert_eq!(ServerAddress::from_str("unix").unwrap_err().0, Error::BadTransportSeparator);
    ServerAddress::from_str("unix:path=/var/run/dbus/system_bus_socket").unwrap();
    assert_eq!(ServerAddress::from_str("unix:path=/var/run/dbus/system_bus_socket,foo=bar").unwrap_err().0, Error::UnknownOption);
    assert_eq!(ServerAddress::from_str("unix:").unwrap_err().0, Error::MissingOption);
}