如何在golang中发送一个假的udp包
How to send a fake udp package in golang
我正在尝试发送一个假的 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)
}
}
在 main 函数中,我只需要调用这个检查 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
}
最后需要添加一个 systemd 检查,在我的例子中,我每 10 分钟发送一次看门狗,因此,systemd 检查将被设置为 11 分钟
[Service]
ExecStartPre=//something
WatchdogSec=11min
Restart=on-failure
我正在尝试发送一个假的 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)
}
}
在 main 函数中,我只需要调用这个检查 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
}
最后需要添加一个 systemd 检查,在我的例子中,我每 10 分钟发送一次看门狗,因此,systemd 检查将被设置为 11 分钟
[Service]
ExecStartPre=//something
WatchdogSec=11min
Restart=on-failure