acceptfunc.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package dns
  2. // MsgAcceptFunc is used early in the server code to accept or reject a message with RcodeFormatError.
  3. // It returns a MsgAcceptAction to indicate what should happen with the message.
  4. type MsgAcceptFunc func(dh Header) MsgAcceptAction
  5. // DefaultMsgAcceptFunc checks the request and will reject if:
  6. //
  7. // * isn't a request (don't respond in that case).
  8. // * opcode isn't OpcodeQuery or OpcodeNotify
  9. // * Zero bit isn't zero
  10. // * has more than 1 question in the question section
  11. // * has more than 1 RR in the Answer section
  12. // * has more than 0 RRs in the Authority section
  13. // * has more than 2 RRs in the Additional section
  14. var DefaultMsgAcceptFunc MsgAcceptFunc = defaultMsgAcceptFunc
  15. // MsgAcceptAction represents the action to be taken.
  16. type MsgAcceptAction int
  17. const (
  18. MsgAccept MsgAcceptAction = iota // Accept the message
  19. MsgReject // Reject the message with a RcodeFormatError
  20. MsgIgnore // Ignore the error and send nothing back.
  21. )
  22. func defaultMsgAcceptFunc(dh Header) MsgAcceptAction {
  23. if isResponse := dh.Bits&_QR != 0; isResponse {
  24. return MsgIgnore
  25. }
  26. // Don't allow dynamic updates, because then the sections can contain a whole bunch of RRs.
  27. opcode := int(dh.Bits>>11) & 0xF
  28. if opcode != OpcodeQuery && opcode != OpcodeNotify {
  29. return MsgReject
  30. }
  31. if isZero := dh.Bits&_Z != 0; isZero {
  32. return MsgReject
  33. }
  34. if dh.Qdcount != 1 {
  35. return MsgReject
  36. }
  37. // NOTIFY requests can have a SOA in the ANSWER section. See RFC 1996 Section 3.7 and 3.11.
  38. if dh.Ancount > 1 {
  39. return MsgReject
  40. }
  41. // IXFR request could have one SOA RR in the NS section. See RFC 1995, section 3.
  42. if dh.Nscount > 1 {
  43. return MsgReject
  44. }
  45. if dh.Arcount > 2 {
  46. return MsgReject
  47. }
  48. return MsgAccept
  49. }