| 1 | package dnsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ |
| 2 | |
| 3 | import ( |
| 4 | "errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/" |
| 5 | "netscip-go gomod github.com/golang/go/src go1.25.0 net/" |
| 6 | "strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/" |
| 7 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 8 | ) |
| 9 | |
| 10 | const hexDigitscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/hexDigit. = "0123456789abcdef" |
| 11 | |
| 12 | // Everything is assumed in ClassINET. |
| 13 | |
| 14 | // SetReply creates a reply message from a request message. |
| 15 | func (dns *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) SetReplyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#SetReply().(request *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg# { |
| 16 | dns.Idscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Id. = request.Idscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Id. |
| 17 | dns.Responsescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Response. = true |
| 18 | dns.Opcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Opcode. = request.Opcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Opcode. |
| 19 | if dns.Opcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Opcode. == OpcodeQueryscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/OpcodeQuery. { |
| 20 | dns.RecursionDesiredscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#RecursionDesired. = request.RecursionDesiredscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#RecursionDesired. // Copy rd bit |
| 21 | dns.CheckingDisabledscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#CheckingDisabled. = request.CheckingDisabledscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#CheckingDisabled. // Copy cd bit |
| 22 | } |
| 23 | dns.Rcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Rcode. = RcodeSuccessscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RcodeSuccess. |
| 24 | if len(request.Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Question.) > 0 { |
| 25 | dns.Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Question. = []Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Question#{request.Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Question.[0]} |
| 26 | } |
| 27 | return dns |
| 28 | } |
| 29 | |
| 30 | // SetQuestion creates a question message, it sets the Question |
| 31 | // section, generates an Id and sets the RecursionDesired (RD) |
| 32 | // bit to true. |
| 33 | func (dns *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) SetQuestionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#SetQuestion().(z string, t uint16) *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg# { |
| 34 | dns.Idscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Id. = Idscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Id.() |
| 35 | dns.RecursionDesiredscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#RecursionDesired. = true |
| 36 | dns.Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Question. = make([]Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Question#, 1) |
| 37 | dns.Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Question.[0] = Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Question#{z, t, ClassINETscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClassINET.} |
| 38 | return dns |
| 39 | } |
| 40 | |
| 41 | // SetNotify creates a notify message, it sets the Question |
| 42 | // section, generates an Id and sets the Authoritative (AA) |
| 43 | // bit to true. |
| 44 | func (dns *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) SetNotifyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#SetNotify().(z string) *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg# { |
| 45 | dns.Opcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Opcode. = OpcodeNotifyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/OpcodeNotify. |
| 46 | dns.Authoritativescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Authoritative. = true |
| 47 | dns.Idscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Id. = Idscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Id.() |
| 48 | dns.Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Question. = make([]Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Question#, 1) |
| 49 | dns.Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Question.[0] = Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Question#{z, TypeSOAscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/TypeSOA., ClassINETscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClassINET.} |
| 50 | return dns |
| 51 | } |
| 52 | |
| 53 | // SetRcode creates an error message suitable for the request. |
| 54 | func (dns *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) SetRcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#SetRcode().(request *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#, rcode int) *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg# { |
| 55 | dns.SetReplyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#SetReply().(request) |
| 56 | dns.Rcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Rcode. = rcode |
| 57 | return dns |
| 58 | } |
| 59 | |
| 60 | // SetRcodeFormatError creates a message with FormError set. |
| 61 | func (dns *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) SetRcodeFormatErrorscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#SetRcodeFormatError().(request *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg# { |
| 62 | dns.Rcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Rcode. = RcodeFormatErrorscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RcodeFormatError. |
| 63 | dns.Opcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Opcode. = OpcodeQueryscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/OpcodeQuery. |
| 64 | dns.Responsescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Response. = true |
| 65 | dns.Authoritativescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Authoritative. = false |
| 66 | dns.Idscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Id. = request.Idscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Id. |
| 67 | return dns |
| 68 | } |
| 69 | |
| 70 | // SetUpdate makes the message a dynamic update message. It |
| 71 | // sets the ZONE section to: z, TypeSOA, ClassINET. |
| 72 | func (dns *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) SetUpdatescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#SetUpdate().(z string) *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg# { |
| 73 | dns.Idscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Id. = Idscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Id.() |
| 74 | dns.Responsescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Response. = false |
| 75 | dns.Opcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Opcode. = OpcodeUpdatescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/OpcodeUpdate. |
| 76 | dns.Compressscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Compress. = false // BIND9 cannot handle compression |
| 77 | dns.Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Question. = make([]Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Question#, 1) |
| 78 | dns.Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Question.[0] = Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Question#{z, TypeSOAscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/TypeSOA., ClassINETscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClassINET.} |
| 79 | return dns |
| 80 | } |
| 81 | |
| 82 | // SetIxfr creates message for requesting an IXFR. |
| 83 | func (dns *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) SetIxfrscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#SetIxfr().(z string, serial uint32, ns, mbox string) *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg# { |
| 84 | dns.Idscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Id. = Idscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Id.() |
| 85 | dns.Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Question. = make([]Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Question#, 1) |
| 86 | dns.Nsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Ns. = make([]RRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#, 1) |
| 87 | s := new(SOAscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SOA#) |
| 88 | s.Hdrscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SOA#Hdr. = RR_Headerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR_Header#{z, TypeSOAscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/TypeSOA., ClassINETscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClassINET., defaultTtlscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/defaultTtl., 0} |
| 89 | s.Serialscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SOA#Serial. = serial |
| 90 | s.Nsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SOA#Ns. = ns |
| 91 | s.Mboxscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SOA#Mbox. = mbox |
| 92 | dns.Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Question.[0] = Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Question#{z, TypeIXFRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/TypeIXFR., ClassINETscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClassINET.} |
| 93 | dns.Nsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Ns.[0] = s |
| 94 | return dns |
| 95 | } |
| 96 | |
| 97 | // SetAxfr creates message for requesting an AXFR. |
| 98 | func (dns *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) SetAxfrscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#SetAxfr().(z string) *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg# { |
| 99 | dns.Idscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Id. = Idscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Id.() |
| 100 | dns.Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Question. = make([]Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Question#, 1) |
| 101 | dns.Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Question.[0] = Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Question#{z, TypeAXFRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/TypeAXFR., ClassINETscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClassINET.} |
| 102 | return dns |
| 103 | } |
| 104 | |
| 105 | // SetTsig appends a TSIG RR to the message. |
| 106 | // This is only a skeleton TSIG RR that is added as the last RR in the |
| 107 | // additional section. The TSIG is calculated when the message is being send. |
| 108 | func (dns *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) SetTsigscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#SetTsig().(z, algo string, fudge uint16, timesigned int64) *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg# { |
| 109 | t := new(TSIGscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/TSIG#) |
| 110 | t.Hdrscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/TSIG#Hdr. = RR_Headerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR_Header#{z, TypeTSIGscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/TypeTSIG., ClassANYscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClassANY., 0, 0} |
| 111 | t.Algorithmscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/TSIG#Algorithm. = algo |
| 112 | t.Fudgescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/TSIG#Fudge. = fudge |
| 113 | t.TimeSignedscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/TSIG#TimeSigned. = uint64(timesigned) |
| 114 | t.OrigIdscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/TSIG#OrigId. = dns.Idscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Id. |
| 115 | dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra. = append(dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra., t) |
| 116 | return dns |
| 117 | } |
| 118 | |
| 119 | // SetEdns0 appends a EDNS0 OPT RR to the message. |
| 120 | // TSIG should always the last RR in a message. |
| 121 | func (dns *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) SetEdns0scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#SetEdns0().(udpsize uint16, do bool) *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg# { |
| 122 | e := new(OPTscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/OPT#) |
| 123 | e.Hdrscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/OPT#Hdr..Namescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR_Header#Name. = "." |
| 124 | e.Hdrscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/OPT#Hdr..Rrtypescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR_Header#Rrtype. = TypeOPTscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/TypeOPT. |
| 125 | e.SetUDPSizescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/OPT#SetUDPSize().(udpsize) |
| 126 | if do { |
| 127 | e.SetDoscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/OPT#SetDo().() |
| 128 | } |
| 129 | dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra. = append(dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra., e) |
| 130 | return dns |
| 131 | } |
| 132 | |
| 133 | // IsTsig checks if the message has a TSIG record as the last record |
| 134 | // in the additional section. It returns the TSIG record found or nil. |
| 135 | func (dns *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) IsTsigscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#IsTsig().() *TSIGscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/TSIG# { |
| 136 | if len(dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra.) > 0 { |
| 137 | if dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra.[len(dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra.)-1].Headerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#Header.().Rrtypescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR_Header#Rrtype. == TypeTSIGscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/TypeTSIG. { |
| 138 | return dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra.[len(dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra.)-1].(*TSIGscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/TSIG#) |
| 139 | } |
| 140 | } |
| 141 | return nil |
| 142 | } |
| 143 | |
| 144 | // IsEdns0 checks if the message has a EDNS0 (OPT) record, any EDNS0 |
| 145 | // record in the additional section will do. It returns the OPT record |
| 146 | // found or nil. |
| 147 | func (dns *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) IsEdns0scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#IsEdns0().() *OPTscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/OPT# { |
| 148 | // RFC 6891, Section 6.1.1 allows the OPT record to appear |
| 149 | // anywhere in the additional record section, but it's usually at |
| 150 | // the end so start there. |
| 151 | for i := len(dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra.) - 1; i >= 0; i-- { |
| 152 | if dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra.[i].Headerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#Header.().Rrtypescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR_Header#Rrtype. == TypeOPTscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/TypeOPT. { |
| 153 | return dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra.[i].(*OPTscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/OPT#) |
| 154 | } |
| 155 | } |
| 156 | return nil |
| 157 | } |
| 158 | |
| 159 | // popEdns0 is like IsEdns0, but it removes the record from the message. |
| 160 | func (dns *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) popEdns0scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#popEdns0().() *OPTscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/OPT# { |
| 161 | // RFC 6891, Section 6.1.1 allows the OPT record to appear |
| 162 | // anywhere in the additional record section, but it's usually at |
| 163 | // the end so start there. |
| 164 | for i := len(dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra.) - 1; i >= 0; i-- { |
| 165 | if dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra.[i].Headerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#Header.().Rrtypescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR_Header#Rrtype. == TypeOPTscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/TypeOPT. { |
| 166 | opt := dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra.[i].(*OPTscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/OPT#) |
| 167 | dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra. = append(dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra.[:i], dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra.[i+1:]...) |
| 168 | return opt |
| 169 | } |
| 170 | } |
| 171 | return nil |
| 172 | } |
| 173 | |
| 174 | // IsDomainName checks if s is a valid domain name, it returns the number of |
| 175 | // labels and true, when a domain name is valid. Note that non fully qualified |
| 176 | // domain name is considered valid, in this case the last label is counted in |
| 177 | // the number of labels. When false is returned the number of labels is not |
| 178 | // defined. Also note that this function is extremely liberal; almost any |
| 179 | // string is a valid domain name as the DNS is 8 bit protocol. It checks if each |
| 180 | // label fits in 63 characters and that the entire name will fit into the 255 |
| 181 | // octet wire format limit. |
| 182 | func IsDomainNamescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/IsDomainName().(s string) (labels int, ok bool) { |
| 183 | // XXX: The logic in this function was copied from packDomainName and |
| 184 | // should be kept in sync with that function. |
| 185 | |
| 186 | const lenmsg = 256 |
| 187 | |
| 188 | if len(s) == 0 { // Ok, for instance when dealing with update RR without any rdata. |
| 189 | return 0, false |
| 190 | } |
| 191 | |
| 192 | s = Fqdnscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Fqdn().(s) |
| 193 | |
| 194 | // Each dot ends a segment of the name. Except for escaped dots (\.), which |
| 195 | // are normal dots. |
| 196 | |
| 197 | var ( |
| 198 | off int |
| 199 | begin int |
| 200 | wasDot bool |
| 201 | escape bool |
| 202 | ) |
| 203 | for i := 0; i < len(s); i++ { |
| 204 | switch s[i] { |
| 205 | case '\\': |
| 206 | escape = !escape |
| 207 | if off+1 > lenmsg { |
| 208 | return labels, false |
| 209 | } |
| 210 | |
| 211 | // check for \DDD |
| 212 | if isDDDscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/isDDD().(s[i+1:]) { |
| 213 | i += 3 |
| 214 | begin += 3 |
| 215 | } else { |
| 216 | i++ |
| 217 | begin++ |
| 218 | } |
| 219 | |
| 220 | wasDot = false |
| 221 | case '.': |
| 222 | escape = false |
| 223 | if i == 0 && len(s) > 1 { |
| 224 | // leading dots are not legal except for the root zone |
| 225 | return labels, false |
| 226 | } |
| 227 | |
| 228 | if wasDot { |
| 229 | // two dots back to back is not legal |
| 230 | return labels, false |
| 231 | } |
| 232 | wasDot = true |
| 233 | |
| 234 | labelLen := i - begin |
| 235 | if labelLen >= 1<<6 { // top two bits of length must be clear |
| 236 | return labels, false |
| 237 | } |
| 238 | |
| 239 | // off can already (we're in a loop) be bigger than lenmsg |
| 240 | // this happens when a name isn't fully qualified |
| 241 | off += 1 + labelLen |
| 242 | if off > lenmsg { |
| 243 | return labels, false |
| 244 | } |
| 245 | |
| 246 | labels++ |
| 247 | begin = i + 1 |
| 248 | default: |
| 249 | escape = false |
| 250 | wasDot = false |
| 251 | } |
| 252 | } |
| 253 | if escape { |
| 254 | return labels, false |
| 255 | } |
| 256 | return labels, true |
| 257 | } |
| 258 | |
| 259 | // IsSubDomain checks if child is indeed a child of the parent. If child and parent |
| 260 | // are the same domain true is returned as well. |
| 261 | func IsSubDomainscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/IsSubDomain().(parent, child string) bool { |
| 262 | // Entire child is contained in parent |
| 263 | return CompareDomainNamescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/CompareDomainName().(parent, child) == CountLabelscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/CountLabel().(parent) |
| 264 | } |
| 265 | |
| 266 | // IsMsg sanity checks buf and returns an error if it isn't a valid DNS packet. |
| 267 | // The checking is performed on the binary payload. |
| 268 | func IsMsgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/IsMsg().(buf []byte) error { |
| 269 | // Header |
| 270 | if len(buf) < headerSizescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/headerSize. { |
| 271 | return errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/.Newscip-go gomod github.com/golang/go/src go1.25.0 errors/New().("dns: bad message header") |
| 272 | } |
| 273 | // Header: Opcode |
| 274 | // TODO(miek): more checks here, e.g. check all header bits. |
| 275 | return nil |
| 276 | } |
| 277 | |
| 278 | // IsFqdn checks if a domain name is fully qualified. |
| 279 | func IsFqdnscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/IsFqdn().(s string) bool { |
| 280 | // Check for (and remove) a trailing dot, returning if there isn't one. |
| 281 | if s == "" || s[len(s)-1] != '.' { |
| 282 | return false |
| 283 | } |
| 284 | s = s[:len(s)-1] |
| 285 | |
| 286 | // If we don't have an escape sequence before the final dot, we know it's |
| 287 | // fully qualified and can return here. |
| 288 | if s == "" || s[len(s)-1] != '\\' { |
| 289 | return true |
| 290 | } |
| 291 | |
| 292 | // Otherwise we have to check if the dot is escaped or not by checking if |
| 293 | // there are an odd or even number of escape sequences before the dot. |
| 294 | i := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.LastIndexFuncscip-go gomod github.com/golang/go/src go1.25.0 strings/LastIndexFunc().(s, func(r rune) bool { |
| 295 | return r != '\\' |
| 296 | }) |
| 297 | return (len(s)-i)%2 != 0 |
| 298 | } |
| 299 | |
| 300 | // IsRRset reports whether a set of RRs is a valid RRset as defined by RFC 2181. |
| 301 | // This means the RRs need to have the same type, name, and class. |
| 302 | func IsRRsetscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/IsRRset().(rrset []RRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#) bool { |
| 303 | if len(rrset) == 0 { |
| 304 | return false |
| 305 | } |
| 306 | |
| 307 | baseH := rrset[0].Headerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#Header.() |
| 308 | for _, rr := range rrset[1:] { |
| 309 | curH := rr.Headerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#Header.() |
| 310 | if curH.Rrtypescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR_Header#Rrtype. != baseH.Rrtypescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR_Header#Rrtype. || curH.Classscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR_Header#Class. != baseH.Classscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR_Header#Class. || curH.Namescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR_Header#Name. != baseH.Namescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR_Header#Name. { |
| 311 | // Mismatch between the records, so this is not a valid rrset for |
| 312 | // signing/verifying |
| 313 | return false |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | return true |
| 318 | } |
| 319 | |
| 320 | // Fqdn return the fully qualified domain name from s. |
| 321 | // If s is already fully qualified, it behaves as the identity function. |
| 322 | func Fqdnscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Fqdn().(s string) string { |
| 323 | if IsFqdnscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/IsFqdn().(s) { |
| 324 | return s |
| 325 | } |
| 326 | return s + "." |
| 327 | } |
| 328 | |
| 329 | // CanonicalName returns the domain name in canonical form. A name in canonical |
| 330 | // form is lowercase and fully qualified. Only US-ASCII letters are affected. See |
| 331 | // Section 6.2 in RFC 4034. |
| 332 | func CanonicalNamescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/CanonicalName().(s string) string { |
| 333 | return stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Mapscip-go gomod github.com/golang/go/src go1.25.0 strings/Map().(func(r rune) rune { |
| 334 | if r >= 'A' && r <= 'Z' { |
| 335 | r += 'a' - 'A' |
| 336 | } |
| 337 | return r |
| 338 | }, Fqdnscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Fqdn().(s)) |
| 339 | } |
| 340 | |
| 341 | // Copied from the official Go code. |
| 342 | |
| 343 | // ReverseAddr returns the in-addr.arpa. or ip6.arpa. hostname of the IP |
| 344 | // address suitable for reverse DNS (PTR) record lookups or an error if it fails |
| 345 | // to parse the IP address. |
| 346 | func ReverseAddrscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ReverseAddr().(addr string) (arpa string, err error) { |
| 347 | ip := netscip-go gomod github.com/golang/go/src go1.25.0 net/.ParseIPscip-go gomod github.com/golang/go/src go1.25.0 net/ParseIP().(addr) |
| 348 | if ip == nil { |
| 349 | return "", &Errorscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Error#{errscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Error#err.: "unrecognized address: " + addr} |
| 350 | } |
| 351 | if v4 := ip.To4scip-go gomod github.com/golang/go/src go1.25.0 net/IP#To4().(); v4 != nil { |
| 352 | buf := make([]byte, 0, netscip-go gomod github.com/golang/go/src go1.25.0 net/.IPv4lenscip-go gomod github.com/golang/go/src go1.25.0 net/IPv4len.*4+len("in-addr.arpa.")) |
| 353 | // Add it, in reverse, to the buffer |
| 354 | for i := len(v4) - 1; i >= 0; i-- { |
| 355 | buf = strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.AppendIntscip-go gomod github.com/golang/go/src go1.25.0 strconv/AppendInt().(buf, int64(v4[i]), 10) |
| 356 | buf = append(buf, '.') |
| 357 | } |
| 358 | // Append "in-addr.arpa." and return (buf already has the final .) |
| 359 | buf = append(buf, "in-addr.arpa."...) |
| 360 | return string(buf), nil |
| 361 | } |
| 362 | // Must be IPv6 |
| 363 | buf := make([]byte, 0, netscip-go gomod github.com/golang/go/src go1.25.0 net/.IPv6lenscip-go gomod github.com/golang/go/src go1.25.0 net/IPv6len.*4+len("ip6.arpa.")) |
| 364 | // Add it, in reverse, to the buffer |
| 365 | for i := len(ip) - 1; i >= 0; i-- { |
| 366 | v := ip[i] |
| 367 | buf = append(buf, hexDigitscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/hexDigit.[v&0xF], '.', hexDigitscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/hexDigit.[v>>4], '.') |
| 368 | } |
| 369 | // Append "ip6.arpa." and return (buf already has the final .) |
| 370 | buf = append(buf, "ip6.arpa."...) |
| 371 | return string(buf), nil |
| 372 | } |
| 373 | |
| 374 | // String returns the string representation for the type t. |
| 375 | func (t Typescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Type#) Stringscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Type#String().() string { |
| 376 | if t1, ok := TypeToStringscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/TypeToString.[uint16(t)]; ok { |
| 377 | return t1 |
| 378 | } |
| 379 | return "TYPE" + strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Itoascip-go gomod github.com/golang/go/src go1.25.0 strconv/Itoa().(int(t)) |
| 380 | } |
| 381 | |
| 382 | // String returns the string representation for the class c. |
| 383 | func (c Classscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Class#) Stringscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Class#String().() string { |
| 384 | if s, ok := ClassToStringscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClassToString.[uint16(c)]; ok { |
| 385 | // Only emit mnemonics when they are unambiguous, specially ANY is in both. |
| 386 | if _, ok := StringToTypescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/StringToType.[s]; !ok { |
| 387 | return s |
| 388 | } |
| 389 | } |
| 390 | return "CLASS" + strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Itoascip-go gomod github.com/golang/go/src go1.25.0 strconv/Itoa().(int(c)) |
| 391 | } |
| 392 | |
| 393 | // String returns the string representation for the name n. |
| 394 | func (n Namescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Name#) Stringscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Name#String().() string { |
| 395 | return sprintNamescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/sprintName().(string(n)) |
| 396 | } |