about summary refs log tree commit diff
path: root/src/libstd/net.rs
blob: ba00ae649333cac93d8e3526d73624f5fefc1f22 (plain)
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
/*
Module: net
*/

import vec;
import uint;

/* Section: Types */

/*
Tag: ip_addr

An IP address
*/
tag ip_addr {
    /*
    Variant: ipv4

    An IPv4 address
    */
    ipv4(u8, u8, u8, u8);
}

/* Section: Operations */

/*
Function: format_addr

Convert an <ip_addr> to a str
*/
fn format_addr(ip: ip_addr) -> str {
    alt ip {
      ipv4(a, b, c, d) {
        #fmt["%u.%u.%u.%u", a as uint, b as uint, c as uint, d as uint]
      }
      _ { fail "Unsupported address type"; }
    }
}

/*
Function: parse_addr

Convert a str to <ip_addr>

Converts a string of the format "x.x.x.x" into an ip_addr tag.

Failure:

String must be a valid IPv4 address
*/
fn parse_addr(ip: str) -> ip_addr {
    let parts = vec::map(str::split(ip, "."[0]), {|s| uint::from_str(s) });
    if vec::len(parts) != 4u { fail "Too many dots in IP address"; }
    for i in parts { if i > 255u { fail "Invalid IP Address part."; } }
    ipv4(parts[0] as u8, parts[1] as u8, parts[2] as u8, parts[3] as u8)
}

#[test]
fn test_format_ip() {
    assert (net::format_addr(net::ipv4(127u8, 0u8, 0u8, 1u8)) == "127.0.0.1")
}

#[test]
fn test_parse_ip() {
    assert (net::parse_addr("127.0.0.1") == net::ipv4(127u8, 0u8, 0u8, 1u8));
}