| 1 | package packets |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | // TODO: refactor to use gopacket when gopacket folks |
| 6 | // will fix this > https://github.com/google/gopacket/issues/334 |
| 7 | "github.com/mdlayher/dhcp6" |
| 8 | ) |
| 9 | |
| 10 | const DHCP6OptDNSServers = 23 |
| 11 | const DHCP6OptDNSDomains = 24 |
| 12 | const DHCP6OptClientFQDN = 39 |
| 13 | |
| 14 | // link-local |
| 15 | const IPv6Prefix = "fe80::" |
| 16 | |
| 17 | var ( |
| 18 | ErrNoCID = errors.New("Unexpected DHCPv6 packet, could not find client id.") |
| 19 | ) |
| 20 | |
| 21 | func DHCP6EncodeList(elements []string) (encoded []byte) { |
| 22 | encoded = make([]byte, 0) |
| 23 | |
| 24 | for _, elem := range elements { |
| 25 | // this would be worth fuzzing btw |
| 26 | encoded = append(encoded, byte(len(elem)&0xff)) |
| 27 | encoded = append(encoded, []byte(elem)...) |
| 28 | } |
| 29 | |
| 30 | return |
| 31 | } |
| 32 | |
| 33 | func DHCP6For(what dhcp6.MessageType, to dhcp6.Packet, duid []byte) (err error, p dhcp6.Packet) { |
| 34 | p = dhcp6.Packet{ |
| 35 | MessageType: what, |
| 36 | TransactionID: to.TransactionID, |
| 37 | Options: make(dhcp6.Options), |
| 38 | } |
| 39 | |
| 40 | var rawCID []byte |
| 41 | if raw, found := to.Options[dhcp6.OptionClientID]; !found || len(raw) < 1 { |
| 42 | return ErrNoCID, p |
| 43 | } else { |
| 44 | rawCID = raw[0] |
| 45 | } |
| 46 | |
| 47 | p.Options.AddRaw(dhcp6.OptionClientID, rawCID) |
| 48 | p.Options.AddRaw(dhcp6.OptionServerID, duid) |
| 49 | |
| 50 | return nil, p |
| 51 | } |