| 1 |
#include <dnswire/version.h> |
| 2 |
#include <dnswire/dnstap.h> |
| 3 |
|
| 4 |
#include <errno.h> |
| 5 |
#include <string.h> |
| 6 |
#include <arpa/inet.h> |
| 7 |
#include <time.h> |
| 8 |
#include <sys/types.h> |
| 9 |
#include <sys/socket.h> |
| 10 |
#include <netinet/in.h> |
| 11 |
|
| 12 |
static char dns_wire_format_placeholder[] = "dns_wire_format_placeholder"; |
| 13 |
|
| 14 |
static inline struct dnstap create_dnstap(const char* identity) |
| 15 |
{ |
| 16 |
/* |
| 17 |
* Now we initialize a DNSTAP message. |
| 18 |
*/ |
| 19 |
|
| 20 |
struct dnstap d = DNSTAP_INITIALIZER; |
| 21 |
|
| 22 |
/* |
| 23 |
* DNSTAP has a header with information about who constructed the |
| 24 |
* message. |
| 25 |
*/ |
| 26 |
|
| 27 |
dnstap_set_identity_string(d, identity); |
| 28 |
dnstap_set_version_string(d, DNSWIRE_VERSION_STRING); |
| 29 |
|
| 30 |
/* |
| 31 |
* Now we specify that this is a DNSTAP message, this is the one that |
| 32 |
* holds the DNS. |
| 33 |
*/ |
| 34 |
|
| 35 |
dnstap_set_type(d, DNSTAP_TYPE_MESSAGE); |
| 36 |
|
| 37 |
/* |
| 38 |
* The message can have different types, we specify this is a query |
| 39 |
* made by a tool. |
| 40 |
*/ |
| 41 |
|
| 42 |
dnstap_message_set_type(d, DNSTAP_MESSAGE_TYPE_TOOL_QUERY); |
| 43 |
|
| 44 |
/* |
| 45 |
* As most DNS comes over the network we can specify over what protocol, |
| 46 |
* where from, to whom it came to and when it happened. |
| 47 |
* |
| 48 |
* Even if all fields are optional and there is no restriction on how |
| 49 |
* many or how few you set, there is a recommended way of filling in |
| 50 |
* the messages based on the message type. |
| 51 |
* |
| 52 |
* Please see the description of this at the bottom of |
| 53 |
* `src/dnstap.pb/dnstap.proto` or |
| 54 |
* <https://github.com/dnstap/dnstap.pb/blob/master/dnstap.proto>. |
| 55 |
*/ |
| 56 |
|
| 57 |
dnstap_message_set_socket_family(d, DNSTAP_SOCKET_FAMILY_INET); |
| 58 |
dnstap_message_set_socket_protocol(d, DNSTAP_SOCKET_PROTOCOL_UDP); |
| 59 |
|
| 60 |
unsigned char query_address[sizeof(struct in_addr)]; |
| 61 |
if (inet_pton(AF_INET, "127.0.0.1", query_address) != 1) { |
| 62 |
fprintf(stderr, "inet_pton(127.0.0.1) failed: %s\n", strerror(errno)); |
| 63 |
} else { |
| 64 |
dnstap_message_set_query_address(d, query_address, sizeof(query_address)); |
| 65 |
} |
| 66 |
dnstap_message_set_query_port(d, 12345); |
| 67 |
|
| 68 |
struct timespec query_time = { 0, 0 }; |
| 69 |
clock_gettime(CLOCK_REALTIME, &query_time); |
| 70 |
dnstap_message_set_query_time_sec(d, query_time.tv_sec); |
| 71 |
dnstap_message_set_query_time_nsec(d, query_time.tv_nsec); |
| 72 |
|
| 73 |
unsigned char response_address[sizeof(struct in_addr)]; |
| 74 |
if (inet_pton(AF_INET, "127.0.0.1", response_address) != 1) { |
| 75 |
fprintf(stderr, "inet_pton(127.0.0.1) failed: %s\n", strerror(errno)); |
| 76 |
} else { |
| 77 |
dnstap_message_set_response_address(d, response_address, sizeof(response_address)); |
| 78 |
} |
| 79 |
dnstap_message_set_response_port(d, 53); |
| 80 |
|
| 81 |
struct timespec response_time = { 0, 0 }; |
| 82 |
clock_gettime(CLOCK_REALTIME, &response_time); |
| 83 |
dnstap_message_set_response_time_sec(d, response_time.tv_sec); |
| 84 |
dnstap_message_set_response_time_nsec(d, response_time.tv_nsec); |
| 85 |
|
| 86 |
/* |
| 87 |
* If we also had the DNS wire format we could use it, now we fill it |
| 88 |
* with a placeholder text. |
| 89 |
* |
| 90 |
* NOTE: This will be invalid DNS if the output file is used with a |
| 91 |
* tool that uses the DNS messages. |
| 92 |
*/ |
| 93 |
|
| 94 |
dnstap_message_set_query_message(d, dns_wire_format_placeholder, sizeof(dns_wire_format_placeholder) - 1); |
| 95 |
|
| 96 |
dnstap_message_set_response_message(d, dns_wire_format_placeholder, sizeof(dns_wire_format_placeholder) - 1); |
| 97 |
|
| 98 |
return d; |
| 99 |
} |