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
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::net::IpAddr;
use std::path::Path;
use std::str::FromStr;

/**
 * Host file format:
 *   File:
 *     Line |
 *     Line newline File
 *
 *   Line:
 *     Comment | Entry
 *
 *   Comment:
 *     # .* newline
 *
 *   Entry:
 *     ws* ip ws+ Name (ws+ Names | $)
 *        (where ip is parsed according to std::net)
 *
 *   ws: space | tab
 *
 *   Name:
 *     [a-z.-]+
 *
 *   Names:
 *     Name ws* | Name ws+ Names
 */

fn parse_ip(input: &str, start_idx: usize) -> Result<(IpAddr, usize), &'static str> {
    let mut chars = input[start_idx..].chars();
    let mut end_idx = start_idx;

    loop {
        let c = chars.next();
        if c.is_none() {
            break;
        }

        let c = c.unwrap();
        if !c.is_digit(10) && c != '.' && !c.is_digit(16) && c != ':' {
            break;
        }

        end_idx += 1;
    }

    let ip = input[start_idx..end_idx].parse::<IpAddr>();
    if ip.is_err() {
        return Err("Couldn't parse a valid IP address");
    }
    Ok((ip.unwrap(), end_idx))
}

fn discard_ws(input: &str, start_idx: usize) -> usize {
    let mut chars = input[start_idx..].chars();
    let mut end_idx = start_idx;

    loop {
        let c = chars.next();
        if c.is_none() || !c.unwrap().is_whitespace() {
            break;
        }

        end_idx += 1;
    }

    end_idx
}

/// A struct representing a line from /etc/hosts that has a host on it
#[derive(Debug, PartialEq)]
pub struct HostEntry {
    pub ip: IpAddr,
    pub names: Vec<String>,
}

impl FromStr for HostEntry {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut pos = discard_ws(s, 0);

        let ip = parse_ip(s, pos);
        if let Err(msg) = ip {
            return Err(msg);
        }
        let ip = ip.unwrap();
        pos = ip.1;
        let ip = ip.0;

        let newpos = discard_ws(s, pos);
        if newpos == pos {
            return Err("Expected whitespace after IP");
        }
        pos = newpos;

        let mut names = Vec::new();
        for name in s[pos..].split_whitespace() {
            // Account for comments at the end of the line
            if let Some(c) = name.chars().next() {
                if c == '#' {
                    break;
                }
            } else {
                continue;
            }
            names.push(name.to_string());
        }

        Ok(HostEntry { ip, names })
    }
}

/// Parse a file using the format described in `man hosts(7)`
pub fn parse_file(path: &Path) -> Result<Vec<HostEntry>, &'static str> {
    if !path.exists() || !path.is_file() {
        return Err("File does not exist or is not a regular file");
    }

    let file = File::open(path);
    if file.is_err() {
        return Err("Could not open file");
    }
    let file = file.unwrap();

    let mut entries = Vec::new();

    let lines = BufReader::new(file).lines();
    for line in lines {
        if let Err(_) = line {
            return Err("Error reading file");
        }
        let line = line.unwrap();

        let start = discard_ws(&line, 0);
        let entryline = &line[start..];
        match entryline.chars().next() {
            Some(c) => {
                if c == '#' {
                    continue;
                }
            }
            // empty line
            None => {
                continue;
            }
        };

        match entryline.parse() {
            Ok(entry) => {
                entries.push(entry);
            }
            Err(msg) => {
                return Err(msg);
            }
        };
    }

    Ok(entries)
}

/// Parse /etc/hosts
pub fn parse_hostfile() -> Result<Vec<HostEntry>, &'static str> {
    parse_file(&Path::new("/etc/hosts"))
}

#[cfg(test)]
mod tests {
    extern crate mktemp;
    use mktemp::Temp;

    use std::io::Write;
    use std::net::{Ipv4Addr, Ipv6Addr};

    use super::*;

    #[test]
    fn parse_ipv4() {
        let input = "127.0.0.1";
        assert_eq!(
            parse_ip(input, 0),
            Ok((IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 9))
        );
    }

