| 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/decoder.h" |
| 25 |
#include "dnswire/trace.h" |
| 26 |
|
| 27 |
#include <assert.h> |
| 28 |
|
| 29 |
const char* const dnswire_decoder_state_string[] = { |
| 30 |
"reading_control", |
| 31 |
"checking_ready", |
| 32 |
"checking_accept", |
| 33 |
"reading_start", |
| 34 |
"checking_start", |
| 35 |
"reading_frames", |
| 36 |
"checking_finish", |
| 37 |
"done", |
| 38 |
}; |
| 39 |
|
| 40 |
#define __state(h, s) \ |
| 41 |
__trace("state %s => %s", dnswire_decoder_state_string[(h)->state], dnswire_decoder_state_string[s]); \ |
| 42 |
(h)->state = s; |
| 43 |
|
| 44 |
enum dnswire_result dnswire_decoder_decode(struct dnswire_decoder* handle, const uint8_t* data, size_t len) |
| 45 |
{ |
| 46 |
assert(handle); |
| 47 |
assert(data); |
| 48 |
assert(len); |
| 49 |
|
| 50 |
switch (handle->state) { |
| 51 |
case dnswire_decoder_reading_control: |
| 52 |
switch (tinyframe_read(&handle->reader, data, len)) { |
| 53 |
case tinyframe_have_control: |
| 54 |
switch (handle->reader.control.type) { |
| 55 |
case TINYFRAME_CONTROL_READY: |
| 56 |
// indicate that we have bidirectional communications |
| 57 |
__state(handle, dnswire_decoder_checking_ready); |
| 58 |
return dnswire_again; |
| 59 |
|
| 60 |
case TINYFRAME_CONTROL_ACCEPT: |
| 61 |
// indicate that we have bidirectional communications |
| 62 |
__state(handle, dnswire_decoder_checking_accept); |
| 63 |
return dnswire_again; |
| 64 |
|
| 65 |
case TINYFRAME_CONTROL_START: |
| 66 |
__state(handle, dnswire_decoder_checking_start); |
| 67 |
return dnswire_again; |
| 68 |
|
| 69 |
default: |
| 70 |
break; |
| 71 |
} |
| 72 |
break; |
| 73 |
|
| 74 |
case tinyframe_need_more: |
| 75 |
return dnswire_need_more; |
| 76 |
|
| 77 |
default: |
| 78 |
break; |
| 79 |
} |
| 80 |
return dnswire_error; |
| 81 |
|
| 82 |
case dnswire_decoder_checking_ready: |
| 83 |
switch (tinyframe_read(&handle->reader, data, len)) { |
| 84 |
case tinyframe_have_control_field: |
| 85 |
if (handle->reader.control_field.type != TINYFRAME_CONTROL_FIELD_CONTENT_TYPE) { |
| 86 |
return dnswire_error; |
| 87 |
} |
| 88 |
if (!strncmp(DNSTAP_PROTOBUF_CONTENT_TYPE, (const char*)handle->reader.control_field.data, handle->reader.control_field.length)) { |
| 89 |
handle->ready_support_dnstap_protobuf = 1; |
| 90 |
} |
| 91 |
if (!handle->reader.control_length_left) { |
| 92 |
__state(handle, dnswire_decoder_reading_start); |
| 93 |
return dnswire_bidirectional; |
| 94 |
} |
| 95 |
return dnswire_again; |
| 96 |
|
| 97 |
case tinyframe_need_more: |
| 98 |
return dnswire_need_more; |
| 99 |
|
| 100 |
default: |
| 101 |
return dnswire_error; |
| 102 |
} |
| 103 |
|
| 104 |
case dnswire_decoder_checking_accept: |
| 105 |
switch (tinyframe_read(&handle->reader, data, len)) { |
| 106 |
case tinyframe_have_control_field: |
| 107 |
if (handle->reader.control_field.type != TINYFRAME_CONTROL_FIELD_CONTENT_TYPE) { |
| 108 |
return dnswire_error; |
| 109 |
} |
| 110 |
if (!strncmp(DNSTAP_PROTOBUF_CONTENT_TYPE, (const char*)handle->reader.control_field.data, handle->reader.control_field.length)) { |
| 111 |
handle->accept_support_dnstap_protobuf = 1; |
| 112 |
} |
| 113 |
if (!handle->reader.control_length_left) { |
| 114 |
__state(handle, dnswire_decoder_checking_finish); |
| 115 |
return dnswire_bidirectional; |
| 116 |
} |
| 117 |
return dnswire_again; |
| 118 |
|
| 119 |
case tinyframe_need_more: |
| 120 |
return dnswire_need_more; |
| 121 |
|
| 122 |
default: |
| 123 |
return dnswire_error; |
| 124 |
} |
| 125 |
|
| 126 |
case dnswire_decoder_reading_start: |
| 127 |
switch (tinyframe_read(&handle->reader, data, len)) { |
| 128 |
case tinyframe_have_control: |
| 129 |
switch (handle->reader.control.type) { |
| 130 |
case TINYFRAME_CONTROL_START: |
| 131 |
__state(handle, dnswire_decoder_checking_start); |
| 132 |
return dnswire_again; |
| 133 |
|
| 134 |
default: |
| 135 |
break; |
| 136 |
} |
| 137 |
return dnswire_error; |
| 138 |
|
| 139 |
case tinyframe_need_more: |
| 140 |
return dnswire_need_more; |
| 141 |
|
| 142 |
default: |
| 143 |
return dnswire_error; |
| 144 |
} |
| 145 |
|
| 146 |
case dnswire_decoder_checking_start: |
| 147 |
switch (tinyframe_read(&handle->reader, data, len)) { |
| 148 |
case tinyframe_have_control_field: |
| 149 |
if (handle->reader.control_field.type != TINYFRAME_CONTROL_FIELD_CONTENT_TYPE) { |
| 150 |
return dnswire_error; |
| 151 |
} |
| 152 |
if (strncmp(DNSTAP_PROTOBUF_CONTENT_TYPE, (const char*)handle->reader.control_field.data, handle->reader.control_field.length)) { |
| 153 |
return dnswire_error; |
| 154 |
} |
| 155 |
__state(handle, dnswire_decoder_reading_frames); |
| 156 |
return dnswire_again; |
| 157 |
|
| 158 |
case tinyframe_need_more: |
| 159 |
return dnswire_need_more; |
| 160 |
|
| 161 |
default: |
| 162 |
return dnswire_error; |
| 163 |
} |
| 164 |
|
| 165 |
case dnswire_decoder_reading_frames: |
| 166 |
switch (tinyframe_read(&handle->reader, data, len)) { |
| 167 |
case tinyframe_have_frame: |
| 168 |
dnstap_cleanup(&handle->dnstap); |
| 169 |
if (dnstap_decode_protobuf(&handle->dnstap, handle->reader.frame.data, handle->reader.frame.length)) { |
| 170 |
return dnswire_error; |
| 171 |
} |
| 172 |
return dnswire_have_dnstap; |
| 173 |
|
| 174 |
case tinyframe_need_more: |
| 175 |
return dnswire_need_more; |
| 176 |
|
| 177 |
case tinyframe_stopped: |
| 178 |
__state(handle, dnswire_decoder_done); |
| 179 |
return dnswire_endofdata; |
| 180 |
|
| 181 |
default: |
| 182 |
return dnswire_error; |
| 183 |
} |
| 184 |
|
| 185 |
case dnswire_decoder_checking_finish: |
| 186 |
switch (tinyframe_read(&handle->reader, data, len)) { |
| 187 |
case tinyframe_finished: |
| 188 |
__state(handle, dnswire_decoder_done); |
| 189 |
return dnswire_endofdata; |
| 190 |
|
| 191 |
case tinyframe_need_more: |
| 192 |
return dnswire_need_more; |
| 193 |
|
| 194 |
default: |
| 195 |
return dnswire_error; |
| 196 |
} |
| 197 |
|
| 198 |
case dnswire_decoder_done: |
| 199 |
break; |
| 200 |
} |
| 201 |
|
| 202 |
return dnswire_error; |
| 203 |
} |