-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
See RFC5970 section 3.2. * Added support of option OptBootFileParam * Added BootfileParam to netboot results Signed-off-by: Your Name <[email protected]>
- Loading branch information
Your Name
committed
Dec 10, 2019
1 parent
3997b8a
commit 603f39f
Showing
4 changed files
with
158 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package dhcpv6 | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"fmt" | ||
) | ||
|
||
var ( | ||
// bootpEndian is the endianness used to parse BOOTP option values | ||
// See RFC2132, section 2. Also keep in mind | ||
// "network byte order" is "big-endian order". | ||
bootpEndian = binary.BigEndian | ||
) | ||
|
||
// ErrBootFileParamUnexpectedEnd is returned when option Bootfile param is | ||
// encoded wrongly. See https://www.ietf.org/rfc/rfc5970.txt (section 3.2). | ||
type ErrBootFileParamUnexpectedEnd struct { | ||
Expected uint16 | ||
Left uint16 | ||
} | ||
|
||
// Error just implements interface "error" | ||
func (err *ErrBootFileParamUnexpectedEnd) Error() string { | ||
return fmt.Sprintf("[bootfile param] unexpected end (expected: %d; left %d)", err.Expected, err.Left) | ||
} | ||
|
||
// OptBootFileParam implements the OptionBootfileParam option | ||
// | ||
// This module defines the OPT_BOOTFILE_PARAM structure. | ||
// https://www.ietf.org/rfc/rfc5970.txt (section 3.2) | ||
type OptBootFileParam []string | ||
|
||
var _ Option = OptBootFileParam(nil) | ||
|
||
// Code returns the option code | ||
func (op OptBootFileParam) Code() OptionCode { | ||
return OptionBootfileParam | ||
} | ||
|
||
// ToBytes serializes the option and returns it as a sequence of bytes | ||
func (op OptBootFileParam) ToBytes() []byte { | ||
var length [2]byte | ||
var buf bytes.Buffer | ||
for _, param := range op { | ||
bootpEndian.PutUint16(length[:], uint16(len(param))) | ||
buf.Write(length[:]) | ||
buf.WriteString(param) | ||
} | ||
return buf.Bytes() | ||
} | ||
|
||
func (op OptBootFileParam) String() string { | ||
return fmt.Sprintf("OptBootFileParam(%v)", ([]string)(op)) | ||
} | ||
|
||
// ParseOptBootFileParam builds an OptBootFileParam structure from a sequence | ||
// of bytes. The input data does not include option code and length bytes. | ||
func ParseOptBootFileParam(data []byte) (result OptBootFileParam, err error) { | ||
for len(data) > 0 { | ||
if len(data) < 2 { | ||
return nil, &ErrBootFileParamUnexpectedEnd{Expected: 2, Left: uint16(len(data))} | ||
} | ||
length := bootpEndian.Uint16(data) | ||
data = data[2:] | ||
|
||
if len(data) < int(length) { | ||
return nil, &ErrBootFileParamUnexpectedEnd{Expected: length, Left: uint16(len(data))} | ||
} | ||
param := string(data[:length]) | ||
data = data[length:] | ||
|
||
result = append(result, param) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package dhcpv6 | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const ( | ||
testBootfileParam = "\x00\x30initrd=http://myserver.mycompany.local/initrd.xz\x00\x0eroot=/dev/sda1\x00\x02rw\x00\x24netconsole=..:\000:.something\000here.::.." | ||
) | ||
|
||
func TestOptBootFileParam(t *testing.T) { | ||
expected := testBootfileParam | ||
opt, err := ParseOptBootFileParam([]byte(expected)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if string(opt.ToBytes()) != expected { | ||
t.Fatalf("Invalid boot file parameter. Expected %v, got %v", expected, opt) | ||
} | ||
} | ||
|
||
func TestParsedTypeOptBootFileParam(t *testing.T) { | ||
opt, err := ParseOption(OptionBootfileParam, []byte(testBootfileParam)) | ||
require.NoError(t, err) | ||
bootfileParamOpt, ok := opt.(OptBootFileParam) | ||
require.True(t, ok, fmt.Sprintf("invalid type: %T instead of %T", opt, bootfileParamOpt)) | ||
require.Equal(t, testBootfileParam, string(bootfileParamOpt.ToBytes())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters