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>
47 #include "hast_checksum.h"
49 #include "hast_compression.h"
50 #include "hast_proto.h"
52 struct hast_main_header {
53 /* Protocol version. */
55 /* Size of nv headers. */
59 typedef int hps_send_t(const struct hast_resource *, struct nv *nv, void **,
61 typedef int hps_recv_t(const struct hast_resource *, struct nv *nv, void **,
64 struct hast_pipe_stage {
70 static struct hast_pipe_stage pipeline[] = {
71 { "compression", compression_send, compression_recv },
73 { "checksum", checksum_send, checksum_recv }
78 * Send the given nv structure via conn.
79 * We keep headers in nv structure and pass data in separate argument.
80 * There can be no data at all (data is NULL then).
83 hast_proto_send(const struct hast_resource *res, struct proto_conn *conn,
84 struct nv *nv, const void *data, size_t size)
86 struct hast_main_header hdr;
93 dptr = (void *)(uintptr_t)data;
100 for (ii = 0; ii < sizeof(pipeline) / sizeof(pipeline[0]);
102 (void)pipeline[ii].hps_send(res, nv, &dptr, &size,
105 nv_add_uint32(nv, size, "size");
106 if (nv_error(nv) != 0) {
107 errno = nv_error(nv);
116 hdr.version = HAST_PROTO_VERSION;
117 hdr.size = htole32((uint32_t)ebuf_size(eb));
118 if (ebuf_add_head(eb, &hdr, sizeof(hdr)) < 0)
121 hptr = ebuf_data(eb, &hsize);
122 if (proto_send(conn, hptr, hsize) < 0)
124 if (data != NULL && proto_send(conn, dptr, size) < 0)
135 hast_proto_recv_hdr(const struct proto_conn *conn, struct nv **nvp)
137 struct hast_main_header hdr;
145 if (proto_recv(conn, &hdr, sizeof(hdr)) < 0)
148 if (hdr.version != HAST_PROTO_VERSION) {
149 errno = ERPCMISMATCH;
153 hdr.size = le32toh(hdr.size);
155 eb = ebuf_alloc(hdr.size);
158 if (ebuf_add_tail(eb, NULL, hdr.size) < 0)
160 hptr = ebuf_data(eb, NULL);
161 assert(hptr != NULL);
162 if (proto_recv(conn, hptr, hdr.size) < 0)
177 hast_proto_recv_data(const struct hast_resource *res, struct proto_conn *conn,
178 struct nv *nv, void *data, size_t size)
186 assert(data != NULL);
193 dsize = nv_get_uint32(nv, "size");
197 } else if (dsize == 0) {
198 (void)nv_set_error(nv, 0);
200 if (proto_recv(conn, data, dsize) < 0)
202 for (ii = sizeof(pipeline) / sizeof(pipeline[0]); ii > 0;
204 ret = pipeline[ii - 1].hps_recv(res, nv, &dptr,
215 bcopy(dptr, data, dsize);