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
|
/*
* Author Jerry Lundström <jerry@dns-oarc.net>
* Copyright (c) 2019, 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/encoder.h"
#include "dnswire/trace.h"
#include <assert.h>
const char* const dnswire_encoder_state_string[] = {
"control_ready",
"control_start",
"control_accept",
"control_finish",
"frames",
"control_stop",
"done",
};
#define __state(h, s) \
__trace("state %s => %s", dnswire_encoder_state_string[(h)->state], dnswire_encoder_state_string[s]); \
(h)->state = s;
static struct tinyframe_control_field _content_type = {
TINYFRAME_CONTROL_FIELD_CONTENT_TYPE,
DNSTAP_PROTOBUF_CONTENT_TYPE_LENGTH,
(uint8_t*)DNSTAP_PROTOBUF_CONTENT_TYPE,
};
enum dnswire_result dnswire_encoder_encode(struct dnswire_encoder* handle, uint8_t* out, size_t len)
{
assert(handle);
assert(out);
assert(len);
switch (handle->state) {
case dnswire_encoder_control_ready:
switch (tinyframe_write_control(&handle->writer, out, len, TINYFRAME_CONTROL_READY, &_content_type, 1)) {
case tinyframe_ok:
__state(handle, dnswire_encoder_control_start);
return dnswire_again;
case tinyframe_need_more:
return dnswire_need_more;
default:
break;
}
return dnswire_error;
case dnswire_encoder_control_start:
switch (tinyframe_write_control_start(&handle->writer, out, len, DNSTAP_PROTOBUF_CONTENT_TYPE, DNSTAP_PROTOBUF_CONTENT_TYPE_LENGTH)) {
case tinyframe_ok:
__state(handle, dnswire_encoder_frames);
return dnswire_again;
case tinyframe_need_more:
return dnswire_need_more;
default:
break;
}
return dnswire_error;
case dnswire_encoder_control_accept:
switch (tinyframe_write_control(&handle->writer, out, len, TINYFRAME_CONTROL_ACCEPT, &_content_type, 1)) {
case tinyframe_ok:
__state(handle, dnswire_encoder_control_finish);
return dnswire_again;
case tinyframe_need_more:
return dnswire_need_more;
default:
break;
}
return dnswire_error;
case dnswire_encoder_control_finish:
switch (tinyframe_write_control(&handle->writer, out, len, TINYFRAME_CONTROL_FINISH, 0, 0)) {
case tinyframe_ok:
__state(handle, dnswire_encoder_done);
return dnswire_endofdata;
case tinyframe_need_more:
return dnswire_need_more;
default:
break;
}
return dnswire_error;
case dnswire_encoder_frames: {
if (!handle->dnstap) {
return dnswire_error;
}
size_t frame_len = dnstap_encode_protobuf_size(handle->dnstap);
if (len < tinyframe_frame_size(frame_len)) {
return dnswire_need_more;
}
uint8_t frame[frame_len];
dnstap_encode_protobuf(handle->dnstap, frame);
switch (tinyframe_write_frame(&handle->writer, out, len, frame, sizeof(frame))) {
case tinyframe_ok:
return dnswire_ok;
case tinyframe_need_more:
return dnswire_need_more;
default:
break;
}
return dnswire_error;
}
case dnswire_encoder_control_stop:
switch (tinyframe_write_control_stop(&handle->writer, out, len)) {
case tinyframe_ok:
__state(handle, dnswire_encoder_done);
return dnswire_endofdata;
case tinyframe_need_more:
return dnswire_need_more;
default:
break;
}
return dnswire_error;
case dnswire_encoder_done:
break;
}
return dnswire_error;
}
enum dnswire_result dnswire_encoder_stop(struct dnswire_encoder* handle)
{
assert(handle);
switch (handle->state) {
case dnswire_encoder_frames:
__state(handle, dnswire_encoder_control_stop);
return dnswire_ok;
default:
break;
}
return dnswire_error;
}
|