1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
/* Copyright (C) 2023 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include "contrib/dnstap/convert.h"
#include "contrib/dnstap/dnstap.pb-c.h"
#include "libknot/probe/data.h"
/*!
* \brief Translation between real and Dnstap value.
*/
typedef struct mapping {
int real;
int dnstap;
} mapping_t;
/*!
* \brief Mapping for network family.
*/
static const mapping_t SOCKET_FAMILY_MAPPING[] = {
{ AF_INET, DNSTAP__SOCKET_FAMILY__INET },
{ AF_INET6, DNSTAP__SOCKET_FAMILY__INET6 },
{ 0 }
};
/*!
* \brief Mapping from network protocol.
*/
static const mapping_t SOCKET_PROTOCOL_MAPPING[] = {
{ KNOT_PROBE_PROTO_UDP, DNSTAP__SOCKET_PROTOCOL__UDP },
{ KNOT_PROBE_PROTO_TCP, DNSTAP__SOCKET_PROTOCOL__TCP },
{ KNOT_PROBE_PROTO_TLS, DNSTAP__SOCKET_PROTOCOL__DOT },
{ KNOT_PROBE_PROTO_HTTPS, DNSTAP__SOCKET_PROTOCOL__DOH },
{ KNOT_PROBE_PROTO_QUIC, DNSTAP__SOCKET_PROTOCOL__DOQ },
{ 0 }
};
/*!
* \brief Get Dnstap value for a given real value.
*/
static int encode(const mapping_t *mapping, int real)
{
for (const mapping_t *m = mapping; m->dnstap != 0; m += 1) {
if (m->real == real) {
return m->dnstap;
}
}
return 0;
}
/*!
* \brief Get real value for a given Dnstap value.
*/
static int decode(const mapping_t *mapping, int dnstap)
{
for (const mapping_t *m = mapping; m->dnstap != 0; m += 1) {
if (m->dnstap == dnstap) {
return m->real;
}
}
return 0;
}
/* -- public API ----------------------------------------------------------- */
Dnstap__SocketFamily dt_family_encode(int family)
{
return encode(SOCKET_FAMILY_MAPPING, family);
}
int dt_family_decode(Dnstap__SocketFamily dnstap_family)
{
return decode(SOCKET_FAMILY_MAPPING, dnstap_family);
}
Dnstap__SocketProtocol dt_protocol_encode(int protocol)
{
return encode(SOCKET_PROTOCOL_MAPPING, protocol);
}
int dt_protocol_decode(Dnstap__SocketProtocol dnstap_protocol)
{
return decode(SOCKET_PROTOCOL_MAPPING, dnstap_protocol);
}
bool dt_message_type_is_query(Dnstap__Message__Type type)
{
switch (type) {
case DNSTAP__MESSAGE__TYPE__AUTH_QUERY:
case DNSTAP__MESSAGE__TYPE__CLIENT_QUERY:
case DNSTAP__MESSAGE__TYPE__FORWARDER_QUERY:
case DNSTAP__MESSAGE__TYPE__RESOLVER_QUERY:
case DNSTAP__MESSAGE__TYPE__STUB_QUERY:
case DNSTAP__MESSAGE__TYPE__TOOL_QUERY:
return true;
default:
return false;
}
}
bool dt_message_type_is_response(Dnstap__Message__Type type)
{
switch (type) {
case DNSTAP__MESSAGE__TYPE__AUTH_RESPONSE:
case DNSTAP__MESSAGE__TYPE__CLIENT_RESPONSE:
case DNSTAP__MESSAGE__TYPE__FORWARDER_RESPONSE:
case DNSTAP__MESSAGE__TYPE__RESOLVER_RESPONSE:
case DNSTAP__MESSAGE__TYPE__STUB_RESPONSE:
case DNSTAP__MESSAGE__TYPE__TOOL_RESPONSE:
return true;
default:
return false;
}
}
bool dt_message_role_is_initiator(Dnstap__Message__Type type)
{
switch (type) {
case DNSTAP__MESSAGE__TYPE__AUTH_QUERY:
case DNSTAP__MESSAGE__TYPE__AUTH_RESPONSE:
case DNSTAP__MESSAGE__TYPE__CLIENT_QUERY:
case DNSTAP__MESSAGE__TYPE__CLIENT_RESPONSE:
return false;
default:
return true;
}
}
|