summaryrefslogtreecommitdiffstats
path: root/src/dnstap.c
blob: 83d5c0f56df3a4afa73b611cbc62412f18d787a5 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * Author Jerry Lundström <jerry@dns-oarc.net>
 * Copyright (c) 2019-2023, OARC, Inc.
 * All rights reserved.
 *
 * This file is part of the dnswire library.
 *
 * dnswire library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * dnswire library 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with dnswire library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "dnswire/dnstap.h"

const char* const DNSTAP_TYPE_STRING[] = {
    "UNKNOWN",
    "MESSAGE",
};
const char* const DNSTAP_MESSAGE_TYPE_STRING[] = {
    "UNKNOWN",
    "AUTH_QUERY",
    "AUTH_RESPONSE",
    "RESOLVER_QUERY",
    "RESOLVER_RESPONSE",
    "CLIENT_QUERY",
    "CLIENT_RESPONSE",
    "FORWARDER_QUERY",
    "FORWARDER_RESPONSE",
    "STUB_QUERY",
    "STUB_RESPONSE",
    "TOOL_QUERY",
    "TOOL_RESPONSE",
    "UPDATE_QUERY",
    "UPDATE_RESPONSE",
};
const char* const DNSTAP_SOCKET_FAMILY_STRING[] = {
    "UNKNOWN",
    "INET",
    "INET6",
};
const char* const DNSTAP_SOCKET_PROTOCOL_STRING[] = {
    "UNKNOWN",
    "UDP",
    "TCP",
    "DOT",
    "DOH",
    "DNSCryptUDP",
    "DNSCryptTCP",
    "DOQ",
};
const char* const DNSTAP_POLICY_ACTION_STRING[] = {
    "UNKNOWN",
    "NXDOMAIN",
    "NODATA",
    "PASS",
    "DROP",
    "TRUNCATE",
    "LOCAL_DATA",
};
const char* const DNSTAP_POLICY_MATCH_STRING[] = {
    "UNKNOWN",
    "QNAME",
    "CLIENT_IP",
    "RESPONSE_IP",
    "NS_NAME",
    "NS_IP",
};

void dnstap_message_clear_policy(struct dnstap* dnstap)
{
    static const Dnstap__Policy policy = DNSTAP__POLICY__INIT;
    assert(dnstap);

    dnstap->message.policy = 0;
    dnstap->policy         = policy;
}

