edns.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. package dns
  2. import (
  3. "encoding/binary"
  4. "encoding/hex"
  5. "errors"
  6. "fmt"
  7. "net"
  8. "strconv"
  9. )
  10. // EDNS0 Option codes.
  11. const (
  12. EDNS0LLQ = 0x1 // long lived queries: http://tools.ietf.org/html/draft-sekar-dns-llq-01
  13. EDNS0UL = 0x2 // update lease draft: http://files.dns-sd.org/draft-sekar-dns-ul.txt
  14. EDNS0NSID = 0x3 // nsid (See RFC 5001)
  15. EDNS0ESU = 0x4 // ENUM Source-URI draft: https://datatracker.ietf.org/doc/html/draft-kaplan-enum-source-uri-00
  16. EDNS0DAU = 0x5 // DNSSEC Algorithm Understood
  17. EDNS0DHU = 0x6 // DS Hash Understood
  18. EDNS0N3U = 0x7 // NSEC3 Hash Understood
  19. EDNS0SUBNET = 0x8 // client-subnet (See RFC 7871)
  20. EDNS0EXPIRE = 0x9 // EDNS0 expire
  21. EDNS0COOKIE = 0xa // EDNS0 Cookie
  22. EDNS0TCPKEEPALIVE = 0xb // EDNS0 tcp keep alive (See RFC 7828)
  23. EDNS0PADDING = 0xc // EDNS0 padding (See RFC 7830)
  24. EDNS0EDE = 0xf // EDNS0 extended DNS errors (See RFC 8914)
  25. EDNS0LOCALSTART = 0xFDE9 // Beginning of range reserved for local/experimental use (See RFC 6891)
  26. EDNS0LOCALEND = 0xFFFE // End of range reserved for local/experimental use (See RFC 6891)
  27. _DO = 1 << 15 // DNSSEC OK
  28. )
  29. // makeDataOpt is used to unpack the EDNS0 option(s) from a message.
  30. func makeDataOpt(code uint16) EDNS0 {
  31. // All the EDNS0.* constants above need to be in this switch.
  32. switch code {
  33. case EDNS0LLQ:
  34. return new(EDNS0_LLQ)
  35. case EDNS0UL:
  36. return new(EDNS0_UL)
  37. case EDNS0NSID:
  38. return new(EDNS0_NSID)
  39. case EDNS0DAU:
  40. return new(EDNS0_DAU)
  41. case EDNS0DHU:
  42. return new(EDNS0_DHU)
  43. case EDNS0N3U:
  44. return new(EDNS0_N3U)
  45. case EDNS0SUBNET:
  46. return new(EDNS0_SUBNET)
  47. case EDNS0EXPIRE:
  48. return new(EDNS0_EXPIRE)
  49. case EDNS0COOKIE:
  50. return new(EDNS0_COOKIE)
  51. case EDNS0TCPKEEPALIVE:
  52. return new(EDNS0_TCP_KEEPALIVE)
  53. case EDNS0PADDING:
  54. return new(EDNS0_PADDING)
  55. case EDNS0EDE:
  56. return new(EDNS0_EDE)
  57. case EDNS0ESU:
  58. return &EDNS0_ESU{Code: EDNS0ESU}
  59. default:
  60. e := new(EDNS0_LOCAL)
  61. e.Code = code
  62. return e
  63. }
  64. }
  65. // OPT is the EDNS0 RR appended to messages to convey extra (meta) information.
  66. // See RFC 6891.
  67. type OPT struct {
  68. Hdr RR_Header
  69. Option []EDNS0 `dns:"opt"`
  70. }
  71. func (rr *OPT) String() string {
  72. s := "\n;; OPT PSEUDOSECTION:\n; EDNS: version " + strconv.Itoa(int(rr.Version())) + "; "
  73. if rr.Do() {
  74. s += "flags: do; "
  75. } else {
  76. s += "flags:; "
  77. }
  78. if rr.Hdr.Ttl&0x7FFF != 0 {
  79. s += fmt.Sprintf("MBZ: 0x%04x, ", rr.Hdr.Ttl&0x7FFF)
  80. }
  81. s += "udp: " + strconv.Itoa(int(rr.UDPSize()))
  82. for _, o := range rr.Option {
  83. switch o.(type) {
  84. case *EDNS0_NSID:
  85. s += "\n; NSID: " + o.String()
  86. h, e := o.pack()
  87. var r string
  88. if e == nil {
  89. for _, c := range h {
  90. r += "(" + string(c) + ")"
  91. }
  92. s += " " + r
  93. }
  94. case *EDNS0_SUBNET:
  95. s += "\n; SUBNET: " + o.String()
  96. case *EDNS0_COOKIE:
  97. s += "\n; COOKIE: " + o.String()
  98. case *EDNS0_EXPIRE:
  99. s += "\n; EXPIRE: " + o.String()
  100. case *EDNS0_TCP_KEEPALIVE:
  101. s += "\n; KEEPALIVE: " + o.String()
  102. case *EDNS0_UL:
  103. s += "\n; UPDATE LEASE: " + o.String()
  104. case *EDNS0_LLQ:
  105. s += "\n; LONG LIVED QUERIES: " + o.String()
  106. case *EDNS0_DAU:
  107. s += "\n; DNSSEC ALGORITHM UNDERSTOOD: " + o.String()
  108. case *EDNS0_DHU:
  109. s += "\n; DS HASH UNDERSTOOD: " + o.String()
  110. case *EDNS0_N3U:
  111. s += "\n; NSEC3 HASH UNDERSTOOD: " + o.String()
  112. case *EDNS0_LOCAL:
  113. s += "\n; LOCAL OPT: " + o.String()
  114. case *EDNS0_PADDING:
  115. s += "\n; PADDING: " + o.String()
  116. case *EDNS0_EDE:
  117. s += "\n; EDE: " + o.String()
  118. case *EDNS0_ESU:
  119. s += "\n; ESU: " + o.String()
  120. }
  121. }
  122. return s
  123. }
  124. func (rr *OPT) len(off int, compression map[string]struct{}) int {
  125. l := rr.Hdr.len(off, compression)
  126. for _, o := range rr.Option {
  127. l += 4 // Account for 2-byte option code and 2-byte option length.
  128. lo, _ := o.pack()
  129. l += len(lo)
  130. }
  131. return l
  132. }
  133. func (*OPT) parse(c *zlexer, origin string) *ParseError {
  134. return &ParseError{err: "OPT records do not have a presentation format"}
  135. }
  136. func (rr *OPT) isDuplicate(r2 RR) bool { return false }
  137. // return the old value -> delete SetVersion?
  138. // Version returns the EDNS version used. Only zero is defined.
  139. func (rr *OPT) Version() uint8 {
  140. return uint8(rr.Hdr.Ttl & 0x00FF0000 >> 16)
  141. }
  142. // SetVersion sets the version of EDNS. This is usually zero.
  143. func (rr *OPT) SetVersion(v uint8) {
  144. rr.Hdr.Ttl = rr.Hdr.Ttl&0xFF00FFFF | uint32(v)<<16
  145. }
  146. // ExtendedRcode returns the EDNS extended RCODE field (the upper 8 bits of the TTL).
  147. func (rr *OPT) ExtendedRcode() int {
  148. return int(rr.Hdr.Ttl&0xFF000000>>24) << 4
  149. }
  150. // SetExtendedRcode sets the EDNS extended RCODE field.
  151. //
  152. // If the RCODE is not an extended RCODE, will reset the extended RCODE field to 0.
  153. func (rr *OPT) SetExtendedRcode(v uint16) {
  154. rr.Hdr.Ttl = rr.Hdr.Ttl&0x00FFFFFF | uint32(v>>4)<<24
  155. }
  156. // UDPSize returns the UDP buffer size.
  157. func (rr *OPT) UDPSize() uint16 {
  158. return rr.Hdr.Class
  159. }
  160. // SetUDPSize sets the UDP buffer size.
  161. func (rr *OPT) SetUDPSize(size uint16) {
  162. rr.Hdr.Class = size
  163. }
  164. // Do returns the value of the DO (DNSSEC OK) bit.
  165. func (rr *OPT) Do() bool {
  166. return rr.Hdr.Ttl&_DO == _DO
  167. }
  168. // SetDo sets the DO (DNSSEC OK) bit.
  169. // If we pass an argument, set the DO bit to that value.
  170. // It is possible to pass 2 or more arguments. Any arguments after the 1st is silently ignored.
  171. func (rr *OPT) SetDo(do ...bool) {
  172. if len(do) == 1 {
  173. if do[0] {
  174. rr.Hdr.Ttl |= _DO
  175. } else {
  176. rr.Hdr.Ttl &^= _DO
  177. }
  178. } else {
  179. rr.Hdr.Ttl |= _DO
  180. }
  181. }
  182. // Z returns the Z part of the OPT RR as a uint16 with only the 15 least significant bits used.
  183. func (rr *OPT) Z() uint16 {
  184. return uint16(rr.Hdr.Ttl & 0x7FFF)
  185. }
  186. // SetZ sets the Z part of the OPT RR, note only the 15 least significant bits of z are used.
  187. func (rr *OPT) SetZ(z uint16) {
  188. rr.Hdr.Ttl = rr.Hdr.Ttl&^0x7FFF | uint32(z&0x7FFF)
  189. }
  190. // EDNS0 defines an EDNS0 Option. An OPT RR can have multiple options appended to it.
  191. type EDNS0 interface {
  192. // Option returns the option code for the option.
  193. Option() uint16
  194. // pack returns the bytes of the option data.
  195. pack() ([]byte, error)
  196. // unpack sets the data as found in the buffer. Is also sets
  197. // the length of the slice as the length of the option data.
  198. unpack([]byte) error
  199. // String returns the string representation of the option.
  200. String() string
  201. // copy returns a deep-copy of the option.
  202. copy() EDNS0
  203. }
  204. // EDNS0_NSID option is used to retrieve a nameserver
  205. // identifier. When sending a request Nsid must be set to the empty string
  206. // The identifier is an opaque string encoded as hex.
  207. // Basic use pattern for creating an nsid option:
  208. //
  209. // o := new(dns.OPT)
  210. // o.Hdr.Name = "."
  211. // o.Hdr.Rrtype = dns.TypeOPT
  212. // e := new(dns.EDNS0_NSID)
  213. // e.Code = dns.EDNS0NSID
  214. // e.Nsid = "AA"
  215. // o.Option = append(o.Option, e)
  216. type EDNS0_NSID struct {
  217. Code uint16 // Always EDNS0NSID
  218. Nsid string // This string needs to be hex encoded
  219. }
  220. func (e *EDNS0_NSID) pack() ([]byte, error) {
  221. h, err := hex.DecodeString(e.Nsid)
  222. if err != nil {
  223. return nil, err
  224. }
  225. return h, nil
  226. }
  227. // Option implements the EDNS0 interface.
  228. func (e *EDNS0_NSID) Option() uint16 { return EDNS0NSID } // Option returns the option code.
  229. func (e *EDNS0_NSID) unpack(b []byte) error { e.Nsid = hex.EncodeToString(b); return nil }
  230. func (e *EDNS0_NSID) String() string { return e.Nsid }
  231. func (e *EDNS0_NSID) copy() EDNS0 { return &EDNS0_NSID{e.Code, e.Nsid} }
  232. // EDNS0_SUBNET is the subnet option that is used to give the remote nameserver
  233. // an idea of where the client lives. See RFC 7871. It can then give back a different
  234. // answer depending on the location or network topology.
  235. // Basic use pattern for creating an subnet option:
  236. //
  237. // o := new(dns.OPT)
  238. // o.Hdr.Name = "."
  239. // o.Hdr.Rrtype = dns.TypeOPT
  240. // e := new(dns.EDNS0_SUBNET)
  241. // e.Code = dns.EDNS0SUBNET // by default this is filled in through unpacking OPT packets (unpackDataOpt)
  242. // e.Family = 1 // 1 for IPv4 source address, 2 for IPv6
  243. // e.SourceNetmask = 32 // 32 for IPV4, 128 for IPv6
  244. // e.SourceScope = 0
  245. // e.Address = net.ParseIP("127.0.0.1").To4() // for IPv4
  246. // // e.Address = net.ParseIP("2001:7b8:32a::2") // for IPV6
  247. // o.Option = append(o.Option, e)
  248. //
  249. // This code will parse all the available bits when unpacking (up to optlen).
  250. // When packing it will apply SourceNetmask. If you need more advanced logic,
  251. // patches welcome and good luck.
  252. type EDNS0_SUBNET struct {
  253. Code uint16 // Always EDNS0SUBNET
  254. Family uint16 // 1 for IP, 2 for IP6
  255. SourceNetmask uint8
  256. SourceScope uint8
  257. Address net.IP
  258. }
  259. // Option implements the EDNS0 interface.
  260. func (e *EDNS0_SUBNET) Option() uint16 { return EDNS0SUBNET }
  261. func (e *EDNS0_SUBNET) pack() ([]byte, error) {
  262. b := make([]byte, 4)
  263. binary.BigEndian.PutUint16(b[0:], e.Family)
  264. b[2] = e.SourceNetmask
  265. b[3] = e.SourceScope
  266. switch e.Family {
  267. case 0:
  268. // "dig" sets AddressFamily to 0 if SourceNetmask is also 0
  269. // We might don't need to complain either
  270. if e.SourceNetmask != 0 {
  271. return nil, errors.New("dns: bad address family")
  272. }
  273. case 1:
  274. if e.SourceNetmask > net.IPv4len*8 {
  275. return nil, errors.New("dns: bad netmask")
  276. }
  277. if len(e.Address.To4()) != net.IPv4len {
  278. return nil, errors.New("dns: bad address")
  279. }
  280. ip := e.Address.To4().Mask(net.CIDRMask(int(e.SourceNetmask), net.IPv4len*8))
  281. needLength := (e.SourceNetmask + 8 - 1) / 8 // division rounding up
  282. b = append(b, ip[:needLength]...)
  283. case 2:
  284. if e.SourceNetmask > net.IPv6len*8 {
  285. return nil, errors.New("dns: bad netmask")
  286. }
  287. if len(e.Address) != net.IPv6len {
  288. return nil, errors.New("dns: bad address")
  289. }
  290. ip := e.Address.Mask(net.CIDRMask(int(e.SourceNetmask), net.IPv6len*8))
  291. needLength := (e.SourceNetmask + 8 - 1) / 8 // division rounding up
  292. b = append(b, ip[:needLength]...)
  293. default:
  294. return nil, errors.New("dns: bad address family")
  295. }
  296. return b, nil
  297. }
  298. func (e *EDNS0_SUBNET) unpack(b []byte) error {
  299. if len(b) < 4 {
  300. return ErrBuf
  301. }
  302. e.Family = binary.BigEndian.Uint16(b)
  303. e.SourceNetmask = b[2]
  304. e.SourceScope = b[3]
  305. switch e.Family {
  306. case 0:
  307. // "dig" sets AddressFamily to 0 if SourceNetmask is also 0
  308. // It's okay to accept such a packet
  309. if e.SourceNetmask != 0 {
  310. return errors.New("dns: bad address family")
  311. }
  312. e.Address = net.IPv4(0, 0, 0, 0)
  313. case 1:
  314. if e.SourceNetmask > net.IPv4len*8 || e.SourceScope > net.IPv4len*8 {
  315. return errors.New("dns: bad netmask")
  316. }
  317. addr := make(net.IP, net.IPv4len)
  318. copy(addr, b[4:])
  319. e.Address = addr.To16()
  320. case 2:
  321. if e.SourceNetmask > net.IPv6len*8 || e.SourceScope > net.IPv6len*8 {
  322. return errors.New("dns: bad netmask")
  323. }
  324. addr := make(net.IP, net.IPv6len)
  325. copy(addr, b[4:])
  326. e.Address = addr
  327. default:
  328. return errors.New("dns: bad address family")
  329. }
  330. return nil
  331. }
  332. func (e *EDNS0_SUBNET) String() (s string) {
  333. if e.Address == nil {
  334. s = "<nil>"
  335. } else if e.Address.To4() != nil {
  336. s = e.Address.String()
  337. } else {
  338. s = "[" + e.Address.String() + "]"
  339. }
  340. s += "/" + strconv.Itoa(int(e.SourceNetmask)) + "/" + strconv.Itoa(int(e.SourceScope))
  341. return
  342. }
  343. func (e *EDNS0_SUBNET) copy() EDNS0 {
  344. return &EDNS0_SUBNET{
  345. e.Code,
  346. e.Family,
  347. e.SourceNetmask,
  348. e.SourceScope,
  349. e.Address,
  350. }
  351. }
  352. // The EDNS0_COOKIE option is used to add a DNS Cookie to a message.
  353. //
  354. // o := new(dns.OPT)
  355. // o.Hdr.Name = "."
  356. // o.Hdr.Rrtype = dns.TypeOPT
  357. // e := new(dns.EDNS0_COOKIE)
  358. // e.Code = dns.EDNS0COOKIE
  359. // e.Cookie = "24a5ac.."
  360. // o.Option = append(o.Option, e)
  361. //
  362. // The Cookie field consists out of a client cookie (RFC 7873 Section 4), that is
  363. // always 8 bytes. It may then optionally be followed by the server cookie. The server
  364. // cookie is of variable length, 8 to a maximum of 32 bytes. In other words:
  365. //
  366. // cCookie := o.Cookie[:16]
  367. // sCookie := o.Cookie[16:]
  368. //
  369. // There is no guarantee that the Cookie string has a specific length.
  370. type EDNS0_COOKIE struct {
  371. Code uint16 // Always EDNS0COOKIE
  372. Cookie string // Hex-encoded cookie data
  373. }
  374. func (e *EDNS0_COOKIE) pack() ([]byte, error) {
  375. h, err := hex.DecodeString(e.Cookie)
  376. if err != nil {
  377. return nil, err
  378. }
  379. return h, nil
  380. }
  381. // Option implements the EDNS0 interface.
  382. func (e *EDNS0_COOKIE) Option() uint16 { return EDNS0COOKIE }
  383. func (e *EDNS0_COOKIE) unpack(b []byte) error { e.Cookie = hex.EncodeToString(b); return nil }
  384. func (e *EDNS0_COOKIE) String() string { return e.Cookie }
  385. func (e *EDNS0_COOKIE) copy() EDNS0 { return &EDNS0_COOKIE{e.Code, e.Cookie} }
  386. // The EDNS0_UL (Update Lease) (draft RFC) option is used to tell the server to set
  387. // an expiration on an update RR. This is helpful for clients that cannot clean
  388. // up after themselves. This is a draft RFC and more information can be found at
  389. // https://tools.ietf.org/html/draft-sekar-dns-ul-02
  390. //
  391. // o := new(dns.OPT)
  392. // o.Hdr.Name = "."
  393. // o.Hdr.Rrtype = dns.TypeOPT
  394. // e := new(dns.EDNS0_UL)
  395. // e.Code = dns.EDNS0UL
  396. // e.Lease = 120 // in seconds
  397. // o.Option = append(o.Option, e)
  398. type EDNS0_UL struct {
  399. Code uint16 // Always EDNS0UL
  400. Lease uint32
  401. KeyLease uint32
  402. }
  403. // Option implements the EDNS0 interface.
  404. func (e *EDNS0_UL) Option() uint16 { return EDNS0UL }
  405. func (e *EDNS0_UL) String() string { return fmt.Sprintf("%d %d", e.Lease, e.KeyLease) }
  406. func (e *EDNS0_UL) copy() EDNS0 { return &EDNS0_UL{e.Code, e.Lease, e.KeyLease} }
  407. // Copied: http://golang.org/src/pkg/net/dnsmsg.go
  408. func (e *EDNS0_UL) pack() ([]byte, error) {
  409. var b []byte
  410. if e.KeyLease == 0 {
  411. b = make([]byte, 4)
  412. } else {
  413. b = make([]byte, 8)
  414. binary.BigEndian.PutUint32(b[4:], e.KeyLease)
  415. }
  416. binary.BigEndian.PutUint32(b, e.Lease)
  417. return b, nil
  418. }
  419. func (e *EDNS0_UL) unpack(b []byte) error {
  420. switch len(b) {
  421. case 4:
  422. e.KeyLease = 0
  423. case 8:
  424. e.KeyLease = binary.BigEndian.Uint32(b[4:])
  425. default:
  426. return ErrBuf
  427. }
  428. e.Lease = binary.BigEndian.Uint32(b)
  429. return nil
  430. }
  431. // EDNS0_LLQ stands for Long Lived Queries: http://tools.ietf.org/html/draft-sekar-dns-llq-01
  432. // Implemented for completeness, as the EDNS0 type code is assigned.
  433. type EDNS0_LLQ struct {
  434. Code uint16 // Always EDNS0LLQ
  435. Version uint16
  436. Opcode uint16
  437. Error uint16
  438. Id uint64
  439. LeaseLife uint32
  440. }
  441. // Option implements the EDNS0 interface.
  442. func (e *EDNS0_LLQ) Option() uint16 { return EDNS0LLQ }
  443. func (e *EDNS0_LLQ) pack() ([]byte, error) {
  444. b := make([]byte, 18)
  445. binary.BigEndian.PutUint16(b[0:], e.Version)
  446. binary.BigEndian.PutUint16(b[2:], e.Opcode)
  447. binary.BigEndian.PutUint16(b[4:], e.Error)
  448. binary.BigEndian.PutUint64(b[6:], e.Id)
  449. binary.BigEndian.PutUint32(b[14:], e.LeaseLife)
  450. return b, nil
  451. }
  452. func (e *EDNS0_LLQ) unpack(b []byte) error {
  453. if len(b) < 18 {
  454. return ErrBuf
  455. }
  456. e.Version = binary.BigEndian.Uint16(b[0:])
  457. e.Opcode = binary.BigEndian.Uint16(b[2:])
  458. e.Error = binary.BigEndian.Uint16(b[4:])
  459. e.Id = binary.BigEndian.Uint64(b[6:])
  460. e.LeaseLife = binary.BigEndian.Uint32(b[14:])
  461. return nil
  462. }
  463. func (e *EDNS0_LLQ) String() string {
  464. s := strconv.FormatUint(uint64(e.Version), 10) + " " + strconv.FormatUint(uint64(e.Opcode), 10) +
  465. " " + strconv.FormatUint(uint64(e.Error), 10) + " " + strconv.FormatUint(e.Id, 10) +
  466. " " + strconv.FormatUint(uint64(e.LeaseLife), 10)
  467. return s
  468. }
  469. func (e *EDNS0_LLQ) copy() EDNS0 {
  470. return &EDNS0_LLQ{e.Code, e.Version, e.Opcode, e.Error, e.Id, e.LeaseLife}
  471. }
  472. // EDNS0_DAU implements the EDNS0 "DNSSEC Algorithm Understood" option. See RFC 6975.
  473. type EDNS0_DAU struct {
  474. Code uint16 // Always EDNS0DAU
  475. AlgCode []uint8
  476. }
  477. // Option implements the EDNS0 interface.
  478. func (e *EDNS0_DAU) Option() uint16 { return EDNS0DAU }
  479. func (e *EDNS0_DAU) pack() ([]byte, error) { return cloneSlice(e.AlgCode), nil }
  480. func (e *EDNS0_DAU) unpack(b []byte) error { e.AlgCode = cloneSlice(b); return nil }
  481. func (e *EDNS0_DAU) String() string {
  482. s := ""
  483. for _, alg := range e.AlgCode {
  484. if a, ok := AlgorithmToString[alg]; ok {
  485. s += " " + a
  486. } else {
  487. s += " " + strconv.Itoa(int(alg))
  488. }
  489. }
  490. return s
  491. }
  492. func (e *EDNS0_DAU) copy() EDNS0 { return &EDNS0_DAU{e.Code, e.AlgCode} }
  493. // EDNS0_DHU implements the EDNS0 "DS Hash Understood" option. See RFC 6975.
  494. type EDNS0_DHU struct {
  495. Code uint16 // Always EDNS0DHU
  496. AlgCode []uint8
  497. }
  498. // Option implements the EDNS0 interface.
  499. func (e *EDNS0_DHU) Option() uint16 { return EDNS0DHU }
  500. func (e *EDNS0_DHU) pack() ([]byte, error) { return cloneSlice(e.AlgCode), nil }
  501. func (e *EDNS0_DHU) unpack(b []byte) error { e.AlgCode = cloneSlice(b); return nil }
  502. func (e *EDNS0_DHU) String() string {
  503. s := ""
  504. for _, alg := range e.AlgCode {
  505. if a, ok := HashToString[alg]; ok {
  506. s += " " + a
  507. } else {
  508. s += " " + strconv.Itoa(int(alg))
  509. }
  510. }
  511. return s
  512. }
  513. func (e *EDNS0_DHU) copy() EDNS0 { return &EDNS0_DHU{e.Code, e.AlgCode} }
  514. // EDNS0_N3U implements the EDNS0 "NSEC3 Hash Understood" option. See RFC 6975.
  515. type EDNS0_N3U struct {
  516. Code uint16 // Always EDNS0N3U
  517. AlgCode []uint8
  518. }
  519. // Option implements the EDNS0 interface.
  520. func (e *EDNS0_N3U) Option() uint16 { return EDNS0N3U }
  521. func (e *EDNS0_N3U) pack() ([]byte, error) { return cloneSlice(e.AlgCode), nil }
  522. func (e *EDNS0_N3U) unpack(b []byte) error { e.AlgCode = cloneSlice(b); return nil }
  523. func (e *EDNS0_N3U) String() string {
  524. // Re-use the hash map
  525. s := ""
  526. for _, alg := range e.AlgCode {
  527. if a, ok := HashToString[alg]; ok {
  528. s += " " + a
  529. } else {
  530. s += " " + strconv.Itoa(int(alg))
  531. }
  532. }
  533. return s
  534. }
  535. func (e *EDNS0_N3U) copy() EDNS0 { return &EDNS0_N3U{e.Code, e.AlgCode} }
  536. // EDNS0_EXPIRE implements the EDNS0 option as described in RFC 7314.
  537. type EDNS0_EXPIRE struct {
  538. Code uint16 // Always EDNS0EXPIRE
  539. Expire uint32
  540. Empty bool // Empty is used to signal an empty Expire option in a backwards compatible way, it's not used on the wire.
  541. }
  542. // Option implements the EDNS0 interface.
  543. func (e *EDNS0_EXPIRE) Option() uint16 { return EDNS0EXPIRE }
  544. func (e *EDNS0_EXPIRE) copy() EDNS0 { return &EDNS0_EXPIRE{e.Code, e.Expire, e.Empty} }
  545. func (e *EDNS0_EXPIRE) pack() ([]byte, error) {
  546. if e.Empty {
  547. return []byte{}, nil
  548. }
  549. b := make([]byte, 4)
  550. binary.BigEndian.PutUint32(b, e.Expire)
  551. return b, nil
  552. }
  553. func (e *EDNS0_EXPIRE) unpack(b []byte) error {
  554. if len(b) == 0 {
  555. // zero-length EXPIRE query, see RFC 7314 Section 2
  556. e.Empty = true
  557. return nil
  558. }
  559. if len(b) < 4 {
  560. return ErrBuf
  561. }
  562. e.Expire = binary.BigEndian.Uint32(b)
  563. e.Empty = false
  564. return nil
  565. }
  566. func (e *EDNS0_EXPIRE) String() (s string) {
  567. if e.Empty {
  568. return ""
  569. }
  570. return strconv.FormatUint(uint64(e.Expire), 10)
  571. }
  572. // The EDNS0_LOCAL option is used for local/experimental purposes. The option
  573. // code is recommended to be within the range [EDNS0LOCALSTART, EDNS0LOCALEND]
  574. // (RFC6891), although any unassigned code can actually be used. The content of
  575. // the option is made available in Data, unaltered.
  576. // Basic use pattern for creating a local option:
  577. //
  578. // o := new(dns.OPT)
  579. // o.Hdr.Name = "."
  580. // o.Hdr.Rrtype = dns.TypeOPT
  581. // e := new(dns.EDNS0_LOCAL)
  582. // e.Code = dns.EDNS0LOCALSTART
  583. // e.Data = []byte{72, 82, 74}
  584. // o.Option = append(o.Option, e)
  585. type EDNS0_LOCAL struct {
  586. Code uint16
  587. Data []byte
  588. }
  589. // Option implements the EDNS0 interface.
  590. func (e *EDNS0_LOCAL) Option() uint16 { return e.Code }
  591. func (e *EDNS0_LOCAL) String() string {
  592. return strconv.FormatInt(int64(e.Code), 10) + ":0x" + hex.EncodeToString(e.Data)
  593. }
  594. func (e *EDNS0_LOCAL) copy() EDNS0 {
  595. return &EDNS0_LOCAL{e.Code, cloneSlice(e.Data)}
  596. }
  597. func (e *EDNS0_LOCAL) pack() ([]byte, error) {
  598. return cloneSlice(e.Data), nil
  599. }
  600. func (e *EDNS0_LOCAL) unpack(b []byte) error {
  601. e.Data = cloneSlice(b)
  602. return nil
  603. }
  604. // EDNS0_TCP_KEEPALIVE is an EDNS0 option that instructs the server to keep
  605. // the TCP connection alive. See RFC 7828.
  606. type EDNS0_TCP_KEEPALIVE struct {
  607. Code uint16 // Always EDNSTCPKEEPALIVE
  608. // Timeout is an idle timeout value for the TCP connection, specified in
  609. // units of 100 milliseconds, encoded in network byte order. If set to 0,
  610. // pack will return a nil slice.
  611. Timeout uint16
  612. // Length is the option's length.
  613. // Deprecated: this field is deprecated and is always equal to 0.
  614. Length uint16
  615. }
  616. // Option implements the EDNS0 interface.
  617. func (e *EDNS0_TCP_KEEPALIVE) Option() uint16 { return EDNS0TCPKEEPALIVE }
  618. func (e *EDNS0_TCP_KEEPALIVE) pack() ([]byte, error) {
  619. if e.Timeout > 0 {
  620. b := make([]byte, 2)
  621. binary.BigEndian.PutUint16(b, e.Timeout)
  622. return b, nil
  623. }
  624. return nil, nil
  625. }
  626. func (e *EDNS0_TCP_KEEPALIVE) unpack(b []byte) error {
  627. switch len(b) {
  628. case 0:
  629. case 2:
  630. e.Timeout = binary.BigEndian.Uint16(b)
  631. default:
  632. return fmt.Errorf("dns: length mismatch, want 0/2 but got %d", len(b))
  633. }
  634. return nil
  635. }
  636. func (e *EDNS0_TCP_KEEPALIVE) String() string {
  637. s := "use tcp keep-alive"
  638. if e.Timeout == 0 {
  639. s += ", timeout omitted"
  640. } else {
  641. s += fmt.Sprintf(", timeout %dms", e.Timeout*100)
  642. }
  643. return s
  644. }
  645. func (e *EDNS0_TCP_KEEPALIVE) copy() EDNS0 { return &EDNS0_TCP_KEEPALIVE{e.Code, e.Timeout, e.Length} }
  646. // EDNS0_PADDING option is used to add padding to a request/response. The default
  647. // value of padding SHOULD be 0x0 but other values MAY be used, for instance if
  648. // compression is applied before encryption which may break signatures.
  649. type EDNS0_PADDING struct {
  650. Padding []byte
  651. }
  652. // Option implements the EDNS0 interface.
  653. func (e *EDNS0_PADDING) Option() uint16 { return EDNS0PADDING }
  654. func (e *EDNS0_PADDING) pack() ([]byte, error) { return cloneSlice(e.Padding), nil }
  655. func (e *EDNS0_PADDING) unpack(b []byte) error { e.Padding = cloneSlice(b); return nil }
  656. func (e *EDNS0_PADDING) String() string { return fmt.Sprintf("%0X", e.Padding) }
  657. func (e *EDNS0_PADDING) copy() EDNS0 { return &EDNS0_PADDING{cloneSlice(e.Padding)} }
  658. // Extended DNS Error Codes (RFC 8914).
  659. const (
  660. ExtendedErrorCodeOther uint16 = iota
  661. ExtendedErrorCodeUnsupportedDNSKEYAlgorithm
  662. ExtendedErrorCodeUnsupportedDSDigestType
  663. ExtendedErrorCodeStaleAnswer
  664. ExtendedErrorCodeForgedAnswer
  665. ExtendedErrorCodeDNSSECIndeterminate
  666. ExtendedErrorCodeDNSBogus
  667. ExtendedErrorCodeSignatureExpired
  668. ExtendedErrorCodeSignatureNotYetValid
  669. ExtendedErrorCodeDNSKEYMissing
  670. ExtendedErrorCodeRRSIGsMissing
  671. ExtendedErrorCodeNoZoneKeyBitSet
  672. ExtendedErrorCodeNSECMissing
  673. ExtendedErrorCodeCachedError
  674. ExtendedErrorCodeNotReady
  675. ExtendedErrorCodeBlocked
  676. ExtendedErrorCodeCensored
  677. ExtendedErrorCodeFiltered
  678. ExtendedErrorCodeProhibited
  679. ExtendedErrorCodeStaleNXDOMAINAnswer
  680. ExtendedErrorCodeNotAuthoritative
  681. ExtendedErrorCodeNotSupported
  682. ExtendedErrorCodeNoReachableAuthority
  683. ExtendedErrorCodeNetworkError
  684. ExtendedErrorCodeInvalidData
  685. )
  686. // ExtendedErrorCodeToString maps extended error info codes to a human readable
  687. // description.
  688. var ExtendedErrorCodeToString = map[uint16]string{
  689. ExtendedErrorCodeOther: "Other",
  690. ExtendedErrorCodeUnsupportedDNSKEYAlgorithm: "Unsupported DNSKEY Algorithm",
  691. ExtendedErrorCodeUnsupportedDSDigestType: "Unsupported DS Digest Type",
  692. ExtendedErrorCodeStaleAnswer: "Stale Answer",
  693. ExtendedErrorCodeForgedAnswer: "Forged Answer",
  694. ExtendedErrorCodeDNSSECIndeterminate: "DNSSEC Indeterminate",
  695. ExtendedErrorCodeDNSBogus: "DNSSEC Bogus",
  696. ExtendedErrorCodeSignatureExpired: "Signature Expired",
  697. ExtendedErrorCodeSignatureNotYetValid: "Signature Not Yet Valid",
  698. ExtendedErrorCodeDNSKEYMissing: "DNSKEY Missing",
  699. ExtendedErrorCodeRRSIGsMissing: "RRSIGs Missing",
  700. ExtendedErrorCodeNoZoneKeyBitSet: "No Zone Key Bit Set",
  701. ExtendedErrorCodeNSECMissing: "NSEC Missing",
  702. ExtendedErrorCodeCachedError: "Cached Error",
  703. ExtendedErrorCodeNotReady: "Not Ready",
  704. ExtendedErrorCodeBlocked: "Blocked",
  705. ExtendedErrorCodeCensored: "Censored",
  706. ExtendedErrorCodeFiltered: "Filtered",
  707. ExtendedErrorCodeProhibited: "Prohibited",
  708. ExtendedErrorCodeStaleNXDOMAINAnswer: "Stale NXDOMAIN Answer",
  709. ExtendedErrorCodeNotAuthoritative: "Not Authoritative",
  710. ExtendedErrorCodeNotSupported: "Not Supported",
  711. ExtendedErrorCodeNoReachableAuthority: "No Reachable Authority",
  712. ExtendedErrorCodeNetworkError: "Network Error",
  713. ExtendedErrorCodeInvalidData: "Invalid Data",
  714. }
  715. // StringToExtendedErrorCode is a map from human readable descriptions to
  716. // extended error info codes.
  717. var StringToExtendedErrorCode = reverseInt16(ExtendedErrorCodeToString)
  718. // EDNS0_EDE option is used to return additional information about the cause of
  719. // DNS errors.
  720. type EDNS0_EDE struct {
  721. InfoCode uint16
  722. ExtraText string
  723. }
  724. // Option implements the EDNS0 interface.
  725. func (e *EDNS0_EDE) Option() uint16 { return EDNS0EDE }
  726. func (e *EDNS0_EDE) copy() EDNS0 { return &EDNS0_EDE{e.InfoCode, e.ExtraText} }
  727. func (e *EDNS0_EDE) String() string {
  728. info := strconv.FormatUint(uint64(e.InfoCode), 10)
  729. if s, ok := ExtendedErrorCodeToString[e.InfoCode]; ok {
  730. info += fmt.Sprintf(" (%s)", s)
  731. }
  732. return fmt.Sprintf("%s: (%s)", info, e.ExtraText)
  733. }
  734. func (e *EDNS0_EDE) pack() ([]byte, error) {
  735. b := make([]byte, 2+len(e.ExtraText))
  736. binary.BigEndian.PutUint16(b[0:], e.InfoCode)
  737. copy(b[2:], e.ExtraText)
  738. return b, nil
  739. }
  740. func (e *EDNS0_EDE) unpack(b []byte) error {
  741. if len(b) < 2 {
  742. return ErrBuf
  743. }
  744. e.InfoCode = binary.BigEndian.Uint16(b[0:])
  745. e.ExtraText = string(b[2:])
  746. return nil
  747. }
  748. // The EDNS0_ESU option for ENUM Source-URI Extension
  749. type EDNS0_ESU struct {
  750. Code uint16
  751. Uri string
  752. }
  753. // Option implements the EDNS0 interface.
  754. func (e *EDNS0_ESU) Option() uint16 { return EDNS0ESU }
  755. func (e *EDNS0_ESU) String() string { return e.Uri }
  756. func (e *EDNS0_ESU) copy() EDNS0 { return &EDNS0_ESU{e.Code, e.Uri} }
  757. func (e *EDNS0_ESU) pack() ([]byte, error) { return []byte(e.Uri), nil }
  758. func (e *EDNS0_ESU) unpack(b []byte) error {
  759. e.Uri = string(b)
  760. return nil
  761. }