pktgen 0.1.2

A modular network packet builder library
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//! DHCP (Dynamic Host Configuration Protocol) implementation.
//!
//! This module provides types and functionality for working with DHCP packets,
//! including message types, options, and packet construction.

use serde::{Deserialize, Serialize};
use crate::{PacketBuilder, PacketError, PacketHeader};
use crate::ethernet::MacAddress;
use crate::ip::Ipv4Address;

/// DHCP message types.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[repr(u8)]
pub enum MessageType {
    /// DHCP Discover message
    Discover = 1,
    /// DHCP Offer message
    Offer = 2,
    /// DHCP Request message
    Request = 3,
    /// DHCP Decline message
    Decline = 4,
    /// DHCP Acknowledge message
    Ack = 5,
    /// DHCP Not Acknowledge message
    Nak = 6,
    /// DHCP Release message
    Release = 7,
    /// DHCP Inform message
    Inform = 8,
}

/// DHCP option codes.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
#[repr(u8)]
pub enum OptionCode {
    /// Pad Option
    Pad = 0,
    /// Subnet Mask
    SubnetMask = 1,
    /// Router Option
    Router = 3,
    /// Domain Name Server Option
    DomainNameServer = 6,
    /// Host Name Option
    HostName = 12,
    /// Domain Name
    DomainName = 15,
    /// Interface MTU
    InterfaceMtu = 26,
    /// Broadcast Address
    BroadcastAddress = 28,
    /// Network Time Protocol Servers
    NtpServers = 42,
    /// Requested IP Address
    RequestedIpAddress = 50,
    /// IP Address Lease Time
    IpAddressLeaseTime = 51,
    /// Message Type
    MessageType = 53,
    /// Server Identifier
    ServerIdentifier = 54,
    /// Parameter Request List
    ParameterRequestList = 55,
    /// Maximum DHCP Message Size
    MaxDhcpMessageSize = 57,
    /// Renewal Time Value
    RenewalTimeValue = 58,
    /// Rebinding Time Value
    RebindingTimeValue = 59,
    /// Client Identifier
    ClientIdentifier = 61,
    /// End Option
    End = 255,
}

/// DHCP option structure.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DhcpOption {
    code: OptionCode,
    length: u8,
    data: Vec<u8>,
}

impl DhcpOption {
    /// Creates a new DHCP option.
    pub fn new(code: OptionCode, data: Vec<u8>) -> Self {
        Self {
            code,
            length: data.len() as u8,
            data,
        }
    }

    /// Creates a message type option.
    pub fn message_type(message_type: MessageType) -> Self {
        Self::new(OptionCode::MessageType, vec![message_type as u8])
    }

    /// Creates a requested IP address option.
    pub fn requested_ip_address(addr: Ipv4Address) -> Self {
        Self::new(OptionCode::RequestedIpAddress, addr.as_bytes().to_vec())
    }

    /// Creates a server identifier option.
    pub fn server_identifier(addr: Ipv4Address) -> Self {
        Self::new(OptionCode::ServerIdentifier, addr.as_bytes().to_vec())
    }

    /// Creates a client identifier option.
    pub fn client_identifier(hardware_type: u8, mac: MacAddress) -> Self {
        let mut data = Vec::with_capacity(7);
        data.push(hardware_type);
        data.extend_from_slice(mac.as_bytes());
        Self::new(OptionCode::ClientIdentifier, data)
    }

    /// Converts the option to its byte representation.
    pub fn as_bytes(&self) -> Vec<u8> {
        let mut bytes = Vec::with_capacity(2 + self.data.len());
        bytes.push(self.code as u8);
        bytes.push(self.length);
        bytes.extend_from_slice(&self.data);
        bytes
    }
}

/// DHCP header structure.
///
/// Contains the fields defined in the DHCP packet format:
/// - Op: Message op code / message type
/// - Htype: Hardware address type
/// - Hlen: Hardware address length
/// - Hops: Client sets to zero, optionally used by relay agents
/// - Xid: Transaction ID
/// - Secs: Seconds elapsed since client began address acquisition
/// - Flags: Flags
/// - Ciaddr: Client IP address
/// - Yiaddr: 'your' (client) IP address
/// - Siaddr: IP address of next server to use in bootstrap
/// - Giaddr: Relay agent IP address
/// - Chaddr: Client hardware address
/// - Options: Optional parameters field
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DhcpHeader {
    pub op: u8,
    pub htype: u8,
    pub hlen: u8,
    pub hops: u8,
    pub xid: u32,
    pub secs: u16,
    pub flags: u16,
    pub ciaddr: Ipv4Address,
    pub yiaddr: Ipv4Address,
    pub siaddr: Ipv4Address,
    pub giaddr: Ipv4Address,
    pub chaddr: MacAddress,
    pub options: Vec<DhcpOption>,
}

