diff options
Diffstat (limited to 'dhcpv6')
-rw-r--r-- | dhcpv6/async/client.go | 11 | ||||
-rw-r--r-- | dhcpv6/async/client_test.go | 7 | ||||
-rw-r--r-- | dhcpv6/client6/client.go (renamed from dhcpv6/client.go) | 58 | ||||
-rw-r--r-- | dhcpv6/client6/client_test.go (renamed from dhcpv6/client_test.go) | 2 | ||||
-rw-r--r-- | dhcpv6/server6/server.go (renamed from dhcpv6/server.go) | 8 | ||||
-rw-r--r-- | dhcpv6/server6/server_test.go (renamed from dhcpv6/server_test.go) | 14 |
6 files changed, 54 insertions, 46 deletions
diff --git a/dhcpv6/async/client.go b/dhcpv6/async/client.go index 7a8b9ec..4c73383 100644 --- a/dhcpv6/async/client.go +++ b/dhcpv6/async/client.go @@ -7,8 +7,9 @@ import ( "sync" "time" - "github.com/fanliao/go-promise" + promise "github.com/fanliao/go-promise" "github.com/insomniacslk/dhcp/dhcpv6" + "github.com/insomniacslk/dhcp/dhcpv6/client6" ) // Client implements an asynchronous DHCPv6 client @@ -32,8 +33,8 @@ type Client struct { // NewClient creates an asynchronous client func NewClient() *Client { return &Client{ - ReadTimeout: dhcpv6.DefaultReadTimeout, - WriteTimeout: dhcpv6.DefaultWriteTimeout, + ReadTimeout: client6.DefaultReadTimeout, + WriteTimeout: client6.DefaultWriteTimeout, } } @@ -164,7 +165,7 @@ func (c *Client) receive(_ dhcpv6.DHCPv6) { c.connection.SetReadDeadline(time.Now().Add(c.ReadTimeout)) for { - buffer := make([]byte, dhcpv6.MaxUDPReceivedPacketSize) + buffer := make([]byte, client6.MaxUDPReceivedPacketSize) n, _, _, _, err := c.connection.ReadMsgUDP(buffer, oobdata) if err != nil { if err, ok := err.(net.Error); !ok || !err.Timeout() { @@ -196,7 +197,7 @@ func (c *Client) receive(_ dhcpv6.DHCPv6) { func (c *Client) remoteAddr() (*net.UDPAddr, error) { if c.RemoteAddr == nil { - return &net.UDPAddr{IP: dhcpv6.AllDHCPRelayAgentsAndServers, Port: dhcpv6.DefaultServerPort}, nil + return &net.UDPAddr{IP: client6.AllDHCPRelayAgentsAndServers, Port: dhcpv6.DefaultServerPort}, nil } if addr, ok := c.RemoteAddr.(*net.UDPAddr); ok { diff --git a/dhcpv6/async/client_test.go b/dhcpv6/async/client_test.go index 25a71a8..a49623e 100644 --- a/dhcpv6/async/client_test.go +++ b/dhcpv6/async/client_test.go @@ -7,6 +7,7 @@ import ( "time" "github.com/insomniacslk/dhcp/dhcpv6" + "github.com/insomniacslk/dhcp/dhcpv6/client6" "github.com/insomniacslk/dhcp/iana" "github.com/stretchr/testify/require" ) @@ -37,7 +38,7 @@ func serve(ctx context.Context, addr *net.UDPAddr, response dhcpv6.DHCPv6) error go func() { defer conn.Close() oobdata := []byte{} - buffer := make([]byte, dhcpv6.MaxUDPReceivedPacketSize) + buffer := make([]byte, client6.MaxUDPReceivedPacketSize) for { select { case <-ctx.Done(): @@ -66,8 +67,8 @@ func serve(ctx context.Context, addr *net.UDPAddr, response dhcpv6.DHCPv6) error func TestNewClient(t *testing.T) { c := NewClient() require.NotNil(t, c) - require.Equal(t, c.ReadTimeout, dhcpv6.DefaultReadTimeout) - require.Equal(t, c.ReadTimeout, dhcpv6.DefaultWriteTimeout) + require.Equal(t, c.ReadTimeout, client6.DefaultReadTimeout) + require.Equal(t, c.ReadTimeout, client6.DefaultWriteTimeout) } func TestOpenInvalidAddrFailes(t *testing.T) { diff --git a/dhcpv6/client.go b/dhcpv6/client6/client.go index a31c426..4075bde 100644 --- a/dhcpv6/client.go +++ b/dhcpv6/client6/client.go @@ -1,10 +1,12 @@ -package dhcpv6 +package client6 import ( "errors" "fmt" "net" "time" + + "github.com/insomniacslk/dhcp/dhcpv6" ) // Client constants @@ -41,8 +43,8 @@ func NewClient() *Client { // Reply). The modifiers will be applied to the Solicit and Request packets. // A common use is to make sure that the Solicit packet has the right options, // see modifiers.go -func (c *Client) Exchange(ifname string, modifiers ...Modifier) ([]DHCPv6, error) { - conversation := make([]DHCPv6, 0) +func (c *Client) Exchange(ifname string, modifiers ...dhcpv6.Modifier) ([]dhcpv6.DHCPv6, error) { + conversation := make([]dhcpv6.DHCPv6, 0) var err error // Solicit @@ -57,7 +59,7 @@ func (c *Client) Exchange(ifname string, modifiers ...Modifier) ([]DHCPv6, error // Decapsulate advertise if it's relayed before passing it to Request if advertise.IsRelay() { - advertiseRelay := advertise.(*DHCPv6Relay) + advertiseRelay := advertise.(*dhcpv6.DHCPv6Relay) advertise, err = advertiseRelay.GetInnerMessage() if err != nil { return conversation, err @@ -74,30 +76,30 @@ func (c *Client) Exchange(ifname string, modifiers ...Modifier) ([]DHCPv6, error return conversation, nil } -func (c *Client) sendReceive(ifname string, packet DHCPv6, expectedType MessageType) (DHCPv6, error) { +func (c *Client) sendReceive(ifname string, packet dhcpv6.DHCPv6, expectedType dhcpv6.MessageType) (dhcpv6.DHCPv6, error) { if packet == nil { return nil, fmt.Errorf("Packet to send cannot be nil") } - if expectedType == MessageTypeNone { + if expectedType == dhcpv6.MessageTypeNone { // infer the expected type from the packet being sent - if packet.Type() == MessageTypeSolicit { - expectedType = MessageTypeAdvertise - } else if packet.Type() == MessageTypeRequest { - expectedType = MessageTypeReply - } else if packet.Type() == MessageTypeRelayForward { - expectedType = MessageTypeRelayReply - } else if packet.Type() == MessageTypeLeaseQuery { - expectedType = MessageTypeLeaseQueryReply + if packet.Type() == dhcpv6.MessageTypeSolicit { + expectedType = dhcpv6.MessageTypeAdvertise + } else if packet.Type() == dhcpv6.MessageTypeRequest { + expectedType = dhcpv6.MessageTypeReply + } else if packet.Type() == dhcpv6.MessageTypeRelayForward { + expectedType = dhcpv6.MessageTypeRelayReply + } else if packet.Type() == dhcpv6.MessageTypeLeaseQuery { + expectedType = dhcpv6.MessageTypeLeaseQueryReply } // and probably more } // if no LocalAddr is specified, get the interface's link-local address var laddr net.UDPAddr if c.LocalAddr == nil { - llAddr, err := GetLinkLocalAddr(ifname) + llAddr, err := dhcpv6.GetLinkLocalAddr(ifname) if err != nil { return nil, err } - laddr = net.UDPAddr{IP: llAddr, Port: DefaultClientPort, Zone: ifname} + laddr = net.UDPAddr{IP: llAddr, Port: dhcpv6.DefaultClientPort, Zone: ifname} } else { if addr, ok := c.LocalAddr.(*net.UDPAddr); ok { laddr = *addr @@ -109,7 +111,7 @@ func (c *Client) sendReceive(ifname string, packet DHCPv6, expectedType MessageT // if no RemoteAddr is specified, use AllDHCPRelayAgentsAndServers var raddr net.UDPAddr if c.RemoteAddr == nil { - raddr = net.UDPAddr{IP: AllDHCPRelayAgentsAndServers, Port: DefaultServerPort} + raddr = net.UDPAddr{IP: AllDHCPRelayAgentsAndServers, Port: dhcpv6.DefaultServerPort} } else { if addr, ok := c.RemoteAddr.(*net.UDPAddr); ok { raddr = *addr @@ -147,11 +149,11 @@ func (c *Client) sendReceive(ifname string, packet DHCPv6, expectedType MessageT oobdata := []byte{} // ignoring oob data conn.SetReadDeadline(time.Now().Add(c.ReadTimeout)) var ( - adv DHCPv6 + adv dhcpv6.DHCPv6 isMessage bool ) defer conn.Close() - msg, ok := packet.(*DHCPv6Message) + msg, ok := packet.(*dhcpv6.DHCPv6Message) if ok { isMessage = true } @@ -161,12 +163,12 @@ func (c *Client) sendReceive(ifname string, packet DHCPv6, expectedType MessageT if err != nil { return nil, err } - adv, err = FromBytes(buf[:n]) + adv, err = dhcpv6.FromBytes(buf[:n]) if err != nil { // skip non-DHCP packets continue } - if recvMsg, ok := adv.(*DHCPv6Message); ok && isMessage { + if recvMsg, ok := adv.(*dhcpv6.DHCPv6Message); ok && isMessage { // if a regular message, check the transaction ID first // XXX should this unpack relay messages and check the XID of the // inner packet too? @@ -175,7 +177,7 @@ func (c *Client) sendReceive(ifname string, packet DHCPv6, expectedType MessageT continue } } - if expectedType == MessageTypeNone { + if expectedType == dhcpv6.MessageTypeNone { // just take whatever arrived break } else if adv.Type() == expectedType { @@ -188,29 +190,29 @@ func (c *Client) sendReceive(ifname string, packet DHCPv6, expectedType MessageT // Solicit sends a Solicit, returns the Solicit, an Advertise (if not nil), and // an error if any. The modifiers will be applied to the Solicit before sending // it, see modifiers.go -func (c *Client) Solicit(ifname string, modifiers ...Modifier) (DHCPv6, DHCPv6, error) { - solicit, err := NewSolicitForInterface(ifname) +func (c *Client) Solicit(ifname string, modifiers ...dhcpv6.Modifier) (dhcpv6.DHCPv6, dhcpv6.DHCPv6, error) { + solicit, err := dhcpv6.NewSolicitForInterface(ifname) if err != nil { return nil, nil, err } for _, mod := range modifiers { solicit = mod(solicit) } - advertise, err := c.sendReceive(ifname, solicit, MessageTypeNone) + advertise, err := c.sendReceive(ifname, solicit, dhcpv6.MessageTypeNone) return solicit, advertise, err } // Request sends a Request built from an Advertise. It returns the Request, a // Reply (if not nil), and an error if any. The modifiers will be applied to // the Request before sending it, see modifiers.go -func (c *Client) Request(ifname string, advertise DHCPv6, modifiers ...Modifier) (DHCPv6, DHCPv6, error) { - request, err := NewRequestFromAdvertise(advertise) +func (c *Client) Request(ifname string, advertise dhcpv6.DHCPv6, modifiers ...dhcpv6.Modifier) (dhcpv6.DHCPv6, dhcpv6.DHCPv6, error) { + request, err := dhcpv6.NewRequestFromAdvertise(advertise) if err != nil { return nil, nil, err } for _, mod := range modifiers { request = mod(request) } - reply, err := c.sendReceive(ifname, request, MessageTypeNone) + reply, err := c.sendReceive(ifname, request, dhcpv6.MessageTypeNone) return request, reply, err } diff --git a/dhcpv6/client_test.go b/dhcpv6/client6/client_test.go index d2efa1f..1e05a62 100644 --- a/dhcpv6/client_test.go +++ b/dhcpv6/client6/client_test.go @@ -1,4 +1,4 @@ -package dhcpv6 +package client6 import ( "testing" diff --git a/dhcpv6/server.go b/dhcpv6/server6/server.go index daefdd0..5c84f97 100644 --- a/dhcpv6/server.go +++ b/dhcpv6/server6/server.go @@ -1,4 +1,4 @@ -package dhcpv6 +package server6 import ( "fmt" @@ -6,6 +6,8 @@ import ( "net" "sync" "time" + + "github.com/insomniacslk/dhcp/dhcpv6" ) /* @@ -56,7 +58,7 @@ func main() { // Handler is a type that defines the handler function to be called every time a // valid DHCPv6 message is received -type Handler func(conn net.PacketConn, peer net.Addr, m DHCPv6) +type Handler func(conn net.PacketConn, peer net.Addr, m dhcpv6.DHCPv6) // Server represents a DHCPv6 server object type Server struct { @@ -129,7 +131,7 @@ func (s *Server) ActivateAndServe() error { continue } log.Printf("Handling request from %v", peer) - m, err := FromBytes(rbuf[:n]) + m, err := dhcpv6.FromBytes(rbuf[:n]) if err != nil { log.Printf("Error parsing DHCPv6 request: %v", err) continue diff --git a/dhcpv6/server_test.go b/dhcpv6/server6/server_test.go index 436e808..2365b96 100644 --- a/dhcpv6/server_test.go +++ b/dhcpv6/server6/server_test.go @@ -1,4 +1,4 @@ -package dhcpv6 +package server6 import ( "log" @@ -6,13 +6,15 @@ import ( "testing" "time" + "github.com/insomniacslk/dhcp/dhcpv6" + "github.com/insomniacslk/dhcp/dhcpv6/client6" "github.com/insomniacslk/dhcp/interfaces" "github.com/stretchr/testify/require" ) // utility function to set up a client and a server instance and run it in // background. The caller needs to call Server.Close() once finished. -func setUpClientAndServer(handler Handler) (*Client, *Server) { +func setUpClientAndServer(handler Handler) (*client6.Client, *Server) { laddr := net.UDPAddr{ IP: net.ParseIP("::1"), Port: 0, @@ -20,7 +22,7 @@ func setUpClientAndServer(handler Handler) (*Client, *Server) { s := NewServer(laddr, handler) go s.ActivateAndServe() - c := NewClient() + c := client6.NewClient() c.LocalAddr = &net.UDPAddr{ IP: net.ParseIP("::1"), } @@ -44,7 +46,7 @@ func TestNewServer(t *testing.T) { IP: net.ParseIP("::1"), Port: 0, } - handler := func(conn net.PacketConn, peer net.Addr, m DHCPv6) {} + handler := func(conn net.PacketConn, peer net.Addr, m dhcpv6.DHCPv6) {} s := NewServer(laddr, handler) defer s.Close() @@ -55,8 +57,8 @@ func TestNewServer(t *testing.T) { } func TestServerActivateAndServe(t *testing.T) { - handler := func(conn net.PacketConn, peer net.Addr, m DHCPv6) { - adv, err := NewAdvertiseFromSolicit(m) + handler := func(conn net.PacketConn, peer net.Addr, m dhcpv6.DHCPv6) { + adv, err := dhcpv6.NewAdvertiseFromSolicit(m) if err != nil { log.Printf("NewAdvertiseFromSolicit failed: %v", err) return |