2 * Copyright (c) 2009-2010 The FreeBSD Foundation
3 * Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>
6 * This software was developed by Pawel Jakub Dawidek under sponsorship from
7 * the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/endian.h>
45 #include "hast_checksum.h"
46 #include "hast_compression.h"
47 #include "hast_proto.h"
49 struct hast_main_header {
50 /* Protocol version. */
52 /* Size of nv headers. */
56 typedef int hps_send_t(const struct hast_resource *, struct nv *nv, void **,
58 typedef int hps_recv_t(const struct hast_resource *, struct nv *nv, void **,
61 struct hast_pipe_stage {
67 static struct hast_pipe_stage pipeline[] = {
68 { "compression", compression_send, compression_recv },
69 { "checksum", checksum_send, checksum_recv }
73 * Send the given nv structure via conn.
74 * We keep headers in nv structure and pass data in separate argument.
75 * There can be no data at all (data is NULL then).
78 hast_proto_send(const struct hast_resource *res, struct proto_conn *conn,
79 struct nv *nv, const void *data, size_t size)
81 struct hast_main_header hdr;
88 dptr = (void *)(uintptr_t)data;
95 for (ii = 0; ii < sizeof(pipeline) / sizeof(pipeline[0]);
97 (void)pipeline[ii].hps_send(res, nv, &dptr, &size,
100 nv_add_uint32(nv, size, "size");
101 if (nv_error(nv) != 0) {
102 errno = nv_error(nv);
111 hdr.version = res != NULL ? res->hr_version : HAST_PROTO_VERSION;
112 hdr.size = htole32((uint32_t)ebuf_size(eb));
113 if (ebuf_add_head(eb, &hdr, sizeof(hdr)) == -1)
116 hptr = ebuf_data(eb, &hsize);
117 if (proto_send(conn, hptr, hsize) == -1)
119 if (data != NULL && proto_send(conn, dptr, size) == -1)
130 hast_proto_recv_hdr(const struct proto_conn *conn, struct nv **nvp)
132 struct hast_main_header hdr;
140 if (proto_recv(conn, &hdr, sizeof(hdr)) == -1)
143 if (hdr.version > HAST_PROTO_VERSION) {
144 errno = ERPCMISMATCH;
148 hdr.size = le32toh(hdr.size);
150 eb = ebuf_alloc(hdr.size);
153 if (ebuf_add_tail(eb, NULL, hdr.size) == -1)
155 hptr = ebuf_data(eb, NULL);
156 PJDLOG_ASSERT(hptr != NULL);
157 if (proto_recv(conn, hptr, hdr.size) == -1)
172 hast_proto_recv_data(const struct hast_resource *res, struct proto_conn *conn,
173 struct nv *nv, void *data, size_t size)
181 PJDLOG_ASSERT(data != NULL);
182 PJDLOG_ASSERT(size > 0);
188 dsize = nv_get_uint32(nv, "size");
192 } else if (dsize == 0) {
193 (void)nv_set_error(nv, 0);
195 if (proto_recv(conn, data, dsize) == -1)
197 for (ii = sizeof(pipeline) / sizeof(pipeline[0]); ii > 0;
199 ret = pipeline[ii - 1].hps_recv(res, nv, &dptr,
210 bcopy(dptr, data, dsize);