2001-03-27 00:44:59 +00:00
|
|
|
/*
|
2018-03-15 18:32:45 -07:00
|
|
|
* Portions Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2007-06-18 23:47:57 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2007-06-18 23:47:57 +00:00
|
|
|
*
|
2018-02-23 09:53:12 +01:00
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
|
|
|
*
|
2018-03-15 18:32:45 -07:00
|
|
|
* Portions Copyright (C) 2001 Nominum, Inc.
|
2001-03-27 00:44:59 +00:00
|
|
|
*
|
2007-08-28 07:20:43 +00:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
2001-03-27 00:44:59 +00:00
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
2004-03-05 05:14:21 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
|
|
|
|
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
|
2001-03-27 20:08:15 +00:00
|
|
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
2004-03-05 05:14:21 +00:00
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2001-03-27 00:44:59 +00:00
|
|
|
*/
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! \file */
|
2001-03-27 00:44:59 +00:00
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
|
|
|
|
2001-03-27 00:44:59 +00:00
|
|
|
#include <isc/mem.h>
|
2020-04-16 13:06:42 -07:00
|
|
|
#include <isc/netmgr.h>
|
2001-03-27 00:44:59 +00:00
|
|
|
#include <isc/result.h>
|
2020-04-16 13:06:42 -07:00
|
|
|
#include <isc/string.h>
|
2001-03-27 00:44:59 +00:00
|
|
|
#include <isc/util.h>
|
|
|
|
|
|
|
|
#include <isccc/ccmsg.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <isccc/events.h>
|
2001-03-27 00:44:59 +00:00
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
#define CCMSG_MAGIC ISC_MAGIC('C', 'C', 'm', 's')
|
2020-02-12 13:59:18 +01:00
|
|
|
#define VALID_CCMSG(foo) ISC_MAGIC_VALID(foo, CCMSG_MAGIC)
|
2001-03-27 00:44:59 +00:00
|
|
|
|
2020-02-14 08:14:03 +01:00
|
|
|
static void
|
2020-07-01 19:07:04 +02:00
|
|
|
recv_data(isc_nmhandle_t *handle, isc_result_t eresult, isc_region_t *region,
|
|
|
|
void *arg) {
|
2020-04-16 13:06:42 -07:00
|
|
|
isccc_ccmsg_t *ccmsg = arg;
|
2020-07-01 19:07:04 +02:00
|
|
|
size_t size;
|
2001-03-27 00:44:59 +00:00
|
|
|
|
2020-05-14 14:03:37 -07:00
|
|
|
INSIST(VALID_CCMSG(ccmsg));
|
|
|
|
|
2020-04-16 13:06:42 -07:00
|
|
|
if (eresult == ISC_R_CANCELED || eresult == ISC_R_EOF) {
|
|
|
|
ccmsg->result = eresult;
|
|
|
|
goto done;
|
2020-07-01 19:07:04 +02:00
|
|
|
} else if (region == NULL && eresult == ISC_R_SUCCESS) {
|
2020-04-16 13:06:42 -07:00
|
|
|
ccmsg->result = ISC_R_EOF;
|
|
|
|
goto done;
|
|
|
|
} else if (eresult != ISC_R_SUCCESS) {
|
|
|
|
ccmsg->result = eresult;
|
|
|
|
goto done;
|
|
|
|
} else {
|
|
|
|
ccmsg->result = eresult;
|
|
|
|
}
|
2001-03-27 00:44:59 +00:00
|
|
|
|
2020-07-01 19:07:04 +02:00
|
|
|
if (!ccmsg->length_received) {
|
|
|
|
if (region->length < sizeof(uint32_t)) {
|
|
|
|
ccmsg->result = ISC_R_UNEXPECTEDEND;
|
|
|
|
goto done;
|
|
|
|
}
|
2001-03-27 00:44:59 +00:00
|
|
|
|
2020-07-01 19:07:04 +02:00
|
|
|
ccmsg->size = ntohl(*(uint32_t *)region->base);
|
2001-03-27 00:44:59 +00:00
|
|
|
|
2020-07-01 19:07:04 +02:00
|
|
|
if (ccmsg->size == 0) {
|
|
|
|
ccmsg->result = ISC_R_UNEXPECTEDEND;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (ccmsg->size > ccmsg->maxsize) {
|
|
|
|
ccmsg->result = ISC_R_RANGE;
|
|
|
|
goto done;
|
|
|
|
}
|
2020-04-16 13:06:42 -07:00
|
|
|
|
2020-07-01 19:07:04 +02:00
|
|
|
isc_region_consume(region, sizeof(uint32_t));
|
|
|
|
isc_buffer_allocate(ccmsg->mctx, &ccmsg->buffer, ccmsg->size);
|
|
|
|
|
|
|
|
ccmsg->length_received = true;
|
2001-03-27 00:44:59 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 13:06:42 -07:00
|
|
|
/*
|
2020-07-01 19:07:04 +02:00
|
|
|
* If there's no more data, wait for more
|
2020-04-16 13:06:42 -07:00
|
|
|
*/
|
2020-07-01 19:07:04 +02:00
|
|
|
if (region->length == 0) {
|
2020-04-16 13:06:42 -07:00
|
|
|
return;
|
2001-03-27 00:44:59 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 19:07:04 +02:00
|
|
|
/* We have some data in the buffer, read it */
|
2020-04-16 13:06:42 -07:00
|
|
|
|
|
|
|
size = ISC_MIN(isc_buffer_availablelength(ccmsg->buffer),
|
|
|
|
region->length);
|
|
|
|
isc_buffer_putmem(ccmsg->buffer, region->base, size);
|
|
|
|
isc_region_consume(region, size);
|
|
|
|
|
|
|
|
if (isc_buffer_usedlength(ccmsg->buffer) == ccmsg->size) {
|
|
|
|
ccmsg->result = ISC_R_SUCCESS;
|
|
|
|
goto done;
|
|
|
|
}
|
2001-03-27 00:44:59 +00:00
|
|
|
|
2020-07-01 19:07:04 +02:00
|
|
|
/* Wait for more data to come */
|
|
|
|
return;
|
2001-03-27 00:44:59 +00:00
|
|
|
|
2020-04-16 13:06:42 -07:00
|
|
|
done:
|
2020-07-01 19:07:04 +02:00
|
|
|
isc_nm_pauseread(handle);
|
2020-04-16 13:06:42 -07:00
|
|
|
ccmsg->cb(handle, ccmsg->result, ccmsg->cbarg);
|
2001-03-27 00:44:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-16 13:06:42 -07:00
|
|
|
isccc_ccmsg_init(isc_mem_t *mctx, isc_nmhandle_t *handle,
|
|
|
|
isccc_ccmsg_t *ccmsg) {
|
2001-03-27 00:44:59 +00:00
|
|
|
REQUIRE(mctx != NULL);
|
2020-04-16 13:06:42 -07:00
|
|
|
REQUIRE(handle != NULL);
|
2001-03-27 00:44:59 +00:00
|
|
|
REQUIRE(ccmsg != NULL);
|
|
|
|
|
2019-12-15 16:45:17 -08:00
|
|
|
*ccmsg = (isccc_ccmsg_t){
|
|
|
|
.magic = CCMSG_MAGIC,
|
|
|
|
.maxsize = 0xffffffffU, /* Largest message possible. */
|
|
|
|
.mctx = mctx,
|
2020-04-16 13:06:42 -07:00
|
|
|
.handle = handle,
|
2019-12-15 16:45:17 -08:00
|
|
|
.result = ISC_R_UNEXPECTED /* None yet. */
|
|
|
|
};
|
2001-03-27 00:44:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isccc_ccmsg_setmaxsize(isccc_ccmsg_t *ccmsg, unsigned int maxsize) {
|
2001-03-27 00:44:59 +00:00
|
|
|
REQUIRE(VALID_CCMSG(ccmsg));
|
|
|
|
|
|
|
|
ccmsg->maxsize = maxsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-04-16 13:06:42 -07:00
|
|
|
isccc_ccmsg_readmessage(isccc_ccmsg_t *ccmsg, isc_nm_cb_t cb, void *cbarg) {
|
2001-03-27 00:44:59 +00:00
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
REQUIRE(VALID_CCMSG(ccmsg));
|
2020-04-16 13:06:42 -07:00
|
|
|
|
|
|
|
if (ccmsg->buffer != NULL) {
|
|
|
|
isc_buffer_free(&ccmsg->buffer);
|
2001-03-27 00:44:59 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 13:06:42 -07:00
|
|
|
ccmsg->cb = cb;
|
|
|
|
ccmsg->cbarg = cbarg;
|
2020-02-12 13:59:18 +01:00
|
|
|
ccmsg->result = ISC_R_UNEXPECTED; /* unknown right now */
|
2020-07-01 19:07:04 +02:00
|
|
|
ccmsg->length_received = false;
|
2001-03-27 00:44:59 +00:00
|
|
|
|
2020-07-01 19:07:04 +02:00
|
|
|
if (ccmsg->reading) {
|
|
|
|
result = isc_nm_resumeread(ccmsg->handle);
|
|
|
|
} else {
|
|
|
|
result = isc_nm_read(ccmsg->handle, recv_data, ccmsg);
|
|
|
|
ccmsg->reading = true;
|
|
|
|
}
|
2020-09-03 13:31:27 -07:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2020-07-01 19:07:04 +02:00
|
|
|
ccmsg->reading = false;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2001-03-27 00:44:59 +00:00
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isccc_ccmsg_cancelread(isccc_ccmsg_t *ccmsg) {
|
2001-03-27 00:44:59 +00:00
|
|
|
REQUIRE(VALID_CCMSG(ccmsg));
|
|
|
|
|
2020-07-01 19:07:04 +02:00
|
|
|
if (ccmsg->reading) {
|
|
|
|
isc_nm_cancelread(ccmsg->handle);
|
2020-09-03 13:31:27 -07:00
|
|
|
ccmsg->reading = false;
|
2020-07-01 19:07:04 +02:00
|
|
|
}
|
2001-03-27 00:44:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isccc_ccmsg_invalidate(isccc_ccmsg_t *ccmsg) {
|
2001-03-27 00:44:59 +00:00
|
|
|
REQUIRE(VALID_CCMSG(ccmsg));
|
|
|
|
|
|
|
|
ccmsg->magic = 0;
|
|
|
|
|
2020-04-16 13:06:42 -07:00
|
|
|
if (ccmsg->buffer != NULL) {
|
|
|
|
isc_buffer_free(&ccmsg->buffer);
|
2001-03-27 00:44:59 +00:00
|
|
|
}
|
|
|
|
}
|