Skip to content

Commit

Permalink
Merge branch 'master' into fix_return_err_shadowing_by_nil
Browse files Browse the repository at this point in the history
  • Loading branch information
pmazzini authored Dec 27, 2024
2 parents 6f6f92a + b56fa0d commit f4ce79a
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 30 deletions.
7 changes: 7 additions & 0 deletions dhcpv4/dhcpv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,13 @@ func (d *DHCPv4) NTPServers() []net.IP {
return GetIPs(OptionNTPServers, d.Options)
}

// NetBIOSNameServers parses the DHCPv4 NetBIOS Name Servers option if present.
//
// The NetBIOS over TCP/IP Name Server option is described by RFC 2132, Section 8.5.
func (d *DHCPv4) NetBIOSNameServers() []net.IP {
return GetIPs(OptionNetBIOSOverTCPIPNameServer, d.Options)
}

// DNS parses the DHCPv4 Domain Name Server option if present.
//
// The DNS server option is described by RFC 2132, Section 3.8.
Expand Down
10 changes: 10 additions & 0 deletions dhcpv4/option_ips.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ func OptNTPServers(ntpServers ...net.IP) Option {
}
}

// OptNetBIOSNameServers returns a new DHCPv4 NetBIOS Name Server option.
//
// The NetBIOS over TCP/IP Name Server option is described by RFC 2132, Section 8.5.
func OptNetBIOSNameServers(netBIOSNameServers ...net.IP) Option {
return Option{
Code: OptionNetBIOSOverTCPIPNameServer,
Value: IPs(netBIOSNameServers),
}
}

// OptDNS returns a new DHCPv4 Domain Name Server option.
//
// The DNS server option is described by RFC 2132, Section 3.8.
Expand Down
19 changes: 19 additions & 0 deletions dhcpv4/option_ips_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@ func TestGetNTPServers(t *testing.T) {
require.Nil(t, m.NTPServers())
}

func TestOptNetBIOSNameServers(t *testing.T) {
o := OptNetBIOSNameServers(net.IPv4(192, 168, 0, 1), net.IPv4(192, 168, 0, 10))
require.Equal(t, OptionNetBIOSOverTCPIPNameServer, o.Code)
require.Equal(t, []byte{192, 168, 0, 1, 192, 168, 0, 10}, o.Value.ToBytes())
require.Equal(t, "NetBIOS over TCP/IP Name Server: 192.168.0.1, 192.168.0.10", o.String())
}

func TestGetNetBIOSNameServers(t *testing.T) {
ips := []net.IP{
net.IP{192, 168, 0, 1},
net.IP{192, 168, 0, 10},
}
m, _ := New(WithOption(OptNetBIOSNameServers(ips...)))
require.Equal(t, ips, m.NetBIOSNameServers())

m, _ = New()
require.Nil(t, m.NetBIOSNameServers())
}