    #[test]
    fn parse_ipv6() {
        let input = "::1";
        assert_eq!(
            parse_ip(input, 0),
            Ok((IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), 3))
        );
    }

    #[test]
    fn test_discard_ws() {
        assert_eq!(discard_ws("    asdf", 0), 4);
        assert_eq!(discard_ws("    ", 0), 4);
        assert_eq!(discard_ws(".", 0), 0);
        assert_eq!(discard_ws("", 0), 0);
    }

    #[test]
    fn parse_entry() {
        assert_eq!(
            "127.0.0.1 localhost".parse(),
            Ok(HostEntry {
                ip: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
                names: vec!(String::from("localhost")),
            })
        );
    }

    #[test]
    fn parse_entry_multiple_names() {
        assert_eq!(
            "127.0.0.1 localhost home  ".parse(),
            Ok(HostEntry {
                ip: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
                names: vec!(String::from("localhost"), String::from("home")),
            })
        );
    }

    #[test]
    fn parse_entry_ipv6() {
        assert_eq!(
            "::1 localhost".parse(),
            Ok(HostEntry {
                ip: IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)),
                names: vec!(String::from("localhost")),
            })
        );
    }

    #[test]
    fn parse_entry_with_ws_and_comments() {
        assert_eq!(
            "    ::1 \tlocalhost # comment".parse(),
            Ok(HostEntry {
                ip: IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)),
                names: vec!(String::from("localhost")),
            })
        );
    }

    #[test]
    fn test_parse_file() {
        let temp_file = Temp::new_file().unwrap();
        let temp_path = temp_file.as_path();
        let mut file = File::create(temp_path).unwrap();

        write!(
            file,
            "\
            # This is a sample hosts file\n\
               \n# Sometimes hosts files can have wonky spacing
            127.0.0.1       localhost\n\
            ::1             localhost\n\
            255.255.255.255 broadcast\n\

            # Comments can really be anywhere\n\
            bad:dad::ded    multiple hostnames for address\n\
        "
        )
        .expect("Could not write to temp file");

        assert_eq!(
            parse_file(&temp_path),
            Ok(vec!(
                HostEntry {
                    ip: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
                    names: vec!(String::from("localhost")),
                },
                HostEntry {
                    ip: IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)),
                    names: vec!(String::from("localhost")),
                },
                HostEntry {
                    ip: IpAddr::V4(Ipv4Addr::new(255, 255, 255, 255)),
                    names: vec!(String::from("broadcast")),
                },
                HostEntry {
                    ip: IpAddr::V6(Ipv6Addr::new(0xbad, 0xdad, 0, 0, 0, 0, 0, 0xded)),
                    names: vec!(
                        String::from("multiple"),
                        String::from("hostnames"),
                        String::from("for"),
                        String::from("address")
                    ),
                },
            ))
        );
    }

    #[test]
    fn test_parse_file_errors() {
        let temp_file = Temp::new_file().unwrap();
        let temp_path = temp_file.as_path();
        let mut file = File::create(temp_path).unwrap();

        write!(
            file,
            "\
            127.0.0.1localhost\n\
        "
        )
        .expect("Could not write to temp file");
        assert_eq!(parse_file(&temp_path), Err("Expected whitespace after IP"));

        file.set_len(0).expect("Could not truncate file");
        write!(
            file,
            "\
            127.0.0 localhost\n\
        "
        )
        .expect("Could not write to temp file");
        assert_eq!(
            parse_file(&temp_path),
            Err("Couldn't parse a valid IP address")
        );

        file.set_len(0).expect("Could not truncate file");
        write!(
            file,
            "\
            127.0.0 local\n\
            host\n\
        "
        )
        .expect("Could not write to temp file");
        assert_eq!(
            parse_file(&temp_path),
            Err("Couldn't parse a valid IP address")
        );

        let temp_dir = Temp::new_dir().unwrap();
        let temp_dir_path = temp_dir.as_path();
        assert_eq!(
            parse_file(&temp_dir_path),
            Err("File does not exist or is not a regular file")
        );
    }
}