Pndpd/responder.go

49 lines
954 B
Go
Raw Permalink Normal View History

2021-12-20 14:53:42 -05:00
package main
import (
2021-12-20 16:46:26 -05:00
"net"
2021-12-20 14:53:42 -05:00
"syscall"
)
var fd int
2021-12-20 16:46:26 -05:00
func respond(iface string, requests chan *NDRequest, respondType NDPType) {
2021-12-20 14:53:42 -05:00
fd, _ = syscall.Socket(syscall.AF_INET6, syscall.SOCK_RAW, syscall.IPPROTO_RAW)
syscall.BindToDevice(fd, iface)
2021-12-20 16:46:26 -05:00
niface, err := net.InterfaceByName(iface)
if err != nil {
panic(err.Error())
}
2021-12-20 14:53:42 -05:00
for {
n := <-requests
2021-12-20 16:46:26 -05:00
pkt(n.srcIP, n.answeringForIP, niface.HardwareAddr, respondType)
2021-12-20 14:53:42 -05:00
}
}
2021-12-20 16:46:26 -05:00
func pkt(srcip []byte, tgtip []byte, mac []byte, respondType NDPType) {
2021-12-20 14:53:42 -05:00
v6 := newIpv6Header(emptyIpv6, srcip)
2021-12-20 16:46:26 -05:00
NDPa := newNdpPacket(tgtip, mac, respondType)
2021-12-20 14:53:42 -05:00
v6.addPayload(NDPa)
response := v6.constructPacket()
var t [16]byte
2021-12-20 16:46:26 -05:00
if respondType == NDP_SOL {
copy(t[:], []byte{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x02})
} else {
copy(t[:], srcip)
}
2021-12-20 14:53:42 -05:00
d := syscall.SockaddrInet6{
Port: 0,
Addr: t,
}
err := syscall.Sendto(fd, response, 0, &d)
if err != nil {
panic(err)
}
syscall.Close(fd)
}