Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Add Get/Set MAC addresses #86

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ func main() {
if err != nil {
log.Fatal(err)
}

ifce.SetMAC("d6:db:b0:42:2c:7e")

mac, err := ifce.GetMAC()
if err != nil {
log.Fatal(err)
}
fmt.Println("MAC address:", mac)

var frame ethernet.Frame

for {
Expand Down
58 changes: 58 additions & 0 deletions if.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package water
import (
"errors"
"io"
"net"
"os"
"syscall"
"unsafe"
)

// Interface is a TUN/TAP interface.
Expand Down Expand Up @@ -78,3 +82,57 @@ func (ifce *Interface) IsTAP() bool {
func (ifce *Interface) Name() string {
return ifce.name
}

// GetMAC returns the created interface's MAC address
func (ifce *Interface) GetMAC() (string, error) {
var req ifReq
req.Flags = syscall.AF_UNIX
copy(req.Name[:], ifce.name)

fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, 0)
if err != nil {
return "", os.NewSyscallError("socket", err)
}
defer syscall.Close(fd)

err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
if err != nil {
return "", os.NewSyscallError("setsockopt", err)
}

err = ioctl(uintptr(fd), syscall.SIOCGIFHWADDR, uintptr(unsafe.Pointer(&req)))
if err != nil {
return "", err
}
return net.HardwareAddr(req.Mac[:]).String(), nil
}

// SetMAC sets the MAC address for the interface
func (ifce *Interface) SetMAC(mac string) error {
var req ifReq
req.Flags = syscall.AF_UNIX
copy(req.Name[:], ifce.name)

macBytes, err := net.ParseMAC(mac)
if err != nil {
return err
}
copy(req.Mac[:], macBytes)

fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, 0)
if err != nil {
return os.NewSyscallError("socket", err)
}
defer syscall.Close(fd)

err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
if err != nil {
return os.NewSyscallError("setsockopt", err)
}

err = ioctl(uintptr(fd), syscall.SIOCSIFHWADDR, uintptr(unsafe.Pointer(&req)))
if err != nil {
return err
}
return nil
}
3 changes: 2 additions & 1 deletion syscalls_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const (
type ifReq struct {
Name [0x10]byte
Flags uint16
pad [0x28 - 0x10 - 2]byte
Mac [6]byte
pad [0x28 - 0x10 - 8]byte
}

func ioctl(fd uintptr, request uintptr, argp uintptr) error {
Expand Down