summaryrefslogtreecommitdiff
path: root/src/protocol/ipv4.rs
blob: 79e7d9746d44f75f76a4d0eef84df1b063b661e7 (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
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
use crate::protocol::ip::{self, IPProtocol};
use nom::bits;
use nom::error::Error;
use nom::number;
use nom::sequence;
use nom::IResult;
use std::net::Ipv4Addr;

/******************************************************************************
 * Struct
 ******************************************************************************/

/*
 * Internet Header Format
 *
 *   0                   1                   2                   3
 *   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *  |Version|  IHL  |Type of Service|          Total Length         |
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *  |         Identification        |Flags|      Fragment Offset    |
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *  |  Time to Live |    Protocol   |         Header Checksum       |
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *  |                       Source Address                          |
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *  |                    Destination Address                        |
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *  |                    Options                    |    Padding    |
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct IPv4Header {
    pub version: u8, // 4 bit
    pub ihl: u8,     // 4 bit
    pub tos: u8,
    pub length: u16,
    pub id: u16,
    pub flags: u8,        //  3 bit
    pub frag_offset: u16, // 13 bit
    pub ttl: u8,
    pub protocol: IPProtocol,
    pub checksum: u16,
    pub src_addr: Ipv4Addr,
    pub dst_addr: Ipv4Addr,
}

/******************************************************************************
 * Parse
 ******************************************************************************/

fn parse_flag_and_frag_offset(input: &[u8]) -> IResult<&[u8], (u8, u16)> {
    bits::bits::<_, _, Error<_>, _, _>(sequence::pair(
        bits::streaming::take(3u8),
        bits::streaming::take(13u16),
    ))(input)
}

fn parse_version_and_header_length(input: &[u8]) -> IResult<&[u8], (u8, u8)> {
    bits::bits::<_, _, Error<_>, _, _>(sequence::pair(
        bits::streaming::take(4u8),
        bits::streaming::take(4u8),
    ))(input)
}

fn parse_ipv4_address(input: &[u8]) -> IResult<&[u8], Ipv4Addr> {
    let (input, ipv4) = nom::bytes::streaming::take(4u8)(input)?;

    Ok((input, Ipv4Addr::from(<[u8; 4]>::try_from(ipv4).unwrap())))
}

pub fn parse_ipv4(input: &[u8]) -> IResult<&[u8], IPv4Header> {
    let (input, verihl) = parse_version_and_header_length(input)?;
    let (input, tos) = number::streaming::be_u8(input)?;
    let (input, length) = number::streaming::be_u16(input)?;
    let (input, id) = number::streaming::be_u16(input)?;
    let (input, flag_frag_offset) = parse_flag_and_frag_offset(input)?;
    let (input, ttl) = number::streaming::be_u8(input)?;
    let (input, protocol) = ip::protocol(input)?;
    let (input, checksum) = number::streaming::be_u16(input)?;
    let (input, src_addr) = parse_ipv4_address(input)?;
    let (input, dst_addr) = parse_ipv4_address(input)?;

    Ok((
        input,
        IPv4Header {
            version: verihl.0,
            ihl: verihl.1,
            tos,
            length,
            id,
            flags: flag_frag_offset.0,
            frag_offset: flag_frag_offset.1,
            ttl,
            protocol,
            checksum,
            src_addr,
            dst_addr,
        },
    ))
}

/******************************************************************************
 * TEST
 ******************************************************************************/

#[cfg(test)]
mod tests {
    use super::parse_ipv4;
    use super::IPv4Header;
    use crate::ip::IPProtocol;
    use std::net::Ipv4Addr;

    const LAST_SLICE: &'static [u8] = &[0xff];

    #[test]
    fn parse_ipv4_works() {
        /*
         * Internet Protocol Version 4, Src: 192.168.0.100, Dst: 14.215.177.38
         *     0100 .... = Version: 4
         *     .... 0101 = Header Length: 20 bytes (5)
         *     Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
         *     Total Length: 64
         *     Identification: 0x0000 (0)
         *     Flags: 0x40, Don't fragment
         *         0... .... = Reserved bit: Not set
         *         .1.. .... = Don't fragment: Set
         *         ..0. .... = More fragments: Not set
         *     ...0 0000 0000 0000 = Fragment Offset: 0
         *     Time to Live: 64
         *     Protocol: TCP (6)
         *     Header Checksum: 0xb9ae [correct]
         *     [Header checksum status: Good]
         *     [Calculated Checksum: 0xb9ae]
         *     Source Address: 192.168.0.100
         *     Destination Address: 14.215.177.38
         */

        let bytes = [
            0x45, /* Version and Header length */
            0x00, /* Differentiated Services Field */
            0x01, 0xda, /* Total Length */
            0x00, 0x00, /* Identification */
            0x40, 0x00, /* Flags and Fragment Offset */
            0x40, /* Time to Live */
            0x06, /* Protocol */
            0xb8, 0x14, /* Header Checksum */
            0xc0, 0xa8, 0x00, 0x64, /* Source Address */
            0x0e, 0xd7, 0xb1, 0x26, /* Destination Address */
            0xff, /* Payload */
        ];

        let expectation = IPv4Header {
            version: 4,
            ihl: 5,
            tos: 0,
            length: 474,
            id: 0x0000,
            flags: 0x2,
            frag_offset: 0,
            ttl: 64,
            protocol: IPProtocol::TCP,
            checksum: 0xb814,
            src_addr: Ipv4Addr::new(192, 168, 0, 100),
            dst_addr: Ipv4Addr::new(14, 215, 177, 38),
        };

        assert_eq!(parse_ipv4(&bytes), Ok((LAST_SLICE, expectation)));
    }
}