2010-02-18 23:16:19 +00:00
|
|
|
/*-
|
2017-11-27 15:37:16 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2010-02-18 23:16:19 +00:00
|
|
|
* Copyright (c) 2009-2010 The FreeBSD Foundation
|
2011-03-06 22:56:14 +00:00
|
|
|
* Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>
|
2010-02-18 23:16:19 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This software was developed by Pawel Jakub Dawidek under sponsorship from
|
|
|
|
* the FreeBSD Foundation.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/endian.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <strings.h>
|
|
|
|
|
|
|
|
#include <hast.h>
|
|
|
|
#include <ebuf.h>
|
|
|
|
#include <nv.h>
|
|
|
|
#include <pjdlog.h>
|
|
|
|
#include <proto.h>
|
|
|
|
|
2011-03-06 22:56:14 +00:00
|
|
|
#include "hast_checksum.h"
|
2011-03-06 23:09:33 +00:00
|
|
|
#include "hast_compression.h"
|
2010-02-18 23:16:19 +00:00
|
|
|
#include "hast_proto.h"
|
|
|
|
|
|
|
|
struct hast_main_header {
|
|
|
|
/* Protocol version. */
|
|
|
|
uint8_t version;
|
|
|
|
/* Size of nv headers. */
|
|
|
|
uint32_t size;
|
|
|
|
} __packed;
|
|
|
|
|
2010-08-30 22:26:42 +00:00
|
|
|
typedef int hps_send_t(const struct hast_resource *, struct nv *nv, void **,
|
|
|
|
size_t *, bool *);
|
|
|
|
typedef int hps_recv_t(const struct hast_resource *, struct nv *nv, void **,
|
|
|
|
size_t *, bool *);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
|
|
|
struct hast_pipe_stage {
|
|
|
|
const char *hps_name;
|
|
|
|
hps_send_t *hps_send;
|
|
|
|
hps_recv_t *hps_recv;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct hast_pipe_stage pipeline[] = {
|
2011-03-06 23:09:33 +00:00
|
|
|
{ "compression", compression_send, compression_recv },
|
2010-02-18 23:16:19 +00:00
|
|
|
{ "checksum", checksum_send, checksum_recv }
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send the given nv structure via conn.
|
|
|
|
* We keep headers in nv structure and pass data in separate argument.
|
|
|
|
* There can be no data at all (data is NULL then).
|
|
|
|
*/
|
|
|
|
int
|
2010-08-30 22:26:42 +00:00
|
|
|
hast_proto_send(const struct hast_resource *res, struct proto_conn *conn,
|
2010-02-18 23:16:19 +00:00
|
|
|
struct nv *nv, const void *data, size_t size)
|
|
|
|
{
|
|
|
|
struct hast_main_header hdr;
|
|
|
|
struct ebuf *eb;
|
|
|
|
bool freedata;
|
|
|
|
void *dptr, *hptr;
|
|
|
|
size_t hsize;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
dptr = (void *)(uintptr_t)data;
|
|
|
|
freedata = false;
|
|
|
|
ret = -1;
|
|
|
|
|
|
|
|
if (data != NULL) {
|
|
|
|
unsigned int ii;
|
|
|
|
|
|
|
|
for (ii = 0; ii < sizeof(pipeline) / sizeof(pipeline[0]);
|
|
|
|
ii++) {
|
2011-03-06 22:56:14 +00:00
|
|
|
(void)pipeline[ii].hps_send(res, nv, &dptr, &size,
|
2010-02-18 23:16:19 +00:00
|
|
|
&freedata);
|
|
|
|
}
|
|
|
|
nv_add_uint32(nv, size, "size");
|
|
|
|
if (nv_error(nv) != 0) {
|
|
|
|
errno = nv_error(nv);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
eb = nv_hton(nv);
|
|
|
|
if (eb == NULL)
|
|
|
|
goto end;
|
|
|
|
|
2013-02-17 21:12:34 +00:00
|
|
|
hdr.version = res != NULL ? res->hr_version : HAST_PROTO_VERSION;
|
2010-02-18 23:16:19 +00:00
|
|
|
hdr.size = htole32((uint32_t)ebuf_size(eb));
|
2012-01-10 22:39:07 +00:00
|
|
|
if (ebuf_add_head(eb, &hdr, sizeof(hdr)) == -1)
|
2010-02-18 23:16:19 +00:00
|
|
|
goto end;
|
|
|
|
|
|
|
|
hptr = ebuf_data(eb, &hsize);
|
2012-01-10 22:39:07 +00:00
|
|
|
if (proto_send(conn, hptr, hsize) == -1)
|
2010-02-18 23:16:19 +00:00
|
|
|
goto end;
|
2012-01-10 22:39:07 +00:00
|
|
|
if (data != NULL && proto_send(conn, dptr, size) == -1)
|
2010-02-18 23:16:19 +00:00
|
|
|
goto end;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
end:
|
|
|
|
if (freedata)
|
|
|
|
free(dptr);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2010-08-30 22:26:42 +00:00
|
|
|
hast_proto_recv_hdr(const struct proto_conn *conn, struct nv **nvp)
|
2010-02-18 23:16:19 +00:00
|
|
|
{
|
|
|
|
struct hast_main_header hdr;
|
|
|
|
struct nv *nv;
|
|
|
|
struct ebuf *eb;
|
|
|
|
void *hptr;
|
|
|
|
|
|
|
|
eb = NULL;
|
|
|
|
nv = NULL;
|
|
|
|
|
2012-01-10 22:39:07 +00:00
|
|
|
if (proto_recv(conn, &hdr, sizeof(hdr)) == -1)
|
2010-02-18 23:16:19 +00:00
|
|
|
goto fail;
|
|
|
|
|
2013-02-17 21:12:34 +00:00
|
|
|
if (hdr.version > HAST_PROTO_VERSION) {
|
2010-02-18 23:16:19 +00:00
|
|
|
errno = ERPCMISMATCH;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
hdr.size = le32toh(hdr.size);
|
|
|
|
|
|
|
|
eb = ebuf_alloc(hdr.size);
|
|
|
|
if (eb == NULL)
|
|
|
|
goto fail;
|
2012-01-10 22:39:07 +00:00
|
|
|
if (ebuf_add_tail(eb, NULL, hdr.size) == -1)
|
2010-02-18 23:16:19 +00:00
|
|
|
goto fail;
|
|
|
|
hptr = ebuf_data(eb, NULL);
|
2011-09-27 08:50:37 +00:00
|
|
|
PJDLOG_ASSERT(hptr != NULL);
|
2012-01-10 22:39:07 +00:00
|
|
|
if (proto_recv(conn, hptr, hdr.size) == -1)
|
2010-02-18 23:16:19 +00:00
|
|
|
goto fail;
|
|
|
|
nv = nv_ntoh(eb);
|
|
|
|
if (nv == NULL)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
*nvp = nv;
|
|
|
|
return (0);
|
|
|
|
fail:
|
2010-06-14 21:01:13 +00:00
|
|
|
if (eb != NULL)
|
2010-02-18 23:16:19 +00:00
|
|
|
ebuf_free(eb);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2010-08-30 22:26:42 +00:00
|
|
|
hast_proto_recv_data(const struct hast_resource *res, struct proto_conn *conn,
|
2010-02-18 23:16:19 +00:00
|
|
|
struct nv *nv, void *data, size_t size)
|
|
|
|
{
|
|
|
|
unsigned int ii;
|
|
|
|
bool freedata;
|
|
|
|
size_t dsize;
|
|
|
|
void *dptr;
|
|
|
|
int ret;
|
|
|
|
|
2011-09-27 08:50:37 +00:00
|
|
|
PJDLOG_ASSERT(data != NULL);
|
|
|
|
PJDLOG_ASSERT(size > 0);
|
2010-02-18 23:16:19 +00:00
|
|
|
|
|
|
|
ret = -1;
|
|
|
|
freedata = false;
|
|
|
|
dptr = data;
|
|
|
|
|
|
|
|
dsize = nv_get_uint32(nv, "size");
|
2011-04-10 15:21:46 +00:00
|
|
|
if (dsize > size) {
|
|
|
|
errno = EINVAL;
|
|
|
|
goto end;
|
|
|
|
} else if (dsize == 0) {
|
2010-02-18 23:16:19 +00:00
|
|
|
(void)nv_set_error(nv, 0);
|
2011-04-10 15:21:46 +00:00
|
|
|
} else {
|
2012-01-10 22:39:07 +00:00
|
|
|
if (proto_recv(conn, data, dsize) == -1)
|
2010-02-18 23:16:19 +00:00
|
|
|
goto end;
|
|
|
|
for (ii = sizeof(pipeline) / sizeof(pipeline[0]); ii > 0;
|
|
|
|
ii--) {
|
|
|
|
ret = pipeline[ii - 1].hps_recv(res, nv, &dptr,
|
|
|
|
&dsize, &freedata);
|
|
|
|
if (ret == -1)
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
ret = -1;
|
2011-03-06 22:56:14 +00:00
|
|
|
if (dsize > size) {
|
|
|
|
errno = EINVAL;
|
2010-02-18 23:16:19 +00:00
|
|
|
goto end;
|
2011-03-06 22:56:14 +00:00
|
|
|
}
|
2010-02-18 23:16:19 +00:00
|
|
|
if (dptr != data)
|
|
|
|
bcopy(dptr, data, dsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
end:
|
|
|
|
if (freedata)
|
|
|
|
free(dptr);
|
|
|
|
return (ret);
|
|
|
|
}
|