Skip to content

Commit

Permalink
dhcpv4: moved client into dhcpv4/client4
Browse files Browse the repository at this point in the history
  • Loading branch information
insomniacslk committed Jan 28, 2019
1 parent c04d263 commit a68a254
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 51 deletions.
9 changes: 5 additions & 4 deletions dhcpv4/async/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"sync"
"time"

"github.com/fanliao/go-promise"
promise "github.com/fanliao/go-promise"
"github.com/insomniacslk/dhcp/dhcpv4"
"github.com/insomniacslk/dhcp/dhcpv4/client4"
)

// Default ports
Expand Down Expand Up @@ -41,8 +42,8 @@ type Client struct {
// NewClient creates an asynchronous client
func NewClient() *Client {
return &Client{
ReadTimeout: dhcpv4.DefaultReadTimeout,
WriteTimeout: dhcpv4.DefaultWriteTimeout,
ReadTimeout: client4.DefaultReadTimeout,
WriteTimeout: client4.DefaultWriteTimeout,
}
}

Expand Down Expand Up @@ -159,7 +160,7 @@ func (c *Client) receive(_ *dhcpv4.DHCPv4) {

c.connection.SetReadDeadline(time.Now().Add(c.ReadTimeout))
for {
buffer := make([]byte, dhcpv4.MaxUDPReceivedPacketSize)
buffer := make([]byte, client4.MaxUDPReceivedPacketSize)
n, _, _, _, err := c.connection.ReadMsgUDP(buffer, oobdata)
if err != nil {
if err, ok := err.(net.Error); !ok || !err.Timeout() {
Expand Down
7 changes: 4 additions & 3 deletions dhcpv4/async/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/insomniacslk/dhcp/dhcpv4"
"github.com/insomniacslk/dhcp/dhcpv4/client4"
"github.com/stretchr/testify/require"
)

Expand All @@ -19,7 +20,7 @@ func serve(ctx context.Context, addr *net.UDPAddr, response *dhcpv4.DHCPv4) erro
go func() {
defer conn.Close()
oobdata := []byte{}
buffer := make([]byte, dhcpv4.MaxUDPReceivedPacketSize)
buffer := make([]byte, client4.MaxUDPReceivedPacketSize)
for {
select {
case <-ctx.Done():
Expand Down Expand Up @@ -48,8 +49,8 @@ func serve(ctx context.Context, addr *net.UDPAddr, response *dhcpv4.DHCPv4) erro
func TestNewClient(t *testing.T) {
c := NewClient()
require.NotNil(t, c)
require.Equal(t, c.ReadTimeout, dhcpv4.DefaultReadTimeout)
require.Equal(t, c.ReadTimeout, dhcpv4.DefaultWriteTimeout)
require.Equal(t, c.ReadTimeout, client4.DefaultReadTimeout)
require.Equal(t, c.ReadTimeout, client4.DefaultWriteTimeout)
}

func TestOpenInvalidAddrFailes(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion dhcpv4/bsdp/bsdp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"

"github.com/insomniacslk/dhcp/dhcpv4"
"github.com/insomniacslk/dhcp/dhcpv4/client4"
)

// MaxDHCPMessageSize is the size set in DHCP option 57 (DHCP Maximum Message Size).
Expand Down Expand Up @@ -39,7 +40,7 @@ func ParseBootImageListFromAck(ack *dhcpv4.DHCPv4) ([]BootImage, error) {
}

func needsReplyPort(replyPort uint16) bool {
return replyPort != 0 && replyPort != dhcpv4.ClientPort
return replyPort != 0 && replyPort != client4.ClientPort
}

// MessageTypeFromPacket extracts the BSDP message type (LIST, SELECT) from the
Expand Down
15 changes: 8 additions & 7 deletions dhcpv4/bsdp/bsdp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/insomniacslk/dhcp/dhcpv4"
"github.com/insomniacslk/dhcp/dhcpv4/client4"
"github.com/insomniacslk/dhcp/iana"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -57,7 +58,7 @@ func TestParseBootImageListFromAckNoVendorOption(t *testing.T) {
func TestNeedsReplyPort(t *testing.T) {
require.True(t, needsReplyPort(123))
require.False(t, needsReplyPort(0))
require.False(t, needsReplyPort(dhcpv4.ClientPort))
require.False(t, needsReplyPort(client4.ClientPort))
}

func TestNewInformList_NoReplyPort(t *testing.T) {
Expand Down Expand Up @@ -214,13 +215,13 @@ func TestInformSelectForAck_ReplyPort(t *testing.T) {
}

func TestNewReplyForInformList_NoDefaultImage(t *testing.T) {
inform, _ := NewInformList(net.HardwareAddr{1, 2, 3, 4, 5, 6}, net.IP{1, 2, 3, 4}, dhcpv4.ClientPort)
inform, _ := NewInformList(net.HardwareAddr{1, 2, 3, 4, 5, 6}, net.IP{1, 2, 3, 4}, client4.ClientPort)
_, err := NewReplyForInformList(inform, ReplyConfig{})
require.Error(t, err)
}

func TestNewReplyForInformList_NoImages(t *testing.T) {
inform, _ := NewInformList(net.HardwareAddr{1, 2, 3, 4, 5, 6}, net.IP{1, 2, 3, 4}, dhcpv4.ClientPort)
inform, _ := NewInformList(net.HardwareAddr{1, 2, 3, 4, 5, 6}, net.IP{1, 2, 3, 4}, client4.ClientPort)
fakeImage := BootImage{
ID: BootImageID{ImageType: BootImageTypeMacOSX},
}
Expand All @@ -238,7 +239,7 @@ func TestNewReplyForInformList_NoImages(t *testing.T) {
}

func TestNewReplyForInformList(t *testing.T) {
inform, _ := NewInformList(net.HardwareAddr{1, 2, 3, 4, 5, 6}, net.IP{1, 2, 3, 4}, dhcpv4.ClientPort)
inform, _ := NewInformList(net.HardwareAddr{1, 2, 3, 4, 5, 6}, net.IP{1, 2, 3, 4}, client4.ClientPort)
images := []BootImage{
BootImage{
ID: BootImageID{
Expand Down Expand Up @@ -292,13 +293,13 @@ func TestNewReplyForInformList(t *testing.T) {
}

func TestNewReplyForInformSelect_NoSelectedImage(t *testing.T) {
inform, _ := NewInformList(net.HardwareAddr{1, 2, 3, 4, 5, 6}, net.IP{1, 2, 3, 4}, dhcpv4.ClientPort)
inform, _ := NewInformList(net.HardwareAddr{1, 2, 3, 4, 5, 6}, net.IP{1, 2, 3, 4}, client4.ClientPort)
_, err := NewReplyForInformSelect(inform, ReplyConfig{})
require.Error(t, err)
}

func TestNewReplyForInformSelect_NoImages(t *testing.T) {
inform, _ := NewInformList(net.HardwareAddr{1, 2, 3, 4, 5, 6}, net.IP{1, 2, 3, 4}, dhcpv4.ClientPort)
inform, _ := NewInformList(net.HardwareAddr{1, 2, 3, 4, 5, 6}, net.IP{1, 2, 3, 4}, client4.ClientPort)
fakeImage := BootImage{
ID: BootImageID{ImageType: BootImageTypeMacOSX},
}
Expand All @@ -316,7 +317,7 @@ func TestNewReplyForInformSelect_NoImages(t *testing.T) {
}

func TestNewReplyForInformSelect(t *testing.T) {
inform, _ := NewInformList(net.HardwareAddr{1, 2, 3, 4, 5, 6}, net.IP{1, 2, 3, 4}, dhcpv4.ClientPort)
inform, _ := NewInformList(net.HardwareAddr{1, 2, 3, 4, 5, 6}, net.IP{1, 2, 3, 4}, client4.ClientPort)
images := []BootImage{
BootImage{
ID: BootImageID{
Expand Down
13 changes: 7 additions & 6 deletions dhcpv4/bsdp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import (
"errors"

"github.com/insomniacslk/dhcp/dhcpv4"
"github.com/insomniacslk/dhcp/dhcpv4/client4"
)

// Client represents a BSDP client that can perform BSDP exchanges via the
// broadcast address.
type Client struct {
dhcpv4.Client
client4.Client
}

// NewClient constructs a new client with default read and write timeouts from
// dhcpv4.Client.
func NewClient() *Client {
return &Client{Client: dhcpv4.Client{}}
return &Client{Client: client4.Client{}}
}

// Exchange runs a full BSDP exchange (Inform[list], Ack, Inform[select],
Expand All @@ -24,17 +25,17 @@ func (c *Client) Exchange(ifname string) ([]*Packet, error) {
conversation := make([]*Packet, 0)

// Get our file descriptor for the broadcast socket.
sendFd, err := dhcpv4.MakeBroadcastSocket(ifname)
sendFd, err := client4.MakeBroadcastSocket(ifname)
if err != nil {
return conversation, err
}
recvFd, err := dhcpv4.MakeListeningSocket(ifname)
recvFd, err := client4.MakeListeningSocket(ifname)
if err != nil {
return conversation, err
}

// INFORM[LIST]
informList, err := NewInformListForInterface(ifname, dhcpv4.ClientPort)
informList, err := NewInformListForInterface(ifname, client4.ClientPort)
if err != nil {
return conversation, err
}
Expand All @@ -59,7 +60,7 @@ func (c *Client) Exchange(ifname string) ([]*Packet, error) {
}

// INFORM[SELECT]
informSelect, err := InformSelectForAck(PacketFor(ackForList), dhcpv4.ClientPort, bootImages[0])
informSelect, err := InformSelectForAck(PacketFor(ackForList), client4.ClientPort, bootImages[0])
if err != nil {
return conversation, err
}
Expand Down
27 changes: 14 additions & 13 deletions dhcpv4/client.go → dhcpv4/client4/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dhcpv4
package client4

import (
"encoding/binary"
Expand All @@ -9,6 +9,7 @@ import (
"reflect"
"time"

"github.com/insomniacslk/dhcp/dhcpv4"
"golang.org/x/net/ipv4"
"golang.org/x/sys/unix"
)
Expand Down Expand Up @@ -88,7 +89,7 @@ func makeRawSocket(ifname string) (int, error) {
if err != nil {
return fd, err
}
err = BindToInterface(fd, ifname)
err = dhcpv4.BindToInterface(fd, ifname)
if err != nil {
return fd, err
}
Expand Down Expand Up @@ -179,8 +180,8 @@ func (c *Client) getRemoteUDPAddr() (*net.UDPAddr, error) {
// ordered as Discovery, Offer, Request and Acknowledge. In case of errors, an
// error is returned, and the list of DHCPv4 objects will be shorted than 4,
// containing all the sent and received DHCPv4 messages.
func (c *Client) Exchange(ifname string, modifiers ...Modifier) ([]*DHCPv4, error) {
conversation := make([]*DHCPv4, 0)
func (c *Client) Exchange(ifname string, modifiers ...dhcpv4.Modifier) ([]*dhcpv4.DHCPv4, error) {
conversation := make([]*dhcpv4.DHCPv4, 0)
raddr, err := c.getRemoteUDPAddr()
if err != nil {
return nil, err
Expand Down Expand Up @@ -220,28 +221,28 @@ func (c *Client) Exchange(ifname string, modifiers ...Modifier) ([]*DHCPv4, erro
}()

// Discover
discover, err := NewDiscoveryForInterface(ifname, modifiers...)
discover, err := dhcpv4.NewDiscoveryForInterface(ifname, modifiers...)
if err != nil {
return conversation, err
}
conversation = append(conversation, discover)

// Offer
offer, err := c.SendReceive(sfd, rfd, discover, MessageTypeOffer)
offer, err := c.SendReceive(sfd, rfd, discover, dhcpv4.MessageTypeOffer)
if err != nil {
return conversation, err
}
conversation = append(conversation, offer)

// Request
request, err := NewRequestFromOffer(offer, modifiers...)
request, err := dhcpv4.NewRequestFromOffer(offer, modifiers...)
if err != nil {
return conversation, err
}
conversation = append(conversation, request)

// Ack
ack, err := c.SendReceive(sfd, rfd, request, MessageTypeAck)
ack, err := c.SendReceive(sfd, rfd, request, dhcpv4.MessageTypeAck)
if err != nil {
return conversation, err
}
Expand All @@ -253,7 +254,7 @@ func (c *Client) Exchange(ifname string, modifiers ...Modifier) ([]*DHCPv4, erro
// SendReceive sends a packet (with some write timeout) and waits for a
// response up to some read timeout value. If the message type is not
// MessageTypeNone, it will wait for a specific message type
func (c *Client) SendReceive(sendFd, recvFd int, packet *DHCPv4, messageType MessageType) (*DHCPv4, error) {
func (c *Client) SendReceive(sendFd, recvFd int, packet *dhcpv4.DHCPv4, messageType dhcpv4.MessageType) (*dhcpv4.DHCPv4, error) {
raddr, err := c.getRemoteUDPAddr()
if err != nil {
return nil, err
Expand All @@ -271,7 +272,7 @@ func (c *Client) SendReceive(sendFd, recvFd int, packet *DHCPv4, messageType Mes
// a certain amount of time.
var (
destination [net.IPv4len]byte
response *DHCPv4
response *dhcpv4.DHCPv4
)
copy(destination[:], raddr.IP.To4())
remoteAddr := unix.SockaddrInet4{Port: laddr.Port, Addr: destination}
Expand Down Expand Up @@ -322,7 +323,7 @@ func (c *Client) SendReceive(sendFd, recvFd int, packet *DHCPv4, messageType Mes
pLen := int(binary.BigEndian.Uint16(udph[4:6]))
payload := buf[iph.Len+8 : iph.Len+8+pLen]

response, innerErr = FromBytes(payload)
response, innerErr = dhcpv4.FromBytes(payload)
if innerErr != nil {
errs <- innerErr
return
Expand All @@ -332,12 +333,12 @@ func (c *Client) SendReceive(sendFd, recvFd int, packet *DHCPv4, messageType Mes
continue
}
// wait for a response message
if response.OpCode != OpcodeBootReply {
if response.OpCode != dhcpv4.OpcodeBootReply {
continue
}
// if we are not requested to wait for a specific message type,
// return what we have
if messageType == MessageTypeNone {
if messageType == dhcpv4.MessageTypeNone {
break
}
// break if it's a reply of the desired type, continue otherwise
Expand Down
2 changes: 1 addition & 1 deletion dhcpv4/defaults.go → dhcpv4/client4/defaults.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dhcpv4
package client4

const (
ServerPort = 67
Expand Down
8 changes: 5 additions & 3 deletions dhcpv4/server.go → dhcpv4/server4/server.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package dhcpv4
package server4

import (
"fmt"
"log"
"net"
"sync"
"time"

"github.com/insomniacslk/dhcp/dhcpv4"
)

/*
Expand Down Expand Up @@ -56,7 +58,7 @@ func main() {

// Handler is a type that defines the handler function to be called every time a
// valid DHCPv4 message is received
type Handler func(conn net.PacketConn, peer net.Addr, m *DHCPv4)
type Handler func(conn net.PacketConn, peer net.Addr, m *dhcpv4.DHCPv4)

// Server represents a DHCPv4 server object
type Server struct {
Expand Down Expand Up @@ -129,7 +131,7 @@ func (s *Server) ActivateAndServe() error {
continue
}
log.Printf("Handling request from %v", peer)
m, err := FromBytes(rbuf[:n])
m, err := dhcpv4.FromBytes(rbuf[:n])
if err != nil {
log.Printf("Error parsing DHCPv4 request: %v", err)
continue
Expand Down
Loading

0 comments on commit a68a254

Please sign in to comment.