| 1 |
/* |
| 2 |
* Author Jerry Lundström <[email protected]> |
| 3 |
* Copyright (c) 2019-2026 OARC, Inc. |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* This file is part of the dnswire library. |
| 7 |
* |
| 8 |
* dnswire library is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* (at your option) any later version. |
| 12 |
* |
| 13 |
* dnswire library is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with dnswire library. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
|
| 24 |
#include "dnswire/dnstap.h" |
| 25 |
|
| 26 |
const char* const DNSTAP_TYPE_STRING[] = { |
| 27 |
"UNKNOWN", |
| 28 |
"MESSAGE", |
| 29 |
}; |
| 30 |
const char* const DNSTAP_MESSAGE_TYPE_STRING[] = { |
| 31 |
"UNKNOWN", |
| 32 |
"AUTH_QUERY", |
| 33 |
"AUTH_RESPONSE", |
| 34 |
"RESOLVER_QUERY", |
| 35 |
"RESOLVER_RESPONSE", |
| 36 |
"CLIENT_QUERY", |
| 37 |
"CLIENT_RESPONSE", |
| 38 |
"FORWARDER_QUERY", |
| 39 |
"FORWARDER_RESPONSE", |
| 40 |
"STUB_QUERY", |
| 41 |
"STUB_RESPONSE", |
| 42 |
"TOOL_QUERY", |
| 43 |
"TOOL_RESPONSE", |
| 44 |
"UPDATE_QUERY", |
| 45 |
"UPDATE_RESPONSE", |
| 46 |
}; |
| 47 |
const char* const DNSTAP_SOCKET_FAMILY_STRING[] = { |
| 48 |
"UNKNOWN", |
| 49 |
"INET", |
| 50 |
"INET6", |
| 51 |
}; |
| 52 |
const char* const DNSTAP_SOCKET_PROTOCOL_STRING[] = { |
| 53 |
"UNKNOWN", |
| 54 |
"UDP", |
| 55 |
"TCP", |
| 56 |
"DOT", |
| 57 |
"DOH", |
| 58 |
"DNSCryptUDP", |
| 59 |
"DNSCryptTCP", |
| 60 |
"DOQ", |
| 61 |
}; |
| 62 |
const char* const DNSTAP_HTTP_PROTOCOL_STRING[] = { |
| 63 |
"UNKNOWN", |
| 64 |
"HTTP1", |
| 65 |
"HTTP2", |
| 66 |
"HTTP3", |
| 67 |
}; |
| 68 |
const char* const DNSTAP_POLICY_ACTION_STRING[] = { |
| 69 |
"UNKNOWN", |
| 70 |
"NXDOMAIN", |
| 71 |
"NODATA", |
| 72 |
"PASS", |
| 73 |
"DROP", |
| 74 |
"TRUNCATE", |
| 75 |
"LOCAL_DATA", |
| 76 |
}; |
| 77 |
const char* const DNSTAP_POLICY_MATCH_STRING[] = { |
| 78 |
"UNKNOWN", |
| 79 |
"QNAME", |
| 80 |
"CLIENT_IP", |
| 81 |
"RESPONSE_IP", |
| 82 |
"NS_NAME", |
| 83 |
"NS_IP", |
| 84 |
}; |
| 85 |
|
| 86 |
void dnstap_message_clear_policy(struct dnstap* dnstap) |
| 87 |
{ |
| 88 |
static const Dnstap__Policy policy = DNSTAP__POLICY__INIT; |
| 89 |
assert(dnstap); |
| 90 |
|
| 91 |
dnstap->message.policy = 0; |
| 92 |
dnstap->policy = policy; |
| 93 |
} |
| 94 |
|
| 95 |
int dnstap_decode_protobuf(struct dnstap* dnstap, const uint8_t* data, size_t len) |
| 96 |
{ |
| 97 |
assert(dnstap); |
| 98 |
assert(data); |
| 99 |
assert(!dnstap->unpacked_dnstap); |
| 100 |
|
| 101 |
if (!(dnstap->unpacked_dnstap = dnstap__dnstap__unpack(NULL, len, data))) { |
| 102 |
return 1; |
| 103 |
} |
| 104 |
|
| 105 |
dnstap->dnstap = *dnstap->unpacked_dnstap; |
| 106 |
|
| 107 |
switch (dnstap->dnstap.type) { |
| 108 |
case DNSTAP_TYPE_MESSAGE: |
| 109 |
break; |
| 110 |
default: |
| 111 |
dnstap->dnstap.type = (enum _Dnstap__Dnstap__Type)DNSTAP_TYPE_UNKNOWN; |
| 112 |
} |
| 113 |
|
| 114 |
if (dnstap->dnstap.message) { |
| 115 |
dnstap->message = *dnstap->dnstap.message; |
| 116 |
|
| 117 |
switch (dnstap->message.type) { |
| 118 |
case DNSTAP_MESSAGE_TYPE_AUTH_QUERY: |
| 119 |
case DNSTAP_MESSAGE_TYPE_AUTH_RESPONSE: |
| 120 |
case DNSTAP_MESSAGE_TYPE_RESOLVER_QUERY: |
| 121 |
case DNSTAP_MESSAGE_TYPE_RESOLVER_RESPONSE: |
| 122 |
case DNSTAP_MESSAGE_TYPE_CLIENT_QUERY: |
| 123 |
case DNSTAP_MESSAGE_TYPE_CLIENT_RESPONSE: |
| 124 |
case DNSTAP_MESSAGE_TYPE_FORWARDER_QUERY: |
| 125 |
case DNSTAP_MESSAGE_TYPE_FORWARDER_RESPONSE: |
| 126 |
case DNSTAP_MESSAGE_TYPE_STUB_QUERY: |
| 127 |
case DNSTAP_MESSAGE_TYPE_STUB_RESPONSE: |
| 128 |
case DNSTAP_MESSAGE_TYPE_TOOL_QUERY: |
| 129 |
case DNSTAP_MESSAGE_TYPE_TOOL_RESPONSE: |
| 130 |
case DNSTAP_MESSAGE_TYPE_UPDATE_QUERY: |
| 131 |
case DNSTAP_MESSAGE_TYPE_UPDATE_RESPONSE: |
| 132 |
break; |
| 133 |
default: |
| 134 |
dnstap->message.type = (enum _Dnstap__Message__Type)DNSTAP_MESSAGE_TYPE_UNKNOWN; |
| 135 |
} |
| 136 |
|
| 137 |
switch (dnstap->message.socket_family) { |
| 138 |
case DNSTAP_SOCKET_FAMILY_INET: |
| 139 |
case DNSTAP_SOCKET_FAMILY_INET6: |
| 140 |
break; |
| 141 |
default: |
| 142 |
dnstap->message.has_socket_family = false; |
| 143 |
dnstap->message.socket_family = (enum _Dnstap__SocketFamily)DNSTAP_SOCKET_FAMILY_UNKNOWN; |
| 144 |
} |
| 145 |
|
| 146 |
switch (dnstap->message.socket_protocol) { |
| 147 |
case DNSTAP_SOCKET_PROTOCOL_UDP: |
| 148 |
case DNSTAP_SOCKET_PROTOCOL_TCP: |
| 149 |
case DNSTAP_SOCKET_PROTOCOL_DOT: |
| 150 |
case DNSTAP_SOCKET_PROTOCOL_DOH: |
| 151 |
case DNSTAP_SOCKET_PROTOCOL_DNSCryptUDP: |
| 152 |
case DNSTAP_SOCKET_PROTOCOL_DNSCryptTCP: |
| 153 |
case DNSTAP_SOCKET_PROTOCOL_DOQ: |
| 154 |
break; |
| 155 |
default: |
| 156 |
dnstap->message.has_socket_protocol = false; |
| 157 |
dnstap->message.socket_protocol = (enum _Dnstap__SocketProtocol)DNSTAP_SOCKET_PROTOCOL_UNKNOWN; |
| 158 |
} |
| 159 |
|
| 160 |
switch (dnstap->message.http_protocol) { |
| 161 |
case DNSTAP_HTTP_PROTOCOL_HTTP1: |
| 162 |
case DNSTAP_HTTP_PROTOCOL_HTTP2: |
| 163 |
case DNSTAP_HTTP_PROTOCOL_HTTP3: |
| 164 |
break; |
| 165 |
default: |
| 166 |
dnstap->message.has_http_protocol = false; |
| 167 |
dnstap->message.http_protocol = (enum _Dnstap__HttpProtocol)DNSTAP_HTTP_PROTOCOL_UNKNOWN; |
| 168 |
} |
| 169 |
|
| 170 |
if (dnstap->message.policy) { |
| 171 |
dnstap->policy = *dnstap->message.policy; |
| 172 |
|
| 173 |
switch (dnstap->policy.action) { |
| 174 |
case DNSTAP_POLICY_ACTION_NXDOMAIN: |
| 175 |
case DNSTAP_POLICY_ACTION_NODATA: |
| 176 |
case DNSTAP_POLICY_ACTION_PASS: |
| 177 |
case DNSTAP_POLICY_ACTION_DROP: |
| 178 |
case DNSTAP_POLICY_ACTION_TRUNCATE: |
| 179 |
case DNSTAP_POLICY_ACTION_LOCAL_DATA: |
| 180 |
break; |
| 181 |
default: |
| 182 |
dnstap->policy.has_action = false; |
| 183 |
dnstap->policy.action = (enum _Dnstap__Policy__Action)DNSTAP_POLICY_ACTION_UNKNOWN; |
| 184 |
} |
| 185 |
|
| 186 |
switch (dnstap->policy.match) { |
| 187 |
case DNSTAP_POLICY_MATCH_QNAME: |
| 188 |
case DNSTAP_POLICY_MATCH_CLIENT_IP: |
| 189 |
case DNSTAP_POLICY_MATCH_RESPONSE_IP: |
| 190 |
case DNSTAP_POLICY_MATCH_NS_NAME: |
| 191 |
case DNSTAP_POLICY_MATCH_NS_IP: |
| 192 |
break; |
| 193 |
default: |
| 194 |
dnstap->policy.has_match = false; |
| 195 |
dnstap->policy.match = (enum _Dnstap__Policy__Match)DNSTAP_POLICY_MATCH_UNKNOWN; |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
void dnstap_cleanup(struct dnstap* dnstap) |
| 204 |
{ |
| 205 |
assert(dnstap); |
| 206 |
|
| 207 |
if (dnstap->unpacked_dnstap) { |
| 208 |
dnstap__dnstap__free_unpacked(dnstap->unpacked_dnstap, NULL); |
| 209 |
dnstap->unpacked_dnstap = 0; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
size_t dnstap_encode_protobuf_size(const struct dnstap* dnstap) |
| 214 |
{ |
| 215 |
assert(dnstap); |
| 216 |
|
| 217 |
return dnstap__dnstap__get_packed_size(&dnstap->dnstap); |
| 218 |
} |
| 219 |
|
| 220 |
size_t dnstap_encode_protobuf(const struct dnstap* dnstap, uint8_t* data) |
| 221 |
{ |
| 222 |
assert(dnstap); |
| 223 |
assert(data); |
| 224 |
|
| 225 |
return dnstap__dnstap__pack(&dnstap->dnstap, data); |
| 226 |
} |