Skip to content

Commit

Permalink
proxy: rm unsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Nov 25, 2021
1 parent d36871a commit c96cc94
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions proxy/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"
"sync"
"time"
"unsafe"

glcache "github.com/AdguardTeam/golibs/cache"
"github.com/AdguardTeam/golibs/log"
Expand Down Expand Up @@ -48,13 +47,15 @@ type cacheItem struct {
}

const (
// uint16sz is the size of uint16 type in bytes.
uint16sz = int(unsafe.Sizeof(uint16(0)))
// uint32sz is the size of uint32 type in bytes.
uint32sz = int(unsafe.Sizeof(uint32(0)))
// packedMsgLenSz is the exact length of byte slice capable to store the
// length of packed DNS message. It's essentially the size of an int.
packedMsgLenSz = 2
// expTimeSz is the exact length of byte slice capable to store the
// expiration time the response. It's essentially the size of a uint32.
expTimeSz = 4

// minPackedLen is the minimum length of the packed cacheItem.
minPackedLen = uint32sz + uint16sz
minPackedLen = expTimeSz + packedMsgLenSz
)

// pack converts the ci into bytes slice.
Expand All @@ -67,7 +68,7 @@ func (ci *cacheItem) pack() (packed []byte) {
binary.BigEndian.PutUint32(packed, uint32(time.Now().Unix())+lowestTTL(ci.m))

// Put the length of the packed message.
binary.BigEndian.PutUint16(packed[4:], uint16(pmLen))
binary.BigEndian.PutUint16(packed[expTimeSz:], uint16(pmLen))

// Put the packed message itself.
packed = append(packed, pm...)
Expand All @@ -90,7 +91,7 @@ func (c *cache) unpackItem(data []byte, req *dns.Msg) (ci *cacheItem, expired bo
}

b := bytes.NewBuffer(data)
expire := int64(binary.BigEndian.Uint32(b.Next(uint32sz)))
expire := int64(binary.BigEndian.Uint32(b.Next(expTimeSz)))
now := time.Now().Unix()
var ttl uint32
if expired = expire <= now; expired {
Expand All @@ -103,7 +104,7 @@ func (c *cache) unpackItem(data []byte, req *dns.Msg) (ci *cacheItem, expired bo
ttl = uint32(expire - now)
}

l := int(binary.BigEndian.Uint16(b.Next(uint16sz)))
l := int(binary.BigEndian.Uint16(b.Next(packedMsgLenSz)))
if l == 0 {
return nil, expired
}
Expand Down Expand Up @@ -414,12 +415,12 @@ func respectTTLOverrides(ttl uint32, cacheMinTTL uint32, cacheMaxTTL uint32) uin
func msgToKey(m *dns.Msg) (b []byte) {
q := m.Question[0]
name := q.Name
b = make([]byte, uint16sz+uint16sz+len(name))
b = make([]byte, packedMsgLenSz+packedMsgLenSz+len(name))

// Put QTYPE, QCLASS, and QNAME.
binary.BigEndian.PutUint16(b, q.Qtype)
binary.BigEndian.PutUint16(b[uint16sz:], q.Qclass)
copy(b[2*uint16sz:], strings.ToLower(name))
binary.BigEndian.PutUint16(b[packedMsgLenSz:], q.Qclass)
copy(b[2*packedMsgLenSz:], strings.ToLower(name))

return b
}
Expand All @@ -428,7 +429,7 @@ func msgToKey(m *dns.Msg) (b []byte) {
// mask, client's IP address and question's name of m.
func msgToKeyWithSubnet(m *dns.Msg, clientIP net.IP, mask uint8) (key []byte) {
q := m.Question[0]
cap := 1 + 2*uint16sz + 1 + len(q.Name)
cap := 1 + 2*packedMsgLenSz + 1 + len(q.Name)
ipLen := len(clientIP)
masked := mask != 0
if masked {
Expand All @@ -449,11 +450,11 @@ func msgToKeyWithSubnet(m *dns.Msg, clientIP net.IP, mask uint8) (key []byte) {

// Put Qtype.
binary.BigEndian.PutUint16(key[:], q.Qtype)
k += uint16sz
k += packedMsgLenSz

// Put Qclass.
binary.BigEndian.PutUint16(key[k:], q.Qclass)
k += uint16sz
k += packedMsgLenSz

// Add mask.
key[k] = mask
Expand Down

0 comments on commit c96cc94

Please sign in to comment.