DHCP报文格式
struct dhcp_packet {
uint8_t op; // 报文类型,1表示请求报文,2表示回应报文
/*
* DHCP 客户端的 MAC 地址类型。MAC 地址类型其实是指明网络类型,htype 值为 1 时表示为最
* 常见的以太网 MAC 地址类型。
*/
uint8_t htype;
uint8_t hlen; // 硬件地址长度。以太网 MAC 地址长度为 6 个字节,即 hlen 值为 6。
/*
* 跳数,DHCP 报文经过的中继数量。每经过一个路由器,该字段就会增加 1。如果没有经过路由器,
* 则值为 0(同一网内)。
*/
uint8_t hops;
uint32_t xid; // 事务 ID,唯一
uint16_t secs; // 从获取到 IP 地址或者续约过程开始到现在所过去的时间
uint16_t flags; // 0 表示采用单播发送方式,1 表示采用广播发送方式。
#define BROADCAST_FLAG 0x8000 // 需要广播回复
uint32_t ciaddr; // DHCP 客户端的 IP 地址
uint32_t yiaddr; // DHCP 服务器分配给客户端的 IP 地址
//为 DHCP 客户端分配 IP 地址等信息的其他 DHCP 服务器 IP 地址
uint32_t siaddr_nip;
// 转发代理(网关)IP 地址,DHCP 客户端发出请求报文后经过的第一个 DHCP 中继的 IP 地址
uint32_t gateway_nip;
uint8_t chaddr[16]; // DHCP 客户端的 MAC 地址。
uint8_t sname[64]; // 为客户端分配 IP 地址的服务器名称(DNS 域名格式)
uint8_t file[128]; //DHCP 服务器为 DHCP 客户端指定的启动配置文件名称及路径信息。
uint32_t cookie; /* DHCP magic bytes: 99,130,83,99 decimal */
// 可选选项,格式为“代码+长度+数据”。
uint8_t options[DHCP_OPTIONS_BUFSIZE + CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS];
};
struct client_data_t {
uint8_t client_mac[6]; // MAC地址
IF_FEATURE_UDHCP_PORT(uint16_t port;)
int ifindex; // 要使用的接口的索引号
uint32_t xid;
uint8_t opt_mask[256 / 8]; // 要发送的选项的位掩码
const char *interface; // 要使用的接口的名称
char *pidfile; // 可选存储进程ID
const char *script; // 在dhcp事件中运行的用户脚本
struct option_set *options; // 要发送到服务器的DHCP选项列表
llist_t *envp; // 用于环境变量的DHCP选项列表
unsigned first_secs;
unsigned last_secs;
int sockfd;
smallint listen_mode;
smallint state;
} FIX_ALIASING;
格外选项格式
struct dhcp_optflag {
uint8_t flags;
uint8_t code;
};
struct dyn_lease {
/* 租约到期时的Unix时间。按主机顺序保存在内存中。写入文件时,转换为网络顺序并进行调整(减去当
* 前时间)。leasetime_t uint32_t
*/
leasetime_t expires;
/* "nip": IP in network order */
uint32_t lease_nip;
/* MAC地址 */
uint8_t lease_mac[6];
char hostname[20];
uint8_t pad[2];
/* total size is a multiply of 4 */
} PACKED;
struct dhcp_scan_state {
int overload;
int rem;
uint8_t *optionptr;
};