From fdac804dc9375ebe7c607059c186b771d5c49800 Mon Sep 17 00:00:00 2001 From: Owen Mooney Date: Wed, 25 Jul 2018 13:14:11 +0100 Subject: Add TFTP server name option (#92) --- dhcpv4/option_tftp_server_name.go | 54 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 dhcpv4/option_tftp_server_name.go (limited to 'dhcpv4/option_tftp_server_name.go') diff --git a/dhcpv4/option_tftp_server_name.go b/dhcpv4/option_tftp_server_name.go new file mode 100644 index 0000000..b4029e1 --- /dev/null +++ b/dhcpv4/option_tftp_server_name.go @@ -0,0 +1,54 @@ +package dhcpv4 + +import ( + "fmt" +) + +// This option implements the TFTP server name option. +// https://tools.ietf.org/html/rfc2132 + +// OptTFTPServerName implements the TFTP server name option. +type OptTFTPServerName struct { + TFTPServerName []byte +} + +// Code returns the option code +func (op *OptTFTPServerName) Code() OptionCode { + return OptionTFTPServerName +} + +// ToBytes serializes the option and returns it as a sequence of bytes +func (op *OptTFTPServerName) ToBytes() []byte { + return append([]byte{byte(op.Code()), byte(op.Length())}, op.TFTPServerName...) +} + +// Length returns the option length in bytes +func (op *OptTFTPServerName) Length() int { + return len(op.TFTPServerName) +} + +func (op *OptTFTPServerName) String() string { + return fmt.Sprintf("OptTFTPServerName{TFTPServerName=%s}", op.TFTPServerName) +} + +// ParseOptTFTPServerName returns a new OptTFTPServerName fomr a byte stream or error if any +func ParseOptTFTPServerName(data []byte) (*OptTFTPServerName, error) { + if len(data) < 3 { + return nil, ErrShortByteStream + } + code := OptionCode(data[0]) + if code != OptionTFTPServerName { + return nil, fmt.Errorf("ParseOptTFTPServerName: invalid code: %v; want %v", + code, OptionTFTPServerName) + } + length := int(data[1]) + if length < 1 { + return nil, fmt.Errorf("TFTP server name has invalid length of %d", length) + } + TFTPServerNameData := data[2:] + if len(TFTPServerNameData) < length { + return nil, fmt.Errorf("ParseOptTFTPServerName: short data: %d bytes; want %d", + len(TFTPServerNameData), length) + } + return &OptTFTPServerName{TFTPServerName: TFTPServerNameData[:length]}, nil +} -- cgit v1.2.3