1package dns
2
3import "encoding/binary"
4
5// rawSetRdlength sets the rdlength in the header of
6// the RR. The offset 'off' must be positioned at the
7// start of the header of the RR, 'end' must be the
8// end of the RR.
9func rawSetRdlength(msg []byte, off, end int) bool {
10	l := len(msg)
11Loop:
12	for {
13		if off+1 > l {
14			return false
15		}
16		c := int(msg[off])
17		off++
18		switch c & 0xC0 {
19		case 0x00:
20			if c == 0x00 {
21				// End of the domainname
22				break Loop
23			}
24			if off+c > l {
25				return false
26			}
27			off += c
28
29		case 0xC0:
30			// pointer, next byte included, ends domainname
31			off++
32			break Loop
33		}
34	}
35	// The domainname has been seen, we at the start of the fixed part in the header.
36	// Type is 2 bytes, class is 2 bytes, ttl 4 and then 2 bytes for the length.
37	off += 2 + 2 + 4
38	if off+2 > l {
39		return false
40	}
41	//off+1 is the end of the header, 'end' is the end of the rr
42	//so 'end' - 'off+2' is the length of the rdata
43	rdatalen := end - (off + 2)
44	if rdatalen > 0xFFFF {
45		return false
46	}
47	binary.BigEndian.PutUint16(msg[off:], uint16(rdatalen))
48	return true
49}
50