2011-08-15 18:38:23 -05:00
|
|
|
import vec;
|
2011-08-15 18:54:02 -05:00
|
|
|
import uint;
|
|
|
|
|
2011-08-19 17:16:48 -05:00
|
|
|
tag ip_addr { ipv4(u8, u8, u8, u8); }
|
2011-08-15 18:54:02 -05:00
|
|
|
|
2011-09-02 17:34:58 -05:00
|
|
|
fn format_addr(ip: ip_addr) -> str {
|
2011-08-19 17:16:48 -05:00
|
|
|
alt ip {
|
2011-08-15 18:54:02 -05:00
|
|
|
ipv4(a, b, c, d) {
|
2011-09-01 20:49:10 -05:00
|
|
|
#fmt["%u.%u.%u.%u", a as uint, b as uint, c as uint, d as uint]
|
2011-08-15 18:54:02 -05:00
|
|
|
}
|
|
|
|
_ { fail "Unsupported address type"; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn parse_addr(ip: str) -> ip_addr {
|
|
|
|
let parts = vec::map({|s| uint::from_str(s) }, str::split(ip, "."[0]));
|
2011-08-15 18:38:23 -05:00
|
|
|
if vec::len(parts) != 4u { fail "Too many dots in IP address"; }
|
2011-08-15 18:54:02 -05:00
|
|
|
for i in parts { if i > 255u { fail "Invalid IP Address part."; } }
|
2011-08-19 17:16:48 -05:00
|
|
|
ipv4(parts[0] as u8, parts[1] as u8, parts[2] as u8, parts[3] as u8)
|
2011-08-15 18:54:02 -05:00
|
|
|
}
|