JFLib

netpkt/dhcp.rs#

Source: lib/netpkt/dhcp.rs

dhcp -- the Dynamic Host Configuration Protocol for IPv4 (RFC 2131; options RFC 2132, long
options RFC 3396, relay agent information RFC 3046)

  DhcpView::parse(b)     the fixed BOOTP part (236 bytes) and the magic cookie; fields
                         without copying; options() walks the options (pad skipped, end
                         stops; with option 52 the file and sname fields follow); opt(code)
                         the first instance of an option; msg_type()
  Dhcp { op, htype, hlen, hops, xid, secs, flags, ciaddr, yiaddr, siaddr, giaddr, chaddr,
         sname, file, opts: Vec<Opt> }
                         read(&view) (repeated options are joined, RFC 3396) / to_bytes(min)
                         (options in their order, long ones split, end, zero padding up to
                         `min` bytes: BOOTP_MIN for the 300 bytes most clients and servers
                         send) / new(op, xid, chaddr) / get(code) / set(opt) / mac()
  Opt                    MsgType, SubnetMask, Routers, DnsServers, HostName, DomainName,
                         RequestedIp, LeaseTime, ServerId, Params, Message, MaxSize, T1, T2,
                         ClientId, RelayInfo, BroadcastAddr, NtpServers, Other(code, bytes)
  MsgType                Discover .. Inform; name()
  option_name(code)      "Subnet Mask", "Router", ... for dissectors

An option whose length does not fit its type is kept as Other(code, bytes), so reading and
writing a message gives the same bytes.
/// the fixed BOOTP header
BOOTP_HDR: usize = 236
/// the fixed header and the magic cookie
DHCP_MIN: usize = 240
/// the size most clients and servers pad their messages to (RFC 1542)
BOOTP_MIN: usize = 300
MAGIC: [u8; 4] = [99, 130, 83, 99]
OP_REQUEST: u8 = 1
OP_REPLY: u8 = 2
/// the broadcast bit of the flags field
FLAG_BROADCAST: u16 = 0x8000
O_PAD: u8 = 0
O_SUBNET_MASK: u8 = 1
O_ROUTER: u8 = 3
O_DNS: u8 = 6
O_HOST_NAME: u8 = 12
O_DOMAIN_NAME: u8 = 15
O_BROADCAST: u8 = 28
O_NTP: u8 = 42
O_REQUESTED_IP: u8 = 50
O_LEASE_TIME: u8 = 51
O_OVERLOAD: u8 = 52
O_MSG_TYPE: u8 = 53
O_SERVER_ID: u8 = 54
O_PARAMS: u8 = 55
O_MESSAGE: u8 = 56
O_MAX_SIZE: u8 = 57
O_T1: u8 = 58
O_T2: u8 = 59
O_CLIENT_ID: u8 = 61
O_RELAY: u8 = 82
O_END: u8 = 255
/// relay agent sub-options (RFC 3046)
RELAY_CIRCUIT_ID: u8 = 1
RELAY_REMOTE_ID: u8 = 2
  • enum MsgType the DHCP message type (option 53)
    • Discover = 1
    • Offer = 2
    • Request = 3
    • Decline = 4
    • Ack = 5
    • Nak = 6
    • Release = 7
    • Inform = 8

impl MsgType

  • fn from_u8(v: u8) -> Option<MsgType>

  • fn name(self) -> &'static str "Discover", "Offer", ... (capture tools write "DHCP Discover")

  • fn option_name(code: u8) -> &'static str the name of an option for dissectors

  • struct DhcpView<'a> a received DHCP message

