-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: support for !linux builds (#6)
Makes ARP magic optional.
- Loading branch information
1 parent
f6d1736
commit 03cb277
Showing
8 changed files
with
118 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package arp | ||
|
||
import ( | ||
"syscall" | ||
) | ||
|
||
// ARP Flag values | ||
// these are not in golang.org/x/sys/unix | ||
const ( | ||
// completed entry (ha valid) | ||
ATF_COM = 0x02 | ||
// permanent entry | ||
ATF_PERM = 0x04 | ||
// publish entry | ||
ATF_PUBL = 0x08 | ||
// has requested trailers | ||
ATF_USETRAILERS = 0x10 | ||
// want to use a netmask (only for proxy entries) | ||
ATF_NETMASK = 0x20 | ||
// don't answer this addresses | ||
ATF_DONTPUB = 0x40 | ||
) | ||
|
||
// https://man7.org/linux/man-pages/man7/arp.7.html | ||
type arpReq struct { | ||
ArpPa syscall.RawSockaddrInet4 | ||
ArpHa syscall.RawSockaddr | ||
Flags int32 | ||
Netmask syscall.RawSockaddr | ||
Dev [16]byte | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
//go:build linux && !amd64 && !arm64 | ||
// +build linux,!amd64,!arm64 | ||
|
||
package arp | ||
|
||
import ( | ||
"net" | ||
"os" | ||
"syscall" | ||
"unsafe" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
// InjectArp injects an ARP entry into dev's ARP table | ||
// syscalls roughly based on https://www.unix.com/302447674-post3.html | ||
// see: | ||
// https://github.com/torvalds/linux/blob/8cf8821e15cd553339a5b48ee555a0439c2b2742/net/ipv4/arp.c#L1179 | ||
// https://github.com/torvalds/linux/blob/8cf8821e15cd553339a5b48ee555a0439c2b2742/net/ipv4/arp.c#L1024 | ||
func InjectArp(ip net.IP, mac net.HardwareAddr, flags int32, dev string) (err error) { | ||
fd, err := unix.Socket(unix.AF_INET, unix.SOCK_DGRAM, unix.IPPROTO_UDP) | ||
if err != nil { | ||
return | ||
} | ||
f := os.NewFile(uintptr(fd), "") | ||
defer f.Close() | ||
|
||
return InjectArpFd(uintptr(fd), ip, mac, flags, dev) | ||
} | ||
|
||
func InjectArpFd(fd uintptr, ip net.IP, mac net.HardwareAddr, flags int32, dev string) (err error) { | ||
arpReq := arpReq{ | ||
ArpPa: syscall.RawSockaddrInet4{ | ||
Family: syscall.AF_INET, | ||
}, | ||
//Flags: 0x02 | 0x04, // ATF_COM | ATF_PERM; | ||
Flags: flags, | ||
} | ||
copy(arpReq.ArpPa.Addr[:], ip.To4()) | ||
copy(arpReq.ArpHa.Data[:], mac) | ||
copy(arpReq.Dev[:], dev) | ||
|
||
_, _, errno := unix.Syscall(unix.SYS_IOCTL, fd, unix.SIOCSARP, uintptr(unsafe.Pointer(&arpReq))) | ||
if errno != 0 { | ||
return errno | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
//go:build !linux | ||
// +build !linux | ||
|
||
package arp | ||
|
||
import ( | ||
"errors" | ||
"net" | ||
) | ||
|
||
// InjectArp injects an ARP entry into dev's ARP table | ||
func InjectArp(ip net.IP, mac net.HardwareAddr, flags int32, dev string) (err error) { | ||
return errors.New("not implemented") | ||
} | ||
|
||
func InjectArpFd(fd uintptr, ip net.IP, mac net.HardwareAddr, flags int32, dev string) (err error) { | ||
return errors.New("not implemented") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters