acceptfunc.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. //
  9. // * opcode isn't OpcodeQuery or OpcodeNotify
  10. //
  11. // * Zero bit isn't zero
  12. //
  13. // * does not have exactly 1 question in the question section
  14. //
  15. // * has more than 1 RR in the Answer section
  16. //
  17. // * has more than 0 RRs in the Authority section
  18. //
  19. // * has more than 2 RRs in the Additional section
  20. var DefaultMsgAcceptFunc MsgAcceptFunc = defaultMsgAcceptFunc
  21. // MsgAcceptAction represents the action to be taken.
  22. type MsgAcceptAction int
  23. // Allowed returned values from a MsgAcceptFunc.
  24. const (
  25. MsgAccept MsgAcceptAction = iota // Accept the message
  26. MsgReject // Reject the message with a RcodeFormatError
  27. MsgIgnore // Ignore the error and send nothing back.
  28. MsgRejectNotImplemented // Reject the message with a RcodeNotImplemented
  29. )
  30. func defaultMsgAcceptFunc(dh Header) MsgAcceptAction {
  31. if isResponse := dh.Bits&_QR != 0; isResponse {
  32. return MsgIgnore
  33. }
  34. // Don't allow dynamic updates, because then the sections can contain a whole bunch of RRs.
  35. opcode := int(dh.Bits>>11) & 0xF
  36. if opcode != OpcodeQuery && opcode != OpcodeNotify {
  37. return MsgRejectNotImplemented
  38. }
  39. if dh.Qdcount != 1 {
  40. return MsgReject
  41. }
  42. // NOTIFY requests can have a SOA in the ANSWER section. See RFC 1996 Section 3.7 and 3.11.
  43. if dh.Ancount > 1 {
  44. return MsgReject
  45. }
  46. // IXFR request could have one SOA RR in the NS section. See RFC 1995, section 3.
  47. if dh.Nscount > 1 {
  48. return MsgReject
  49. }
  50. if dh.Arcount > 2 {
  51. return MsgReject
  52. }
  53. return MsgAccept
  54. }