|
| 1 | +package dhcpv6 |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net" |
| 6 | + |
| 7 | + "github.com/insomniacslk/dhcp/rfc1035label" |
| 8 | + "github.com/u-root/uio/uio" |
| 9 | +) |
| 10 | + |
| 11 | +// NTPSuboptionSrvAddr is NTP_SUBOPTION_SRV_ADDR according to RFC 5908. |
| 12 | +type NTPSuboptionSrvAddr net.IP |
| 13 | + |
| 14 | +// Code returns the suboption code. |
| 15 | +func (n *NTPSuboptionSrvAddr) Code() OptionCode { |
| 16 | + return NTPSuboptionSrvAddrCode |
| 17 | +} |
| 18 | + |
| 19 | +// ToBytes returns the byte serialization of the suboption. |
| 20 | +func (n *NTPSuboptionSrvAddr) ToBytes() []byte { |
| 21 | + buf := uio.NewBigEndianBuffer(nil) |
| 22 | + buf.Write16(uint16(NTPSuboptionSrvAddrCode)) |
| 23 | + buf.Write16(uint16(net.IPv6len)) |
| 24 | + buf.WriteBytes(net.IP(*n).To16()) |
| 25 | + return buf.Data() |
| 26 | +} |
| 27 | + |
| 28 | +func (n *NTPSuboptionSrvAddr) String() string { |
| 29 | + return fmt.Sprintf("Server Address: %s", net.IP(*n).String()) |
| 30 | +} |
| 31 | + |
| 32 | +// NTPSuboptionMCAddr is NTP_SUBOPTION_MC_ADDR according to RFC 5908. |
| 33 | +type NTPSuboptionMCAddr net.IP |
| 34 | + |
| 35 | +// Code returns the suboption code. |
| 36 | +func (n *NTPSuboptionMCAddr) Code() OptionCode { |
| 37 | + return NTPSuboptionMCAddrCode |
| 38 | +} |
| 39 | + |
| 40 | +// ToBytes returns the byte serialization of the suboption. |
| 41 | +func (n *NTPSuboptionMCAddr) ToBytes() []byte { |
| 42 | + buf := uio.NewBigEndianBuffer(nil) |
| 43 | + buf.Write16(uint16(NTPSuboptionMCAddrCode)) |
| 44 | + buf.Write16(uint16(net.IPv6len)) |
| 45 | + buf.WriteBytes(net.IP(*n).To16()) |
| 46 | + return buf.Data() |
| 47 | +} |
| 48 | + |
| 49 | +func (n *NTPSuboptionMCAddr) String() string { |
| 50 | + return fmt.Sprintf("Multicast Address: %s", net.IP(*n).String()) |
| 51 | +} |
| 52 | + |
| 53 | +// NTPSuboptionSrvFQDN is NTP_SUBOPTION_SRV_FQDN according to RFC 5908. |
| 54 | +type NTPSuboptionSrvFQDN rfc1035label.Labels |
| 55 | + |
| 56 | +// Code returns the suboption code. |
| 57 | +func (n *NTPSuboptionSrvFQDN) Code() OptionCode { |
| 58 | + return NTPSuboptionSrvFQDNCode |
| 59 | +} |
| 60 | + |
| 61 | +// ToBytes returns the byte serialization of the suboption. |
| 62 | +func (n *NTPSuboptionSrvFQDN) ToBytes() []byte { |
| 63 | + buf := uio.NewBigEndianBuffer(nil) |
| 64 | + buf.Write16(uint16(NTPSuboptionSrvFQDNCode)) |
| 65 | + l := rfc1035label.Labels(*n) |
| 66 | + buf.Write16(uint16(l.Length())) |
| 67 | + buf.WriteBytes(l.ToBytes()) |
| 68 | + return buf.Data() |
| 69 | +} |
| 70 | + |
| 71 | +func (n *NTPSuboptionSrvFQDN) String() string { |
| 72 | + l := rfc1035label.Labels(*n) |
| 73 | + return fmt.Sprintf("Server FQDN: %s", l.String()) |
| 74 | +} |
| 75 | + |
| 76 | +// NTPSuboptionSrvAddr is the value of NTP_SUBOPTION_SRV_ADDR according to RFC 5908. |
| 77 | +const ( |
| 78 | + NTPSuboptionSrvAddrCode = OptionCode(1) |
| 79 | + NTPSuboptionMCAddrCode = OptionCode(2) |
| 80 | + NTPSuboptionSrvFQDNCode = OptionCode(3) |
| 81 | +) |
| 82 | + |
| 83 | +// parseNTPSuboption implements the OptionParser interface. |
| 84 | +func parseNTPSuboption(code OptionCode, data []byte) (Option, error) { |
| 85 | + //var o Options |
| 86 | + buf := uio.NewBigEndianBuffer(data) |
| 87 | + length := len(data) |
| 88 | + data, err := buf.ReadN(length) |
| 89 | + if err != nil { |
| 90 | + return nil, fmt.Errorf("failed to read %d bytes for suboption: %w", length, err) |
| 91 | + } |
| 92 | + switch code { |
| 93 | + case NTPSuboptionSrvAddrCode, NTPSuboptionMCAddrCode: |
| 94 | + if length != net.IPv6len { |
| 95 | + return nil, fmt.Errorf("invalid suboption length, want %d, got %d", net.IPv6len, length) |
| 96 | + } |
| 97 | + var so Option |
| 98 | + switch code { |
| 99 | + case NTPSuboptionSrvAddrCode: |
| 100 | + sos := NTPSuboptionSrvAddr(data) |
| 101 | + so = &sos |
| 102 | + case NTPSuboptionMCAddrCode: |
| 103 | + som := NTPSuboptionMCAddr(data) |
| 104 | + so = &som |
| 105 | + } |
| 106 | + return so, nil |
| 107 | + case NTPSuboptionSrvFQDNCode: |
| 108 | + l, err := rfc1035label.FromBytes(data) |
| 109 | + if err != nil { |
| 110 | + return nil, fmt.Errorf("failed to parse rfc1035 labels: %w", err) |
| 111 | + } |
| 112 | + // TODO according to rfc3315, this label must not be compressed. |
| 113 | + // Need to add support for compression detection to the |
| 114 | + // `rfc1035label` package in order to do that. |
| 115 | + so := NTPSuboptionSrvFQDN(*l) |
| 116 | + return &so, nil |
| 117 | + default: |
| 118 | + gopt := OptionGeneric{OptionCode: code, OptionData: data} |
| 119 | + return &gopt, nil |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +// ParseOptNTPServer parses a sequence of bytes into an OptNTPServer object. |
| 124 | +func ParseOptNTPServer(data []byte) (*OptNTPServer, error) { |
| 125 | + var so Options |
| 126 | + if err := so.FromBytesWithParser(data, parseNTPSuboption); err != nil { |
| 127 | + return nil, err |
| 128 | + } |
| 129 | + return &OptNTPServer{ |
| 130 | + Suboptions: so, |
| 131 | + }, nil |
| 132 | +} |
| 133 | + |
| 134 | +// OptNTPServer is an option NTP server as defined by RFC 5908. |
| 135 | +type OptNTPServer struct { |
| 136 | + Suboptions Options |
| 137 | +} |
| 138 | + |
| 139 | +// Code returns the option code |
| 140 | +func (op *OptNTPServer) Code() OptionCode { |
| 141 | + return OptionNTPServer |
| 142 | +} |
| 143 | + |
| 144 | +// ToBytes returns the option serialized to bytes. |
| 145 | +func (op *OptNTPServer) ToBytes() []byte { |
| 146 | + buf := uio.NewBigEndianBuffer(nil) |
| 147 | + for _, so := range op.Suboptions { |
| 148 | + buf.WriteBytes(so.ToBytes()) |
| 149 | + } |
| 150 | + return buf.Data() |
| 151 | +} |
| 152 | + |
| 153 | +func (op *OptNTPServer) String() string { |
| 154 | + return fmt.Sprintf("NTP: %v", op.Suboptions) |
| 155 | +} |
0 commit comments