diff options
author | Pablo Mazzini <pmazzini@gmail.com> | 2018-08-01 18:25:54 +0200 |
---|---|---|
committer | Pablo Mazzini <pmazzini@gmail.com> | 2018-08-01 18:25:54 +0200 |
commit | 833497ba74dfdd0188176ae38a19de7a03a7858a (patch) | |
tree | 4422669864704455d6c1e22128629368ab48f384 | |
parent | 1f58265bc02e94b0c530719cda87da780a3813c1 (diff) |
extract rfc1035label
-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 | ||||
-rw-r--r-- | dhcpv6/option_domainsearchlist.go | 6 | ||||
-rw-r--r-- | dnscompress/label.go (renamed from dhcpv6/option_rfc1035label.go) | 10 | ||||
-rw-r--r-- | dnscompress/label_test.go (renamed from dhcpv6/option_rfc1035label_test.go) | 2 |
6 files changed, 17 insertions, 126 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) - } -} diff --git a/dhcpv6/option_domainsearchlist.go b/dhcpv6/option_domainsearchlist.go index 402c68e..cc56ee0 100644 --- a/dhcpv6/option_domainsearchlist.go +++ b/dhcpv6/option_domainsearchlist.go @@ -6,6 +6,8 @@ package dhcpv6 import ( "encoding/binary" "fmt" + + "github.com/insomniacslk/dhcp/dnscompress" ) // OptDomainSearchList list implements a DOMAIN_SEARCH_LIST option @@ -21,7 +23,7 @@ func (op *OptDomainSearchList) ToBytes() []byte { buf := make([]byte, 4) binary.BigEndian.PutUint16(buf[0:2], uint16(DOMAIN_SEARCH_LIST)) binary.BigEndian.PutUint16(buf[2:4], uint16(op.Length())) - buf = append(buf, LabelsToBytes(op.DomainSearchList)...) + buf = append(buf, dnscompress.LabelsToBytes(op.DomainSearchList)...) return buf } @@ -42,7 +44,7 @@ func (op *OptDomainSearchList) String() string { func ParseOptDomainSearchList(data []byte) (*OptDomainSearchList, error) { opt := OptDomainSearchList{} var err error - opt.DomainSearchList, err = LabelsFromBytes(data) + opt.DomainSearchList, err = dnscompress.LabelsFromBytes(data) if err != nil { return nil, err } diff --git a/dhcpv6/option_rfc1035label.go b/dnscompress/label.go index 06870b5..3d84708 100644 --- a/dhcpv6/option_rfc1035label.go +++ b/dnscompress/label.go @@ -1,10 +1,14 @@ -package dhcpv6 +package dnscompress import ( "fmt" "strings" ) +// This implements the compression from RFC 1035, section 4.1.4 +// https://tools.ietf.org/html/rfc1035 + +// LabelsFromBytes decodes a serialized stream and returns a list of labels func LabelsFromBytes(buf []byte) ([]string, error) { var ( pos = 0 @@ -30,9 +34,9 @@ func LabelsFromBytes(buf []byte) ([]string, error) { label += string(buf[pos : pos+length]) pos += length } - return domains, nil } +// LabelToBytes encodes a label and returns a serialized stream of bytes func LabelToBytes(label string) []byte { var encodedLabel []byte if len(label) == 0 { @@ -45,6 +49,8 @@ func LabelToBytes(label string) []byte { return append(encodedLabel, 0) } +// LabelsToBytes encodes a list of labels and returns a serialized stream of +// bytes func LabelsToBytes(labels []string) []byte { var encodedLabels []byte for _, label := range labels { diff --git a/dhcpv6/option_rfc1035label_test.go b/dnscompress/label_test.go index f99c209..af8e405 100644 --- a/dhcpv6/option_rfc1035label_test.go +++ b/dnscompress/label_test.go @@ -1,4 +1,4 @@ -package dhcpv6 +package dnscompress import ( "bytes" |