summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/client.go
diff options
context:
space:
mode:
authorSean Karlage <skarlage@fb.com>2018-10-07 00:13:46 -0700
committerinsomniac <insomniacslk@users.noreply.github.com>2018-10-07 06:50:11 -0700
commit4eedccfd12105dbcde88298e87bbbcf41aa7c1df (patch)
treea6e822b763e4a16f3947b6856683b27c0697e912 /dhcpv4/client.go
parentc3fc64094fbed445c0d131b77fcc13e56da991e7 (diff)
DHCPv4: syscall -> x/sys/unix
Fixes #156 Build a simple client like so: ``` package main import ( "fmt" "time" "github.com/insomniacslk/dhcp/dhcpv4" ) func main() { client := dhcpv4.Client{ReadTimeout: 5 * time.Second, WriteTimeout: 5 * time.Second} conversation, err := client.Exchange("en0", nil) if err != nil { fmt.Println(err) } for _, m := range conversation { fmt.Println(m.Summary()) } } ``` and run: ``` $ sudo ./main Password: DHCPv4 opcode=BootRequest hwtype=Ethernet hwaddrlen=6 hopcount=0 transactionid=0xabfad715 numseconds=0 flags=Broadcast (0x8000) clientipaddr=0.0.0.0 youripaddr=0.0.0.0 serveripaddr=0.0.0.0 gatewayipaddr=0.0.0.0 clienthwaddr=8c:85:90:20:2e:33 serverhostname= bootfilename= options= DHCP Message Type -> DISCOVER Parameter Request List -> [Subnet Mask, Router, Domain Name, Domain Name Server] End -> [] DHCPv4 opcode=BootReply hwtype=Ethernet hwaddrlen=6 hopcount=0 transactionid=0xabfad715 numseconds=0 flags=Broadcast (0x8000) clientipaddr=0.0.0.0 youripaddr=192.168.0.105 serveripaddr=0.0.0.0 gatewayipaddr=0.0.0.0 clienthwaddr=8c:85:90:20:2e:33 serverhostname= bootfilename= options= DHCP Message Type -> OFFER Server Identifier -> 192.168.0.1 IP Addresses Lease Time -> 5648 Subnet Mask -> ffffff00 Routers -> 192.168.0.1 Domain Name Servers -> 8.8.8.8, 8.8.4.4 End -> [] DHCPv4 opcode=BootRequest hwtype=Ethernet hwaddrlen=6 hopcount=0 transactionid=0xabfad715 numseconds=0 flags=Broadcast (0x8000) clientipaddr=0.0.0.0 youripaddr=0.0.0.0 serveripaddr=192.168.0.1 gatewayipaddr=0.0.0.0 clienthwaddr=8c:85:90:20:2e:33 serverhostname= bootfilename= options= DHCP Message Type -> REQUEST Requested IP Address -> 192.168.0.105 Server Identifier -> 192.168.0.1 End -> [] DHCPv4 opcode=BootReply hwtype=Ethernet hwaddrlen=6 hopcount=0 transactionid=0xabfad715 numseconds=0 flags=Broadcast (0x8000) clientipaddr=0.0.0.0 youripaddr=192.168.0.105 serveripaddr=0.0.0.0 gatewayipaddr=0.0.0.0 clienthwaddr=8c:85:90:20:2e:33 serverhostname= bootfilename= options= DHCP Message Type -> ACK Server Identifier -> 192.168.0.1 IP Addresses Lease Time -> 7200 Subnet Mask -> ffffff00 Routers -> 192.168.0.1 Domain Name Servers -> 8.8.8.8, 8.8.4.4 End -> [] ```
Diffstat (limited to 'dhcpv4/client.go')
-rw-r--r--dhcpv4/client.go42
1 files changed, 22 insertions, 20 deletions
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
}