Encapsulate all numeric option additions into options.c.
This commit is contained in:
parent
0229373378
commit
286548b754
30
ndhc/dhcp.c
30
ndhc/dhcp.c
@ -584,7 +584,7 @@ static struct dhcpmsg init_packet(char type, uint32_t xid)
|
|||||||
.options[0] = DCODE_END,
|
.options[0] = DCODE_END,
|
||||||
.xid = xid,
|
.xid = xid,
|
||||||
};
|
};
|
||||||
add_u8_option(&packet, DCODE_MSGTYPE, type);
|
add_option_msgtype(&packet, type);
|
||||||
memcpy(packet.chaddr, client_config.arp, 6);
|
memcpy(packet.chaddr, client_config.arp, 6);
|
||||||
add_option_clientid(&packet);
|
add_option_clientid(&packet);
|
||||||
return packet;
|
return packet;
|
||||||
@ -594,9 +594,8 @@ int send_discover(struct client_state_t *cs)
|
|||||||
{
|
{
|
||||||
struct dhcpmsg packet = init_packet(DHCPDISCOVER, cs->xid);
|
struct dhcpmsg packet = init_packet(DHCPDISCOVER, cs->xid);
|
||||||
if (cs->clientAddr)
|
if (cs->clientAddr)
|
||||||
add_u32_option(&packet, DCODE_REQIP, cs->clientAddr);
|
add_option_reqip(&packet, cs->clientAddr);
|
||||||
add_u16_option(&packet, DCODE_MAX_SIZE,
|
add_option_maxsize(&packet);
|
||||||
htons(sizeof(struct ip_udp_dhcp_packet)));
|
|
||||||
add_option_request_list(&packet);
|
add_option_request_list(&packet);
|
||||||
add_option_vendor(&packet);
|
add_option_vendor(&packet);
|
||||||
add_option_hostname(&packet);
|
add_option_hostname(&packet);
|
||||||
@ -608,10 +607,9 @@ int send_selecting(struct client_state_t *cs)
|
|||||||
{
|
{
|
||||||
char clibuf[INET_ADDRSTRLEN];
|
char clibuf[INET_ADDRSTRLEN];
|
||||||
struct dhcpmsg packet = init_packet(DHCPREQUEST, cs->xid);
|
struct dhcpmsg packet = init_packet(DHCPREQUEST, cs->xid);
|
||||||
add_u32_option(&packet, DCODE_REQIP, cs->clientAddr);
|
add_option_reqip(&packet, cs->clientAddr);
|
||||||
add_u32_option(&packet, DCODE_SERVER_ID, cs->serverAddr);
|
add_option_serverid(&packet, cs->serverAddr);
|
||||||
add_u16_option(&packet, DCODE_MAX_SIZE,
|
add_option_maxsize(&packet);
|
||||||
htons(sizeof(struct ip_udp_dhcp_packet)));
|
|
||||||
add_option_request_list(&packet);
|
add_option_request_list(&packet);
|
||||||
add_option_vendor(&packet);
|
add_option_vendor(&packet);
|
||||||
add_option_hostname(&packet);
|
add_option_hostname(&packet);
|
||||||
@ -625,8 +623,7 @@ int send_renew(struct client_state_t *cs)
|
|||||||
{
|
{
|
||||||
struct dhcpmsg packet = init_packet(DHCPREQUEST, cs->xid);
|
struct dhcpmsg packet = init_packet(DHCPREQUEST, cs->xid);
|
||||||
packet.ciaddr = cs->clientAddr;
|
packet.ciaddr = cs->clientAddr;
|
||||||
add_u16_option(&packet, DCODE_MAX_SIZE,
|
add_option_maxsize(&packet);
|
||||||
htons(sizeof(struct ip_udp_dhcp_packet)));
|
|
||||||
add_option_request_list(&packet);
|
add_option_request_list(&packet);
|
||||||
add_option_vendor(&packet);
|
add_option_vendor(&packet);
|
||||||
add_option_hostname(&packet);
|
add_option_hostname(&packet);
|
||||||
@ -638,9 +635,8 @@ int send_rebind(struct client_state_t *cs)
|
|||||||
{
|
{
|
||||||
struct dhcpmsg packet = init_packet(DHCPREQUEST, cs->xid);
|
struct dhcpmsg packet = init_packet(DHCPREQUEST, cs->xid);
|
||||||
packet.ciaddr = cs->clientAddr;
|
packet.ciaddr = cs->clientAddr;
|
||||||
add_u32_option(&packet, DCODE_REQIP, cs->clientAddr);
|
add_option_reqip(&packet, cs->clientAddr);
|
||||||
add_u16_option(&packet, DCODE_MAX_SIZE,
|
add_option_maxsize(&packet);
|
||||||
htons(sizeof(struct ip_udp_dhcp_packet)));
|
|
||||||
add_option_request_list(&packet);
|
add_option_request_list(&packet);
|
||||||
add_option_vendor(&packet);
|
add_option_vendor(&packet);
|
||||||
add_option_hostname(&packet);
|
add_option_hostname(&packet);
|
||||||
@ -651,8 +647,8 @@ int send_rebind(struct client_state_t *cs)
|
|||||||
int send_decline(struct client_state_t *cs, uint32_t server)
|
int send_decline(struct client_state_t *cs, uint32_t server)
|
||||||
{
|
{
|
||||||
struct dhcpmsg packet = init_packet(DHCPDECLINE, cs->xid);
|
struct dhcpmsg packet = init_packet(DHCPDECLINE, cs->xid);
|
||||||
add_u32_option(&packet, DCODE_REQIP, cs->clientAddr);
|
add_option_reqip(&packet, cs->clientAddr);
|
||||||
add_u32_option(&packet, DCODE_SERVER_ID, server);
|
add_option_serverid(&packet, server);
|
||||||
log_line("Sending a decline message...");
|
log_line("Sending a decline message...");
|
||||||
return send_dhcp_raw(&packet);
|
return send_dhcp_raw(&packet);
|
||||||
}
|
}
|
||||||
@ -661,8 +657,8 @@ int send_release(struct client_state_t *cs)
|
|||||||
{
|
{
|
||||||
struct dhcpmsg packet = init_packet(DHCPRELEASE, libc_random_u32());
|
struct dhcpmsg packet = init_packet(DHCPRELEASE, libc_random_u32());
|
||||||
packet.ciaddr = cs->clientAddr;
|
packet.ciaddr = cs->clientAddr;
|
||||||
add_u32_option(&packet, DCODE_REQIP, cs->clientAddr);
|
add_option_reqip(&packet, cs->clientAddr);
|
||||||
add_u32_option(&packet, DCODE_SERVER_ID, cs->serverAddr);
|
add_option_serverid(&packet, cs->serverAddr);
|
||||||
log_line("Sending a release message...");
|
log_line("Sending a release message...");
|
||||||
return send_dhcp_cooked(cs, &packet);
|
return send_dhcp_cooked(cs, &packet);
|
||||||
}
|
}
|
||||||
|
@ -258,7 +258,7 @@ static ssize_t add_option_check(struct dhcpmsg *packet, uint8_t code,
|
|||||||
return end;
|
return end;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t add_u8_option(struct dhcpmsg *packet, uint8_t code, uint8_t data)
|
static size_t add_u8_option(struct dhcpmsg *packet, uint8_t code, uint8_t data)
|
||||||
{
|
{
|
||||||
ssize_t end = add_option_check(packet, code, 1);
|
ssize_t end = add_option_check(packet, code, 1);
|
||||||
if (end < 0)
|
if (end < 0)
|
||||||
@ -271,7 +271,8 @@ size_t add_u8_option(struct dhcpmsg *packet, uint8_t code, uint8_t data)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Data should be in network byte order.
|
// Data should be in network byte order.
|
||||||
size_t add_u16_option(struct dhcpmsg *packet, uint8_t code, uint16_t data)
|
static size_t add_u16_option(struct dhcpmsg *packet, uint8_t code,
|
||||||
|
uint16_t data)
|
||||||
{
|
{
|
||||||
ssize_t end = add_option_check(packet, code, 2);
|
ssize_t end = add_option_check(packet, code, 2);
|
||||||
if (end < 0)
|
if (end < 0)
|
||||||
@ -286,7 +287,8 @@ size_t add_u16_option(struct dhcpmsg *packet, uint8_t code, uint16_t data)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Data should be in network byte order.
|
// Data should be in network byte order.
|
||||||
size_t add_u32_option(struct dhcpmsg *packet, uint8_t code, uint32_t data)
|
static size_t add_u32_option(struct dhcpmsg *packet, uint8_t code,
|
||||||
|
uint32_t data)
|
||||||
{
|
{
|
||||||
ssize_t end = add_option_check(packet, code, 4);
|
ssize_t end = add_option_check(packet, code, 4);
|
||||||
if (end < 0)
|
if (end < 0)
|
||||||
@ -314,3 +316,24 @@ size_t add_option_request_list(struct dhcpmsg *packet)
|
|||||||
return add_option_string(packet, DCODE_PARAM_REQ, (char *)reqdata, j);
|
return add_option_string(packet, DCODE_PARAM_REQ, (char *)reqdata, j);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void add_option_msgtype(struct dhcpmsg *packet, uint8_t type)
|
||||||
|
{
|
||||||
|
add_u8_option(packet, DCODE_MSGTYPE, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
void add_option_reqip(struct dhcpmsg *packet, uint32_t ip)
|
||||||
|
{
|
||||||
|
add_u32_option(packet, DCODE_REQIP, ip);
|
||||||
|
}
|
||||||
|
|
||||||
|
void add_option_maxsize(struct dhcpmsg *packet)
|
||||||
|
{
|
||||||
|
add_u16_option(packet, DCODE_MAX_SIZE,
|
||||||
|
htons(sizeof(struct ip_udp_dhcp_packet)));
|
||||||
|
}
|
||||||
|
|
||||||
|
void add_option_serverid(struct dhcpmsg *packet, uint32_t sid)
|
||||||
|
{
|
||||||
|
add_u32_option(packet, DCODE_SERVER_ID, sid);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -74,9 +74,10 @@ uint8_t *get_option_data(struct dhcpmsg *packet, int code, ssize_t *optlen);
|
|||||||
ssize_t get_end_option_idx(struct dhcpmsg *packet);
|
ssize_t get_end_option_idx(struct dhcpmsg *packet);
|
||||||
size_t add_option_string(struct dhcpmsg *packet, uint8_t code, char *str,
|
size_t add_option_string(struct dhcpmsg *packet, uint8_t code, char *str,
|
||||||
size_t slen);
|
size_t slen);
|
||||||
size_t add_u8_option(struct dhcpmsg *packet, uint8_t code, uint8_t data);
|
|
||||||
size_t add_u16_option(struct dhcpmsg *packet, uint8_t code, uint16_t data);
|
|
||||||
size_t add_u32_option(struct dhcpmsg *packet, uint8_t code, uint32_t data);
|
|
||||||
size_t add_option_request_list(struct dhcpmsg *packet);
|
size_t add_option_request_list(struct dhcpmsg *packet);
|
||||||
|
void add_option_msgtype(struct dhcpmsg *packet, uint8_t type);
|
||||||
|
void add_option_reqip(struct dhcpmsg *packet, uint32_t ip);
|
||||||
|
void add_option_maxsize(struct dhcpmsg *packet);
|
||||||
|
void add_option_serverid(struct dhcpmsg *packet, uint32_t sid);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user