You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

77 lines
2.7 KiB

  1. #include "config.h"
  2. #include "gettime.hh"
  3. #include "dnstap.hh"
  4. DnstapMessage::DnstapMessage(const std::string& identity, const ComboAddress* requestor, const ComboAddress* responder, bool isTCP, const char* packet, const size_t len, const struct timespec* queryTime, const struct timespec* responseTime)
  5. {
  6. #ifdef HAVE_PROTOBUF
  7. const struct dnsheader* dh = reinterpret_cast<const struct dnsheader*>(packet);
  8. proto_message.set_identity(identity);
  9. proto_message.set_version(PACKAGE_STRING);
  10. proto_message.set_type(dnstap::Dnstap::MESSAGE);
  11. dnstap::Message* message = proto_message.mutable_message();
  12. message->set_type(!dh->qr ? dnstap::Message_Type_CLIENT_QUERY : dnstap::Message_Type_CLIENT_RESPONSE);
  13. message->set_socket_protocol(isTCP ? dnstap::TCP : dnstap::UDP);
  14. if (requestor != nullptr) {
  15. message->set_socket_family(requestor->sin4.sin_family == AF_INET ? dnstap::INET : dnstap::INET6);
  16. if (requestor->sin4.sin_family == AF_INET) {
  17. message->set_query_address(&requestor->sin4.sin_addr.s_addr, sizeof(requestor->sin4.sin_addr.s_addr));
  18. } else if (requestor->sin4.sin_family == AF_INET6) {
  19. message->set_query_address(&requestor->sin6.sin6_addr.s6_addr, sizeof(requestor->sin6.sin6_addr.s6_addr));
  20. }
  21. message->set_query_port(ntohs(requestor->sin4.sin_port));
  22. }
  23. if (responder != nullptr) {
  24. message->set_socket_family(responder->sin4.sin_family == AF_INET ? dnstap::INET : dnstap::INET6);
  25. if (responder->sin4.sin_family == AF_INET) {
  26. message->set_response_address(&responder->sin4.sin_addr.s_addr, sizeof(responder->sin4.sin_addr.s_addr));
  27. } else if (responder->sin4.sin_family == AF_INET6) {
  28. message->set_response_address(&responder->sin6.sin6_addr.s6_addr, sizeof(responder->sin6.sin6_addr.s6_addr));
  29. }
  30. message->set_response_port(ntohs(responder->sin4.sin_port));
  31. }
  32. if (queryTime != nullptr) {
  33. message->set_query_time_sec(queryTime->tv_sec);
  34. message->set_query_time_nsec(queryTime->tv_nsec);
  35. }
  36. if (responseTime != nullptr) {
  37. message->set_response_time_sec(responseTime->tv_sec);
  38. message->set_response_time_nsec(responseTime->tv_nsec);
  39. }
  40. if (!dh->qr) {
  41. message->set_query_message(packet, len);
  42. } else {
  43. message->set_response_message(packet, len);
  44. }
  45. #endif /* HAVE_PROTOBUF */
  46. }
  47. void DnstapMessage::serialize(std::string& data) const
  48. {
  49. #ifdef HAVE_PROTOBUF
  50. proto_message.SerializeToString(&data);
  51. #endif /* HAVE_PROTOBUF */
  52. }
  53. std::string DnstapMessage::toDebugString() const
  54. {
  55. return
  56. #ifdef HAVE_PROTOBUF
  57. proto_message.DebugString();
  58. #else
  59. "";
  60. #endif /* HAVE_PROTOBUF */
  61. }
  62. void DnstapMessage::setExtra(const std::string& extra)
  63. {
  64. #ifdef HAVE_PROTOBUF
  65. proto_message.set_extra(extra);
  66. #endif /* HAVE_PROTOBUF */
  67. }