| 1 | // DNS packet assembly, see RFC 1035. Converting from - Unpack() - |
| 2 | // and to - Pack() - wire format. |
| 3 | // All the packers and unpackers take a (msg []byte, off int) |
| 4 | // and return (off1 int, ok bool). If they return ok==false, they |
| 5 | // also return off1==len(msg), so that the next unpacker will |
| 6 | // also fail. This lets us avoid checks of ok until the end of a |
| 7 | // packing sequence. |
| 8 | |
| 9 | package dnsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ |
| 10 | |
| 11 | //go:generate go run msg_generate.go |
| 12 | |
| 13 | import ( |
| 14 | "crypto/randscip-go gomod github.com/golang/go/src go1.25.0 `crypto/rand`/" |
| 15 | "encoding/binaryscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/" |
| 16 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 17 | "math/bigscip-go gomod github.com/golang/go/src go1.25.0 `math/big`/" |
| 18 | "strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/" |
| 19 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 20 | ) |
| 21 | |
| 22 | const ( |
| 23 | maxCompressionOffsetscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/maxCompressionOffset. = 2 << 13 // We have 14 bits for the compression pointer |
| 24 | maxDomainNameWireOctetsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/maxDomainNameWireOctets. = 255 // See RFC 1035 section 2.3.4 |
| 25 | |
| 26 | // This is the maximum number of compression pointers that should occur in a |
| 27 | // semantically valid message. Each label in a domain name must be at least one |
| 28 | // octet and is separated by a period. The root label won't be represented by a |
| 29 | // compression pointer to a compression pointer, hence the -2 to exclude the |
| 30 | // smallest valid root label. |
| 31 | // |
| 32 | // It is possible to construct a valid message that has more compression pointers |
| 33 | // than this, and still doesn't loop, by pointing to a previous pointer. This is |
| 34 | // not something a well written implementation should ever do, so we leave them |
| 35 | // to trip the maximum compression pointer check. |
| 36 | maxCompressionPointersscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/maxCompressionPointers. = (maxDomainNameWireOctetsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/maxDomainNameWireOctets.+1)/2 - 2 |
| 37 | |
| 38 | // This is the maximum length of a domain name in presentation format. The |
| 39 | // maximum wire length of a domain name is 255 octets (see above), with the |
| 40 | // maximum label length being 63. The wire format requires one extra byte over |
| 41 | // the presentation format, reducing the number of octets by 1. Each label in |
| 42 | // the name will be separated by a single period, with each octet in the label |
| 43 | // expanding to at most 4 bytes (\DDD). If all other labels are of the maximum |
| 44 | // length, then the final label can only be 61 octets long to not exceed the |
| 45 | // maximum allowed wire length. |
| 46 | maxDomainNamePresentationLengthscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/maxDomainNamePresentationLength. = 61*4 + 1 + 63*4 + 1 + 63*4 + 1 + 63*4 + 1 |
| 47 | ) |
| 48 | |
| 49 | // Errors defined in this package. |
| 50 | var ( |
| 51 | ErrAlgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrAlg. error = &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.: "bad algorithm"} // ErrAlg indicates an error with the (DNSSEC) algorithm. |
| 52 | ErrAuthscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrAuth. error = &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.: "bad authentication"} // ErrAuth indicates an error in the TSIG authentication. |
| 53 | ErrBufscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrBuf. error = &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.: "buffer size too small"} // ErrBuf indicates that the buffer used is too small for the message. |
| 54 | ErrConnEmptyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrConnEmpty. error = &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.: "conn has no connection"} // ErrConnEmpty indicates a connection is being used before it is initialized. |
| 55 | ErrExtendedRcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrExtendedRcode. error = &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.: "bad extended rcode"} // ErrExtendedRcode ... |
| 56 | ErrFqdnscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrFqdn. error = &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.: "domain must be fully qualified"} // ErrFqdn indicates that a domain name does not have a closing dot. |
| 57 | ErrIdscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrId. error = &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.: "id mismatch"} // ErrId indicates there is a mismatch with the message's ID. |
| 58 | ErrKeyAlgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrKeyAlg. error = &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.: "bad key algorithm"} // ErrKeyAlg indicates that the algorithm in the key is not valid. |
| 59 | ErrKeyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrKey. error = &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.: "bad key"} |
| 60 | ErrKeySizescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrKeySize. error = &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.: "bad key size"} |
| 61 | ErrLongDomainscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrLongDomain. error = &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.: fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("domain name exceeded %d wire-format octets", maxDomainNameWireOctetsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/maxDomainNameWireOctets.)} |
| 62 | ErrNoSigscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrNoSig. error = &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.: "no signature found"} |
| 63 | ErrPrivKeyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrPrivKey. error = &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.: "bad private key"} |
| 64 | ErrRcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrRcode. error = &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.: "bad rcode"} |
| 65 | ErrRdatascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrRdata. error = &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.: "bad rdata"} |
| 66 | ErrRRsetscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrRRset. error = &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.: "bad rrset"} |
| 67 | ErrSecretscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrSecret. error = &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.: "no secrets defined"} |
| 68 | ErrShortReadscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrShortRead. error = &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.: "short read"} |
| 69 | ErrSigscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrSig. error = &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.: "bad signature"} // ErrSig indicates that a signature can not be cryptographically validated. |
| 70 | ErrSoascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrSoa. error = &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.: "no SOA"} // ErrSOA indicates that no SOA RR was seen when doing zone transfers. |
| 71 | ErrTimescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrTime. error = &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.: "bad time"} // ErrTime indicates a timing error in TSIG authentication. |
| 72 | ) |
| 73 | |
| 74 | // Id by default returns a 16-bit random number to be used as a message id. The |
| 75 | // number is drawn from a cryptographically secure random number generator. |
| 76 | // This being a variable the function can be reassigned to a custom function. |
| 77 | // For instance, to make it return a static value for testing: |
| 78 | // |
| 79 | // dns.Id = func() uint16 { return 3 } |
| 80 | var Idscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Id. = idscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/id(). |
| 81 | |
| 82 | // id returns a 16 bits random number to be used as a |
| 83 | // message id. The random provided should be good enough. |
| 84 | func idscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/id().() uint16 { |
| 85 | var output uint16 |
| 86 | err := binaryscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/.Readscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/Read().(randscip-go gomod github.com/golang/go/src go1.25.0 `crypto/rand`/.Readerscip-go gomod github.com/golang/go/src go1.25.0 `crypto/rand`/Reader., binaryscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/BigEndian., &output) |
| 87 | if err != nil { |
| 88 | panic("dns: reading random id failed: " + err.Error()) |
| 89 | } |
| 90 | return output |
| 91 | } |
| 92 | |
| 93 | // MsgHdr is a a manually-unpacked version of (id, bits). |
| 94 | type MsgHdrscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr# struct { |
| 95 | Idscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Id. uint16 |
| 96 | Responsescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Response. bool |
| 97 | Opcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Opcode. int |
| 98 | Authoritativescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Authoritative. bool |
| 99 | Truncatedscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Truncated. bool |
| 100 | RecursionDesiredscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#RecursionDesired. bool |
| 101 | RecursionAvailablescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#RecursionAvailable. bool |
| 102 | Zeroscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Zero. bool |
| 103 | AuthenticatedDatascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#AuthenticatedData. bool |
| 104 | CheckingDisabledscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#CheckingDisabled. bool |
| 105 | Rcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Rcode. int |
| 106 | } |
| 107 | |
| 108 | // Msg contains the layout of a DNS message. |
| 109 | type Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg# struct { |
| 110 | MsgHdrscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#MsgHdr. |
| 111 | Compressscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Compress. bool `json:"-"` // If true, the message will be compressed when converted to wire format. |
| 112 | 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# // Holds the RR(s) of the question section. |
| 113 | Answerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Answer. []RRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR# // Holds the RR(s) of the answer section. |
| 114 | Nsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Ns. []RRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR# // Holds the RR(s) of the authority section. |
| 115 | Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra. []RRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR# // Holds the RR(s) of the additional section. |
| 116 | } |
| 117 | |
| 118 | // ClassToString is a maps Classes to strings for each CLASS wire type. |
| 119 | var ClassToStringscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClassToString. = map[uint16]string{ |
| 120 | ClassINETscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClassINET.: "IN", |
| 121 | ClassCSNETscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClassCSNET.: "CS", |
| 122 | ClassCHAOSscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClassCHAOS.: "CH", |
| 123 | ClassHESIODscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClassHESIOD.: "HS", |
| 124 | ClassNONEscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClassNONE.: "NONE", |
| 125 | ClassANYscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClassANY.: "ANY", |
| 126 | } |
| 127 | |
| 128 | // OpcodeToString maps Opcodes to strings. |
| 129 | var OpcodeToStringscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/OpcodeToString. = map[int]string{ |
| 130 | OpcodeQueryscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/OpcodeQuery.: "QUERY", |
| 131 | OpcodeIQueryscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/OpcodeIQuery.: "IQUERY", |
| 132 | OpcodeStatusscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/OpcodeStatus.: "STATUS", |
| 133 | OpcodeNotifyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/OpcodeNotify.: "NOTIFY", |
| 134 | OpcodeUpdatescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/OpcodeUpdate.: "UPDATE", |
| 135 | } |
| 136 | |
| 137 | // RcodeToString maps Rcodes to strings. |
| 138 | var RcodeToStringscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RcodeToString. = map[int]string{ |
| 139 | RcodeSuccessscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RcodeSuccess.: "NOERROR", |
| 140 | RcodeFormatErrorscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RcodeFormatError.: "FORMERR", |
| 141 | RcodeServerFailurescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RcodeServerFailure.: "SERVFAIL", |
| 142 | RcodeNameErrorscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RcodeNameError.: "NXDOMAIN", |
| 143 | RcodeNotImplementedscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RcodeNotImplemented.: "NOTIMP", |
| 144 | RcodeRefusedscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RcodeRefused.: "REFUSED", |
| 145 | RcodeYXDomainscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RcodeYXDomain.: "YXDOMAIN", // See RFC 2136 |
| 146 | RcodeYXRrsetscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RcodeYXRrset.: "YXRRSET", |
| 147 | RcodeNXRrsetscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RcodeNXRrset.: "NXRRSET", |
| 148 | RcodeNotAuthscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RcodeNotAuth.: "NOTAUTH", |
| 149 | RcodeNotZonescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RcodeNotZone.: "NOTZONE", |
| 150 | RcodeStatefulTypeNotImplementedscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RcodeStatefulTypeNotImplemented.: "DSOTYPENI", |
| 151 | RcodeBadSigscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RcodeBadSig.: "BADSIG", // Also known as RcodeBadVers, see RFC 6891 |
| 152 | // RcodeBadVers: "BADVERS", |
| 153 | RcodeBadKeyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RcodeBadKey.: "BADKEY", |
| 154 | RcodeBadTimescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RcodeBadTime.: "BADTIME", |
| 155 | RcodeBadModescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RcodeBadMode.: "BADMODE", |
| 156 | RcodeBadNamescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RcodeBadName.: "BADNAME", |
| 157 | RcodeBadAlgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RcodeBadAlg.: "BADALG", |
| 158 | RcodeBadTruncscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RcodeBadTrunc.: "BADTRUNC", |
| 159 | RcodeBadCookiescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RcodeBadCookie.: "BADCOOKIE", |
| 160 | } |
| 161 | |
| 162 | // compressionMap is used to allow a more efficient compression map |
| 163 | // to be used for internal packDomainName calls without changing the |
| 164 | // signature or functionality of public API. |
| 165 | // |
| 166 | // In particular, map[string]uint16 uses 25% less per-entry memory |
| 167 | // than does map[string]int. |
| 168 | type compressionMapscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap# struct { |
| 169 | extscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#ext. map[string]int // external callers |
| 170 | intscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#int. map[string]uint16 // internal callers |
| 171 | } |
| 172 | |
| 173 | func (m compressionMapscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#) validscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#valid().() bool { |
| 174 | return m.intscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#int. != nil || m.extscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#ext. != nil |
| 175 | } |
| 176 | |
| 177 | func (m compressionMapscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#) insertscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#insert().(s string, pos int) { |
| 178 | if m.extscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#ext. != nil { |
| 179 | m.extscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#ext.[s] = pos |
| 180 | } else { |
| 181 | m.intscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#int.[s] = uint16(pos) |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | func (m compressionMapscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#) findscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#find().(s string) (int, bool) { |
| 186 | if m.extscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#ext. != nil { |
| 187 | pos, ok := m.extscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#ext.[s] |
| 188 | return pos, ok |
| 189 | } |
| 190 | |
| 191 | pos, ok := m.intscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#int.[s] |
| 192 | return int(pos), ok |
| 193 | } |
| 194 | |
| 195 | // Domain names are a sequence of counted strings |
| 196 | // split at the dots. They end with a zero-length string. |
| 197 | |
| 198 | // PackDomainName packs a domain name s into msg[off:]. |
| 199 | // If compression is wanted compress must be true and the compression |
| 200 | // map needs to hold a mapping between domain names and offsets |
| 201 | // pointing into msg. |
| 202 | func PackDomainNamescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/PackDomainName().(s string, msg []byte, off int, compression map[string]int, compress bool) (off1 int, err error) { |
| 203 | return packDomainNamescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/packDomainName().(s, msg, off, compressionMapscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#{extscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#ext.: compression}, compress) |
| 204 | } |
| 205 | |
| 206 | func packDomainNamescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/packDomainName().(s string, msg []byte, off int, compression compressionMapscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#, compress bool) (off1 int, err error) { |
| 207 | // XXX: A logical copy of this function exists in IsDomainName and |
| 208 | // should be kept in sync with this function. |
| 209 | |
| 210 | ls := len(s) |
| 211 | if ls == 0 { // Ok, for instance when dealing with update RR without any rdata. |
| 212 | return off, nil |
| 213 | } |
| 214 | |
| 215 | // If not fully qualified, error out. |
| 216 | if !IsFqdnscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/IsFqdn().(s) { |
| 217 | return len(msg), ErrFqdnscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrFqdn. |
| 218 | } |
| 219 | |
| 220 | // Each dot ends a segment of the name. |
| 221 | // We trade each dot byte for a length byte. |
| 222 | // Except for escaped dots (\.), which are normal dots. |
| 223 | // There is also a trailing zero. |
| 224 | |
| 225 | // Compression |
| 226 | pointer := -1 |
| 227 | |
| 228 | // Emit sequence of counted strings, chopping at dots. |
| 229 | var ( |
| 230 | begin int |
| 231 | compBegin int |
| 232 | compOff int |
| 233 | bs []byte |
| 234 | wasDot bool |
| 235 | ) |
| 236 | loop: |
| 237 | for i := 0; i < ls; i++ { |
| 238 | var c byte |
| 239 | if bs == nil { |
| 240 | c = s[i] |
| 241 | } else { |
| 242 | c = bs[i] |
| 243 | } |
| 244 | |
| 245 | switch c { |
| 246 | case '\\': |
| 247 | if off+1 > len(msg) { |
| 248 | return len(msg), ErrBufscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrBuf. |
| 249 | } |
| 250 | |
| 251 | if bs == nil { |
| 252 | bs = []byte(s) |
| 253 | } |
| 254 | |
| 255 | // check for \DDD |
| 256 | if isDDDscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/isDDD().(bs[i+1:]) { |
| 257 | bs[i] = dddToBytescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/dddToByte().(bs[i+1:]) |
| 258 | copy(bs[i+1:ls-3], bs[i+4:]) |
| 259 | ls -= 3 |
| 260 | compOff += 3 |
| 261 | } else { |
| 262 | copy(bs[i:ls-1], bs[i+1:]) |
| 263 | ls-- |
| 264 | compOff++ |
| 265 | } |
| 266 | |
| 267 | wasDot = false |
| 268 | case '.': |
| 269 | if i == 0 && len(s) > 1 { |
| 270 | // leading dots are not legal except for the root zone |
| 271 | return len(msg), ErrRdatascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrRdata. |
| 272 | } |
| 273 | |
| 274 | if wasDot { |
| 275 | // two dots back to back is not legal |
| 276 | return len(msg), ErrRdatascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrRdata. |
| 277 | } |
| 278 | wasDot = true |
| 279 | |
| 280 | labelLen := i - begin |
| 281 | if labelLen >= 1<<6 { // top two bits of length must be clear |
| 282 | return len(msg), ErrRdatascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrRdata. |
| 283 | } |
| 284 | |
| 285 | // off can already (we're in a loop) be bigger than len(msg) |
| 286 | // this happens when a name isn't fully qualified |
| 287 | if off+1+labelLen > len(msg) { |
| 288 | return len(msg), ErrBufscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrBuf. |
| 289 | } |
| 290 | |
| 291 | // Don't try to compress '.' |
| 292 | // We should only compress when compress is true, but we should also still pick |
| 293 | // up names that can be used for *future* compression(s). |
| 294 | if compression.validscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#valid().() && !isRootLabelscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/isRootLabel().(s, bs, begin, ls) { |
| 295 | if p, ok := compression.findscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#find().(s[compBegin:]); ok { |
| 296 | // The first hit is the longest matching dname |
| 297 | // keep the pointer offset we get back and store |
| 298 | // the offset of the current name, because that's |
| 299 | // where we need to insert the pointer later |
| 300 | |
| 301 | // If compress is true, we're allowed to compress this dname |
| 302 | if compress { |
| 303 | pointer = p // Where to point to |
| 304 | break loop |
| 305 | } |
| 306 | } else if off < maxCompressionOffsetscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/maxCompressionOffset. { |
| 307 | // Only offsets smaller than maxCompressionOffset can be used. |
| 308 | compression.insertscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#insert().(s[compBegin:], off) |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | // The following is covered by the length check above. |
| 313 | msg[off] = byte(labelLen) |
| 314 | |
| 315 | if bs == nil { |
| 316 | copy(msg[off+1:], s[begin:i]) |
| 317 | } else { |
| 318 | copy(msg[off+1:], bs[begin:i]) |
| 319 | } |
| 320 | off += 1 + labelLen |
| 321 | |
| 322 | begin = i + 1 |
| 323 | compBegin = begin + compOff |
| 324 | default: |
| 325 | wasDot = false |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | // Root label is special |
| 330 | if isRootLabelscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/isRootLabel().(s, bs, 0, ls) { |
| 331 | return off, nil |
| 332 | } |
| 333 | |
| 334 | // If we did compression and we find something add the pointer here |
| 335 | if pointer != -1 { |
| 336 | // We have two bytes (14 bits) to put the pointer in |
| 337 | binaryscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/BigEndian..PutUint16scip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/bigEndian#PutUint16().(msg[off:], uint16(pointer^0xC000)) |
| 338 | return off + 2, nil |
| 339 | } |
| 340 | |
| 341 | // Trailing root label |
| 342 | if off < len(msg) { |
| 343 | msg[off] = 0 |
| 344 | return off + 1, nil |
| 345 | } |
| 346 | |
| 347 | return off, ErrBufscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrBuf. |
| 348 | } |
| 349 | |
| 350 | // isRootLabel returns whether s or bs, from off to end, is the root |
| 351 | // label ".". |
| 352 | // |
| 353 | // If bs is nil, s will be checked, otherwise bs will be checked. |
| 354 | func isRootLabelscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/isRootLabel().(s string, bs []byte, off, end int) bool { |
| 355 | if bs == nil { |
| 356 | return s[off:end] == "." |
| 357 | } |
| 358 | |
| 359 | return end-off == 1 && bs[off] == '.' |
| 360 | } |
| 361 | |
| 362 | // Unpack a domain name. |
| 363 | // In addition to the simple sequences of counted strings above, |
| 364 | // domain names are allowed to refer to strings elsewhere in the |
| 365 | // packet, to avoid repeating common suffixes when returning |
| 366 | // many entries in a single domain. The pointers are marked |
| 367 | // by a length byte with the top two bits set. Ignoring those |
| 368 | // two bits, that byte and the next give a 14 bit offset from msg[0] |
| 369 | // where we should pick up the trail. |
| 370 | // Note that if we jump elsewhere in the packet, |
| 371 | // we return off1 == the offset after the first pointer we found, |
| 372 | // which is where the next record will start. |
| 373 | // In theory, the pointers are only allowed to jump backward. |
| 374 | // We let them jump anywhere and stop jumping after a while. |
| 375 | |
| 376 | // UnpackDomainName unpacks a domain name into a string. It returns |
| 377 | // the name, the new offset into msg and any error that occurred. |
| 378 | // |
| 379 | // When an error is encountered, the unpacked name will be discarded |
| 380 | // and len(msg) will be returned as the offset. |
| 381 | func UnpackDomainNamescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/UnpackDomainName().(msg []byte, off int) (string, int, error) { |
| 382 | s := make([]byte, 0, maxDomainNamePresentationLengthscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/maxDomainNamePresentationLength.) |
| 383 | off1 := 0 |
| 384 | lenmsg := len(msg) |
| 385 | budget := maxDomainNameWireOctetsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/maxDomainNameWireOctets. |
| 386 | ptr := 0 // number of pointers followed |
| 387 | Loop: |
| 388 | for { |
| 389 | if off >= lenmsg { |
| 390 | return "", lenmsg, ErrBufscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrBuf. |
| 391 | } |
| 392 | c := int(msg[off]) |
| 393 | off++ |
| 394 | switch c & 0xC0 { |
| 395 | case 0x00: |
| 396 | if c == 0x00 { |
| 397 | // end of name |
| 398 | break Loop |
| 399 | } |
| 400 | // literal string |
| 401 | if off+c > lenmsg { |
| 402 | return "", lenmsg, ErrBufscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrBuf. |
| 403 | } |
| 404 | budget -= c + 1 // +1 for the label separator |
| 405 | if budget <= 0 { |
| 406 | return "", lenmsg, ErrLongDomainscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrLongDomain. |
| 407 | } |
| 408 | for _, b := range msg[off : off+c] { |
| 409 | if isDomainNameLabelSpecialscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/isDomainNameLabelSpecial().(b) { |
| 410 | s = append(s, '\\', b) |
| 411 | } else if b < ' ' || b > '~' { |
| 412 | s = append(s, escapeBytescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/escapeByte().(b)...) |
| 413 | } else { |
| 414 | s = append(s, b) |
| 415 | } |
| 416 | } |
| 417 | s = append(s, '.') |
| 418 | off += c |
| 419 | case 0xC0: |
| 420 | // pointer to somewhere else in msg. |
| 421 | // remember location after first ptr, |
| 422 | // since that's how many bytes we consumed. |
| 423 | // also, don't follow too many pointers -- |
| 424 | // maybe there's a loop. |
| 425 | if off >= lenmsg { |
| 426 | return "", lenmsg, ErrBufscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrBuf. |
| 427 | } |
| 428 | c1 := msg[off] |
| 429 | off++ |
| 430 | if ptr == 0 { |
| 431 | off1 = off |
| 432 | } |
| 433 | if ptr++; ptr > maxCompressionPointersscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/maxCompressionPointers. { |
| 434 | return "", lenmsg, &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.: "too many compression pointers"} |
| 435 | } |
| 436 | // pointer should guarantee that it advances and points forwards at least |
| 437 | // but the condition on previous three lines guarantees that it's |
| 438 | // at least loop-free |
| 439 | off = (c^0xC0)<<8 | int(c1) |
| 440 | default: |
| 441 | // 0x80 and 0x40 are reserved |
| 442 | return "", lenmsg, ErrRdatascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrRdata. |
| 443 | } |
| 444 | } |
| 445 | if ptr == 0 { |
| 446 | off1 = off |
| 447 | } |
| 448 | if len(s) == 0 { |
| 449 | return ".", off1, nil |
| 450 | } |
| 451 | return string(s), off1, nil |
| 452 | } |
| 453 | |
| 454 | func packTxtscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/packTxt().(txt []string, msg []byte, offset int) (int, error) { |
| 455 | if len(txt) == 0 { |
| 456 | if offset >= len(msg) { |
| 457 | return offset, ErrBufscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrBuf. |
| 458 | } |
| 459 | msg[offset] = 0 |
| 460 | return offset, nil |
| 461 | } |
| 462 | var err error |
| 463 | for _, s := range txt { |
| 464 | offset, err = packTxtStringscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/packTxtString().(s, msg, offset) |
| 465 | if err != nil { |
| 466 | return offset, err |
| 467 | } |
| 468 | } |
| 469 | return offset, nil |
| 470 | } |
| 471 | |
| 472 | func packTxtStringscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/packTxtString().(s string, msg []byte, offset int) (int, error) { |
| 473 | lenByteOffset := offset |
| 474 | if offset >= len(msg) || len(s) > 256*4+1 /* If all \DDD */ { |
| 475 | return offset, ErrBufscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrBuf. |
| 476 | } |
| 477 | offset++ |
| 478 | for i := 0; i < len(s); i++ { |
| 479 | if len(msg) <= offset { |
| 480 | return offset, ErrBufscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrBuf. |
| 481 | } |
| 482 | if s[i] == '\\' { |
| 483 | i++ |
| 484 | if i == len(s) { |
| 485 | break |
| 486 | } |
| 487 | // check for \DDD |
| 488 | if isDDDscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/isDDD().(s[i:]) { |
| 489 | msg[offset] = dddToBytescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/dddToByte().(s[i:]) |
| 490 | i += 2 |
| 491 | } else { |
| 492 | msg[offset] = s[i] |
| 493 | } |
| 494 | } else { |
| 495 | msg[offset] = s[i] |
| 496 | } |
| 497 | offset++ |
| 498 | } |
| 499 | l := offset - lenByteOffset - 1 |
| 500 | if l > 255 { |
| 501 | return offset, &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.: "string exceeded 255 bytes in txt"} |
| 502 | } |
| 503 | msg[lenByteOffset] = byte(l) |
| 504 | return offset, nil |
| 505 | } |
| 506 | |
| 507 | func packOctetStringscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/packOctetString().(s string, msg []byte, offset int) (int, error) { |
| 508 | if offset >= len(msg) || len(s) > 256*4+1 { |
| 509 | return offset, ErrBufscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrBuf. |
| 510 | } |
| 511 | for i := 0; i < len(s); i++ { |
| 512 | if len(msg) <= offset { |
| 513 | return offset, ErrBufscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrBuf. |
| 514 | } |
| 515 | if s[i] == '\\' { |
| 516 | i++ |
| 517 | if i == len(s) { |
| 518 | break |
| 519 | } |
| 520 | // check for \DDD |
| 521 | if isDDDscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/isDDD().(s[i:]) { |
| 522 | msg[offset] = dddToBytescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/dddToByte().(s[i:]) |
| 523 | i += 2 |
| 524 | } else { |
| 525 | msg[offset] = s[i] |
| 526 | } |
| 527 | } else { |
| 528 | msg[offset] = s[i] |
| 529 | } |
| 530 | offset++ |
| 531 | } |
| 532 | return offset, nil |
| 533 | } |
| 534 | |
| 535 | func unpackTxtscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/unpackTxt().(msg []byte, off0 int) (ss []string, off int, err error) { |
| 536 | off = off0 |
| 537 | var s string |
| 538 | for off < len(msg) && err == nil { |
| 539 | s, off, err = unpackStringscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/unpackString().(msg, off) |
| 540 | if err == nil { |
| 541 | ss = append(ss, s) |
| 542 | } |
| 543 | } |
| 544 | return |
| 545 | } |
| 546 | |
| 547 | // Helpers for dealing with escaped bytes |
| 548 | func isDigitscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/isDigit().(b byte) bool { return b >= '0' && b <= '9' } |
| 549 | |
| 550 | func isDDDscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/isDDD().[T ~[]byte | ~string](s T) bool { |
| 551 | return len(s) >= 3 && isDigitscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/isDigit().(s[0]) && isDigitscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/isDigit().(s[1]) && isDigitscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/isDigit().(s[2]) |
| 552 | } |
| 553 | |
| 554 | func dddToBytescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/dddToByte().[T ~[]byte | ~string](s T) byte { |
| 555 | _ = s[2] // bounds check hint to compiler; see golang.org/issue/14808 |
| 556 | return byte((s[0]-'0')*100 + (s[1]-'0')*10 + (s[2] - '0')) |
| 557 | } |
| 558 | |
| 559 | // Helper function for packing and unpacking |
| 560 | func intToBytesscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/intToBytes().(i *bigscip-go gomod github.com/golang/go/src go1.25.0 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.25.0 `math/big`/Int#, length int) []byte { |
| 561 | buf := i.Bytesscip-go gomod github.com/golang/go/src go1.25.0 `math/big`/Int#Bytes().() |
| 562 | if len(buf) < length { |
| 563 | b := make([]byte, length) |
| 564 | copy(b[length-len(buf):], buf) |
| 565 | return b |
| 566 | } |
| 567 | return buf |
| 568 | } |
| 569 | |
| 570 | // PackRR packs a resource record rr into msg[off:]. |
| 571 | // See PackDomainName for documentation about the compression. |
| 572 | func PackRRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/PackRR().(rr RRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#, msg []byte, off int, compression map[string]int, compress bool) (off1 int, err error) { |
| 573 | headerEnd, off1, err := packRRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/packRR().(rr, msg, off, compressionMapscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#{extscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#ext.: compression}, compress) |
| 574 | if err == nil { |
| 575 | // packRR no longer sets the Rdlength field on the rr, but |
| 576 | // callers might be expecting it so we set it here. |
| 577 | rr.Headerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#Header.().Rdlengthscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR_Header#Rdlength. = uint16(off1 - headerEnd) |
| 578 | } |
| 579 | return off1, err |
| 580 | } |
| 581 | |
| 582 | func packRRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/packRR().(rr RRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#, msg []byte, off int, compression compressionMapscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#, compress bool) (headerEnd int, off1 int, err error) { |
| 583 | if rr == nil { |
| 584 | return len(msg), len(msg), &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.: "nil rr"} |
| 585 | } |
| 586 | |
| 587 | headerEnd, err = rr.Headerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#Header.().packHeaderscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR_Header#packHeader().(msg, off, compression, compress) |
| 588 | if err != nil { |
| 589 | return headerEnd, len(msg), err |
| 590 | } |
| 591 | |
| 592 | off1, err = rr.packscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#pack.(msg, headerEnd, compression, compress) |
| 593 | if err != nil { |
| 594 | return headerEnd, len(msg), err |
| 595 | } |
| 596 | |
| 597 | rdlength := off1 - headerEnd |
| 598 | if int(uint16(rdlength)) != rdlength { // overflow |
| 599 | return headerEnd, len(msg), ErrRdatascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrRdata. |
| 600 | } |
| 601 | |
| 602 | // The RDLENGTH field is the last field in the header and we set it here. |
| 603 | binaryscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/BigEndian..PutUint16scip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/bigEndian#PutUint16().(msg[headerEnd-2:], uint16(rdlength)) |
| 604 | return headerEnd, off1, nil |
| 605 | } |
| 606 | |
| 607 | // UnpackRR unpacks msg[off:] into an RR. |
| 608 | func UnpackRRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/UnpackRR().(msg []byte, off int) (rr RRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#, off1 int, err error) { |
| 609 | h, off, msg, err := unpackHeaderscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/unpackHeader().(msg, off) |
| 610 | if err != nil { |
| 611 | return nil, len(msg), err |
| 612 | } |
| 613 | |
| 614 | return UnpackRRWithHeaderscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/UnpackRRWithHeader().(h, msg, off) |
| 615 | } |
| 616 | |
| 617 | // UnpackRRWithHeader unpacks the record type specific payload given an existing |
| 618 | // RR_Header. |
| 619 | func UnpackRRWithHeaderscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/UnpackRRWithHeader().(h RR_Headerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR_Header#, msg []byte, off int) (rr RRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#, off1 int, err error) { |
| 620 | if newFn, ok := TypeToRRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/TypeToRR.[h.Rrtypescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR_Header#Rrtype.]; ok { |
| 621 | rr = newFn() |
| 622 | *rr.Headerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#Header.() = h |
| 623 | } else { |
| 624 | rr = &RFC3597scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RFC3597#{Hdrscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RFC3597#Hdr.: h} |
| 625 | } |
| 626 | |
| 627 | if off < 0 || off > len(msg) { |
| 628 | return &h, off, &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.: "bad off"} |
| 629 | } |
| 630 | |
| 631 | end := off + int(h.Rdlengthscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR_Header#Rdlength.) |
| 632 | if end < off || end > len(msg) { |
| 633 | return &h, end, &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.: "bad rdlength"} |
| 634 | } |
| 635 | |
| 636 | if noRdatascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/noRdata().(h) { |
| 637 | return rr, off, nil |
| 638 | } |
| 639 | |
| 640 | off, err = rr.unpackscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#unpack.(msg, off) |
| 641 | if err != nil { |
| 642 | return nil, end, err |
| 643 | } |
| 644 | if off != end { |
| 645 | return &h, end, &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.: "bad rdlength"} |
| 646 | } |
| 647 | |
| 648 | return rr, off, nil |
| 649 | } |
| 650 | |
| 651 | // unpackRRslice unpacks msg[off:] into an []RR. |
| 652 | // If we cannot unpack the whole array, then it will return nil |
| 653 | func unpackRRslicescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/unpackRRslice().(l int, msg []byte, off int) (dst1 []RRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#, off1 int, err error) { |
| 654 | var r RRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR# |
| 655 | // Don't pre-allocate, l may be under attacker control |
| 656 | var dst []RRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR# |
| 657 | for i := 0; i < l; i++ { |
| 658 | off1 := off |
| 659 | r, off, err = UnpackRRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/UnpackRR().(msg, off) |
| 660 | if err != nil { |
| 661 | off = len(msg) |
| 662 | break |
| 663 | } |
| 664 | // If offset does not increase anymore, l is a lie |
| 665 | if off1 == off { |
| 666 | break |
| 667 | } |
| 668 | dst = append(dst, r) |
| 669 | } |
| 670 | if err != nil && off == len(msg) { |
| 671 | dst = nil |
| 672 | } |
| 673 | return dst, off, err |
| 674 | } |
| 675 | |
| 676 | // Convert a MsgHdr to a string, with dig-like headers: |
| 677 | // |
| 678 | // ;; opcode: QUERY, status: NOERROR, id: 48404 |
| 679 | // |
| 680 | // ;; flags: qr aa rd ra; |
| 681 | func (h *MsgHdrscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#) Stringscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#String().() string { |
| 682 | if h == nil { |
| 683 | return "<nil> MsgHdr" |
| 684 | } |
| 685 | |
| 686 | s := ";; opcode: " + OpcodeToStringscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/OpcodeToString.[h.Opcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Opcode.] |
| 687 | s += ", status: " + RcodeToStringscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RcodeToString.[h.Rcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Rcode.] |
| 688 | s += ", id: " + 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(h.Idscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Id.)) + "\n" |
| 689 | |
| 690 | s += ";; flags:" |
| 691 | if h.Responsescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Response. { |
| 692 | s += " qr" |
| 693 | } |
| 694 | if h.Authoritativescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Authoritative. { |
| 695 | s += " aa" |
| 696 | } |
| 697 | if h.Truncatedscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Truncated. { |
| 698 | s += " tc" |
| 699 | } |
| 700 | if h.RecursionDesiredscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#RecursionDesired. { |
| 701 | s += " rd" |
| 702 | } |
| 703 | if h.RecursionAvailablescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#RecursionAvailable. { |
| 704 | s += " ra" |
| 705 | } |
| 706 | if h.Zeroscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Zero. { // Hmm |
| 707 | s += " z" |
| 708 | } |
| 709 | if h.AuthenticatedDatascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#AuthenticatedData. { |
| 710 | s += " ad" |
| 711 | } |
| 712 | if h.CheckingDisabledscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#CheckingDisabled. { |
| 713 | s += " cd" |
| 714 | } |
| 715 | |
| 716 | s += ";" |
| 717 | return s |
| 718 | } |
| 719 | |
| 720 | // Pack packs a Msg: it is converted to wire format. |
| 721 | // If the dns.Compress is true the message will be in compressed wire format. |
| 722 | func (dns *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) Packscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Pack().() (msg []byte, err error) { |
| 723 | return dns.PackBufferscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#PackBuffer().(nil) |
| 724 | } |
| 725 | |
| 726 | // PackBuffer packs a Msg, using the given buffer buf. If buf is too small a new buffer is allocated. |
| 727 | func (dns *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) PackBufferscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#PackBuffer().(buf []byte) (msg []byte, err error) { |
| 728 | // If this message can't be compressed, avoid filling the |
| 729 | // compression map and creating garbage. |
| 730 | if dns.Compressscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Compress. && dns.isCompressiblescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#isCompressible().() { |
| 731 | compression := make(map[string]uint16) // Compression pointer mappings. |
| 732 | return dns.packBufferWithCompressionMapscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#packBufferWithCompressionMap().(buf, compressionMapscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#{intscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#int.: compression}, true) |
| 733 | } |
| 734 | |
| 735 | return dns.packBufferWithCompressionMapscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#packBufferWithCompressionMap().(buf, compressionMapscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#{}, false) |
| 736 | } |
| 737 | |
| 738 | // packBufferWithCompressionMap packs a Msg, using the given buffer buf. |
| 739 | func (dns *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) packBufferWithCompressionMapscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#packBufferWithCompressionMap().(buf []byte, compression compressionMapscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#, compress bool) (msg []byte, err error) { |
| 740 | if dns.Rcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Rcode. < 0 || dns.Rcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Rcode. > 0xFFF { |
| 741 | return nil, ErrRcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrRcode. |
| 742 | } |
| 743 | |
| 744 | // Set extended rcode unconditionally if we have an opt, this will allow |
| 745 | // resetting the extended rcode bits if they need to. |
| 746 | if opt := dns.IsEdns0scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#IsEdns0().(); opt != nil { |
| 747 | opt.SetExtendedRcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/OPT#SetExtendedRcode().(uint16(dns.Rcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Rcode.)) |
| 748 | } else if dns.Rcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Rcode. > 0xF { |
| 749 | // If Rcode is an extended one and opt is nil, error out. |
| 750 | return nil, ErrExtendedRcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrExtendedRcode. |
| 751 | } |
| 752 | |
| 753 | // Convert convenient Msg into wire-like Header. |
| 754 | var dh Headerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header# |
| 755 | dh.Idscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Id. = dns.Idscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Id. |
| 756 | dh.Bitsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Bits. = uint16(dns.Opcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Opcode.)<<11 | uint16(dns.Rcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Rcode.&0xF) |
| 757 | if dns.Responsescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Response. { |
| 758 | dh.Bitsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Bits. |= _QRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/_QR. |
| 759 | } |
| 760 | if dns.Authoritativescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Authoritative. { |
| 761 | dh.Bitsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Bits. |= _AAscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/_AA. |
| 762 | } |
| 763 | if dns.Truncatedscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Truncated. { |
| 764 | dh.Bitsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Bits. |= _TCscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/_TC. |
| 765 | } |
| 766 | if dns.RecursionDesiredscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#RecursionDesired. { |
| 767 | dh.Bitsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Bits. |= _RDscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/_RD. |
| 768 | } |
| 769 | if dns.RecursionAvailablescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#RecursionAvailable. { |
| 770 | dh.Bitsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Bits. |= _RAscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/_RA. |
| 771 | } |
| 772 | if dns.Zeroscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Zero. { |
| 773 | dh.Bitsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Bits. |= _Zscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/_Z. |
| 774 | } |
| 775 | if dns.AuthenticatedDatascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#AuthenticatedData. { |
| 776 | dh.Bitsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Bits. |= _ADscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/_AD. |
| 777 | } |
| 778 | if dns.CheckingDisabledscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#CheckingDisabled. { |
| 779 | dh.Bitsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Bits. |= _CDscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/_CD. |
| 780 | } |
| 781 | |
| 782 | dh.Qdcountscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Qdcount. = uint16(len(dns.Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Question.)) |
| 783 | dh.Ancountscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Ancount. = uint16(len(dns.Answerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Answer.)) |
| 784 | dh.Nscountscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Nscount. = uint16(len(dns.Nsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Ns.)) |
| 785 | dh.Arcountscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Arcount. = uint16(len(dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra.)) |
| 786 | |
| 787 | // We need the uncompressed length here, because we first pack it and then compress it. |
| 788 | msg = buf |
| 789 | uncompressedLen := msgLenWithCompressionMapscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/msgLenWithCompressionMap().(dns, nil) |
| 790 | if packLen := uncompressedLen + 1; len(msg) < packLen { |
| 791 | msg = make([]byte, packLen) |
| 792 | } |
| 793 | |
| 794 | // Pack it in: header and then the pieces. |
| 795 | off := 0 |
| 796 | off, err = dh.packscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#pack().(msg, off, compression, compress) |
| 797 | if err != nil { |
| 798 | return nil, err |
| 799 | } |
| 800 | for _, r := range dns.Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Question. { |
| 801 | off, err = r.packscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Question#pack().(msg, off, compression, compress) |
| 802 | if err != nil { |
| 803 | return nil, err |
| 804 | } |
| 805 | } |
| 806 | for _, r := range dns.Answerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Answer. { |
| 807 | _, off, err = packRRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/packRR().(r, msg, off, compression, compress) |
| 808 | if err != nil { |
| 809 | return nil, err |
| 810 | } |
| 811 | } |
| 812 | for _, r := range dns.Nsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Ns. { |
| 813 | _, off, err = packRRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/packRR().(r, msg, off, compression, compress) |
| 814 | if err != nil { |
| 815 | return nil, err |
| 816 | } |
| 817 | } |
| 818 | for _, r := range dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra. { |
| 819 | _, off, err = packRRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/packRR().(r, msg, off, compression, compress) |
| 820 | if err != nil { |
| 821 | return nil, err |
| 822 | } |
| 823 | } |
| 824 | return msg[:off], nil |
| 825 | } |
| 826 | |
| 827 | func (dns *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) unpackscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#unpack().(dh Headerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#, msg []byte, off int) (err error) { |
| 828 | // If we are at the end of the message we should return *just* the |
| 829 | // header. This can still be useful to the caller. 9.9.9.9 sends these |
| 830 | // when responding with REFUSED for instance. |
| 831 | if off == len(msg) { |
| 832 | // reset sections before returning |
| 833 | dns.Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Question., dns.Answerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Answer., dns.Nsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Ns., dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra. = nil, nil, nil, nil |
| 834 | return nil |
| 835 | } |
| 836 | |
| 837 | // Qdcount, Ancount, Nscount, Arcount can't be trusted, as they are |
| 838 | // attacker controlled. This means we can't use them to pre-allocate |
| 839 | // slices. |
| 840 | dns.Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Question. = nil |
| 841 | for i := 0; i < int(dh.Qdcountscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Qdcount.); i++ { |
| 842 | off1 := off |
| 843 | var q Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Question# |
| 844 | q, off, err = unpackQuestionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/unpackQuestion().(msg, off) |
| 845 | if err != nil { |
| 846 | return err |
| 847 | } |
| 848 | if off1 == off { // Offset does not increase anymore, dh.Qdcount is a lie! |
| 849 | dh.Qdcountscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Qdcount. = uint16(i) |
| 850 | break |
| 851 | } |
| 852 | dns.Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Question. = append(dns.Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Question., q) |
| 853 | } |
| 854 | |
| 855 | dns.Answerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Answer., off, err = unpackRRslicescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/unpackRRslice().(int(dh.Ancountscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Ancount.), msg, off) |
| 856 | // The header counts might have been wrong so we need to update it |
| 857 | dh.Ancountscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Ancount. = uint16(len(dns.Answerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Answer.)) |
| 858 | if err == nil { |
| 859 | dns.Nsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Ns., off, err = unpackRRslicescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/unpackRRslice().(int(dh.Nscountscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Nscount.), msg, off) |
| 860 | } |
| 861 | // The header counts might have been wrong so we need to update it |
| 862 | dh.Nscountscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Nscount. = uint16(len(dns.Nsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Ns.)) |
| 863 | if err == nil { |
| 864 | dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra., _, err = unpackRRslicescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/unpackRRslice().(int(dh.Arcountscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Arcount.), msg, off) |
| 865 | } |
| 866 | // The header counts might have been wrong so we need to update it |
| 867 | dh.Arcountscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Arcount. = uint16(len(dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra.)) |
| 868 | |
| 869 | // Set extended Rcode |
| 870 | if opt := dns.IsEdns0scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#IsEdns0().(); opt != nil { |
| 871 | dns.Rcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Rcode. |= opt.ExtendedRcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/OPT#ExtendedRcode().() |
| 872 | } |
| 873 | |
| 874 | // TODO(miek) make this an error? |
| 875 | // use PackOpt to let people tell how detailed the error reporting should be? |
| 876 | // if off != len(msg) { |
| 877 | // // println("dns: extra bytes in dns packet", off, "<", len(msg)) |
| 878 | // } |
| 879 | return err |
| 880 | } |
| 881 | |
| 882 | // Unpack unpacks a binary message to a Msg structure. |
| 883 | func (dns *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) Unpackscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Unpack().(msg []byte) (err error) { |
| 884 | dh, off, err := unpackMsgHdrscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/unpackMsgHdr().(msg, 0) |
| 885 | if err != nil { |
| 886 | return err |
| 887 | } |
| 888 | |
| 889 | dns.setHdrscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#setHdr().(dh) |
| 890 | return dns.unpackscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#unpack().(dh, msg, off) |
| 891 | } |
| 892 | |
| 893 | // Convert a complete message to a string with dig-like output. |
| 894 | func (dns *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) Stringscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#String().() string { |
| 895 | if dns == nil { |
| 896 | return "<nil> MsgHdr" |
| 897 | } |
| 898 | s := dns.MsgHdrscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#MsgHdr..Stringscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#String().() + " " |
| 899 | if dns.MsgHdrscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#MsgHdr..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. { |
| 900 | s += "ZONE: " + 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().(len(dns.Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Question.)) + ", " |
| 901 | s += "PREREQ: " + 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().(len(dns.Answerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Answer.)) + ", " |
| 902 | s += "UPDATE: " + 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().(len(dns.Nsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Ns.)) + ", " |
| 903 | s += "ADDITIONAL: " + 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().(len(dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra.)) + "\n" |
| 904 | } else { |
| 905 | s += "QUERY: " + 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().(len(dns.Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Question.)) + ", " |
| 906 | s += "ANSWER: " + 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().(len(dns.Answerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Answer.)) + ", " |
| 907 | s += "AUTHORITY: " + 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().(len(dns.Nsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Ns.)) + ", " |
| 908 | s += "ADDITIONAL: " + 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().(len(dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra.)) + "\n" |
| 909 | } |
| 910 | opt := dns.IsEdns0scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#IsEdns0().() |
| 911 | if opt != nil { |
| 912 | // OPT PSEUDOSECTION |
| 913 | s += opt.Stringscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/OPT#String().() + "\n" |
| 914 | } |
| 915 | if len(dns.Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Question.) > 0 { |
| 916 | if dns.MsgHdrscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#MsgHdr..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. { |
| 917 | s += "\n;; ZONE SECTION:\n" |
| 918 | } else { |
| 919 | s += "\n;; QUESTION SECTION:\n" |
| 920 | } |
| 921 | for _, r := range dns.Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Question. { |
| 922 | s += r.Stringscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Question#String().() + "\n" |
| 923 | } |
| 924 | } |
| 925 | if len(dns.Answerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Answer.) > 0 { |
| 926 | if dns.MsgHdrscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#MsgHdr..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. { |
| 927 | s += "\n;; PREREQUISITE SECTION:\n" |
| 928 | } else { |
| 929 | s += "\n;; ANSWER SECTION:\n" |
| 930 | } |
| 931 | for _, r := range dns.Answerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Answer. { |
| 932 | if r != nil { |
| 933 | s += r.Stringscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#String.() + "\n" |
| 934 | } |
| 935 | } |
| 936 | } |
| 937 | if len(dns.Nsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Ns.) > 0 { |
| 938 | if dns.MsgHdrscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#MsgHdr..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. { |
| 939 | s += "\n;; UPDATE SECTION:\n" |
| 940 | } else { |
| 941 | s += "\n;; AUTHORITY SECTION:\n" |
| 942 | } |
| 943 | for _, r := range dns.Nsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Ns. { |
| 944 | if r != nil { |
| 945 | s += r.Stringscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#String.() + "\n" |
| 946 | } |
| 947 | } |
| 948 | } |
| 949 | if len(dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra.) > 0 && (opt == nil || len(dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra.) > 1) { |
| 950 | s += "\n;; ADDITIONAL SECTION:\n" |
| 951 | for _, r := range dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra. { |
| 952 | if r != nil && r.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. { |
| 953 | s += r.Stringscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#String.() + "\n" |
| 954 | } |
| 955 | } |
| 956 | } |
| 957 | return s |
| 958 | } |
| 959 | |
| 960 | // isCompressible returns whether the msg may be compressible. |
| 961 | func (dns *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) isCompressiblescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#isCompressible().() bool { |
| 962 | // If we only have one question, there is nothing we can ever compress. |
| 963 | return len(dns.Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Question.) > 1 || len(dns.Answerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Answer.) > 0 || |
| 964 | len(dns.Nsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Ns.) > 0 || len(dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra.) > 0 |
| 965 | } |
| 966 | |
| 967 | // Len returns the message length when in (un)compressed wire format. |
| 968 | // If dns.Compress is true compression it is taken into account. Len() |
| 969 | // is provided to be a faster way to get the size of the resulting packet, |
| 970 | // than packing it, measuring the size and discarding the buffer. |
| 971 | func (dns *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) Lenscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Len().() int { |
| 972 | // If this message can't be compressed, avoid filling the |
| 973 | // compression map and creating garbage. |
| 974 | if dns.Compressscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Compress. && dns.isCompressiblescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#isCompressible().() { |
| 975 | compression := make(map[string]struct{}) |
| 976 | return msgLenWithCompressionMapscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/msgLenWithCompressionMap().(dns, compression) |
| 977 | } |
| 978 | |
| 979 | return msgLenWithCompressionMapscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/msgLenWithCompressionMap().(dns, nil) |
| 980 | } |
| 981 | |
| 982 | func msgLenWithCompressionMapscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/msgLenWithCompressionMap().(dns *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#, compression map[string]struct{}) int { |
| 983 | l := headerSizescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/headerSize. |
| 984 | |
| 985 | for _, r := range dns.Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Question. { |
| 986 | l += r.lenscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Question#len().(l, compression) |
| 987 | } |
| 988 | for _, r := range dns.Answerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Answer. { |
| 989 | if r != nil { |
| 990 | l += r.lenscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#len.(l, compression) |
| 991 | } |
| 992 | } |
| 993 | for _, r := range dns.Nsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Ns. { |
| 994 | if r != nil { |
| 995 | l += r.lenscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#len.(l, compression) |
| 996 | } |
| 997 | } |
| 998 | for _, r := range dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra. { |
| 999 | if r != nil { |
| 1000 | l += r.lenscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#len.(l, compression) |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | return l |
| 1005 | } |
| 1006 | |
| 1007 | func domainNameLenscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/domainNameLen().(s string, off int, compression map[string]struct{}, compress bool) int { |
| 1008 | if s == "" || s == "." { |
| 1009 | return 1 |
| 1010 | } |
| 1011 | |
| 1012 | escaped := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(s, "\\") |
| 1013 | |
| 1014 | if compression != nil && (compress || off < maxCompressionOffsetscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/maxCompressionOffset.) { |
| 1015 | // compressionLenSearch will insert the entry into the compression |
| 1016 | // map if it doesn't contain it. |
| 1017 | if l, ok := compressionLenSearchscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionLenSearch().(compression, s, off); ok && compress { |
| 1018 | if escaped { |
| 1019 | return escapedNameLenscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/escapedNameLen().(s[:l]) + 2 |
| 1020 | } |
| 1021 | |
| 1022 | return l + 2 |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | if escaped { |
| 1027 | return escapedNameLenscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/escapedNameLen().(s) + 1 |
| 1028 | } |
| 1029 | |
| 1030 | return len(s) + 1 |
| 1031 | } |
| 1032 | |
| 1033 | func escapedNameLenscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/escapedNameLen().(s string) int { |
| 1034 | nameLen := len(s) |
| 1035 | for i := 0; i < len(s); i++ { |
| 1036 | if s[i] != '\\' { |
| 1037 | continue |
| 1038 | } |
| 1039 | |
| 1040 | if isDDDscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/isDDD().(s[i+1:]) { |
| 1041 | nameLen -= 3 |
| 1042 | i += 3 |
| 1043 | } else { |
| 1044 | nameLen-- |
| 1045 | i++ |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | return nameLen |
| 1050 | } |
| 1051 | |
| 1052 | func compressionLenSearchscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionLenSearch().(c map[string]struct{}, s string, msgOff int) (int, bool) { |
| 1053 | for off, end := 0, false; !end; off, end = NextLabelscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/NextLabel().(s, off) { |
| 1054 | if _, ok := c[s[off:]]; ok { |
| 1055 | return off, true |
| 1056 | } |
| 1057 | |
| 1058 | if msgOff+off < maxCompressionOffsetscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/maxCompressionOffset. { |
| 1059 | c[s[off:]] = struct{}{} |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | return 0, false |
| 1064 | } |
| 1065 | |
| 1066 | // Copy returns a new RR which is a deep-copy of r. |
| 1067 | func Copyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Copy().(r RRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#) RRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR# { return r.copyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#copy.() } |
| 1068 | |
| 1069 | // Len returns the length (in octets) of the uncompressed RR in wire format. |
| 1070 | func Lenscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Len().(r RRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#) int { return r.lenscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#len.(0, nil) } |
| 1071 | |
| 1072 | // Copy returns a new *Msg which is a deep-copy of dns. |
| 1073 | func (dns *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) Copyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Copy().() *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg# { return dns.CopyToscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#CopyTo().(new(Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#)) } |
| 1074 | |
| 1075 | // CopyTo copies the contents to the provided message using a deep-copy and returns the copy. |
| 1076 | func (dns *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) CopyToscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#CopyTo().(r1 *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg# { |
| 1077 | r1.MsgHdrscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#MsgHdr. = dns.MsgHdrscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#MsgHdr. |
| 1078 | r1.Compressscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Compress. = dns.Compressscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Compress. |
| 1079 | |
| 1080 | if len(dns.Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Question.) > 0 { |
| 1081 | // TODO(miek): Question is an immutable value, ok to do a shallow-copy |
| 1082 | r1.Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Question. = cloneSlicescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/cloneSlice().(dns.Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Question.) |
| 1083 | } |
| 1084 | |
| 1085 | rrArr := make([]RRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#, len(dns.Answerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Answer.)+len(dns.Nsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Ns.)+len(dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra.)) |
| 1086 | r1.Answerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Answer., rrArr = rrArr[:0:len(dns.Answerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Answer.)], rrArr[len(dns.Answerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Answer.):] |
| 1087 | r1.Nsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Ns., rrArr = rrArr[:0:len(dns.Nsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Ns.)], rrArr[len(dns.Nsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Ns.):] |
| 1088 | r1.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra. = rrArr[:0:len(dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra.)] |
| 1089 | |
| 1090 | for _, r := range dns.Answerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Answer. { |
| 1091 | r1.Answerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Answer. = append(r1.Answerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Answer., r.copyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#copy.()) |
| 1092 | } |
| 1093 | |
| 1094 | for _, r := range dns.Nsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Ns. { |
| 1095 | r1.Nsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Ns. = append(r1.Nsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Ns., r.copyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#copy.()) |
| 1096 | } |
| 1097 | |
| 1098 | for _, r := range dns.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra. { |
| 1099 | r1.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra. = append(r1.Extrascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Extra., r.copyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#copy.()) |
| 1100 | } |
| 1101 | |
| 1102 | return r1 |
| 1103 | } |
| 1104 | |
| 1105 | func (q *Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Question#) packscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Question#pack().(msg []byte, off int, compression compressionMapscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#, compress bool) (int, error) { |
| 1106 | off, err := packDomainNamescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/packDomainName().(q.Namescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Question#Name., msg, off, compression, compress) |
| 1107 | if err != nil { |
| 1108 | return off, err |
| 1109 | } |
| 1110 | off, err = packUint16scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/packUint16().(q.Qtypescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Question#Qtype., msg, off) |
| 1111 | if err != nil { |
| 1112 | return off, err |
| 1113 | } |
| 1114 | off, err = packUint16scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/packUint16().(q.Qclassscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Question#Qclass., msg, off) |
| 1115 | if err != nil { |
| 1116 | return off, err |
| 1117 | } |
| 1118 | return off, nil |
| 1119 | } |
| 1120 | |
| 1121 | func unpackQuestionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/unpackQuestion().(msg []byte, off int) (Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Question#, int, error) { |
| 1122 | var ( |
| 1123 | q Questionscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Question# |
| 1124 | err error |
| 1125 | ) |
| 1126 | q.Namescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Question#Name., off, err = UnpackDomainNamescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/UnpackDomainName().(msg, off) |
| 1127 | if err != nil { |
| 1128 | return q, off, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("bad question name: %w", err) |
| 1129 | } |
| 1130 | if off == len(msg) { |
| 1131 | return q, off, nil |
| 1132 | } |
| 1133 | q.Qtypescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Question#Qtype., off, err = unpackUint16scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/unpackUint16().(msg, off) |
| 1134 | if err != nil { |
| 1135 | return q, off, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("bad question qtype: %w", err) |
| 1136 | } |
| 1137 | if off == len(msg) { |
| 1138 | return q, off, nil |
| 1139 | } |
| 1140 | q.Qclassscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Question#Qclass., off, err = unpackUint16scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/unpackUint16().(msg, off) |
| 1141 | if err != nil { |
| 1142 | return q, off, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("bad question qclass: %w", err) |
| 1143 | } |
| 1144 | |
| 1145 | if off == len(msg) { |
| 1146 | return q, off, nil |
| 1147 | } |
| 1148 | |
| 1149 | return q, off, nil |
| 1150 | } |
| 1151 | |
| 1152 | func (dh *Headerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#) packscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#pack().(msg []byte, off int, compression compressionMapscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/compressionMap#, compress bool) (int, error) { |
| 1153 | off, err := packUint16scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/packUint16().(dh.Idscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Id., msg, off) |
| 1154 | if err != nil { |
| 1155 | return off, err |
| 1156 | } |
| 1157 | off, err = packUint16scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/packUint16().(dh.Bitsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Bits., msg, off) |
| 1158 | if err != nil { |
| 1159 | return off, err |
| 1160 | } |
| 1161 | off, err = packUint16scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/packUint16().(dh.Qdcountscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Qdcount., msg, off) |
| 1162 | if err != nil { |
| 1163 | return off, err |
| 1164 | } |
| 1165 | off, err = packUint16scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/packUint16().(dh.Ancountscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Ancount., msg, off) |
| 1166 | if err != nil { |
| 1167 | return off, err |
| 1168 | } |
| 1169 | off, err = packUint16scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/packUint16().(dh.Nscountscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Nscount., msg, off) |
| 1170 | if err != nil { |
| 1171 | return off, err |
| 1172 | } |
| 1173 | off, err = packUint16scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/packUint16().(dh.Arcountscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Arcount., msg, off) |
| 1174 | if err != nil { |
| 1175 | return off, err |
| 1176 | } |
| 1177 | return off, nil |
| 1178 | } |
| 1179 | |
| 1180 | func unpackMsgHdrscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/unpackMsgHdr().(msg []byte, off int) (Headerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#, int, error) { |
| 1181 | var ( |
| 1182 | dh Headerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header# |
| 1183 | err error |
| 1184 | ) |
| 1185 | dh.Idscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Id., off, err = unpackUint16scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/unpackUint16().(msg, off) |
| 1186 | if err != nil { |
| 1187 | return dh, off, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("bad header id: %w", err) |
| 1188 | } |
| 1189 | dh.Bitsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Bits., off, err = unpackUint16scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/unpackUint16().(msg, off) |
| 1190 | if err != nil { |
| 1191 | return dh, off, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("bad header bits: %w", err) |
| 1192 | } |
| 1193 | dh.Qdcountscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Qdcount., off, err = unpackUint16scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/unpackUint16().(msg, off) |
| 1194 | if err != nil { |
| 1195 | return dh, off, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("bad header question count: %w", err) |
| 1196 | } |
| 1197 | dh.Ancountscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Ancount., off, err = unpackUint16scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/unpackUint16().(msg, off) |
| 1198 | if err != nil { |
| 1199 | return dh, off, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("bad header answer count: %w", err) |
| 1200 | } |
| 1201 | dh.Nscountscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Nscount., off, err = unpackUint16scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/unpackUint16().(msg, off) |
| 1202 | if err != nil { |
| 1203 | return dh, off, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("bad header ns count: %w", err) |
| 1204 | } |
| 1205 | dh.Arcountscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Arcount., off, err = unpackUint16scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/unpackUint16().(msg, off) |
| 1206 | if err != nil { |
| 1207 | return dh, off, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("bad header extra count: %w", err) |
| 1208 | } |
| 1209 | return dh, off, nil |
| 1210 | } |
| 1211 | |
| 1212 | // setHdr set the header in the dns using the binary data in dh. |
| 1213 | func (dns *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) setHdrscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#setHdr().(dh Headerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#) { |
| 1214 | dns.Idscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Id. = dh.Idscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Id. |
| 1215 | dns.Responsescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Response. = dh.Bitsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Bits.&_QRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/_QR. != 0 |
| 1216 | dns.Opcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Opcode. = int(dh.Bitsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Bits.>>11) & 0xF |
| 1217 | dns.Authoritativescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Authoritative. = dh.Bitsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Bits.&_AAscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/_AA. != 0 |
| 1218 | dns.Truncatedscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Truncated. = dh.Bitsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Bits.&_TCscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/_TC. != 0 |
| 1219 | dns.RecursionDesiredscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#RecursionDesired. = dh.Bitsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Bits.&_RDscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/_RD. != 0 |
| 1220 | dns.RecursionAvailablescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#RecursionAvailable. = dh.Bitsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Bits.&_RAscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/_RA. != 0 |
| 1221 | dns.Zeroscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Zero. = dh.Bitsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Bits.&_Zscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/_Z. != 0 // _Z covers the zero bit, which should be zero; not sure why we set it to the opposite. |
| 1222 | dns.AuthenticatedDatascip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#AuthenticatedData. = dh.Bitsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Bits.&_ADscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/_AD. != 0 |
| 1223 | dns.CheckingDisabledscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#CheckingDisabled. = dh.Bitsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Bits.&_CDscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/_CD. != 0 |
| 1224 | dns.Rcodescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/MsgHdr#Rcode. = int(dh.Bitsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Header#Bits. & 0xF) |
| 1225 | } |