【发布时间】:2017-09-12 09:50:42
【问题描述】:
我想编写一个程序,从带有 MAC 地址的接口发送 DHCP 请求并从 DHCP 服务器获取 IP 地址、网关、掩码和广播地址。
我使用了 DHCP 客户端的this implementation,但它只给出了 IP 地址和网关地址。
我阅读了有关 DHCP 消息格式的信息,但没有找到掩码或广播文件。但是,当使用 dhcpclient 命令行时,我可以看到使用 ifconfig 接口具有掩码和广播地址,如下所示:
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)
谁能告诉我这些信息是如何获得的?
【问题讨论】:
-
你想获取远程机器的信息还是你的?
-
@ARG:我想获取我机器的信息,但使用我的程序而不是使用命令行工具,如 dhcpclient
-
@ARM 你可以使用类似 SIOCGIFADDR, microhowto.info/howto/…
-
@RustyX:我在 struct dhcp 中看不到子网掩码或广播地址字段
-
@ARG:您说的是获取已配置网络接口的信息。但我想要的是从 DHCP serer 获取这些信息,然后使用它们来配置接口
标签: c++ c ubuntu network-programming dhcp