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 },
72 { "checksum", checksum_send, checksum_recv }
76 * Send the given nv structure via conn.
77 * We keep headers in nv structure and pass data in separate argument.
78 * There can be no data at all (data is NULL then).
81 hast_proto_send(const struct hast_resource *res, struct proto_conn *conn,
82 struct nv *nv, const void *data, size_t size)
84 struct hast_main_header hdr;
91 dptr = (void *)(uintptr_t)data;
98 for (ii = 0; ii < sizeof(pipeline) / sizeof(pipeline[0]);
100 (void)pipeline[ii].hps_send(res, nv, &dptr, &size,
103 nv_add_uint32(nv, size, "size");
104 if (nv_error(nv) != 0) {
105 errno = nv_error(nv);
114 hdr.version = HAST_PROTO_VERSION;
115 hdr.size = htole32((uint32_t)ebuf_size(eb));
116 if (ebuf_add_head(eb, &hdr, sizeof(hdr)) < 0)
119 hptr = ebuf_data(eb, &hsize);
120 if (proto_send(conn, hptr, hsize) < 0)
122 if (data != NULL && proto_send(conn, dptr, size) < 0)
133 hast_proto_recv_hdr(const struct proto_conn *conn, struct nv **nvp)
135 struct hast_main_header hdr;
143 if (proto_recv(conn, &hdr, sizeof(hdr)) < 0)
146 if (hdr.version != HAST_PROTO_VERSION) {
147 errno = ERPCMISMATCH;
151 hdr.size = le32toh(hdr.size);
153 eb = ebuf_alloc(hdr.size);
156 if (ebuf_add_tail(eb, NULL, hdr.size) < 0)
158 hptr = ebuf_data(eb, NULL);
159 assert(hptr != NULL);
160 if (proto_recv(conn, hptr, hdr.size) < 0)
175 hast_proto_recv_data(const struct hast_resource *res, struct proto_conn *conn,
176 struct nv *nv, void *data, size_t size)
184 assert(data != NULL);
191 dsize = nv_get_uint32(nv, "size");
193 (void)nv_set_error(nv, 0);
195 if (proto_recv(conn, data, dsize) < 0)
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);
221 hast_proto_recv(const struct hast_resource *res, struct proto_conn *conn,
222 struct nv **nvp, void *data, size_t size)
228 ret = hast_proto_recv_hdr(conn, &nv);
231 dsize = nv_get_uint32(nv, "size");
233 (void)nv_set_error(nv, 0);
235 ret = hast_proto_recv_data(res, conn, nv, data, size);