我正在尝试发送一个假的 udp(一个随机的 mac 地址,比如说 01:ff:ff:ff:ff:ff)包,由服务器端的 ServeDHCP 处理,我正在运行以下 dhcpv4 github 存储库 github.com/krolaw/dhcp4 .
发送发现包的目的是检查 dhcp 是否存在。
事实上,我创建了一个名为 check 的新函数
func (h *DHCPHandler) check () {
con, err = net.Dial("udp", "127.0.0.1:67")
for {
//fake udp package???
time.Sleep(10 * time.Minute)
}
在函数的主体中,我有以下调用 go handler.check()
在 ServeDHCP 中我应该传递这些参数:
func (h *DHCPHandler) ServeDHCP(p dhcp.Packet, msgType dhcp.MessageType, options dhcp.Options)
我怎么能从功能检查中发送一个假的更新包?
最佳答案
最后,我成功地完成了这项检查,以在 10 分钟的时间内发送一个更新包,其中包含我知道永远无法到达的 mac 地址。
func (h *DHCPHandler) check() {
//Fetch parameters from config file
config := getConfig() // here is a mac saved on a json file 01:ff:ff:ff:ff:ff
macFake, err := net.ParseMAC(config.MacFake)
if err != nil {
fmt.Println("error with the fake mac provided on the json file", err)
}
// create connection
conn, err := net.Dial("udp4", "127.0.0.1:67")
if err != nil {
fmt.Println("error with the connection", err)
}
//stay alive
for {
fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n")
conn.Write(dhcp.RequestPacket(dhcp.Discover, macFake, h.ip, []byte{0, 1, 2, 3}, true, nil))
time.Sleep(10 * time.Minute)
}
在主函数上,我只需要调用这个检查 goroutine 并在服务器端(ServeDHCP)添加这段代码:
mac := p.CHAddr().String()
//Fetch parameters from config file
config := getConfig()
if mac == config.MacFake { //udp package received and a mac saved on a json file 01:ff:ff:ff:ff:ff
// send notification to restart if failure on a systemd
daemon.SdNotify(false, "WATCHDOG=1")
return
}
需要的最后一部分是添加系统检查,在我的例子中,我每 10 分钟发送一次看门狗,因此系统检查将设置为 11 分钟
[Service]
ExecStartPre=//something
WatchdogSec=11min
Restart=on-failure
关于go - 如何在golang中发送一个假的udp包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47633520/