1
我想编写一个程序,该程序从具有MAC地址的接口发送DHCP请求并获取IP地址,网关,掩码和广播来自DHCP服务器的地址。使用DHCP获取IP地址,网关,掩码和广播信息
我使用DHCP客户端的this implementation,但只给出了IP地址和网关地址。
我读了关于DHCP消息格式,我没有找到掩码或广播文件。但是,使用dhcpclient命令行的时候,我可以看到用ifconfig,该接口具有屏蔽和广播地址,如下图所示:
[email protected]:~/dhclient wlp2s0
[email protected]:~/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)
有人能告诉我这个信息是如何获得的?
你想获得远程机器或你的信息? – ARG
@ARG:我想获取我的机器的信息,但使用我的程序,而不是使用命令行工具,如dhcpclient – ARM
@ARM,您可以使用像SIOCGIFADDR,http://www.microhowto.info/howto/get_the_ip_address_of_a_network_interface_in_c_using_siocgifaddr.html – ARG