68 lines
1.6 KiB
C++
68 lines
1.6 KiB
C++
#include <string.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include "dhcp4.h"
|
|
|
|
namespace dhcp4 {
|
|
|
|
int make_message(void * buf,
|
|
uint32_t bootp_message_type,
|
|
uint32_t transaction_id,
|
|
uint8_t const * chaddr,
|
|
uint32_t dhcp_message_type)
|
|
{
|
|
memset(buf, 0, (sizeof (message)));
|
|
message * msg = reinterpret_cast<message *>(buf);
|
|
|
|
msg->op = bootp_message_type;
|
|
msg->htype = htype::ethernet;
|
|
msg->hlen = 6;
|
|
msg->xid = htonl(transaction_id);
|
|
memcpy(msg->chaddr, chaddr, 6);
|
|
|
|
int index = 0;
|
|
|
|
uint8_t * cookie = &msg->options[index];
|
|
index += 4;
|
|
cookie[0] = 0x63;
|
|
cookie[1] = 0x82;
|
|
cookie[2] = 0x53;
|
|
cookie[3] = 0x63;
|
|
|
|
uint8_t * message_type = &msg->options[index];
|
|
index += 2 + 1;
|
|
message_type[0] = option::message_type;
|
|
message_type[1] = 1; // length
|
|
message_type[2] = dhcp_message_type;
|
|
|
|
{
|
|
uint8_t * prl = &msg->options[index];
|
|
int length = 4;
|
|
index += 2 + length;
|
|
prl[0] = option::parameter_request_list;
|
|
prl[1] = length;
|
|
prl[2] = 1; // subnet mask
|
|
prl[3] = 3; // router
|
|
prl[4] = 6; // domain name server
|
|
prl[5] = 28; // broadcast address
|
|
}
|
|
|
|
if (0) {
|
|
uint8_t * message_size = &msg->options[index];
|
|
int length = 2;
|
|
index += 2 + length;
|
|
message_size[0] = option::maximum_message_size;
|
|
message_size[1] = length;
|
|
message_size[2] = 0x05;
|
|
message_size[3] = 0xc0;
|
|
}
|
|
|
|
uint8_t * end = &msg->options[index];
|
|
index += 1;
|
|
end[0] = 255;
|
|
|
|
return (sizeof (message)) + index;
|
|
}
|
|
}
|