diff options
Diffstat (limited to 'dhcpv4')
-rw-r--r-- | dhcpv4/bindtodevice_darwin.go | 7 | ||||
-rw-r--r-- | dhcpv4/bindtodevice_linux.go | 6 | ||||
-rw-r--r-- | dhcpv4/bsdp/vendor_class_identifier_darwin.go | 5 | ||||
-rw-r--r-- | dhcpv4/client.go | 42 |
4 files changed, 32 insertions, 28 deletions
diff --git a/dhcpv4/bindtodevice_darwin.go b/dhcpv4/bindtodevice_darwin.go index e9580ce..3a1406b 100644 --- a/dhcpv4/bindtodevice_darwin.go +++ b/dhcpv4/bindtodevice_darwin.go @@ -4,13 +4,16 @@ package dhcpv4 import ( "net" - "syscall" + + "golang.org/x/sys/unix" ) +// BindToInterface emulates linux's SO_BINDTODEVICE option for a socket by using +// SO_IP_BOUND_IF. func BindToInterface(fd int, ifname string) error { iface, err := net.InterfaceByName(ifname) if err != nil { return err } - return syscall.SetsockoptInt(fd, syscall.IPPROTO_IP, syscall.IP_BOUND_IF, iface.Index) + return unix.SetsockoptInt(fd, unix.IPPROTO_IP, unix.IP_BOUND_IF, iface.Index) } diff --git a/dhcpv4/bindtodevice_linux.go b/dhcpv4/bindtodevice_linux.go index 957744a..c1ae025 100644 --- a/dhcpv4/bindtodevice_linux.go +++ b/dhcpv4/bindtodevice_linux.go @@ -2,10 +2,8 @@ package dhcpv4 -import ( - "syscall" -) +import "golang.org/x/sys/unix" func BindToInterface(fd int, ifname string) error { - return syscall.BindToDevice(fd, ifname) + return unix.BindToDevice(fd, ifname) } diff --git a/dhcpv4/bsdp/vendor_class_identifier_darwin.go b/dhcpv4/bsdp/vendor_class_identifier_darwin.go index d1b4c2b..46ef656 100644 --- a/dhcpv4/bsdp/vendor_class_identifier_darwin.go +++ b/dhcpv4/bsdp/vendor_class_identifier_darwin.go @@ -2,14 +2,15 @@ package bsdp import ( "fmt" - "syscall" + + "golang.org/x/sys/unix" ) // MakeVendorClassIdentifier calls the sysctl syscall on macOS to get the // platform model. func MakeVendorClassIdentifier() (string, error) { // Fetch hardware model for class ID. - hwModel, err := syscall.Sysctl("hw.model") + hwModel, err := unix.Sysctl("hw.model") if err != nil { return "", err } diff --git a/dhcpv4/client.go b/dhcpv4/client.go index 9fd8501..6f296e0 100644 --- a/dhcpv4/client.go +++ b/dhcpv4/client.go @@ -5,10 +5,10 @@ import ( "errors" "net" "os" - "syscall" "time" "golang.org/x/net/ipv4" + "golang.org/x/sys/unix" ) // MaxUDPReceivedPacketSize is the (arbitrary) maximum UDP packet size supported @@ -69,22 +69,22 @@ func MakeRawBroadcastPacket(payload []byte) ([]byte, error) { return ret, nil } -// MakeBroadcastSocket creates a socket that can be passed to syscall.Sendto +// MakeBroadcastSocket creates a socket that can be passed to unix.Sendto // that will send packets out to the broadcast address. func MakeBroadcastSocket(ifname string) (int, error) { - fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_RAW, syscall.IPPROTO_RAW) + fd, err := unix.Socket(unix.AF_INET, unix.SOCK_RAW, unix.IPPROTO_RAW) if err != nil { return fd, err } - err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1) + err = unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_REUSEADDR, 1) if err != nil { return fd, err } - err = syscall.SetsockoptInt(fd, syscall.IPPROTO_IP, syscall.IP_HDRINCL, 1) + err = unix.SetsockoptInt(fd, unix.IPPROTO_IP, unix.IP_HDRINCL, 1) if err != nil { return fd, err } - err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1) + err = unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_BROADCAST, 1) if err != nil { return fd, err } @@ -98,17 +98,17 @@ func MakeBroadcastSocket(ifname string) (int, error) { // MakeListeningSocket creates a listening socket on 0.0.0.0 for the DHCP client // port and returns it. func MakeListeningSocket(ifname string) (int, error) { - fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_UDP) + fd, err := unix.Socket(unix.AF_INET, unix.SOCK_DGRAM, unix.IPPROTO_UDP) if err != nil { return fd, err } - err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1) + err = unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_REUSEADDR, 1) if err != nil { return fd, err } var addr [4]byte copy(addr[:], net.IPv4zero.To4()) - if err = syscall.Bind(fd, &syscall.SockaddrInet4{Port: ClientPort, Addr: addr}); err != nil { + if err = unix.Bind(fd, &unix.SockaddrInet4{Port: ClientPort, Addr: addr}); err != nil { return fd, err } err = BindToInterface(fd, ifname) @@ -184,15 +184,17 @@ func BroadcastSendReceive(sendFd, recvFd int, packet *DHCPv4, readTimeout, write // Create a goroutine to perform the blocking send, and time it out after // a certain amount of time. - var destination [4]byte + var ( + destination [4]byte + response *DHCPv4 + ) copy(destination[:], net.IPv4bcast.To4()) - remoteAddr := syscall.SockaddrInet4{Port: ClientPort, Addr: destination} + remoteAddr := unix.SockaddrInet4{Port: ClientPort, Addr: destination} recvErrors := make(chan error, 1) - var response *DHCPv4 go func(errs chan<- error) { - conn, err := net.FileConn(os.NewFile(uintptr(recvFd), "")) + conn, innerErr := net.FileConn(os.NewFile(uintptr(recvFd), "")) if err != nil { - errs <- err + errs <- innerErr return } defer conn.Close() @@ -200,15 +202,15 @@ func BroadcastSendReceive(sendFd, recvFd int, packet *DHCPv4, readTimeout, write for { buf := make([]byte, MaxUDPReceivedPacketSize) - n, _, _, _, err := conn.(*net.UDPConn).ReadMsgUDP(buf, []byte{}) - if err != nil { - errs <- err + n, _, _, _, innerErr := conn.(*net.UDPConn).ReadMsgUDP(buf, []byte{}) + if innerErr != nil { + errs <- innerErr return } - response, err = FromBytes(buf[:n]) + response, innerErr = FromBytes(buf[:n]) if err != nil { - errs <- err + errs <- innerErr return } // check that this is a response to our message @@ -231,7 +233,7 @@ func BroadcastSendReceive(sendFd, recvFd int, packet *DHCPv4, readTimeout, write } recvErrors <- nil }(recvErrors) - if err = syscall.Sendto(sendFd, packetBytes, 0, &remoteAddr); err != nil { + if err = unix.Sendto(sendFd, packetBytes, 0, &remoteAddr); err != nil { return nil, err } |