diff options
Diffstat (limited to 'dhcpv4')
-rw-r--r-- | dhcpv4/option_domain_search.go | 6 | ||||
-rw-r--r-- | dhcpv4/option_rfc1035label.go | 53 | ||||
-rw-r--r-- | dhcpv4/option_rfc1035label_test.go | 66 |
3 files changed, 4 insertions, 121 deletions
diff --git a/dhcpv4/option_domain_search.go b/dhcpv4/option_domain_search.go index b845b59..5ca17a8 100644 --- a/dhcpv4/option_domain_search.go +++ b/dhcpv4/option_domain_search.go @@ -5,6 +5,8 @@ package dhcpv4 import ( "fmt" + + "github.com/insomniacslk/dhcp/dnscompress" ) // OptDomainSearch represents an option encapsulating a domain search list. @@ -20,7 +22,7 @@ func (op *OptDomainSearch) Code() OptionCode { // 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, dnscompress.LabelsToBytes(op.DomainSearch)...) return buf } @@ -53,7 +55,7 @@ func ParseOptDomainSearch(data []byte) (*OptDomainSearch, error) { if len(data) < 2+length { return nil, ErrShortByteStream } - domainSearch, err := labelsFromBytes(data[2:length+2]) + domainSearch, err := dnscompress.LabelsFromBytes(data[2:length+2]) if err != nil { return nil, err } diff --git a/dhcpv4/option_rfc1035label.go b/dhcpv4/option_rfc1035label.go deleted file mode 100644 index d0972ed..0000000 --- a/dhcpv4/option_rfc1035label.go +++ /dev/null @@ -1,53 +0,0 @@ -package dhcpv4 - -import ( - "fmt" - "strings" -) - -func labelsFromBytes(buf []byte) ([]string, error) { - var ( - pos = 0 - domains = make([]string, 0) - label = "" - ) - for { - if pos >= len(buf) { - return domains, nil - } - length := int(buf[pos]) - pos++ - if length == 0 { - domains = append(domains, label) - label = "" - } - if len(buf)-pos < length { - return nil, fmt.Errorf("DomainNamesFromBytes: invalid short label length") - } - if label != "" { - label += "." - } - label += string(buf[pos : pos+length]) - pos += length - } -} - -func labelToBytes(label string) []byte { - var encodedLabel []byte - if len(label) == 0 { - return []byte{0} - } - for _, part := range strings.Split(label, ".") { - encodedLabel = append(encodedLabel, byte(len(part))) - encodedLabel = append(encodedLabel, []byte(part)...) - } - return append(encodedLabel, 0) -} - -func labelsToBytes(labels []string) []byte { - var encodedLabels []byte - for _, label := range labels { - encodedLabels = append(encodedLabels, labelToBytes(label)...) - } - return encodedLabels -} diff --git a/dhcpv4/option_rfc1035label_test.go b/dhcpv4/option_rfc1035label_test.go deleted file mode 100644 index cd8189a..0000000 --- a/dhcpv4/option_rfc1035label_test.go +++ /dev/null @@ -1,66 +0,0 @@ -package dhcpv4 - -import ( - "bytes" - "testing" -) - -func TestLabelsFromBytes(t *testing.T) { - labels, err := labelsFromBytes([]byte{ - 0x9, 's', 'l', 'a', 'c', 'k', 'w', 'a', 'r', 'e', - 0x2, 'i', 't', - 0x0, - }) - if err != nil { - t.Fatal(err) - } - if len(labels) != 1 { - t.Fatalf("Invalid labels length. Expected: 1, got: %v", len(labels)) - } - if labels[0] != "slackware.it" { - t.Fatalf("Invalid label. Expected: %v, got: %v'", "slackware.it", labels[0]) - } -} - -func TestLabelsFromBytesZeroLength(t *testing.T) { - labels, err := labelsFromBytes([]byte{}) - if err != nil { - t.Fatal(err) - } - if len(labels) != 0 { - t.Fatalf("Invalid labels length. Expected: 0, got: %v", len(labels)) - } -} - -func TestLabelsFromBytesInvalidLength(t *testing.T) { - labels, err := labelsFromBytes([]byte{0x3, 0xaa, 0xbb}) // short length - if err == nil { - t.Fatal("Expected error, got nil") - } - if len(labels) != 0 { - t.Fatalf("Invalid labels length. Expected: 0, got: %v", len(labels)) - } - if labels != nil { - t.Fatalf("Invalid label. Expected nil, got %v", labels) - } -} - -func TestLabelToBytes(t *testing.T) { - encodedLabel := labelToBytes("slackware.it") - expected := []byte{ - 0x9, 's', 'l', 'a', 'c', 'k', 'w', 'a', 'r', 'e', - 0x2, 'i', 't', - 0x0, - } - if !bytes.Equal(encodedLabel, expected) { - t.Fatalf("Invalid label. Expected: %v, got: %v", expected, encodedLabel) - } -} - -func TestLabelToBytesZeroLength(t *testing.T) { - encodedLabel := labelToBytes("") - expected := []byte{0} - if !bytes.Equal(encodedLabel, expected) { - t.Fatalf("Invalid label. Expected: %v, got: %v", expected, encodedLabel) - } -} |