本文整理匯總了Golang中github.com/miekg/dns.Copy函數的典型用法代碼示例。如果您正苦於以下問題:Golang Copy函數的具體用法?Golang Copy怎麽用?Golang Copy使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Copy函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: signSet
func (s *server) signSet(r []dns.RR, now time.Time, incep, expir uint32) (*dns.RRSIG, error) {
key := cache.Key(r)
if m, exp, hit := s.scache.Search(key); hit { // There can only be one sig in this cache.
// Is it still valid 24 hours from now?
if now.Add(+24*time.Hour).Sub(exp) < -24*time.Hour {
return m.Answer[0].(*dns.RRSIG), nil
}
s.scache.Remove(key)
}
logf("scache miss for %s type %d", r[0].Header().Name, r[0].Header().Rrtype)
StatsDnssecCacheMiss.Inc(1)
promCacheMiss.WithLabelValues("signature").Inc()
sig, err, shared := inflight.Do(key, func() (*dns.RRSIG, error) {
sig1 := s.NewRRSIG(incep, expir)
sig1.Header().Ttl = r[0].Header().Ttl
if r[0].Header().Rrtype == dns.TypeTXT {
sig1.OrigTtl = 0
}
e := sig1.Sign(s.config.PrivKey, r)
if e != nil {
logf("failed to sign: %s", e.Error())
}
return sig1, e
})
if err != nil {
return nil, err
}
if !shared {
s.scache.InsertSignature(key, sig)
}
return dns.Copy(sig).(*dns.RRSIG), nil
}
示例2: signSet
func (s *server) signSet(r []dns.RR, now time.Time, incep, expir uint32) (*dns.RRSIG, error) {
key := cache.key(r)
if sig := cache.search(key); sig != nil {
// Is it still valid 24 hours from now?
if sig.ValidityPeriod(now.Add(+24 * time.Hour)) {
return sig, nil
}
cache.remove(key)
}
s.config.log.Infof("cache miss for %s type %d", r[0].Header().Name, r[0].Header().Rrtype)
StatsDnssecCacheMiss.Inc(1)
sig, err, shared := inflight.Do(key, func() (*dns.RRSIG, error) {
sig1 := s.NewRRSIG(incep, expir)
sig1.Header().Ttl = r[0].Header().Ttl
if r[0].Header().Rrtype == dns.TypeTXT {
sig1.OrigTtl = 0
}
e := sig1.Sign(s.config.PrivKey, r)
if e != nil {
s.config.log.Errorf("failed to sign: %s", e.Error())
}
return sig1, e
})
if err != nil {
return nil, err
}
if !shared {
cache.insert(key, sig)
}
return dns.Copy(sig).(*dns.RRSIG), nil
}
示例3: copyRRSlice
func copyRRSlice(a []dns.RR) []dns.RR {
b := make([]dns.RR, 0, len(a))
for _, rr := range a {
b = append(b, dns.Copy(rr))
}
return b
}
示例4: signSet
func (s *server) signSet(r []dns.RR, now time.Time, incep, expir uint32) (*dns.RRSIG, error) {
key := cache.KeyRRset(r)
if m, exp, hit := s.scache.Search(key); hit { // There can only be one sig in this cache.
// Is it still valid 24 hours from now?
if now.Add(+24*time.Hour).Sub(exp) < -24*time.Hour {
return m.Answer[0].(*dns.RRSIG), nil
}
s.scache.Remove(key)
}
if s.config.Verbose {
logf("scache miss for %s type %d", r[0].Header().Name, r[0].Header().Rrtype)
}
metrics.ReportCacheMiss("signature")
sig, err := inflight.Do(key, func() (interface{}, error) {
sig1 := s.NewRRSIG(incep, expir)
sig1.Header().Ttl = r[0].Header().Ttl
if r[0].Header().Rrtype == dns.TypeTXT {
sig1.OrigTtl = 0
}
e := sig1.Sign(s.config.PrivKey, r)
if e != nil {
logf("failed to sign: %s", e.Error())
}
return sig1, e
})
if err != nil {
return nil, err
}
s.scache.InsertSignature(key, sig.(*dns.RRSIG))
return dns.Copy(sig.(*dns.RRSIG)).(*dns.RRSIG), nil
}
示例5: cacheCopy
// cacheCopy performs a deep copy of the given resource records
func cacheCopy(rr []dns.RR) []dns.RR {
clone := make([]dns.RR, len(rr))
for i := range rr {
clone[i] = dns.Copy(rr[i])
}
return clone
}
示例6: wildcardReplace
// wildcardReplace replaces the ownername with the original query name.
func wildcardReplace(qname, ce string, rrs []dns.RR) []dns.RR {
// need to copy here, otherwise we change in zone stuff
ret := make([]dns.RR, len(rrs))
for i, r := range rrs {
ret[i] = dns.Copy(r)
ret[i].Header().Name = qname
}
return ret
}
示例7: Do
func (g LimitAnswers) Do(request, response *dns.Msg) {
answers := make([]dns.RR, 0)
for i, answer := range response.Answer {
if i >= g.Limit {
break
}
answers = append(answers, dns.Copy(answer))
}
response.Answer = answers
}
示例8: search
func (c *sigCache) search(s string) *dns.RRSIG {
c.RLock()
defer c.RUnlock()
if s, ok := c.m[s]; ok {
// we want to return a copy here, because if we didn't the RRSIG
// could be removed by another goroutine before the packet containing
// this signature is send out.
return dns.Copy(s).(*dns.RRSIG)
}
return nil
}
示例9: Search
// Search returns .... and a boolean indicating if we found something
// in the cache.
func (c *Cache) Search(s string) ([]dns.RR, []dns.RR, time.Time, bool) {
if c.capacity == 0 {
return nil, nil, time.Time{}, false
}
c.Lock()
defer c.Unlock()
if e, ok := c.m[s]; ok {
c.l.MoveToFront(e)
e := e.Value.(*elem)
answer := make([]dns.RR, len(e.answer))
extra := make([]dns.RR, len(e.extra))
for i, r := range e.answer {
// we want to return a copy here, because if we didn't the RRSIG
// could be removed by another goroutine before the packet containing
// this signature is send out.
answer[i] = dns.Copy(r)
}
for i, r := range e.extra {
extra[i] = dns.Copy(r)
}
return answer, extra, e.expiration, true
}
return nil, nil, time.Time{}, false
}
示例10: InsertSignature
// InsertSignature inserts a signature, the expiration time is used as the cache ttl.
func (c *Cache) InsertSignature(s string, sig *dns.RRSIG) {
if c.capacity <= 0 {
return
}
c.Lock()
if _, ok := c.m[s]; !ok {
m := ((int64(sig.Expiration) - time.Now().Unix()) / (1 << 31)) - 1
if m < 0 {
m = 0
}
t := time.Unix(int64(sig.Expiration)-(m*(1<<31)), 0).UTC()
c.m[s] = &elem{t, &dns.Msg{Answer: []dns.RR{dns.Copy(sig)}}}
}
c.EvictRandom()
c.Unlock()
}
示例11: getDNSKEY
// getDNSKEY returns the correct DNSKEY to the client. Signatures are added when do is true.
func (d Dnssec) getDNSKEY(state middleware.State, zone string, do bool) *dns.Msg {
keys := make([]dns.RR, len(d.keys))
for i, k := range d.keys {
keys[i] = dns.Copy(k.K)
keys[i].Header().Name = zone
}
m := new(dns.Msg)
m.SetReply(state.Req)
m.Answer = keys
if !do {
return m
}
incep, expir := incepExpir(time.Now().UTC())
if sigs, err := d.sign(keys, zone, 3600, incep, expir); err == nil {
m.Answer = append(m.Answer, sigs...)
}
return m
}
示例12: TestMarshalCanonicalCAASet
func TestMarshalCanonicalCAASet(t *testing.T) {
a, b := new(dns.CAA), new(dns.CAA)
a.Value, b.Value = "a", "b"
setA := []*dns.CAA{a, b}
setB := []*dns.CAA{b, a}
canonA, err := marshalCanonicalCAASet(setA)
test.AssertNotError(t, err, "marshalCanonicalCAASet failed")
canonB, err := marshalCanonicalCAASet(setB)
test.AssertNotError(t, err, "marshalCanonicalCAASet failed")
test.Assert(t, bytes.Equal(canonA, canonB), "sets do not match")
cRR := dns.Copy(b)
c := cRR.(*dns.CAA)
c.Value = "c"
c.Hdr.Ttl = 100
hashC, err := marshalCanonicalCAASet([]*dns.CAA{c, a})
test.AssertNotError(t, err, "marshalCanonicalCAASet failed")
test.AssertEquals(t, c.Hdr.Ttl, uint32(100))
test.Assert(t, bytes.Equal(canonA, canonB), fmt.Sprintf("Mismatching sets had same bytes: %x == %x", hashC, canonB))
}
示例13: Do
func (g ReplaceType) Do(request, response *dns.Msg) {
from_type := "*"
to_type := ""
if strings.Index(g.Type, ":") >= 0 {
fields := strings.Split(g.Type, ":")
from_type, to_type = fields[0], fields[1]
} else {
to_type = g.Type
}
for i, answer := range response.Answer {
answer = dns.Copy(answer)
header := answer.Header()
if from_type == "*" || header.Rrtype == dns.StringToType[from_type] {
header.Rrtype = dns.StringToType[to_type]
}
response.Answer[i] = answer
}
}
示例14: serve
//.........這裏部分代碼省略.........
netmask = 16
}
edns.SourceScope = uint8(netmask)
m.Extra = append(m.Extra, opt_rr)
}
}
labels, labelQtype := z.findLabels(label, targets, qTypes{dns.TypeMF, dns.TypeCNAME, qtype})
if labelQtype == 0 {
labelQtype = qtype
}
if labels == nil {
permitDebug := !*flagPrivateDebug || (realIP != nil && realIP.IsLoopback())
firstLabel := (strings.Split(label, "."))[0]
if qle != nil {
qle.LabelName = firstLabel
}
if permitDebug && firstLabel == "_status" {
if qtype == dns.TypeANY || qtype == dns.TypeTXT {
m.Answer = statusRR(label + "." + z.Origin + ".")
} else {
m.Ns = append(m.Ns, z.SoaRR())
}
m.Authoritative = true
w.WriteMsg(m)
return
}
if firstLabel == "_country" {
if qtype == dns.TypeANY || qtype == dns.TypeTXT {
h := dns.RR_Header{Ttl: 1, Class: dns.ClassINET, Rrtype: dns.TypeTXT}
h.Name = label + "." + z.Origin + "."
txt := []string{
w.RemoteAddr().String(),
ip.String(),
}
targets, netmask := z.Options.Targeting.GetTargets(ip)
txt = append(txt, strings.Join(targets, " "))
txt = append(txt, fmt.Sprintf("/%d", netmask), serverID, serverIP)
m.Answer = []dns.RR{&dns.TXT{Hdr: h,
Txt: txt,
}}
} else {
m.Ns = append(m.Ns, z.SoaRR())
}
m.Authoritative = true
w.WriteMsg(m)
return
}
// return NXDOMAIN
m.SetRcode(req, dns.RcodeNameError)
m.Authoritative = true
m.Ns = []dns.RR{z.SoaRR()}
w.WriteMsg(m)
return
}
if servers := labels.Picker(labelQtype, labels.MaxHosts); servers != nil {
var rrs []dns.RR
for _, record := range servers {
rr := dns.Copy(record.RR)
rr.Header().Name = qname
rrs = append(rrs, rr)
}
m.Answer = rrs
}
if len(m.Answer) == 0 {
// Return a SOA so the NOERROR answer gets cached
m.Ns = append(m.Ns, z.SoaRR())
}
logPrintln(m)
if qle != nil {
qle.LabelName = labels.Label
qle.Answers = len(m.Answer)
qle.Rcode = m.Rcode
}
err := w.WriteMsg(m)
if err != nil {
// if Pack'ing fails the Write fails. Return SERVFAIL.
log.Println("Error writing packet", m)
dns.HandleFailed(w, req)
}
return
}
示例15: sign
// sign signs a message m, it takes care of negative or nodata responses as
// well by synthesising NSEC3 records. It will also cache the signatures, using
// a hash of the signed data as a key.
// We also fake the origin TTL in the signature, because we don't want to
// throw away signatures when services decide to have longer TTL. So we just
// set the origTTL to 60.
func (s *server) sign(m *dns.Msg, bufsize uint16) {
now := time.Now().UTC()
incep := uint32(now.Add(-2 * time.Hour).Unix()) // 2 hours, be sure to catch daylight saving time and such
expir := uint32(now.Add(7 * 24 * time.Hour).Unix()) // sign for a week
// TODO(miek): repeating this two times?
for _, r := range rrSets(m.Answer) {
if r[0].Header().Rrtype == dns.TypeRRSIG {
continue
}
key := cache.key(r)
if s := cache.search(key); s != nil {
if s.ValidityPeriod(now.Add(-24 * time.Hour)) {
m.Answer = append(m.Answer, s)
continue
}
cache.remove(key)
}
sig, err, shared := inflight.Do(key, func() (*dns.RRSIG, error) {
sig1 := s.NewRRSIG(incep, expir)
if r[0].Header().Rrtype == dns.TypeNSEC3 {
sig1.OrigTtl = s.config.MinTtl
sig1.Header().Ttl = s.config.MinTtl
}
e := sig1.Sign(s.config.PrivKey, r)
if e != nil {
log.Printf("failed to sign: %s\n", e.Error())
}
return sig1, e
})
if err != nil {
continue
}
if !shared {
// is it possible to miss this, due the the c.dups > 0 in Do()? TODO(miek)
cache.insert(key, sig)
}
m.Answer = append(m.Answer, dns.Copy(sig).(*dns.RRSIG))
}
for _, r := range rrSets(m.Ns) {
if r[0].Header().Rrtype == dns.TypeRRSIG {
continue
}
key := cache.key(r)
if s := cache.search(key); s != nil {
if s.ValidityPeriod(now.Add(-24 * time.Hour)) {
m.Ns = append(m.Ns, s)
continue
}
cache.remove(key)
}
sig, err, shared := inflight.Do(key, func() (*dns.RRSIG, error) {
sig1 := s.NewRRSIG(incep, expir)
if r[0].Header().Rrtype == dns.TypeNSEC3 {
sig1.OrigTtl = s.config.MinTtl
sig1.Header().Ttl = s.config.MinTtl
}
e := sig1.Sign(s.config.PrivKey, r)
if e != nil {
log.Printf("failed to sign: %s\n", e.Error())
}
return sig1, e
})
if err != nil {
continue
}
if !shared {
// is it possible to miss this, due the the c.dups > 0 in Do()? TODO(miek)
cache.insert(key, sig)
}
m.Ns = append(m.Ns, dns.Copy(sig).(*dns.RRSIG))
}
// TODO(miek): Forget the additional section for now
if bufsize >= 512 || bufsize <= 4096 {
m.Truncated = m.Len() > int(bufsize)
}
o := new(dns.OPT)
o.Hdr.Name = "."
o.Hdr.Rrtype = dns.TypeOPT
o.SetDo()
o.SetUDPSize(4096)
m.Extra = append(m.Extra, o)
return
}