unpackItem converts the data into cacheItem using req as a request message. expired is true if the item exists but expired. The expired cached items are only returned if c is optimistic and optimistic max age is not exceeded. req must not be nil.
(data []byte, req *dns.Msg)
| 114 | // only returned if c is optimistic and optimistic max age is not exceeded. req |
| 115 | // must not be nil. |
| 116 | func (c *cache) unpackItem(data []byte, req *dns.Msg) (ci *cacheItem, expired bool) { |
| 117 | if len(data) < minPackedLen { |
| 118 | return nil, false |
| 119 | } |
| 120 | |
| 121 | b := bytes.NewBuffer(data) |
| 122 | expire := time.Unix(int64(binary.BigEndian.Uint32(b.Next(expTimeSz))), 0) |
| 123 | now := time.Now() |
| 124 | var ttl uint32 |
| 125 | if expired = now.After(expire); expired { |
| 126 | optimisticExpire := expire.Add(c.optimisticMaxAge) |
| 127 | if !c.optimistic || now.After(optimisticExpire) { |
| 128 | return nil, expired |
| 129 | } |
| 130 | |
| 131 | ttl = uint32(c.optimisticTTL.Seconds()) |
| 132 | } else { |
| 133 | ttl = uint32(expire.Unix() - now.Unix()) |
| 134 | } |
| 135 | |
| 136 | l := int(binary.BigEndian.Uint16(b.Next(packedMsgLenSz))) |
| 137 | if l == 0 { |
| 138 | return nil, expired |
| 139 | } |
| 140 | |
| 141 | m := &dns.Msg{} |
| 142 | if m.Unpack(b.Next(l)) != nil { |
| 143 | return nil, expired |
| 144 | } |
| 145 | |
| 146 | res := (&dns.Msg{}).SetRcode(req, m.Rcode) |
| 147 | res.AuthenticatedData = m.AuthenticatedData |
| 148 | res.RecursionAvailable = m.RecursionAvailable |
| 149 | |
| 150 | var doBit bool |
| 151 | if o := req.IsEdns0(); o != nil { |
| 152 | doBit = o.Do() |
| 153 | } |
| 154 | |
| 155 | // Don't return OPT records from cache since it's deprecated by RFC 6891. |
| 156 | // If the request has DO bit set we only remove all the OPT RRs, and also |
| 157 | // all DNSSEC RRs otherwise. |
| 158 | filterMsg(res, m, req.AuthenticatedData, doBit, ttl) |
| 159 | |
| 160 | return &cacheItem{ |
| 161 | m: res, |
| 162 | u: string(b.Next(b.Len())), |
| 163 | }, expired |
| 164 | } |
| 165 | |
| 166 | // initCache initializes cache if it's enabled. |
| 167 | func (p *Proxy) initCache() { |