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
//! 4.1.3.  Address Literals (RFC 5321)

use crate::parse::command::Ldh_str;
use abnf_core::streaming::is_DIGIT;
use nom::{
    branch::alt,
    bytes::streaming::{tag, tag_no_case, take_while1, take_while_m_n},
    character::is_hex_digit,
    combinator::{opt, recognize},
    multi::{count, many_m_n},
    sequence::tuple,
    IResult,
};

/// IPv4-address-literal = Snum 3("."  Snum)
pub fn IPv4_address_literal(input: &[u8]) -> IResult<&[u8], &[u8]> {
    let parser = tuple((Snum, count(tuple((tag(b"."), Snum)), 3)));

    let (remaining, parsed) = recognize(parser)(input)?;

    Ok((remaining, parsed))
}

/// IPv6-address-literal = "IPv6:" IPv6-addr
pub fn IPv6_address_literal(input: &[u8]) -> IResult<&[u8], &[u8]> {
    let parser = tuple((tag_no_case(b"IPv6:"), IPv6_addr));

    let (remaining, parsed) = recognize(parser)(input)?;

    Ok((remaining, parsed))
}

/// General-address-literal = Standardized-tag ":" 1*dcontent
pub fn General_address_literal(input: &[u8]) -> IResult<&[u8], &[u8]> {
    let parser = tuple((Standardized_tag, tag(b":"), take_while1(is_dcontent)));

    let (remaining, parsed) = recognize(parser)(input)?;

    Ok((remaining, parsed))
}

/// Standardized-tag MUST be specified in a Standards-Track RFC and registered with IANA
///
/// Standardized-tag = Ldh-str
pub fn Standardized_tag(input: &[u8]) -> IResult<&[u8], &[u8]> {
    let parser = Ldh_str;

    let (remaining, parsed) = recognize(parser)(input)?;

    Ok((remaining, parsed))
}

/// Printable US-ASCII excl. "[", "\", "]"
///
/// dcontent = %d33-90 / %d94-126
pub fn is_dcontent(byte: u8) -> bool {
    matches!(byte, 33..=90 | 94..=126)
}

/// Representing a decimal integer value in the range 0 through 255
///
/// Snum = 1*3DIGIT
pub fn Snum(input: &[u8]) -> IResult<&[u8], &[u8]> {
    let parser = take_while_m_n(1, 3, is_DIGIT);

    let (remaining, parsed) = recognize(parser)(input)?;

    Ok((remaining, parsed))
}

/// IPv6-addr = IPv6-full / IPv6-comp / IPv6v4-full / IPv6v4-comp
pub fn IPv6_addr(input: &[u8]) -> IResult<&[u8], &[u8]> {
    let parser = alt((IPv6_full, IPv6_comp, IPv6v4_full, IPv6v4_comp));

    let (remaining, parsed) = recognize(parser)(input)?;

    Ok((remaining, parsed))
}

/// IPv6-hex = 1*4HEXDIG
pub fn IPv6_hex(input: &[u8]) -> IResult<&[u8], &[u8]> {
    let parser = take_while_m_n(1, 4, is_hex_digit);

    let (remaining, parsed) = recognize(parser)(input)?;

    Ok((remaining, parsed))
}

/// IPv6-full = IPv6-hex 7(":" IPv6-hex)
pub fn IPv6_full(input: &[u8]) -> IResult<&[u8], &[u8]> {
    let parser = tuple((IPv6_hex, count(tuple((tag(b":"), IPv6_hex)), 7)));

    let (remaining, parsed) = recognize(parser)(input)?;

    Ok((remaining, parsed))
}

/// The "::" represents at least 2 16-bit groups of zeros.
/// No more than 6 groups in addition to the "::" may be present.
///
/// IPv6-comp = [IPv6-hex *5(":" IPv6-hex)] "::" [IPv6-hex *5(":" IPv6-hex)]
pub fn IPv6_comp(input: &[u8]) -> IResult<&[u8], &[u8]> {
    let parser = tuple((
        opt(tuple((
            IPv6_hex,
            many_m_n(0, 5, tuple((tag(b":"), IPv6_hex))),
        ))),
        tag(b"::"),
        opt(tuple((
            IPv6_hex,
            many_m_n(0, 5, tuple((tag(b":"), IPv6_hex))),
        ))),
    ));

    let (remaining, parsed) = recognize(parser)(input)?;

    Ok((remaining, parsed))
}

/// IPv6v4-full = IPv6-hex 5(":" IPv6-hex) ":" IPv4-address-literal
pub fn IPv6v4_full(input: &[u8]) -> IResult<&[u8], &[u8]> {
    let parser = tuple((
        IPv6_hex,
        count(tuple((tag(b":"), IPv6_hex)), 5),
        tag(b":"),
        IPv4_address_literal,
    ));

    let (remaining, parsed) = recognize(parser)(input)?;

    Ok((remaining, parsed))
}

/// The "::" represents at least 2 16-bit groups of zeros.
/// No more than 4 groups in addition to the "::" and IPv4-address-literal may be present.
///
/// IPv6v4-comp = [IPv6-hex *3(":" IPv6-hex)] "::"
///               [IPv6-hex *3(":" IPv6-hex) ":"]
///               IPv4-address-literal
pub fn IPv6v4_comp(input: &[u8]) -> IResult<&[u8], &[u8]> {
    let parser = tuple((
        opt(tuple((
            IPv6_hex,
            many_m_n(0, 3, tuple((tag(b":"), IPv6_hex))),
        ))),
        tag(b"::"),
        opt(tuple((
            IPv6_hex,
            many_m_n(0, 3, tuple((tag(b":"), IPv6_hex))),
            tag(b":"),
        ))),
        IPv4_address_literal,
    ));

    let (remaining, parsed) = recognize(parser)(input)?;

    Ok((remaining, parsed))
}