DHCP Option

80 views
Skip to first unread message

Karsten Kosmala

unread,
Aug 9, 2014, 3:36:44 PM8/9/14
to lib...@googlegroups.com
Hi,

I'm trying to craft a DHCP packet to test my DHCP server.
I recorded some dhcp-requests with a mac and my linux pc with wireshark and now wanted to replay these and want to know why the server sometimes isn't responding.
This is my code:

  PacketSender sender("enp0s25");
  NetworkInterface iface = NetworkInterface::default_interface();
  NetworkInterface::Info info = iface.addresses();

  IP pkt = IP("192.168.0.75") / UDP(67, 68) / DHCP();

  pkt.rfind_pdu<DHCP>().chaddr(info.hw_addr);
  pkt.rfind_pdu<DHCP>().type(DHCP::REQUEST);
  pkt.rfind_pdu<DHCP>().add_option(DHCP::DHCP_CLIENT_IDENTIFIER);
  pkt.rfind_pdu<DHCP>().end();

  sender.send(pkt);

Everything is fine and the server get the message, but now I want to add some data to the option DHCP_CLIENT_IDENTIFIER.
How can I do that?

Karsten

Matias Fontanini

unread,
Aug 9, 2014, 5:31:04 PM8/9/14
to lib...@googlegroups.com
DHCP::option is an instantiation of the PDUOption class template. So any of the constructors you see on its documentation can be used. In your code, you're implicitly calling the constructor that takes an identifier and optionally a data and data size parameters.

For example, you could do the following:

// Fill a vector with the data you want.
std::vector<uint8_t> data = ...; 
// Create a DHCP::option 
DHCP::option opt(DHCP::DHCP_CLIENT_IDENTIFIER, data.begin, data.end());
// Add the option to the DHCP PDU 
dhcp.add_option(opt);

That should work. Let me know if you have any troubles.

Karsten Kosmala

unread,
Aug 9, 2014, 7:14:11 PM8/9/14
to lib...@googlegroups.com
Hi Matias,

thanks for your answer.

What do I have to put here: std::vector<uint8_t> data = ???;
I want to transmit the hardware type (1 for ethernet) and my clients mac address.

Karsten

Matias Fontanini

unread,
Aug 9, 2014, 8:00:40 PM8/9/14
to lib...@googlegroups.com
I thought there was a wrapper implemented for this option, but apparently not. It's should be easy to generate the option's payload anyway (I haven't tested this, but it should work unless my understanding of the option's format is not correct):

std::vector<uint8_t> data(7);
data[0] = 1; // ethernet
Tins::HWAddress<6> mac_address = "00:01:02:03:04:05"; // your client's mac address
mac_address.copy(&data[1]); // copy this address into data
// now add the option like in the previous post
Reply all
Reply to author
Forward
0 new messages