edns.go 25 KB

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