edns.go 25 KB

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