impl<'a> DhcpView<'a>

  • fn parse(b: &'a [u8]) -> Option<DhcpView<'a>> a BOOTP message with the DHCP magic cookie

  • fn bytes(&self) -> &'a [u8]

  • fn op(&self) -> u8

  • fn htype(&self) -> u8

  • fn hlen(&self) -> u8

  • fn hops(&self) -> u8

  • fn xid(&self) -> u32

  • fn secs(&self) -> u16

  • fn flags(&self) -> u16

  • fn broadcast(&self) -> bool

  • fn ciaddr(&self) -> Ipv4

  • fn yiaddr(&self) -> Ipv4

  • fn siaddr(&self) -> Ipv4

  • fn giaddr(&self) -> Ipv4

  • fn chaddr(&self) -> [u8; 16] the client hardware address field (16 bytes)

  • fn mac(&self) -> Option<Mac> the client's MAC (Ethernet: htype 1, hlen 6)

  • fn sname(&self) -> &'a [u8] the server host name field up to its zero

  • fn file(&self) -> &'a [u8] the boot file name field up to its zero

  • fn options(&self) -> Vec<(u8, &'a [u8], usize)> every option in order: (code, data, offset of the code byte); pad and end are left out

  • fn opt(&self, code: u8) -> Option<&'a [u8]> the data of the first instance of an option

  • fn msg_type(&self) -> Option<MsgType>

  • enum Opt an option with its value

    • MsgType(MsgType)
    • SubnetMask(Ipv4)
    • Routers(Vec<Ipv4>)
    • DnsServers(Vec<Ipv4>)
    • HostName(String)
    • DomainName(String)
    • BroadcastAddr(Ipv4)
    • NtpServers(Vec<Ipv4>)
    • RequestedIp(Ipv4)
    • LeaseTime(u32) -- seconds (0xFFFF_FFFF: infinite)
    • ServerId(Ipv4)
    • Params(Vec<u8>) -- option codes the client asks for
    • Message(String) -- a text a server adds to a NAK, or a client to a DECLINE
    • MaxSize(u16)
    • T1(u32) -- renewal time in seconds
    • T2(u32) -- rebinding time in seconds
    • ClientId(Vec<u8>) -- the type byte (1: Ethernet) and the identifier
    • RelayInfo(Vec<(u8, Vec<u8>)>) -- relay agent sub-options (code, data)
    • Other(u8, Vec<u8>)

impl Opt

  • fn decode(code: u8, d: &[u8]) -> Opt the option of code with data (Other when the data does not fit the type)

  • fn code(&self) -> u8

  • fn data(&self) -> Vec<u8> the data bytes of the option

  • struct Dhcp a DHCP message to read or write

    • op: u8
    • htype: u8
    • hlen: u8
    • hops: u8
    • xid: u32
    • secs: u16
    • flags: u16
    • ciaddr: Ipv4
    • yiaddr: Ipv4
    • siaddr: Ipv4
    • giaddr: Ipv4
    • chaddr: [u8; 16]
    • sname: Vec<u8>
    • file: Vec<u8>
    • opts: Vec<Opt> -- in the order they are written

impl Dhcp

  • fn new(op: u8, xid: u32, mac: Mac) -> Dhcp an empty message of an Ethernet client
  • fn read(v: &DhcpView) -> Dhcp a message as received; an option that occurs more than once is joined (RFC 3396); with option 52 the sname and file fields only carried options
  • fn parse(b: &[u8]) -> Option<Dhcp> parses a message (None: no BOOTP message with the DHCP cookie)
  • fn mac(&self) -> Mac the client's MAC (Ethernet)
  • fn broadcast(&self) -> bool
  • fn get(&self, code: u8) -> Option<&Opt>
  • fn set(&mut self, o: Opt) adds an option or replaces the one with the same code
  • fn remove(&mut self, code: u8)
  • fn msg_type(&self) -> Option<MsgType>
  • fn server_id(&self) -> Option<Ipv4>
  • fn requested_ip(&self) -> Option<Ipv4>
  • fn lease_time(&self) -> Option<u32>
  • fn subnet_mask(&self) -> Option<Ipv4>
  • fn routers(&self) -> Vec<Ipv4>
  • fn dns_servers(&self) -> Vec<Ipv4>
  • fn domain_name(&self) -> Option<&str>
  • fn host_name(&self) -> Option<&str>
  • fn t1(&self) -> Option<u32>
  • fn t2(&self) -> Option<u32>
  • fn client_id(&self) -> Option<&[u8]>
  • fn to_bytes(&self, min_len: usize) -> Vec<u8> the message as bytes, padded with zeros to at least min_len bytes

Source: docs/API.md in the JFLib repository.