dns/rawmsg.go

50 lines
1.1 KiB
Go
Raw Normal View History

2011-08-08 22:09:05 +10:00
package dns
import "encoding/binary"
2012-08-24 20:42:41 +10:00
// rawSetRdlength sets the rdlength in the header of
2012-01-12 20:50:01 +11:00
// the RR. The offset 'off' must be positioned at the
2012-01-13 07:10:29 +11:00
// start of the header of the RR, 'end' must be the
// end of the RR.
func rawSetRdlength(msg []byte, off, end int) bool {
l := len(msg)
2012-01-12 02:02:05 +11:00
Loop:
for {
if off+1 > l {
return false
}
2012-01-12 02:02:05 +11:00
c := int(msg[off])
off++
switch c & 0xC0 {
2012-01-12 02:02:05 +11:00
case 0x00:
if c == 0x00 {
// End of the domainname
break Loop
}
2013-06-15 04:42:55 +10:00
if off+c > l {
return false
}
2013-06-15 04:42:55 +10:00
off += c
2012-01-12 02:02:05 +11:00
case 0xC0:
2012-01-13 07:10:29 +11:00
// pointer, next byte included, ends domainname
2012-01-12 02:02:05 +11:00
off++
break Loop
}
}
2012-01-13 07:10:29 +11:00
// The domainname has been seen, we at the start of the fixed part in the header.
// Type is 2 bytes, class is 2 bytes, ttl 4 and then 2 bytes for the length.
2012-01-12 02:02:05 +11:00
off += 2 + 2 + 4
if off+2 > l {
return false
}
2012-01-13 07:10:29 +11:00
//off+1 is the end of the header, 'end' is the end of the rr
//so 'end' - 'off+2' is the length of the rdata
rdatalen := end - (off + 2)
if rdatalen > 0xFFFF {
return false
}
binary.BigEndian.PutUint16(msg[off:], uint16(rdatalen))
return true
2012-01-12 02:02:05 +11:00
}