File: option_dns.go

package info (click to toggle)
golang-github-insomniacslk-dhcp 0.0~git20250417.5f8cf70-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 1,096 kB
  • sloc: makefile: 3
file content (45 lines) | stat: -rw-r--r-- 1,025 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package dhcpv6

import (
	"fmt"
	"net"

	"github.com/u-root/uio/uio"
)

// OptDNS returns a DNS Recursive Name Server option as defined by RFC 3646.
func OptDNS(ip ...net.IP) Option {
	return &optDNS{NameServers: ip}
}

type optDNS struct {
	NameServers []net.IP
}

// Code returns the option code
func (op *optDNS) Code() OptionCode {
	return OptionDNSRecursiveNameServer
}

// ToBytes returns the option serialized to bytes.
func (op *optDNS) ToBytes() []byte {
	buf := uio.NewBigEndianBuffer(nil)
	for _, ns := range op.NameServers {
		buf.WriteBytes(ns.To16())
	}
	return buf.Data()
}

func (op *optDNS) String() string {
	return fmt.Sprintf("%s: %v", op.Code(), op.NameServers)
}

// FromBytes builds an optDNS structure from a sequence of bytes. The input
// data does not include option code and length bytes.
func (op *optDNS) FromBytes(data []byte) error {
	buf := uio.NewBigEndianBuffer(data)
	for buf.Has(net.IPv6len) {
		op.NameServers = append(op.NameServers, buf.CopyN(net.IPv6len))
	}
	return buf.FinError()
}