| 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/writer.h" |
| 25 |
#include "dnswire/trace.h" |
| 26 |
#include "dnswire/dnswire.h" |
| 27 |
|
| 28 |
#include <assert.h> |
| 29 |
#include <stdlib.h> |
| 30 |
|
| 31 |
const char* const dnswire_writer_state_string[] = { |
| 32 |
"encoding_ready", |
| 33 |
"writing_ready", |
| 34 |
"reading_accept", |
| 35 |
"decoding_accept", |
| 36 |
"encoding", |
| 37 |
"writing", |
| 38 |
"stopping", |
| 39 |
"encoding_stop", |
| 40 |
"writing_stop", |
| 41 |
"reading_finish", |
| 42 |
"decoding_finish", |
| 43 |
"done", |
| 44 |
}; |
| 45 |
|
| 46 |
#define __state(h, s) \ |
| 47 |
__trace("state %s => %s", dnswire_writer_state_string[(h)->state], dnswire_writer_state_string[s]); \ |
| 48 |
(h)->state = s; |
| 49 |
|
| 50 |
static struct dnswire_writer _defaults = { |
| 51 |
.state = dnswire_writer_encoding, |
| 52 |
|
| 53 |
.encoder = DNSWIRE_ENCODER_INITIALIZER, |
| 54 |
.buf = 0, |
| 55 |
.size = DNSWIRE_DEFAULT_BUF_SIZE, |
| 56 |
.inc = DNSWIRE_DEFAULT_BUF_SIZE, |
| 57 |
.max = DNSWIRE_MAXIMUM_BUF_SIZE, |
| 58 |
.at = 0, |
| 59 |
.left = 0, |
| 60 |
.popped = 0, |
| 61 |
|
| 62 |
.decoder = DNSWIRE_DECODER_INITIALIZER, |
| 63 |
.read_buf = 0, |
| 64 |
.read_size = DNSWIRE_DEFAULT_BUF_SIZE, |
| 65 |
.read_inc = DNSWIRE_DEFAULT_BUF_SIZE, |
| 66 |
.read_max = DNSWIRE_MAXIMUM_BUF_SIZE, |
| 67 |
.read_at = 0, |
| 68 |
.read_left = 0, |
| 69 |
.read_pushed = 0, |
| 70 |
|
| 71 |
.bidirectional = false, |
| 72 |
}; |
| 73 |
|
| 74 |
enum dnswire_result dnswire_writer_init(struct dnswire_writer* handle) |
| 75 |
{ |
| 76 |
assert(handle); |
| 77 |
|
| 78 |
*handle = _defaults; |
| 79 |
|
| 80 |
if (!(handle->buf = malloc(handle->size))) { |
| 81 |
return dnswire_error; |
| 82 |
} |
| 83 |
|
| 84 |
return dnswire_ok; |
| 85 |
} |
| 86 |
|
| 87 |
enum dnswire_result dnswire_writer_set_bidirectional(struct dnswire_writer* handle, bool bidirectional) |
| 88 |
{ |
| 89 |
assert(handle); |
| 90 |
|
| 91 |
if (bidirectional) { |
| 92 |
if (!handle->read_buf) { |
| 93 |
if (!(handle->read_buf = malloc(handle->read_size))) { |
| 94 |
return dnswire_error; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
handle->encoder.state = dnswire_encoder_control_ready; |
| 99 |
__state(handle, dnswire_writer_encoding_ready); |
| 100 |
} else { |
| 101 |
handle->encoder.state = dnswire_encoder_control_start; |
| 102 |
__state(handle, dnswire_writer_encoding); |
| 103 |
} |
| 104 |
|
| 105 |
handle->bidirectional = bidirectional; |
| 106 |
|
| 107 |
return dnswire_ok; |
| 108 |
} |
| 109 |
|
| 110 |
enum dnswire_result dnswire_writer_set_bufsize(struct dnswire_writer* handle, size_t size) |
| 111 |
{ |
| 112 |
assert(handle); |
| 113 |
assert(size); |
| 114 |
assert(handle->buf); |
| 115 |
|
| 116 |
if (handle->left > size) { |
| 117 |
// we got data and it doesn't fit in the new size |
| 118 |
return dnswire_error; |
| 119 |
} |
| 120 |
if (size > handle->max) { |
| 121 |
// don't expand over max |
| 122 |
return dnswire_error; |
| 123 |
} |
| 124 |
|
| 125 |
if (handle->at + handle->left > size) { |
| 126 |
// move what's left to the start |
| 127 |
if (handle->left) { |
| 128 |
memmove(handle->buf, &handle->buf[handle->at], handle->left); |
| 129 |
} |
| 130 |
handle->at = 0; |
| 131 |
} |
| 132 |
|
| 133 |
uint8_t* buf = realloc(handle->buf, size); |
| 134 |
if (!buf) { |
| 135 |
return dnswire_error; |
| 136 |
} |
| 137 |
|
| 138 |
handle->buf = buf; |
| 139 |
handle->size = size; |
| 140 |
|
| 141 |
return dnswire_ok; |
| 142 |
} |
| 143 |
|
| 144 |
enum dnswire_result dnswire_writer_set_bufinc(struct dnswire_writer* handle, size_t inc) |
| 145 |
{ |
| 146 |
assert(handle); |
| 147 |
assert(inc); |
| 148 |
|
| 149 |
handle->inc = inc; |
| 150 |
|
| 151 |
return dnswire_ok; |
| 152 |
} |
| 153 |
|
| 154 |
enum dnswire_result dnswire_writer_set_bufmax(struct dnswire_writer* handle, size_t max) |
| 155 |
{ |
| 156 |
assert(handle); |
| 157 |
assert(max); |
| 158 |
|
| 159 |
if (max < handle->size) { |
| 160 |
return dnswire_error; |
| 161 |
} |
| 162 |
|
| 163 |
handle->max = max; |
| 164 |
|
| 165 |
return dnswire_ok; |
| 166 |
} |
| 167 |
|
| 168 |
static enum dnswire_result _encoding(struct dnswire_writer* handle) |
| 169 |
{ |
| 170 |
enum dnswire_result res; |
| 171 |
|
| 172 |
while (1) { |
| 173 |
res = dnswire_encoder_encode(&handle->encoder, &handle->buf[handle->at], handle->size - handle->at); |
| 174 |
__trace("encode %s", dnswire_result_string[res]); |
| 175 |
|
| 176 |
switch (res) { |
| 177 |
case dnswire_ok: |
| 178 |
case dnswire_again: |
| 179 |
case dnswire_endofdata: |
| 180 |
handle->at += dnswire_encoder_encoded(handle->encoder); |
| 181 |
handle->left += dnswire_encoder_encoded(handle->encoder); |
| 182 |
break; |
| 183 |
|
| 184 |
case dnswire_need_more: { |
| 185 |
if (handle->size >= handle->max) { |
| 186 |
// already at max size and it's not enough |
| 187 |
return dnswire_error; |
| 188 |
} |
| 189 |
|
| 190 |
// no space left, expand |
| 191 |
size_t size = handle->size + handle->inc > handle->max ? handle->max : handle->size + handle->inc; |
| 192 |
uint8_t* buf = realloc(handle->buf, size); |
| 193 |
if (!buf) { |
| 194 |
return dnswire_error; |
| 195 |
} |
| 196 |
handle->buf = buf; |
| 197 |
handle->size = size; |
| 198 |
continue; |
| 199 |
} |
| 200 |
default: |
| 201 |
break; |
| 202 |
} |
| 203 |
break; |
| 204 |
} |
| 205 |
return res; |
| 206 |
} |
| 207 |
|
| 208 |
enum dnswire_result dnswire_writer_pop(struct dnswire_writer* handle, uint8_t* data, size_t len, uint8_t* in_data, size_t* in_len) |
| 209 |
{ |
| 210 |
assert(handle); |
| 211 |
assert(data); |
| 212 |
assert(len); |
| 213 |
assert(handle->buf); |
| 214 |
assert(!handle->bidirectional || in_data); |
| 215 |
|
| 216 |
handle->popped = 0; |
| 217 |
size_t in_len_orig = 0; |
| 218 |
if (in_len) { |
| 219 |
in_len_orig = *in_len; |
| 220 |
*in_len = 0; |
| 221 |
} |
| 222 |
|
| 223 |
enum dnswire_result res = dnswire_again; |
| 224 |
|
| 225 |
__trace("state %s", dnswire_writer_state_string[handle->state]); |
| 226 |
|
| 227 |
switch (handle->state) { |
| 228 |
case dnswire_writer_encoding_ready: |
| 229 |
res = _encoding(handle); |
| 230 |
__trace("left %zu", handle->left); |
| 231 |
if (res != dnswire_error && handle->left) { |
| 232 |
__state(handle, dnswire_writer_writing); |
| 233 |
// fallthrough |
| 234 |
} else { |
| 235 |
break; |
| 236 |
} |
| 237 |
|
| 238 |
case dnswire_writer_writing_ready: |
| 239 |
handle->popped = len < handle->left ? len : handle->left; |
| 240 |
memcpy(data, &handle->buf[handle->at - handle->left], handle->popped); |
| 241 |
__trace("wrote %zd", handle->popped); |
| 242 |
handle->left -= handle->popped; |
| 243 |
__trace("left %zu", handle->left); |
| 244 |
if (handle->left) { |
| 245 |
break; |
| 246 |
} |
| 247 |
handle->at = 0; |
| 248 |
__state(handle, dnswire_writer_reading_accept); |
| 249 |
|
| 250 |
case dnswire_writer_reading_accept: |
| 251 |
if (!in_len_orig) { |
| 252 |
return dnswire_need_more; |
| 253 |
} |
| 254 |
*in_len = handle->read_size - handle->read_at - handle->read_left < in_len_orig ? handle->read_size - handle->read_at - handle->read_left : in_len_orig; |
| 255 |
if (*in_len) { |
| 256 |
memcpy(&handle->read_buf[handle->read_at + handle->read_left], in_data, *in_len); |
| 257 |
__trace("%s", __printable_string(&handle->read_buf[handle->read_at + handle->read_left], *in_len)); |
| 258 |
handle->left += *in_len; |
| 259 |
} |
| 260 |
__state(handle, dnswire_writer_decoding_accept); |
| 261 |
// fallthrough |
| 262 |
|
| 263 |
case dnswire_writer_decoding_accept: |
| 264 |
switch (dnswire_decoder_decode(&handle->decoder, &handle->read_buf[handle->read_at], handle->read_left)) { |
| 265 |
case dnswire_bidirectional: |
| 266 |
handle->read_at += dnswire_decoder_decoded(handle->decoder); |
| 267 |
handle->read_left -= dnswire_decoder_decoded(handle->decoder); |
| 268 |
if (!handle->read_left) { |
| 269 |
handle->read_at = 0; |
| 270 |
} |
| 271 |
|
| 272 |
if (!handle->decoder.accept_support_dnstap_protobuf) { |
| 273 |
return dnswire_error; |
| 274 |
} |
| 275 |
|
| 276 |
__state(handle, dnswire_writer_encoding); |
| 277 |
return dnswire_again; |
| 278 |
|
| 279 |
case dnswire_again: |
| 280 |
handle->read_at += dnswire_decoder_decoded(handle->decoder); |
| 281 |
handle->read_left -= dnswire_decoder_decoded(handle->decoder); |
| 282 |
if (!handle->read_left) { |
| 283 |
handle->read_at = 0; |
| 284 |
__state(handle, dnswire_writer_reading_accept); |
| 285 |
} |
| 286 |
return dnswire_again; |
| 287 |
|
| 288 |
case dnswire_need_more: |
| 289 |
if (handle->read_left < handle->read_size) { |
| 290 |
// still space left to fill |
| 291 |
if (handle->read_at) { |
| 292 |
// move what's left to the start |
| 293 |
if (handle->read_left) { |
| 294 |
memmove(handle->read_buf, &handle->read_buf[handle->read_at], handle->read_left); |
| 295 |
} |
| 296 |
handle->read_at = 0; |
| 297 |
} |
| 298 |
} else if (handle->read_size < handle->read_max) { |
| 299 |
// no space left, expand |
| 300 |
size_t size = handle->read_size + handle->read_inc > handle->read_max ? handle->read_max : handle->read_size + handle->read_inc; |
| 301 |
uint8_t* buf = realloc(handle->read_buf, size); |
| 302 |
if (!buf) { |
| 303 |
return dnswire_error; |
| 304 |
} |
| 305 |
handle->read_buf = buf; |
| 306 |
handle->read_size = size; |
| 307 |
} else { |
| 308 |
// already at max size, and full |
| 309 |
return dnswire_error; |
| 310 |
} |
| 311 |
__state(handle, dnswire_writer_reading_accept); |
| 312 |
return dnswire_need_more; |
| 313 |
|
| 314 |
default: |
| 315 |
break; |
| 316 |
} |
| 317 |
return dnswire_error; |
| 318 |
|
| 319 |
case dnswire_writer_encoding: |
| 320 |
res = _encoding(handle); |
| 321 |
__trace("left %zu", handle->left); |
| 322 |
if (res != dnswire_error && handle->left) { |
| 323 |
__state(handle, dnswire_writer_writing); |
| 324 |
// fallthrough |
| 325 |
} else { |
| 326 |
break; |
| 327 |
} |
| 328 |
|
| 329 |
case dnswire_writer_writing: |
| 330 |
handle->popped = len < handle->left ? len : handle->left; |
| 331 |
memcpy(data, &handle->buf[handle->at - handle->left], handle->popped); |
| 332 |
__trace("wrote %zd", handle->popped); |
| 333 |
handle->left -= handle->popped; |
| 334 |
__trace("left %zu", handle->left); |
| 335 |
if (!handle->left) { |
| 336 |
handle->at = 0; |
| 337 |
__state(handle, dnswire_writer_encoding); |
| 338 |
} |
| 339 |
break; |
| 340 |
|
| 341 |
case dnswire_writer_stopping: |
| 342 |
if (handle->left) { |
| 343 |
handle->popped = len < handle->left ? len : handle->left; |
| 344 |
memcpy(data, &handle->buf[handle->at - handle->left], handle->popped); |
| 345 |
__trace("wrote %zd", handle->popped); |
| 346 |
handle->left -= handle->popped; |
| 347 |
if (handle->left) { |
| 348 |
__trace("left %zu", handle->left); |
| 349 |
return dnswire_again; |
| 350 |
} |
| 351 |
handle->at = 0; |
| 352 |
} |
| 353 |
__state(handle, dnswire_writer_encoding_stop); |
| 354 |
// fallthrough |
| 355 |
|
| 356 |
case dnswire_writer_encoding_stop: |
| 357 |
res = _encoding(handle); |
| 358 |
if (res == dnswire_endofdata) { |
| 359 |
__state(handle, dnswire_writer_writing_stop); |
| 360 |
// fallthrough |
| 361 |
} else { |
| 362 |
break; |
| 363 |
} |
| 364 |
|
| 365 |
case dnswire_writer_writing_stop: |
| 366 |
if (handle->left) { |
| 367 |
handle->popped = len < handle->left ? len : handle->left; |
| 368 |
memcpy(data, &handle->buf[handle->at - handle->left], handle->popped); |
| 369 |
__trace("wrote %zd", handle->popped); |
| 370 |
handle->left -= handle->popped; |
| 371 |
if (handle->left) { |
| 372 |
__trace("left %zu", handle->left); |
| 373 |
return dnswire_again; |
| 374 |
} |
| 375 |
handle->at = 0; |
| 376 |
} |
| 377 |
if (handle->bidirectional) { |
| 378 |
__state(handle, dnswire_writer_reading_finish); |
| 379 |
return dnswire_again; |
| 380 |
} |
| 381 |
__state(handle, dnswire_writer_done); |
| 382 |
return dnswire_endofdata; |
| 383 |
|
| 384 |
case dnswire_writer_reading_finish: |
| 385 |
if (!in_len_orig) { |
| 386 |
return dnswire_need_more; |
| 387 |
} |
| 388 |
*in_len = handle->read_size - handle->read_at - handle->read_left < in_len_orig ? handle->read_size - handle->read_at - handle->read_left : in_len_orig; |
| 389 |
if (*in_len) { |
| 390 |
memcpy(&handle->read_buf[handle->read_at + handle->read_left], in_data, *in_len); |
| 391 |
__trace("%s", __printable_string(&handle->read_buf[handle->read_at + handle->read_left], *in_len)); |
| 392 |
handle->left += *in_len; |
| 393 |
} |
| 394 |
__state(handle, dnswire_writer_decoding_finish); |
| 395 |
// fallthrough |
| 396 |
|
| 397 |
case dnswire_writer_decoding_finish: |
| 398 |
switch (dnswire_decoder_decode(&handle->decoder, &handle->read_buf[handle->read_at], handle->read_left)) { |
| 399 |
case dnswire_endofdata: |
| 400 |
__state(handle, dnswire_writer_done); |
| 401 |
return dnswire_endofdata; |
| 402 |
|
| 403 |
case dnswire_need_more: |
| 404 |
if (handle->read_left < handle->read_size) { |
| 405 |
// still space left to fill |
| 406 |
if (handle->read_at) { |
| 407 |
// move what's left to the start |
| 408 |
if (handle->read_left) { |
| 409 |
memmove(handle->read_buf, &handle->read_buf[handle->read_at], handle->read_left); |
| 410 |
} |
| 411 |
handle->read_at = 0; |
| 412 |
} |
| 413 |
} else if (handle->read_size < handle->read_max) { |
| 414 |
// no space left, expand |
| 415 |
size_t size = handle->read_size + handle->read_inc > handle->read_max ? handle->read_max : handle->read_size + handle->read_inc; |
| 416 |
uint8_t* buf = realloc(handle->read_buf, size); |
| 417 |
if (!buf) { |
| 418 |
return dnswire_error; |
| 419 |
} |
| 420 |
handle->read_buf = buf; |
| 421 |
handle->read_size = size; |
| 422 |
} else { |
| 423 |
// already at max size, and full |
| 424 |
return dnswire_error; |
| 425 |
} |
| 426 |
__state(handle, dnswire_writer_reading_accept); |
| 427 |
return dnswire_need_more; |
| 428 |
|
| 429 |
default: |
| 430 |
break; |
| 431 |
} |
| 432 |
return dnswire_error; |
| 433 |
|
| 434 |
case dnswire_writer_done: |
| 435 |
return dnswire_error; |
| 436 |
} |
| 437 |
|
| 438 |
return res; |
| 439 |
} |
| 440 |
|
| 441 |
enum dnswire_result dnswire_writer_write(struct dnswire_writer* handle, int fd) |
| 442 |
{ |
| 443 |
assert(handle); |
| 444 |
assert(handle->buf); |
| 445 |
|
| 446 |
enum dnswire_result res = dnswire_again; |
| 447 |
|
| 448 |
__trace("state %s", dnswire_writer_state_string[handle->state]); |
| 449 |
|
| 450 |
switch (handle->state) { |
| 451 |
case dnswire_writer_encoding_ready: |
| 452 |
res = _encoding(handle); |
| 453 |
__trace("left %zu", handle->left); |
| 454 |
if (res != dnswire_error && handle->left) { |
| 455 |
__state(handle, dnswire_writer_writing); |
| 456 |
// fallthrough |
| 457 |
} else { |
| 458 |
break; |
| 459 |
} |
| 460 |
|
| 461 |
case dnswire_writer_writing_ready: { |
| 462 |
ssize_t nwrote = write(fd, &handle->buf[handle->at - handle->left], handle->left); |
| 463 |
__trace("wrote %zd", nwrote); |
| 464 |
if (nwrote < 0) { |
| 465 |
// TODO |
| 466 |
return dnswire_error; |
| 467 |
} else if (!nwrote) { |
| 468 |
// TODO |
| 469 |
return dnswire_error; |
| 470 |
} |
| 471 |
|
| 472 |
handle->left -= nwrote; |
| 473 |
__trace("left %zu", handle->left); |
| 474 |
if (handle->left) { |
| 475 |
break; |
| 476 |
} |
| 477 |
handle->at = 0; |
| 478 |
__state(handle, dnswire_writer_reading_accept); |
| 479 |
// fallthrough |
| 480 |
} |
| 481 |
|
| 482 |
case dnswire_writer_reading_accept: { |
| 483 |
ssize_t nread = read(fd, &handle->read_buf[handle->read_at + handle->read_left], handle->read_size - handle->read_at - handle->read_left); |
| 484 |
if (nread < 0) { |
| 485 |
// TODO |
| 486 |
return dnswire_error; |
| 487 |
} else if (!nread) { |
| 488 |
// TODO |
| 489 |
return dnswire_error; |
| 490 |
} |
| 491 |
__trace("%s", __printable_string(&handle->read_buf[handle->read_at + handle->read_left], nread)); |
| 492 |
handle->read_left += nread; |
| 493 |
__state(handle, dnswire_writer_decoding_accept); |
| 494 |
// fallthrough |
| 495 |
} |
| 496 |
|
| 497 |
case dnswire_writer_decoding_accept: |
| 498 |
switch (dnswire_decoder_decode(&handle->decoder, &handle->read_buf[handle->read_at], handle->read_left)) { |
| 499 |
case dnswire_bidirectional: |
| 500 |
handle->read_at += dnswire_decoder_decoded(handle->decoder); |
| 501 |
handle->read_left -= dnswire_decoder_decoded(handle->decoder); |
| 502 |
if (!handle->read_left) { |
| 503 |
handle->read_at = 0; |
| 504 |
} |
| 505 |
|
| 506 |
if (!handle->decoder.accept_support_dnstap_protobuf) { |
| 507 |
return dnswire_error; |
| 508 |
} |
| 509 |
|
| 510 |
__state(handle, dnswire_writer_encoding); |
| 511 |
return dnswire_again; |
| 512 |
|
| 513 |
case dnswire_again: |
| 514 |
handle->read_at += dnswire_decoder_decoded(handle->decoder); |
| 515 |
handle->read_left -= dnswire_decoder_decoded(handle->decoder); |
| 516 |
if (!handle->read_left) { |
| 517 |
handle->read_at = 0; |
| 518 |
__state(handle, dnswire_writer_reading_accept); |
| 519 |
} |
| 520 |
return dnswire_again; |
| 521 |
|
| 522 |
case dnswire_need_more: |
| 523 |
if (handle->read_left < handle->read_size) { |
| 524 |
// still space left to fill |
| 525 |
if (handle->read_at) { |
| 526 |
// move what's left to the start |
| 527 |
if (handle->read_left) { |
| 528 |
memmove(handle->read_buf, &handle->read_buf[handle->read_at], handle->read_left); |
| 529 |
} |
| 530 |
handle->read_at = 0; |
| 531 |
} |
| 532 |
} else if (handle->read_size < handle->read_max) { |
| 533 |
// no space left, expand |
| 534 |
size_t size = handle->read_size + handle->read_inc > handle->read_max ? handle->read_max : handle->read_size + handle->read_inc; |
| 535 |
uint8_t* buf = realloc(handle->read_buf, size); |
| 536 |
if (!buf) { |
| 537 |
return dnswire_error; |
| 538 |
} |
| 539 |
handle->read_buf = buf; |
| 540 |
handle->read_size = size; |
| 541 |
} else { |
| 542 |
// already at max size, and full |
| 543 |
return dnswire_error; |
| 544 |
} |
| 545 |
__state(handle, dnswire_writer_reading_accept); |
| 546 |
return dnswire_need_more; |
| 547 |
|
| 548 |
default: |
| 549 |
break; |
| 550 |
} |
| 551 |
return dnswire_error; |
| 552 |
|
| 553 |
case dnswire_writer_encoding: |
| 554 |
res = _encoding(handle); |
| 555 |
__trace("left %zu", handle->left); |
| 556 |
if (res != dnswire_error && handle->left) { |
| 557 |
__state(handle, dnswire_writer_writing); |
| 558 |
// fallthrough |
| 559 |
} else { |
| 560 |
break; |
| 561 |
} |
| 562 |
|
| 563 |
case dnswire_writer_writing: { |
| 564 |
ssize_t nwrote = write(fd, &handle->buf[handle->at - handle->left], handle->left); |
| 565 |
__trace("wrote %zd", nwrote); |
| 566 |
if (nwrote < 0) { |
| 567 |
// TODO |
| 568 |
return dnswire_error; |
| 569 |
} else if (!nwrote) { |
| 570 |
// TODO |
| 571 |
return dnswire_error; |
| 572 |
} |
| 573 |
|
| 574 |
handle->left -= nwrote; |
| 575 |
__trace("left %zu", handle->left); |
| 576 |
if (!handle->left) { |
| 577 |
handle->at = 0; |
| 578 |
__state(handle, dnswire_writer_encoding); |
| 579 |
} |
| 580 |
break; |
| 581 |
} |
| 582 |
|
| 583 |
case dnswire_writer_stopping: |
| 584 |
if (handle->left) { |
| 585 |
ssize_t nwrote = write(fd, &handle->buf[handle->at - handle->left], handle->left); |
| 586 |
__trace("wrote %zd", nwrote); |
| 587 |
if (nwrote < 0) { |
| 588 |
// TODO |
| 589 |
return dnswire_error; |
| 590 |
} else if (!nwrote) { |
| 591 |
// TODO |
| 592 |
return dnswire_error; |
| 593 |
} |
| 594 |
|
| 595 |
handle->left -= nwrote; |
| 596 |
if (handle->left) { |
| 597 |
__trace("left %zu", handle->left); |
| 598 |
return dnswire_again; |
| 599 |
} |
| 600 |
handle->at = 0; |
| 601 |
} |
| 602 |
__state(handle, dnswire_writer_encoding_stop); |
| 603 |
// fallthrough |
| 604 |
|
| 605 |
case dnswire_writer_encoding_stop: |
| 606 |
res = _encoding(handle); |
| 607 |
if (res == dnswire_endofdata) { |
| 608 |
__state(handle, dnswire_writer_writing_stop); |
| 609 |
// fallthrough |
| 610 |
} else { |
| 611 |
break; |
| 612 |
} |
| 613 |
|
| 614 |
case dnswire_writer_writing_stop: |
| 615 |
if (handle->left) { |
| 616 |
ssize_t nwrote = write(fd, &handle->buf[handle->at - handle->left], handle->left); |
| 617 |
__trace("wrote %zd", nwrote); |
| 618 |
if (nwrote < 0) { |
| 619 |
// TODO |
| 620 |
return dnswire_error; |
| 621 |
} else if (!nwrote) { |
| 622 |
// TODO |
| 623 |
return dnswire_error; |
| 624 |
} |
| 625 |
|
| 626 |
handle->left -= nwrote; |
| 627 |
if (handle->left) { |
| 628 |
__trace("left %zu", handle->left); |
| 629 |
return dnswire_again; |
| 630 |
} |
| 631 |
handle->at = 0; |
| 632 |
} |
| 633 |
if (handle->bidirectional) { |
| 634 |
__state(handle, dnswire_writer_reading_finish); |
| 635 |
return dnswire_again; |
| 636 |
} |
| 637 |
__state(handle, dnswire_writer_done); |
| 638 |
return dnswire_endofdata; |
| 639 |
|
| 640 |
case dnswire_writer_reading_finish: { |
| 641 |
ssize_t nread = read(fd, &handle->read_buf[handle->read_at + handle->read_left], handle->read_size - handle->read_at - handle->read_left); |
| 642 |
if (nread < 0) { |
| 643 |
// TODO |
| 644 |
return dnswire_error; |
| 645 |
} else if (!nread) { |
| 646 |
// TODO |
| 647 |
return dnswire_error; |
| 648 |
} |
| 649 |
__trace("%s", __printable_string(&handle->read_buf[handle->read_at + handle->read_left], nread)); |
| 650 |
handle->read_left += nread; |
| 651 |
__state(handle, dnswire_writer_decoding_finish); |
| 652 |
// fallthrough |
| 653 |
} |
| 654 |
|
| 655 |
case dnswire_writer_decoding_finish: |
| 656 |
switch (dnswire_decoder_decode(&handle->decoder, &handle->read_buf[handle->read_at], handle->read_left)) { |
| 657 |
case dnswire_endofdata: |
| 658 |
__state(handle, dnswire_writer_done); |
| 659 |
return dnswire_endofdata; |
| 660 |
|
| 661 |
case dnswire_need_more: |
| 662 |
if (handle->read_left < handle->read_size) { |
| 663 |
// still space left to fill |
| 664 |
if (handle->read_at) { |
| 665 |
// move what's left to the start |
| 666 |
if (handle->read_left) { |
| 667 |
memmove(handle->read_buf, &handle->read_buf[handle->read_at], handle->read_left); |
| 668 |
} |
| 669 |
handle->read_at = 0; |
| 670 |
} |
| 671 |
} else if (handle->read_size < handle->read_max) { |
| 672 |
// no space left, expand |
| 673 |
size_t size = handle->read_size + handle->read_inc > handle->read_max ? handle->read_max : handle->read_size + handle->read_inc; |
| 674 |
uint8_t* buf = realloc(handle->read_buf, size); |
| 675 |
if (!buf) { |
| 676 |
return dnswire_error; |
| 677 |
} |
| 678 |
handle->read_buf = buf; |
| 679 |
handle->read_size = size; |
| 680 |
} else { |
| 681 |
// already at max size, and full |
| 682 |
return dnswire_error; |
| 683 |
} |
| 684 |
__state(handle, dnswire_writer_reading_accept); |
| 685 |
return dnswire_need_more; |
| 686 |
|
| 687 |
default: |
| 688 |
break; |
| 689 |
} |
| 690 |
return dnswire_error; |
| 691 |
|
| 692 |
case dnswire_writer_done: |
| 693 |
return dnswire_error; |
| 694 |
} |
| 695 |
|
| 696 |
return res; |
| 697 |
} |
| 698 |
|
| 699 |
enum dnswire_result dnswire_writer_stop(struct dnswire_writer* handle) |
| 700 |
{ |
| 701 |
assert(handle); |
| 702 |
|
| 703 |
enum dnswire_result res = dnswire_encoder_stop(&handle->encoder); |
| 704 |
|
| 705 |
if (res == dnswire_ok) { |
| 706 |
__state(handle, dnswire_writer_stopping); |
| 707 |
} |
| 708 |
|
| 709 |
return res; |
| 710 |
} |