int dnstap_decode_protobuf(struct dnstap* dnstap, const uint8_t* data, size_t len)
{
    assert(dnstap);
    assert(data);
    assert(!dnstap->unpacked_dnstap);

    if (!(dnstap->unpacked_dnstap = dnstap__dnstap__unpack(NULL, len, data))) {
        return 1;
    }

    dnstap->dnstap = *dnstap->unpacked_dnstap;

    switch (dnstap->dnstap.type) {
    case DNSTAP_TYPE_MESSAGE:
        break;
    default:
        dnstap->dnstap.type = (enum _Dnstap__Dnstap__Type)DNSTAP_TYPE_UNKNOWN;
    }

    if (dnstap->dnstap.message) {
        dnstap->message = *dnstap->dnstap.message;

        switch (dnstap->message.type) {
        case DNSTAP_MESSAGE_TYPE_AUTH_QUERY:
        case DNSTAP_MESSAGE_TYPE_AUTH_RESPONSE:
        case DNSTAP_MESSAGE_TYPE_RESOLVER_QUERY:
        case DNSTAP_MESSAGE_TYPE_RESOLVER_RESPONSE:
        case DNSTAP_MESSAGE_TYPE_CLIENT_QUERY:
        case DNSTAP_MESSAGE_TYPE_CLIENT_RESPONSE:
        case DNSTAP_MESSAGE_TYPE_FORWARDER_QUERY:
        case DNSTAP_MESSAGE_TYPE_FORWARDER_RESPONSE:
        case DNSTAP_MESSAGE_TYPE_STUB_QUERY:
        case DNSTAP_MESSAGE_TYPE_STUB_RESPONSE:
        case DNSTAP_MESSAGE_TYPE_TOOL_QUERY:
        case DNSTAP_MESSAGE_TYPE_TOOL_RESPONSE:
        case DNSTAP_MESSAGE_TYPE_UPDATE_QUERY:
        case DNSTAP_MESSAGE_TYPE_UPDATE_RESPONSE:
            break;
        default:
            dnstap->message.type = (enum _Dnstap__Message__Type)DNSTAP_MESSAGE_TYPE_UNKNOWN;
        }

        switch (dnstap->message.socket_family) {
        case DNSTAP_SOCKET_FAMILY_INET:
        case DNSTAP_SOCKET_FAMILY_INET6:
            break;
        default:
            dnstap->message.has_socket_family = false;
            dnstap->message.socket_family     = (enum _Dnstap__SocketFamily)DNSTAP_SOCKET_FAMILY_UNKNOWN;
        }

        switch (dnstap->message.socket_protocol) {
        case DNSTAP_SOCKET_PROTOCOL_UDP:
        case DNSTAP_SOCKET_PROTOCOL_TCP:
        case DNSTAP_SOCKET_PROTOCOL_DOT:
        case DNSTAP_SOCKET_PROTOCOL_DOH:
        case DNSTAP_SOCKET_PROTOCOL_DNSCryptUDP:
        case DNSTAP_SOCKET_PROTOCOL_DNSCryptTCP:
        case DNSTAP_SOCKET_PROTOCOL_DOQ:
            break;
        default:
            dnstap->message.has_socket_protocol = false;
            dnstap->message.socket_protocol     = (enum _Dnstap__SocketProtocol)DNSTAP_SOCKET_PROTOCOL_UNKNOWN;
        }

        if (dnstap->message.policy) {
            dnstap->policy = *dnstap->message.policy;

            switch (dnstap->policy.action) {
            case DNSTAP_POLICY_ACTION_NXDOMAIN:
            case DNSTAP_POLICY_ACTION_NODATA:
            case DNSTAP_POLICY_ACTION_PASS:
            case DNSTAP_POLICY_ACTION_DROP:
            case DNSTAP_POLICY_ACTION_TRUNCATE:
            case DNSTAP_POLICY_ACTION_LOCAL_DATA:
                break;
            default:
                dnstap->policy.has_action = false;
                dnstap->policy.action     = (enum _Dnstap__Policy__Action)DNSTAP_POLICY_ACTION_UNKNOWN;
            }

            switch (dnstap->policy.match) {
            case DNSTAP_POLICY_MATCH_QNAME:
            case DNSTAP_POLICY_MATCH_CLIENT_IP:
            case DNSTAP_POLICY_MATCH_RESPONSE_IP:
            case DNSTAP_POLICY_MATCH_NS_NAME:
            case DNSTAP_POLICY_MATCH_NS_IP:
                break;
            default:
                dnstap->policy.has_match = false;
                dnstap->policy.match     = (enum _Dnstap__Policy__Match)DNSTAP_POLICY_MATCH_UNKNOWN;
            }
        }
    }

    return 0;
}

void dnstap_cleanup(struct dnstap* dnstap)
{
    assert(dnstap);

    if (dnstap->unpacked_dnstap) {
        dnstap__dnstap__free_unpacked(dnstap->unpacked_dnstap, NULL);
        dnstap->unpacked_dnstap = 0;
    }
}

size_t dnstap_encode_protobuf_size(const struct dnstap* dnstap)
{
    assert(dnstap);

    return dnstap__dnstap__get_packed_size(&dnstap->dnstap);
}

size_t dnstap_encode_protobuf(const struct dnstap* dnstap, uint8_t* data)
{
    assert(dnstap);
    assert(data);

    return dnstap__dnstap__pack(&dnstap->dnstap, data);
}