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 211 212 213 214 215 216 217 218
|
/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* SPDX-License-Identifier: MPL-2.0 AND ISC
*
* 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
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
*/
/*
* Copyright (C) 2001 Nominum, Inc.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* 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
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*! \file */
#include <inttypes.h>
#include <isc/mem.h>
#include <isc/netmgr.h>
#include <isc/result.h>
#include <isc/string.h>
#include <isc/util.h>
#include <isccc/ccmsg.h>
#define CCMSG_MAGIC ISC_MAGIC('C', 'C', 'm', 's')
#define VALID_CCMSG(foo) ISC_MAGIC_VALID(foo, CCMSG_MAGIC)
/*
* Try parsing a message from the internal read_buffer and set state
* accordingly. Returns true if a message was successfully parsed, false if not.
* If no message could be parsed the ccmsg struct remains untouched.
*/
static isc_result_t
try_parse_message(isccc_ccmsg_t *ccmsg) {
REQUIRE(ccmsg != NULL);
uint32_t len = 0;
if (isc_buffer_peekuint32(ccmsg->buffer, &len) != ISC_R_SUCCESS) {
return ISC_R_NOMORE;
}
if (len == 0) {
return ISC_R_UNEXPECTEDEND;
}
if (len > ccmsg->maxsize) {
return ISC_R_RANGE;
}
if (isc_buffer_remaininglength(ccmsg->buffer) < sizeof(uint32_t) + len)
{
return ISC_R_NOMORE;
}
/* Skip the size we just peeked */
isc_buffer_forward(ccmsg->buffer, sizeof(uint32_t));
ccmsg->size = len;
return ISC_R_SUCCESS;
}
static void
recv_data(isc_nmhandle_t *handle, isc_result_t eresult, isc_region_t *region,
void *arg) {
isccc_ccmsg_t *ccmsg = arg;
REQUIRE(VALID_CCMSG(ccmsg));
REQUIRE(handle == ccmsg->handle);
if (eresult != ISC_R_SUCCESS) {
goto done;
}
REQUIRE(region != NULL);
/* Copy the received data to our reassembly buffer */
eresult = isc_buffer_copyregion(ccmsg->buffer, region);
if (eresult != ISC_R_SUCCESS) {
goto done;
}
isc_region_consume(region, region->length);
/* Try to parse a single message of the buffer */
eresult = try_parse_message(ccmsg);
/* No results from parsing, we need more data */
if (eresult == ISC_R_NOMORE) {
return;
}
done:
isc_nm_read_stop(handle);
ccmsg->recv_cb(handle, eresult, ccmsg->recv_cbarg);
return;
}
void
isccc_ccmsg_init(isc_mem_t *mctx, isc_nmhandle_t *handle,
isccc_ccmsg_t *ccmsg) {
REQUIRE(mctx != NULL);
REQUIRE(handle != NULL);
REQUIRE(ccmsg != NULL);
*ccmsg = (isccc_ccmsg_t){
.magic = CCMSG_MAGIC,
.maxsize = 0xffffffffU, /* Largest message possible. */
.mctx = mctx,
};
/* Preallocate the buffer to maximum single TCP read */
isc_buffer_allocate(ccmsg->mctx, &ccmsg->buffer,
UINT16_MAX + sizeof(uint16_t));
isc_nmhandle_attach(handle, &ccmsg->handle);
}
void
isccc_ccmsg_setmaxsize(isccc_ccmsg_t *ccmsg, unsigned int maxsize) {
REQUIRE(VALID_CCMSG(ccmsg));
ccmsg->maxsize = maxsize;
}
void
isccc_ccmsg_readmessage(isccc_ccmsg_t *ccmsg, isc_nm_cb_t cb, void *cbarg) {
REQUIRE(VALID_CCMSG(ccmsg));
if (ccmsg->size != 0) {
/* Remove the previously read message from the buffer */
isc_buffer_forward(ccmsg->buffer, ccmsg->size);
ccmsg->size = 0;
isc_buffer_trycompact(ccmsg->buffer);
}
ccmsg->recv_cb = cb;
ccmsg->recv_cbarg = cbarg;
/* If we have previous data still in the buffer, try to parse it */
isc_result_t result = try_parse_message(ccmsg);
if (result == ISC_R_NOMORE) {
/* We need to read more data */
isc_nm_read(ccmsg->handle, recv_data, ccmsg);
return;
}
ccmsg->recv_cb(ccmsg->handle, result, ccmsg->recv_cbarg);
}
static void
ccmsg_senddone(isc_nmhandle_t *handle, isc_result_t eresult, void *arg) {
isccc_ccmsg_t *ccmsg = arg;
REQUIRE(VALID_CCMSG(ccmsg));
REQUIRE(ccmsg->send_cb != NULL);
isc_nm_cb_t send_cb = ccmsg->send_cb;
ccmsg->send_cb = NULL;
send_cb(handle, eresult, ccmsg->send_cbarg);
isc_nmhandle_detach(&handle);
}
void
isccc_ccmsg_sendmessage(isccc_ccmsg_t *ccmsg, isc_region_t *region,
isc_nm_cb_t cb, void *cbarg) {
REQUIRE(VALID_CCMSG(ccmsg));
REQUIRE(ccmsg->send_cb == NULL);
ccmsg->send_cb = cb;
ccmsg->send_cbarg = cbarg;
isc_nmhandle_ref(ccmsg->handle);
isc_nm_send(ccmsg->handle, region, ccmsg_senddone, ccmsg);
}
void
isccc_ccmsg_disconnect(isccc_ccmsg_t *ccmsg) {
REQUIRE(VALID_CCMSG(ccmsg));
if (ccmsg->handle != NULL) {
isc_nm_read_stop(ccmsg->handle);
isc_nmhandle_close(ccmsg->handle);
isc_nmhandle_detach(&ccmsg->handle);
}
}
void
isccc_ccmsg_invalidate(isccc_ccmsg_t *ccmsg) {
REQUIRE(VALID_CCMSG(ccmsg));
REQUIRE(ccmsg->handle == NULL);
ccmsg->magic = 0;
isc_buffer_free(&ccmsg->buffer);
}
void
isccc_ccmsg_toregion(isccc_ccmsg_t *ccmsg, isccc_region_t *ccregion) {
REQUIRE(VALID_CCMSG(ccmsg));
REQUIRE(ccmsg->buffer);
REQUIRE(isc_buffer_remaininglength(ccmsg->buffer) >= ccmsg->size);
ccregion->rstart = isc_buffer_current(ccmsg->buffer);
ccregion->rend = ccregion->rstart + ccmsg->size;
}
|