diff options
author | Pablo Mazzini <pmazzini@gmail.com> | 2018-08-01 17:59:36 +0100 |
---|---|---|
committer | insomniac <insomniacslk@users.noreply.github.com> | 2018-08-01 17:59:36 +0100 |
commit | 1ce17c65bf3f13919fa2fbb1a14ce8306a0eca04 (patch) | |
tree | dc5a823e91d82a7d5ebd8d73cabd9dddc2c8545a | |
parent | 0f1b0b63aa938c56794e19e5c360d54dc02fd62a (diff) |
add OptNTPServers (#115)
-rw-r--r-- | dhcpv4/option_ntp_servers.go | 70 | ||||
-rw-r--r-- | dhcpv4/option_ntp_servers_test.go | 65 | ||||
-rw-r--r-- | dhcpv4/option_router.go | 2 | ||||
-rw-r--r-- | dhcpv4/options.go | 2 |
4 files changed, 138 insertions, 1 deletions
diff --git a/dhcpv4/option_ntp_servers.go b/dhcpv4/option_ntp_servers.go new file mode 100644 index 0000000..39881d6 --- /dev/null +++ b/dhcpv4/option_ntp_servers.go @@ -0,0 +1,70 @@ +package dhcpv4 + +import ( + "fmt" + "net" +) + +// This option implements the network time protocol servers option +// https://tools.ietf.org/html/rfc2132 + +// OptNTPServers represents an option encapsulating the NTP servers. +type OptNTPServers struct { + NTPServers []net.IP +} + +// ParseOptNTPServers returns a new OptNTPServers from a byte stream, or error if any. +func ParseOptNTPServers(data []byte) (*OptNTPServers, error) { + if len(data) < 2 { + return nil, ErrShortByteStream + } + code := OptionCode(data[0]) + if code != OptionNTPServers { + return nil, fmt.Errorf("expected code %v, got %v", OptionNTPServers, code) + } + length := int(data[1]) + if length == 0 || length%4 != 0 { + return nil, fmt.Errorf("Invalid length: expected multiple of 4 larger than 4, got %v", length) + } + if len(data) < 2+length { + return nil, ErrShortByteStream + } + ntpServers := make([]net.IP, 0, length%4) + for idx := 0; idx < length; idx += 4 { + b := data[2+idx : 2+idx+4] + ntpServers = append(ntpServers, net.IPv4(b[0], b[1], b[2], b[3])) + } + return &OptNTPServers{NTPServers: ntpServers}, nil +} + +// Code returns the option code. +func (o *OptNTPServers) Code() OptionCode { + return OptionNTPServers +} + +// ToBytes returns a serialized stream of bytes for this option. +func (o *OptNTPServers) ToBytes() []byte { + ret := []byte{byte(o.Code()), byte(o.Length())} + for _, ntp := range o.NTPServers { + ret = append(ret, ntp.To4()...) + } + return ret +} + +// String returns a human-readable string. +func (o *OptNTPServers) String() string { + var ntpServers string + for idx, ntp := range o.NTPServers { + ntpServers += ntp.String() + if idx < len(o.NTPServers)-1 { + ntpServers += ", " + } + } + return fmt.Sprintf("NTP Servers -> %v", ntpServers) +} + +// Length returns the length of the data portion (excluding option code an byte +// length). +func (o *OptNTPServers) Length() int { + return len(o.NTPServers) * 4 +} diff --git a/dhcpv4/option_ntp_servers_test.go b/dhcpv4/option_ntp_servers_test.go new file mode 100644 index 0000000..e7bcefd --- /dev/null +++ b/dhcpv4/option_ntp_servers_test.go @@ -0,0 +1,65 @@ +package dhcpv4 + +import ( + "net" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestOptNTPServersInterfaceMethods(t *testing.T) { + ntpServers := []net.IP{ + net.IPv4(192, 168, 0, 10), + net.IPv4(192, 168, 0, 20), + } + o := OptNTPServers{NTPServers: ntpServers} + require.Equal(t, OptionNTPServers, o.Code(), "Code") + require.Equal(t, net.IPv4len*len(ntpServers), o.Length(), "Length") + require.Equal(t, ntpServers, o.NTPServers, "NTPServers") +} + +func TestParseOptNTPServers(t *testing.T) { + data := []byte{ + byte(OptionNTPServers), + 8, // Length + 192, 168, 0, 10, // NTP server #1 + 192, 168, 0, 20, // NTP server #2 + } + o, err := ParseOptNTPServers(data) + require.NoError(t, err) + ntpServers := []net.IP{ + net.IPv4(192, 168, 0, 10), + net.IPv4(192, 168, 0, 20), + } + require.Equal(t, &OptNTPServers{NTPServers: ntpServers}, o) + + // Short byte stream + data = []byte{byte(OptionNTPServers)} + _, err = ParseOptNTPServers(data) + require.Error(t, err, "should get error from short byte stream") + + // Wrong code + data = []byte{54, 2, 1, 1} + _, err = ParseOptNTPServers(data) + require.Error(t, err, "should get error from wrong code") + + // Bad length + data = []byte{byte(OptionNTPServers), 6, 1, 1, 1} + _, err = ParseOptNTPServers(data) + require.Error(t, err, "should get error from bad length") +} + +func TestParseOptNTPserversNoNTPServers(t *testing.T) { + // RFC2132 requires that at least one NTP server IP is specified + data := []byte{ + byte(OptionNTPServers), + 0, // Length + } + _, err := ParseOptNTPServers(data) + require.Error(t, err) +} + +func TestOptNTPServersString(t *testing.T) { + o := OptNTPServers{NTPServers: []net.IP{net.IPv4(192, 168, 0, 1), net.IPv4(192, 168, 0, 10)}} + require.Equal(t, "NTP Servers -> 192.168.0.1, 192.168.0.10", o.String()) +} diff --git a/dhcpv4/option_router.go b/dhcpv4/option_router.go index b9bdff1..3154edd 100644 --- a/dhcpv4/option_router.go +++ b/dhcpv4/option_router.go @@ -13,7 +13,7 @@ type OptRouter struct { Routers []net.IP } -// ParseOptRouter returns a new OptRouter from a byte stream, or error if any. +// ParseOptRouter returns a new OptRouter from a byte stream, or error if any. func ParseOptRouter(data []byte) (*OptRouter, error) { if len(data) < 2 { return nil, ErrShortByteStream diff --git a/dhcpv4/options.go b/dhcpv4/options.go index 53b2ecf..22e6eee 100644 --- a/dhcpv4/options.go +++ b/dhcpv4/options.go @@ -46,6 +46,8 @@ func ParseOption(data []byte) (Option, error) { opt, err = ParseOptRouter(data) case OptionHostName: opt, err = ParseOptHostName(data) + case OptionNTPServers: + opt, err = ParseOptNTPServers(data) case OptionRequestedIPAddress: opt, err = ParseOptRequestedIPAddress(data) case OptionIPAddressLeaseTime: |