diff options
Diffstat (limited to 'dhcpv4')
-rw-r--r-- | dhcpv4/option_domain_search.go | 12 | ||||
-rw-r--r-- | dhcpv4/option_rfc1035label.go | 8 | ||||
-rw-r--r-- | dhcpv4/option_rfc1035label_test.go | 10 |
3 files changed, 18 insertions, 12 deletions
diff --git a/dhcpv4/option_domain_search.go b/dhcpv4/option_domain_search.go index c30cbe1..75626ed 100644 --- a/dhcpv4/option_domain_search.go +++ b/dhcpv4/option_domain_search.go @@ -12,16 +12,20 @@ type OptDomainSearch struct { DomainSearch []string } +// Code returns the option code. func (op *OptDomainSearch) Code() OptionCode { return OptionDNSDomainSearchList } +// ToBytes returns a serialized stream of bytes for this option. func (op *OptDomainSearch) ToBytes() []byte { buf := []byte{byte(op.Code()), byte(op.Length())} - buf = append(buf, LabelsToBytes(op.DomainSearch)...) + buf = append(buf, labelsToBytes(op.DomainSearch)...) return buf } +// Length returns the length of the data portion (excluding option code an byte +// length). func (op *OptDomainSearch) Length() int { var length int for _, label := range op.DomainSearch { @@ -30,11 +34,13 @@ func (op *OptDomainSearch) Length() int { return length } +// String returns a human-readable string. func (op *OptDomainSearch) String() string { return fmt.Sprintf("DNS Domain Search List ->", op.DomainSearch) } -// build an OptDomainSearch structure from a sequence of bytes. +// ParseOptDomainSearch returns a new OptDomainSearch from a byte stream, or +// error if any. func ParseOptDomainSearch(data []byte) (*OptDomainSearch, error) { if len(data) < 2 { return nil, ErrShortByteStream @@ -47,7 +53,7 @@ func ParseOptDomainSearch(data []byte) (*OptDomainSearch, error) { if len(data) < 2+length { return nil, ErrShortByteStream } - domainSearch, err := LabelsFromBytes(data[2:length+2]) + domainSearch, err := labelsFromBytes(data[2:length+2]) if err != nil { return nil, err } diff --git a/dhcpv4/option_rfc1035label.go b/dhcpv4/option_rfc1035label.go index b78d8da..d0972ed 100644 --- a/dhcpv4/option_rfc1035label.go +++ b/dhcpv4/option_rfc1035label.go @@ -5,7 +5,7 @@ import ( "strings" ) -func LabelsFromBytes(buf []byte) ([]string, error) { +func labelsFromBytes(buf []byte) ([]string, error) { var ( pos = 0 domains = make([]string, 0) @@ -32,7 +32,7 @@ func LabelsFromBytes(buf []byte) ([]string, error) { } } -func LabelToBytes(label string) []byte { +func labelToBytes(label string) []byte { var encodedLabel []byte if len(label) == 0 { return []byte{0} @@ -44,10 +44,10 @@ func LabelToBytes(label string) []byte { return append(encodedLabel, 0) } -func LabelsToBytes(labels []string) []byte { +func labelsToBytes(labels []string) []byte { var encodedLabels []byte for _, label := range labels { - encodedLabels = append(encodedLabels, LabelToBytes(label)...) + encodedLabels = append(encodedLabels, labelToBytes(label)...) } return encodedLabels } diff --git a/dhcpv4/option_rfc1035label_test.go b/dhcpv4/option_rfc1035label_test.go index 30c87c8..cd8189a 100644 --- a/dhcpv4/option_rfc1035label_test.go +++ b/dhcpv4/option_rfc1035label_test.go @@ -6,7 +6,7 @@ import ( ) func TestLabelsFromBytes(t *testing.T) { - labels, err := LabelsFromBytes([]byte{ + labels, err := labelsFromBytes([]byte{ 0x9, 's', 'l', 'a', 'c', 'k', 'w', 'a', 'r', 'e', 0x2, 'i', 't', 0x0, @@ -23,7 +23,7 @@ func TestLabelsFromBytes(t *testing.T) { } func TestLabelsFromBytesZeroLength(t *testing.T) { - labels, err := LabelsFromBytes([]byte{}) + labels, err := labelsFromBytes([]byte{}) if err != nil { t.Fatal(err) } @@ -33,7 +33,7 @@ func TestLabelsFromBytesZeroLength(t *testing.T) { } func TestLabelsFromBytesInvalidLength(t *testing.T) { - labels, err := LabelsFromBytes([]byte{0x3, 0xaa, 0xbb}) // short length + labels, err := labelsFromBytes([]byte{0x3, 0xaa, 0xbb}) // short length if err == nil { t.Fatal("Expected error, got nil") } @@ -46,7 +46,7 @@ func TestLabelsFromBytesInvalidLength(t *testing.T) { } func TestLabelToBytes(t *testing.T) { - encodedLabel := LabelToBytes("slackware.it") + encodedLabel := labelToBytes("slackware.it") expected := []byte{ 0x9, 's', 'l', 'a', 'c', 'k', 'w', 'a', 'r', 'e', 0x2, 'i', 't', @@ -58,7 +58,7 @@ func TestLabelToBytes(t *testing.T) { } func TestLabelToBytesZeroLength(t *testing.T) { - encodedLabel := LabelToBytes("") + encodedLabel := labelToBytes("") expected := []byte{0} if !bytes.Equal(encodedLabel, expected) { t.Fatalf("Invalid label. Expected: %v, got: %v", expected, encodedLabel) |