dns: optimized the parse answer function

This commit is contained in:
nadoo 2018-01-06 13:33:09 +08:00
parent ee0da7cacb
commit 9354caac5c
2 changed files with 21 additions and 17 deletions

28
dns.go
View File

@ -224,23 +224,27 @@ func parseAnswers(p []byte) []*dnsAnswer {
var answers []*dnsAnswer
for i := 0; i < len(p); {
l := int(p[i])
if l == 0 {
i++
// https://tools.ietf.org/html/rfc1035#section-4.1.4
// "Message compression",
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | 1 1| OFFSET |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
if p[i]>>6 == 3 {
i += 2
} else {
// TODO: none compressed query name and Additional records will be ignored
break
}
answer := &dnsAnswer{}
// https://tools.ietf.org/html/rfc1035#section-4.1.4
// i+2 assumes the ANSWER always using "Message compression", start with 2 bytes offset of the query domain.
// TODO: check here
answer.QueryType = binary.BigEndian.Uint16(p[i+2:])
answer.QueryClass = binary.BigEndian.Uint16(p[i+4:])
answer.TTL = binary.BigEndian.Uint32(p[i+6:])
answer.DataLength = binary.BigEndian.Uint16(p[i+10:])
answer.Data = p[i+12 : i+12+int(answer.DataLength)]
answer.QueryType = binary.BigEndian.Uint16(p[i:])
answer.QueryClass = binary.BigEndian.Uint16(p[i+2:])
answer.TTL = binary.BigEndian.Uint32(p[i+4:])
answer.DataLength = binary.BigEndian.Uint16(p[i+8:])
answer.Data = p[i+10 : i+10+int(answer.DataLength)]
if answer.QueryType == DNSQueryTypeA {
answer.IP = net.IP(answer.Data[:net.IPv4len]).String()
@ -250,7 +254,7 @@ func parseAnswers(p []byte) []*dnsAnswer {
answers = append(answers, answer)
i = i + 12 + int(answer.DataLength)
i = i + 10 + int(answer.DataLength)
}
return answers

View File

@ -141,10 +141,10 @@ func CreateSet(fd int, lsa syscall.SockaddrNetlink, setName string) {
}
if len(setName) > IPSET_MAXNAMELEN {
log.Fatal("ipset name too long")
log.Fatal("ipset: name too long")
}
logf("ipset: create %s hash:net", setName)
logf("ipset create %s hash:net", setName)
req := NewNetlinkRequest(IPSET_CMD_CREATE|(NFNL_SUBSYS_IPSET<<8), syscall.NLM_F_REQUEST)
@ -179,7 +179,7 @@ func CreateSet(fd int, lsa syscall.SockaddrNetlink, setName string) {
}
func FlushSet(fd int, lsa syscall.SockaddrNetlink, setName string) {
logf("ipset: flush %s", setName)
logf("ipset flush %s", setName)
req := NewNetlinkRequest(IPSET_CMD_FLUSH|(NFNL_SUBSYS_IPSET<<8), syscall.NLM_F_REQUEST)
@ -201,10 +201,10 @@ func AddToSet(fd int, lsa syscall.SockaddrNetlink, setName, entry string) {
}
if len(setName) > IPSET_MAXNAMELEN {
logf("ipset name too long")
logf("ipset: name too long")
}
logf("ipset: add %s %s", setName, entry)
logf("ipset add %s %s", setName, entry)
var ip net.IP
var cidr *net.IPNet