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 MsgTypethe DHCP message type (option 53)Discover = 1Offer = 2Request = 3Decline = 4Ack = 5Nak = 6Release = 7Inform = 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 strthe name of an option for dissectorsstruct 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 cookiefn bytes(&self) -> &'a [u8]fn op(&self) -> u8fn htype(&self) -> u8fn hlen(&self) -> u8fn hops(&self) -> u8fn xid(&self) -> u32fn secs(&self) -> u16fn flags(&self) -> u16fn broadcast(&self) -> boolfn ciaddr(&self) -> Ipv4fn yiaddr(&self) -> Ipv4fn siaddr(&self) -> Ipv4fn giaddr(&self) -> Ipv4fn 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 zerofn file(&self) -> &'a [u8]the boot file name field up to its zerofn options(&self) -> Vec<(u8, &'a [u8], usize)>every option in order: (code, data, offset of the code byte); pad and end are left outfn opt(&self, code: u8) -> Option<&'a [u8]>the data of the first instance of an optionfn msg_type(&self) -> Option<MsgType>enum Optan option with its valueMsgType(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 forMessage(String)-- a text a server adds to a NAK, or a client to a DECLINEMaxSize(u16)T1(u32)-- renewal time in secondsT2(u32)-- rebinding time in secondsClientId(Vec<u8>)-- the type byte (1: Ethernet) and the identifierRelayInfo(Vec<(u8, Vec<u8>)>)-- relay agent sub-options (code, data)Other(u8, Vec<u8>)
impl Opt
fn decode(code: u8, d: &[u8]) -> Optthe option ofcodewithdata(Other when the data does not fit the type)fn code(&self) -> u8fn data(&self) -> Vec<u8>the data bytes of the optionstruct Dhcpa DHCP message to read or writeop: u8htype: u8hlen: u8hops: u8xid: u32secs: u16flags: u16ciaddr: Ipv4yiaddr: Ipv4siaddr: Ipv4giaddr: Ipv4chaddr: [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) -> Dhcpan empty message of an Ethernet clientfn read(v: &DhcpView) -> Dhcpa message as received; an option that occurs more than once is joined (RFC 3396); with option 52 the sname and file fields only carried optionsfn parse(b: &[u8]) -> Option<Dhcp>parses a message (None: no BOOTP message with the DHCP cookie)fn mac(&self) -> Macthe client's MAC (Ethernet)fn broadcast(&self) -> boolfn get(&self, code: u8) -> Option<&Opt>fn set(&mut self, o: Opt)adds an option or replaces the one with the same codefn 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 leastmin_lenbytes