impl DhcpHeader {
    /// Creates a new DHCP header with the specified parameters.
    fn new(
        op: u8,
        xid: u32,
        ciaddr: Ipv4Address,
        yiaddr: Ipv4Address,
        siaddr: Ipv4Address,
        giaddr: Ipv4Address,
        chaddr: MacAddress,
        options: Vec<DhcpOption>,
    ) -> Self {
        Self {
            op,
            htype: 1, // Ethernet
            hlen: 6,  // MAC address length
            hops: 0,
            xid,
            secs: 0,
            flags: 0,
            ciaddr,
            yiaddr,
            siaddr,
            giaddr,
            chaddr,
            options,
        }
    }
}

/// Complete DHCP packet structure.
///
/// Contains the DHCP header and any additional options.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DhcpPacket {
    pub header: DhcpHeader,
}

/// Builder for constructing DHCP packets.
///
/// Provides a fluent interface for creating DHCP packets with proper
/// validation and error handling.
#[derive(Debug, Default)]
pub struct DhcpBuilder {
    op: Option<u8>,
    xid: Option<u32>,
    ciaddr: Option<Ipv4Address>,
    yiaddr: Option<Ipv4Address>,
    siaddr: Option<Ipv4Address>,
    giaddr: Option<Ipv4Address>,
    chaddr: Option<MacAddress>,
    options: Vec<DhcpOption>,
}

impl DhcpBuilder {
    /// Creates a new DHCP packet builder with default values.
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the operation code.
    pub fn op(mut self, op: u8) -> Self {
        self.op = Some(op);
        self
    }

    /// Sets the transaction ID.
    pub fn xid(mut self, xid: u32) -> Self {
        self.xid = Some(xid);
        self
    }

    /// Sets the client IP address.
    pub fn ciaddr(mut self, addr: Ipv4Address) -> Self {
        self.ciaddr = Some(addr);
        self
    }

    /// Sets the 'your' IP address.
    pub fn yiaddr(mut self, addr: Ipv4Address) -> Self {
        self.yiaddr = Some(addr);
        self
    }

    /// Sets the server IP address.
    pub fn siaddr(mut self, addr: Ipv4Address) -> Self {
        self.siaddr = Some(addr);
        self
    }

    /// Sets the relay agent IP address.
    pub fn giaddr(mut self, addr: Ipv4Address) -> Self {
        self.giaddr = Some(addr);
        self
    }

    /// Sets the client hardware address.
    pub fn chaddr(mut self, addr: MacAddress) -> Self {
        self.chaddr = Some(addr);
        self
    }

    /// Adds a DHCP option.
    pub fn add_option(mut self, option: DhcpOption) -> Self {
        self.options.push(option);
        self
    }

    /// Builds the DHCP packet.
    ///
    /// # Returns
    /// - `Ok(DhcpPacket)` - The constructed DHCP packet
    /// - `Err(PacketError)` - If any required fields are missing
    pub fn build(self) -> Result<DhcpPacket, PacketError> {
        let op = self.op.ok_or_else(|| 
            PacketError::InvalidFieldValue("Operation code not set".to_string()))?;
        let xid = self.xid.ok_or_else(|| 
            PacketError::InvalidFieldValue("Transaction ID not set".to_string()))?;
        let ciaddr = self.ciaddr.unwrap_or_else(|| Ipv4Address::new([0, 0, 0, 0]));
        let yiaddr = self.yiaddr.unwrap_or_else(|| Ipv4Address::new([0, 0, 0, 0]));
        let siaddr = self.siaddr.unwrap_or_else(|| Ipv4Address::new([0, 0, 0, 0]));
        let giaddr = self.giaddr.unwrap_or_else(|| Ipv4Address::new([0, 0, 0, 0]));
        let chaddr = self.chaddr.ok_or_else(|| 
            PacketError::InvalidFieldValue("Client hardware address not set".to_string()))?;

        let packet = DhcpPacket {
            header: DhcpHeader::new(
                op,
                xid,
                ciaddr,
                yiaddr,
                siaddr,
                giaddr,
                chaddr,
                self.options,
            ),
        };

        packet.validate()?;
        Ok(packet)
    }
}

impl DhcpPacket {
    /// Creates a new DHCP packet builder.
    pub fn builder() -> DhcpBuilder {
        DhcpBuilder::new()
    }

    /// Creates a new DHCP Discover packet.
    ///
    /// # Arguments
    /// * `xid` - Transaction ID
    /// * `chaddr` - Client hardware address
    pub fn discover(xid: u32, chaddr: MacAddress) -> Result<Self, PacketError> {
        DhcpBuilder::new()
            .op(1) // BOOTREQUEST
            .xid(xid)
            .chaddr(chaddr)
            .add_option(DhcpOption::message_type(MessageType::Discover))
            .add_option(DhcpOption::client_identifier(1, chaddr))
            .build()
    }

