程序=算法+数据结构,数据结构是了解DHCP服务器的代码的重要途径。通过分析DHCP服务器的主要数据结构,可以深入了解DHCP服务器的工作流程和实现原理。
dhcpMessage
定义在packet.h的struct dhcpMessage是DHCP报文的格式,包括了协议中所定义的所有DHCP报文所定义的内容,如下所示。
struct dhcpMessage {
u_int8_t op;
u_int8_t htype;
u_int8_t hlen;
u_int8_t hops;
u_int32_t xid;
u_int16_t secs;
u_int16_t flags;
u_int32_t ciaddr;
u_int32_t yiaddr;
u_int32_t siaddr;
u_int32_t giaddr;
u_int8_t chaddr[16];
u_int8_t sname[64];
u_int8_t file[128];
u_int32_t cookie;
u_int8_t options[308]; /* 312 - cookie */
};
Struct中的数据主要是根据RFC2131定义的,RFC中的定义如下,在32位机中采用4字节对齐。
FIELD | OCTETS | DESCRIPTION |
---|---|---|
op | 1 | Message op code / message type. 1 = BOOTREQUEST, 2 = BOOTREPLY |
htype | 1 | Hardware address type, see ARP section in “Assigned Numbers” RFC; e.g., ’1’ = 10mb ethernet. |
hlen | 1 | Hardware address length (e.g. ’6’ for 10mb ethernet). |
hops | 1 | Client sets to zero, optionally used by relay agents when booting via a relay agent. |
xid | 4 | Transaction ID, a random number chosen by the client, used by the client and server to associate messages and responses between a client and a server. |
secs | 2 | Filled in by client, seconds elapsed since client began address acquisition or renewal process. |
flags | 2 | Flags (see figure 2). |
ciaddr | 4 | Client IP address; only filled in if client is in BOUND, RENEW o |