dns/rawmsg.go

65 lines
1.8 KiB
Go
Raw Normal View History

2012-01-12 20:53:19 +11:00
// Copyright 2012 Miek Gieben. All rights reserved.
2011-08-08 22:09:05 +10:00
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package dns
2012-05-03 16:53:38 +10:00
// These Raw* functions do not use reflection, they directly set the values
// in the buffer. There are faster than their reflection counterparts.
2012-03-19 08:41:29 +11:00
// RawSetId sets the message id in buf.
func RawSetId(msg []byte, i uint16) {
msg[0], msg[1] = packUint16(i)
}
2012-08-24 20:42:41 +10:00
// rawSetQuestionLen sets the lenght of the question section.
func rawSetQuestionLen(msg []byte, i uint16) {
msg[4], msg[5] = packUint16(i)
}
2012-08-24 20:42:41 +10:00
// rawSetAnswerLen sets the lenght of the answer section.
func rawSetAnswerLen(msg []byte, i uint16) {
msg[6], msg[7] = packUint16(i)
}
2012-08-24 20:42:41 +10:00
// rawSetsNsLen sets the lenght of the authority section.
func rawSetNsLen(msg []byte, i uint16) {
msg[8], msg[9] = packUint16(i)
}
2012-08-24 20:42:41 +10:00
// rawSetExtraLen sets the lenght of the additional section.
func rawSetExtraLen(msg []byte, i uint16) {
msg[10], msg[11] = packUint16(i)
2011-08-09 00:29:13 +10:00
}
2012-01-12 02:02:05 +11:00
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. There is no check if we overrun the buffer.
2012-08-24 20:42:41 +10:00
func rawSetRdlength(msg []byte, off, end int) {
2012-01-12 02:02:05 +11:00
Loop:
for {
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
}
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
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 lenght of the rdata
msg[off], msg[off+1] = packUint16(uint16(end - (off + 2)))
return
2012-01-12 02:02:05 +11:00
}