    /// Creates a new DHCP Request packet.
    ///
    /// # Arguments
    /// * `xid` - Transaction ID
    /// * `chaddr` - Client hardware address
    /// * `requested_ip` - Requested IP address
    /// * `server_id` - Server identifier
    pub fn request(
        xid: u32,
        chaddr: MacAddress,
        requested_ip: Ipv4Address,
        server_id: Ipv4Address,
    ) -> Result<Self, PacketError> {
        DhcpBuilder::new()
            .op(1) // BOOTREQUEST
            .xid(xid)
            .chaddr(chaddr)
            .add_option(DhcpOption::message_type(MessageType::Request))
            .add_option(DhcpOption::client_identifier(1, chaddr))
            .add_option(DhcpOption::requested_ip_address(requested_ip))
            .add_option(DhcpOption::server_identifier(server_id))
            .build()
    }
}

impl PacketHeader for DhcpHeader {
    fn header_length(&self) -> usize {
        let mut length = 240; // Fixed DHCP header size
        for option in &self.options {
            length += 2 + option.data.len(); // Code + Length + Data
        }
        length + 1 // End option
    }

    fn as_bytes(&self) -> Result<Vec<u8>, PacketError> {
        let mut bytes = Vec::with_capacity(self.header_length());
        
        // Fixed header fields
        bytes.push(self.op);
        bytes.push(self.htype);
        bytes.push(self.hlen);
        bytes.push(self.hops);
        bytes.extend_from_slice(&self.xid.to_be_bytes());
        bytes.extend_from_slice(&self.secs.to_be_bytes());
        bytes.extend_from_slice(&self.flags.to_be_bytes());
        bytes.extend_from_slice(self.ciaddr.as_bytes());
        bytes.extend_from_slice(self.yiaddr.as_bytes());
        bytes.extend_from_slice(self.siaddr.as_bytes());
        bytes.extend_from_slice(self.giaddr.as_bytes());
        bytes.extend_from_slice(self.chaddr.as_bytes());
        
        // Add padding to reach 236 bytes (16 bytes for chaddr + 64 bytes for sname + 128 bytes for file)
        bytes.extend(std::iter::repeat(0).take(236 - bytes.len()));
        
        // Magic cookie (required for DHCP)
        bytes.extend_from_slice(&[99, 130, 83, 99]);
        
        // Options
        for option in &self.options {
            bytes.extend_from_slice(&option.as_bytes());
        }
        
        // End option
        bytes.push(255);
        
        Ok(bytes)
    }
}

impl PacketBuilder for DhcpPacket {
    fn build(&self) -> Result<Vec<u8>, PacketError> {
        self.header.as_bytes()
    }

    fn length(&self) -> usize {
        self.header.header_length()
    }

    fn validate(&self) -> Result<(), PacketError> {
        // Validate message type option is present
        let has_message_type = self.header.options.iter().any(|opt| 
            opt.code == OptionCode::MessageType
        );
        
        if !has_message_type {
            return Err(PacketError::InvalidFieldValue(
                "DHCP message type option is required".to_string()
            ));
        }
        
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn create_test_addresses() -> (MacAddress, Ipv4Address) {
        let chaddr = MacAddress::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55]);
        let server_id = Ipv4Address::new([192, 168, 1, 1]);
        (chaddr, server_id)
    }

    #[test]
    fn test_dhcp_discover() {
        let (chaddr, _) = create_test_addresses();
        let xid = 0x12345678;
        
        let packet = DhcpPacket::discover(xid, chaddr).unwrap();
        
        assert_eq!(packet.header.op, 1);
        assert_eq!(packet.header.xid, xid);
        assert_eq!(packet.header.chaddr, chaddr);
        assert!(packet.validate().is_ok());
    }

    #[test]
    fn test_dhcp_request() {
        let (chaddr, server_id) = create_test_addresses();
        let xid = 0x12345678;
        let requested_ip = Ipv4Address::new([192, 168, 1, 100]);
        
        let packet = DhcpPacket::request(xid, chaddr, requested_ip, server_id).unwrap();
        
        assert_eq!(packet.header.op, 1);
        assert_eq!(packet.header.xid, xid);
        assert_eq!(packet.header.chaddr, chaddr);
        assert!(packet.validate().is_ok());
    }

    #[test]
    fn test_dhcp_builder() {
        let (chaddr, server_id) = create_test_addresses();
        
        let packet = DhcpPacket::builder()
            .op(1)
            .xid(0x12345678)
            .chaddr(chaddr)
            .add_option(DhcpOption::message_type(MessageType::Discover))
            .add_option(DhcpOption::server_identifier(server_id))
            .build()
            .unwrap();

        assert_eq!(packet.header.op, 1);
        assert_eq!(packet.header.chaddr, chaddr);
        assert!(packet.validate().is_ok());
    }

    #[test]
    fn test_invalid_dhcp_packet() {
        let (chaddr, _) = create_test_addresses();
        
        // Create packet without message type option
        let result = DhcpPacket::builder()
            .op(1)
            .xid(0x12345678)
            .chaddr(chaddr)
            .build();
            
        // The build should fail with the appropriate error
        assert!(result.is_err());
        match result {
            Err(PacketError::InvalidFieldValue(msg)) => {
                assert_eq!(msg, "DHCP message type option is required");
            }
            _ => panic!("Expected InvalidFieldValue error"),
        }
    }
}