hostfile 1.1.1

A rust crate for parsing /etc/hosts
Documentation
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
use std::fs::File;
use std::io::{BufRead, BufReader, Read, Seek, SeekFrom};
use std::net::{AddrParseError, IpAddr};
use std::path::{Path, PathBuf};
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) -> Result<(IpAddr, &str), AddrParseError> {
    let non_ip_char_idx = input.find(|c: char| c != '.' && c != ':' && !c.is_digit(16));
    let (ip, remainder) = input.split_at(non_ip_char_idx.unwrap_or(input.len()));
    Ok((ip.parse()?, remainder))
}

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

impl FromStr for HostEntry {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut input = s;
        input = input.trim_start();

        let ip =
            parse_ip(input).map_err(|err| format!("Couldn't parse a valid IP address: {err}"))?;
        input = ip.1;
        let ip = ip.0;

        match input.chars().next() {
            Some(' ') | Some('\t') => {}
            _ => {
                return Err("Expected whitespace after IP".to_string());
            }
        }
        input = input.trim_start();

        let mut names = Vec::new();
        for name in input.split_whitespace() {
            // Account for comments at the end of the line
            match name.chars().next() {
                Some('#') => break,
                Some(_) => {}
                None => unreachable!(),
            }
            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>, String> {
    if !path.exists() || !path.is_file() {
        return Err(format!(
            "File ({:?}) does not exist or is not a regular file",
            path
        ));
    }

    let mut file = File::open(path).map_err(|_x| format!("Could not open file ({:?})", path))?;
    let mut buf = vec![0; 3];
    let n = file
        .read(&mut buf)
        .map_err(|err| format!("Reading header failed {}", err))?;
    if n == 3 {
        // The file is at least 3 bytes, check if the file begins with a UTF-8 BOM. If not, reset
        // the file's position.
        if &buf != b"\xef\xbb\xbf" {
            file.seek(SeekFrom::Start(0))
                .map_err(|err| format!("Seek failed {}", err))?;
        }
    }

    let mut entries = Vec::new();

    let lines = BufReader::new(file).lines();
    let mut line_count = 1;
    for line in lines {
        let line = line.map_err(|err| format!("Error reading file at line {line_count}: {err}"))?;
        let line = line.trim_start();
        match line.chars().next() {
            // comment
            Some('#') => continue,
            // empty line
            None => continue,
            // valid line
            Some(_) => {
                entries.push(
                    line.parse().map_err(|err| {
                        format!("{err} at line {line_count} with content: '{line}'")
                    })?,
                );
                line_count += 1;
            }
        }
    }

    Ok(entries)
}

/// Parse system hostfile.
///
/// - `/etc/hosts` on Unix.
/// - `C:\Windows\system32\drivers\etc\hosts` on Windows.
pub fn parse_hostfile() -> Result<Vec<HostEntry>, String> {
    parse_file(&get_hostfile_path()?)
}

/// Get path to the system hostfile.
pub fn get_hostfile_path() -> Result<PathBuf, String> {
    #[cfg(not(windows))]
    {
        Ok(PathBuf::from("/etc/hosts"))
    }

    #[cfg(windows)]
    {
        // Implementation adapted from cargo's `home`.
        // See https://crates.io/crates/home
        use std::ffi::OsString;
        use std::os::windows::ffi::OsStringExt;
        use std::ptr::null_mut;
        use std::slice;
        use windows_sys::Win32::{
            Foundation::S_OK,
            System::Com::CoTaskMemFree,
            UI::Shell::{FOLDERID_System, SHGetKnownFolderPath, KF_FLAG_DONT_VERIFY},
        };

        extern "C" {
            fn wcslen(buf: *const u16) -> usize;
        }

        let mut ptr = null_mut::<u16>();
        let ret = unsafe {
            SHGetKnownFolderPath(
                &FOLDERID_System,
                KF_FLAG_DONT_VERIFY as u32,
                null_mut(),
                &mut ptr,
            )
        };

        match ret {
            S_OK => {
                let path_slice = unsafe { slice::from_raw_parts(ptr, wcslen(ptr)) };
                let os_str = OsString::from_wide(path_slice);
                unsafe { CoTaskMemFree(ptr.cast()) };
                let mut pathbuf = PathBuf::from(&os_str);
                pathbuf.push("drivers\\etc\\hosts");
                Ok(pathbuf)
            }
            _ => {
                // free any allocated memory even on failure (a null ptr is a no-op for `CoTaskMemFree`)
                unsafe { CoTaskMemFree(ptr.cast()) };
                Err(format!(
                    "Could not get path to Windows hosts file: {}",
                    std::io::Error::last_os_error(),
                ))
            }
        }
    }
}

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

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

    use super::*;

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

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

    #[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\
            \n\
            # Comments can really be anywhere\n\
            bad:dad::ded    multiple hostnames for address\n\
            1.1.1.1\ttabSeperatedHostname\n\
            1.1.1.2\t tabAndSpaceSeparatedHostName\n\
            \t1.1.1.3\t\t\tlineStartsWithTab\n\
              1.1.1.4 lineStartsWithSpace\n\
            1.1.1.5 skip_blank_line
        "
        )
        .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")
                    ),
                },
                HostEntry {
                    ip: IpAddr::V4(Ipv4Addr::new(1, 1, 1, 1)),
                    names: vec!(String::from("tabSeperatedHostname")),
                },
                HostEntry {
                    ip: IpAddr::V4(Ipv4Addr::new(1, 1, 1, 2)),
                    names: vec!(String::from("tabAndSpaceSeparatedHostName")),
                },
                HostEntry {
                    ip: IpAddr::V4(Ipv4Addr::new(1, 1, 1, 3)),
                    names: vec!(String::from("lineStartsWithTab")),
                },
                HostEntry {
                    ip: IpAddr::V4(Ipv4Addr::new(1, 1, 1, 4)),
                    names: vec!(String::from("lineStartsWithSpace")),
                },
                HostEntry {
                    ip: IpAddr::V4(Ipv4Addr::new(1, 1, 1, 5)),
                    names: vec!(String::from("skip_blank_line")),
                },
            ))
        );
    }

    #[test]
    fn test_parse_file_with_bom() {
        let temp_file = Temp::new_file().unwrap();
        let temp_path = temp_file.as_path();
        let mut file = File::create(temp_path).unwrap();
        file.write_all(b"\xef\xbb\xbf\n127.0.0.1       localhost\n")
            .expect("Could not write to temp file");
        parse_file(&temp_path).unwrap();
    }

    #[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("");
        assert_eq!(
            parse_file(&temp_path),
            Err(
                "Expected whitespace after IP at line 1 with content: '127.0.0.1localhost'"
                    .to_string()
            )
        );

        file.set_len(0).expect("Could not truncate file");
        file.seek(SeekFrom::Start(0)).expect("");
        write!(file, "127.0.0 localhost\n").expect("");
        assert_eq!(
            parse_file(&temp_path),
            Err("Couldn't parse a valid IP address: invalid IP address syntax at line 1 with content: '127.0.0 localhost'".to_string())
        );

        file.set_len(0).expect("");
        file.seek(SeekFrom::Start(0)).expect("");
        write!(file, "127.0.0 local\nhost\n").expect("");
        assert_eq!(
            parse_file(&temp_path),
            Err("Couldn't parse a valid IP address: invalid IP address syntax at line 1 with content: '127.0.0 local'".to_string())
        );

        file.set_len(0).expect("");
        file.seek(SeekFrom::Start(0)).expect("");
        write!(file, "127.0.0.1 localhost\nlocalhost myhost").expect("");
        assert_eq!(
            parse_file(&temp_path),
            Err("Couldn't parse a valid IP address: invalid IP address syntax at line 2 with content: 'localhost myhost'".to_string())
        );

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

    #[test]
    fn test_clone() {
        let host_entry = HostEntry {
            ip: IpAddr::V4(Ipv4Addr::new(192, 168, 42, 42)),
            names: vec![String::from("comp1"), String::from("computer1")],
        };
        let cloned = host_entry.clone();
        assert_eq!(host_entry, cloned)
    }

    #[test]
    fn test_get_hostfile_path() {
        let maybe_path = get_hostfile_path();
        assert!(maybe_path.is_ok());
        assert!(maybe_path.unwrap().exists());
    }

    // The next test only runs on GitHub Actions.
    //
    // Unix systems *typically* include localhost in /etc/hosts,
    // but we only assume that for GitHub Actions hostfile.
    //
    // In GitHub Actions, the Windows runnner includes an entry like
    // "10.1.0.85 <long-whatever>.cloudapp.net", localhost is commented.
    #[test_with::env(GITHUB_ACTIONS)]
    #[test]
    fn test_parse_hostfile() {
        let maybe_hostfile = parse_hostfile();
        assert!(maybe_hostfile.is_ok());
        let hostfile = maybe_hostfile.unwrap();
        assert!(!hostfile.is_empty());

        #[cfg(not(windows))]
        {
            let localhost = hostfile
                .iter()
                .find(|entry| entry.names.contains(&String::from("localhost")));
            assert!(localhost.is_some());
        }
    }
}