func TestOptRouter(t *testing.T) {
o := OptRouter(net.IPv4(192, 168, 0, 1), net.IPv4(192, 168, 0, 10))
require.Equal(t, OptionRouter, o.Code)
Expand Down
10 changes: 9 additions & 1 deletion dhcpv4/option_relay_agent_information.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ type RelayOptions struct {

var relayHumanizer = OptionHumanizer{
ValueHumanizer: func(code OptionCode, data []byte) fmt.Stringer {
var d OptionDecoder
switch code {
case LinkSelectionSubOption, ServerIdentifierOverrideSubOption:
d = &IPs{}
}
if d != nil && d.FromBytes(data) == nil {
return d
}
return raiSubOptionValue{data}
},
CodeHumanizer: func(c uint8) OptionCode {
Expand Down Expand Up @@ -42,7 +50,7 @@ type raiSubOptionValue struct {
}

func (rv raiSubOptionValue) String() string {
return fmt.Sprintf("%s (%v)", string(rv.val), rv.val)
return fmt.Sprintf("%q (%v)", string(rv.val), rv.val)
}

type raiSubOptionCode uint8
Expand Down
6 changes: 5 additions & 1 deletion dhcpv4/option_relay_agent_information_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ func TestOptRelayAgentInfo(t *testing.T) {
opt := OptRelayAgentInfo(
OptGeneric(GenericOptionCode(1), []byte("linux")),
OptGeneric(GenericOptionCode(2), []byte("boot")),
OptGeneric(GenericOptionCode(3), []byte{2, 30, 99}),
OptGeneric(GenericOptionCode(LinkSelectionSubOption), []byte{192, 0, 2, 1}),
)
wantBytes := []byte{
1, 5, 'l', 'i', 'n', 'u', 'x',
2, 4, 'b', 'o', 'o', 't',
3, 3, 2, 30, 99,
5, 4, 192, 0, 2, 1,
}
wantString := "Relay Agent Information:\n\n Agent Circuit ID Sub-option: linux ([108 105 110 117 120])\n Agent Remote ID Sub-option: boot ([98 111 111 116])\n"
wantString := "Relay Agent Information:\n\n Agent Circuit ID Sub-option: \"linux\" ([108 105 110 117 120])\n Agent Remote ID Sub-option: \"boot\" ([98 111 111 116])\n unknown (3): \"\\x02\\x1ec\" ([2 30 99])\n Link Selection Sub-option: 192.0.2.1\n"
require.Equal(t, wantBytes, opt.Value.ToBytes())
require.Equal(t, OptionRelayAgentInformation, opt.Code)
require.Equal(t, wantString, opt.String())
Expand Down
20 changes: 5 additions & 15 deletions dhcpv4/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ var (
// ErrZeroLengthByteStream is an error that is thrown any time a zero-length
// byte stream is encountered.
ErrZeroLengthByteStream = errors.New("zero-length byte stream")

// ErrInvalidOptions is returned when invalid options data is
// encountered during parsing. The data could report an incorrect
// length or have trailing bytes which are not part of the option.
ErrInvalidOptions = errors.New("invalid options data")
)

// OptionValue is an interface that all DHCP v4 options adhere to.
Expand Down Expand Up @@ -161,14 +156,6 @@ func (o Options) fromBytesCheckEnd(data []byte, checkEndOption bool) error {
return io.ErrUnexpectedEOF
}

// Any bytes left must be padding.
var pad uint8
for buf.Len() >= 1 {
pad = buf.Read8()
if pad != optPad && pad != optEnd {
return ErrInvalidOptions
}
}
return nil
}

Expand Down Expand Up @@ -344,7 +331,8 @@ func getOption(code OptionCode, data []byte, vendorDecoder OptionDecoder) fmt.St
d = &OptionCodeList{}

case OptionHostName, OptionDomainName, OptionRootPath,
OptionClassIdentifier, OptionTFTPServerName, OptionBootfileName:
OptionClassIdentifier, OptionTFTPServerName, OptionBootfileName,
OptionMessage, OptionReferenceToTZDatabase:
var s String
d = &s

Expand All @@ -354,7 +342,9 @@ func getOption(code OptionCode, data []byte, vendorDecoder OptionDecoder) fmt.St
case OptionDNSDomainSearchList:
d = &rfc1035label.Labels{}

case OptionIPAddressLeaseTime, OptionRenewTimeValue, OptionRebindingTimeValue, OptionIPv6OnlyPreferred:
case OptionIPAddressLeaseTime, OptionRenewTimeValue,
OptionRebindingTimeValue, OptionIPv6OnlyPreferred, OptionArpCacheTimeout,
OptionTimeOffset:
var dur Duration
d = &dur

Expand Down
8 changes: 4 additions & 4 deletions dhcpv4/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func TestParseOption(t *testing.T) {
{
code: OptionRelayAgentInformation,
value: []byte{1, 12, 99, 105, 114, 99, 117, 105, 116, 45, 105, 100, 45, 49},
want: "\n Agent Circuit ID Sub-option: circuit-id-1 ([99 105 114 99 117 105 116 45 105 100 45 49])\n",
want: "\n Agent Circuit ID Sub-option: \"circuit-id-1\" ([99 105 114 99 117 105 116 45 105 100 45 49])\n",
},
{
code: OptionClientSystemArchitectureType,
Expand Down Expand Up @@ -255,9 +255,9 @@ func TestOptionsUnmarshal(t *testing.T) {
wantError: true,
},
{
// Option present after the End is a nono.
input: []byte{byte(OptionEnd), 3},
wantError: true,
// Option present after the End.
input: []byte{byte(OptionEnd), 3},
want: Options{},
},
{
input: []byte{byte(OptionEnd)},
Expand Down
16 changes: 13 additions & 3 deletions dhcpv4/server4/conn_windows.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
//go:build windows

package server4

import (
"errors"
"fmt"
"net"
)

// NewIPv4UDPConn fails on Windows. Use WithConn() to pass the connection.
// NewIPv4UDPConn returns an UDPv4 connection bound to the IP and port provider
func NewIPv4UDPConn(iface string, addr *net.UDPAddr) (*net.UDPConn, error) {
return nil, errors.New("not implemented on Windows")
connection, err := net.ListenPacket("udp4", addr.String())
if err != nil {
return nil, fmt.Errorf("We cannot listen on %s and port %d: %v", addr.IP, addr.Port, err)
}
udpConn, ok := connection.(*net.UDPConn)
if !ok {
return nil, fmt.Errorf("The connection is not of the proper type")
}
return udpConn, nil
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ require (
github.com/mdlayher/packet v1.1.2
github.com/stretchr/testify v1.6.1
github.com/u-root/uio v0.0.0-20230220225925-ffce2a382923
golang.org/x/net v0.23.0
golang.org/x/sys v0.18.0
golang.org/x/net v0.33.0
golang.org/x/sys v0.28.0
)

require (
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/u-root/uio v0.0.0-20230220225925-ffce2a382923 h1:tHNk7XK9GkmKUR6Gh8gVBKXc2MVSZ4G/NnWLtzw4gNA=
github.com/u-root/uio v0.0.0-20230220225925-ffce2a382923/go.mod h1:eLL9Nub3yfAho7qB0MzZizFhTU2QkLeoVsWdHtDW264=
golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2 h1:Jvc7gsqn21cJHCmAWx0LiimpP18LZmUxkT5Mp7EZ1mI=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20220622161953-175b2fd9d664/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down

0 comments on commit f4ce79a

Please sign in to comment.