mirror of
https://github.com/nadoo/glider.git
synced 2025-02-24 01:45:39 +08:00
dns: optimized the parse answer function
This commit is contained in:
parent
ee0da7cacb
commit
9354caac5c
28
dns.go
28
dns.go
@ -224,23 +224,27 @@ func parseAnswers(p []byte) []*dnsAnswer {
|
|||||||
var answers []*dnsAnswer
|
var answers []*dnsAnswer
|
||||||
|
|
||||||
for i := 0; i < len(p); {
|
for i := 0; i < len(p); {
|
||||||
l := int(p[i])
|
|
||||||
|
|
||||||
if l == 0 {
|
// https://tools.ietf.org/html/rfc1035#section-4.1.4
|
||||||
i++
|
// "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
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
answer := &dnsAnswer{}
|
answer := &dnsAnswer{}
|
||||||
|
|
||||||
// https://tools.ietf.org/html/rfc1035#section-4.1.4
|
answer.QueryType = binary.BigEndian.Uint16(p[i:])
|
||||||
// i+2 assumes the ANSWER always using "Message compression", start with 2 bytes offset of the query domain.
|
answer.QueryClass = binary.BigEndian.Uint16(p[i+2:])
|
||||||
// TODO: check here
|
answer.TTL = binary.BigEndian.Uint32(p[i+4:])
|
||||||
answer.QueryType = binary.BigEndian.Uint16(p[i+2:])
|
answer.DataLength = binary.BigEndian.Uint16(p[i+8:])
|
||||||
answer.QueryClass = binary.BigEndian.Uint16(p[i+4:])
|
answer.Data = p[i+10 : i+10+int(answer.DataLength)]
|
||||||
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)]
|
|
||||||
|
|
||||||
if answer.QueryType == DNSQueryTypeA {
|
if answer.QueryType == DNSQueryTypeA {
|
||||||
answer.IP = net.IP(answer.Data[:net.IPv4len]).String()
|
answer.IP = net.IP(answer.Data[:net.IPv4len]).String()
|
||||||
@ -250,7 +254,7 @@ func parseAnswers(p []byte) []*dnsAnswer {
|
|||||||
|
|
||||||
answers = append(answers, answer)
|
answers = append(answers, answer)
|
||||||
|
|
||||||
i = i + 12 + int(answer.DataLength)
|
i = i + 10 + int(answer.DataLength)
|
||||||
}
|
}
|
||||||
|
|
||||||
return answers
|
return answers
|
||||||
|
@ -141,10 +141,10 @@ func CreateSet(fd int, lsa syscall.SockaddrNetlink, setName string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(setName) > IPSET_MAXNAMELEN {
|
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)
|
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) {
|
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)
|
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 {
|
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 ip net.IP
|
||||||
var cidr *net.IPNet
|
var cidr *net.IPNet
|
||||||
|
Loading…
Reference in New Issue
Block a user