1

I would like to write a program that from an interface with a MAC address, sends a DHCP request and gets back the IP address, the gateway, the mask and the broadcast address from the DHCP server.

I used this implementation of a DHCP client, but it only gives the IP address and the gateway addresses.

I read about DHCP message format and I didn't find a mask or broadcast fileds. However, when using dhcpclient command line, I can see using ifconfig that the interface has a mask and broadcast addresses, as below:

arm@arm-desktop:~/dhclient wlp2s0
arm@arm-desktop:~/ifconfig wlp2s0
wlp2s0 Link encap:Ethernet  HWaddr 54:da:d0:15:4b:04  
      inet addr:153.179.154.55  Bcast:153.179.169.255  Mask:255.255.240.0
      inet6 addr: fe80::2037:c031:5db1:445c/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:113 errors:0 dropped:0 overruns:0 frame:0
      TX packets:56 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000 
      RX bytes:112130 (112.1 KB)  TX bytes:9520 (9.5 KB)

Could somebody tell me how this information is acquired?

ARM
  • 363
  • 1
  • 7
  • 18
  • You want to get the information of the remote machine or yours? – ARG Sep 12 '17 at 09:56
  • @ARG: I want to get information of my machine but using my program and not using command line tools like dhcpclient – ARM Sep 12 '17 at 10:05
  • @ARM you can use something like SIOCGIFADDR, http://www.microhowto.info/howto/get_the_ip_address_of_a_network_interface_in_c_using_siocgifaddr.html – ARG Sep 12 '17 at 10:07
  • @RustyX: I can't see in the struct dhcp a subnet mask or broadcast address fields – ARM Sep 12 '17 at 10:13
  • @ARG: You are talking about getting information of already configured network interface. But what I want is to get these information from the DHCP serer then using them to configure the interface – ARM Sep 12 '17 at 10:15

1 Answers1

3

See this question, it may be what you want.

I take it you are writing your own DHCP client that uses raw sockets or something else to put bytes directly on the wire.

You can read more about DHCP here or elsewhere if you google. As you can see, the subnet mask can be acquired from the DHCP server, as well as the broadcast address. This is done through DHCP options when comunicating with the DHCP server.

You then have to put the right bytes on the wire in the right order, and read the bytes coming back to be able to get the information you want.

Using the client you linked, you can expand the code to request and process more information from the DHCP server, like broadcast address and subnet mask. See the DHCP documentation for how to ask for information in the DHCP options field.