Deleted Added
full compact
hast_proto.c (209175) hast_proto.c (212033)
1/*-
2 * Copyright (c) 2009-2010 The FreeBSD Foundation
3 * All rights reserved.
4 *
5 * This software was developed by Pawel Jakub Dawidek under sponsorship from
6 * the FreeBSD Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 14 unchanged lines hidden (view full) ---

23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2009-2010 The FreeBSD Foundation
3 * All rights reserved.
4 *
5 * This software was developed by Pawel Jakub Dawidek under sponsorship from
6 * the FreeBSD Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 14 unchanged lines hidden (view full) ---

23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: head/sbin/hastd/hast_proto.c 209175 2010-06-14 21:01:13Z pjd $");
31__FBSDID("$FreeBSD: head/sbin/hastd/hast_proto.c 212033 2010-08-30 22:26:42Z pjd $");
32
33#include <sys/endian.h>
34
35#include <assert.h>
36#include <errno.h>
37#include <string.h>
38#include <strings.h>
39

--- 11 unchanged lines hidden (view full) ---

51
52struct hast_main_header {
53 /* Protocol version. */
54 uint8_t version;
55 /* Size of nv headers. */
56 uint32_t size;
57} __packed;
58
32
33#include <sys/endian.h>
34
35#include <assert.h>
36#include <errno.h>
37#include <string.h>
38#include <strings.h>
39

--- 11 unchanged lines hidden (view full) ---

51
52struct hast_main_header {
53 /* Protocol version. */
54 uint8_t version;
55 /* Size of nv headers. */
56 uint32_t size;
57} __packed;
58
59typedef int hps_send_t(struct hast_resource *, struct nv *nv, void **, size_t *, bool *);
60typedef int hps_recv_t(struct hast_resource *, struct nv *nv, void **, size_t *, bool *);
59typedef int hps_send_t(const struct hast_resource *, struct nv *nv, void **,
60 size_t *, bool *);
61typedef int hps_recv_t(const struct hast_resource *, struct nv *nv, void **,
62 size_t *, bool *);
61
62struct hast_pipe_stage {
63 const char *hps_name;
64 hps_send_t *hps_send;
65 hps_recv_t *hps_recv;
66};
67
63
64struct hast_pipe_stage {
65 const char *hps_name;
66 hps_send_t *hps_send;
67 hps_recv_t *hps_recv;
68};
69
68static int compression_send(struct hast_resource *res, struct nv *nv,
70static int compression_send(const struct hast_resource *res, struct nv *nv,
69 void **datap, size_t *sizep, bool *freedatap);
71 void **datap, size_t *sizep, bool *freedatap);
70static int compression_recv(struct hast_resource *res, struct nv *nv,
72static int compression_recv(const struct hast_resource *res, struct nv *nv,
71 void **datap, size_t *sizep, bool *freedatap);
72#ifdef HAVE_CRYPTO
73 void **datap, size_t *sizep, bool *freedatap);
74#ifdef HAVE_CRYPTO
73static int checksum_send(struct hast_resource *res, struct nv *nv,
75static int checksum_send(const struct hast_resource *res, struct nv *nv,
74 void **datap, size_t *sizep, bool *freedatap);
76 void **datap, size_t *sizep, bool *freedatap);
75static int checksum_recv(struct hast_resource *res, struct nv *nv,
77static int checksum_recv(const struct hast_resource *res, struct nv *nv,
76 void **datap, size_t *sizep, bool *freedatap);
77#endif
78
79static struct hast_pipe_stage pipeline[] = {
80 { "compression", compression_send, compression_recv },
81#ifdef HAVE_CRYPTO
82 { "checksum", checksum_send, checksum_recv }
83#endif
84};
85
86static int
78 void **datap, size_t *sizep, bool *freedatap);
79#endif
80
81static struct hast_pipe_stage pipeline[] = {
82 { "compression", compression_send, compression_recv },
83#ifdef HAVE_CRYPTO
84 { "checksum", checksum_send, checksum_recv }
85#endif
86};
87
88static int
87compression_send(struct hast_resource *res, struct nv *nv, void **datap,
89compression_send(const struct hast_resource *res, struct nv *nv, void **datap,
88 size_t *sizep, bool *freedatap)
89{
90 unsigned char *newbuf;
91
92 res = res; /* TODO */
93
94 /*
95 * TODO: For now we emulate compression.

--- 31 unchanged lines hidden (view full) ---

127 * It is not critical for compression to succeed.
128 */
129 }
130
131 return (0);
132}
133
134static int
90 size_t *sizep, bool *freedatap)
91{
92 unsigned char *newbuf;
93
94 res = res; /* TODO */
95
96 /*
97 * TODO: For now we emulate compression.

--- 31 unchanged lines hidden (view full) ---

129 * It is not critical for compression to succeed.
130 */
131 }
132
133 return (0);
134}
135
136static int
135compression_recv(struct hast_resource *res, struct nv *nv, void **datap,
137compression_recv(const struct hast_resource *res, struct nv *nv, void **datap,
136 size_t *sizep, bool *freedatap)
137{
138 unsigned char *newbuf;
139 const char *algo;
140 size_t origsize;
141
142 res = res; /* TODO */
143

--- 20 unchanged lines hidden (view full) ---

164 *datap = newbuf;
165 *sizep = origsize;
166
167 return (0);
168}
169
170#ifdef HAVE_CRYPTO
171static int
138 size_t *sizep, bool *freedatap)
139{
140 unsigned char *newbuf;
141 const char *algo;
142 size_t origsize;
143
144 res = res; /* TODO */
145

--- 20 unchanged lines hidden (view full) ---

166 *datap = newbuf;
167 *sizep = origsize;
168
169 return (0);
170}
171
172#ifdef HAVE_CRYPTO
173static int
172checksum_send(struct hast_resource *res, struct nv *nv, void **datap,
174checksum_send(const struct hast_resource *res, struct nv *nv, void **datap,
173 size_t *sizep, bool *freedatap __unused)
174{
175 unsigned char hash[SHA256_DIGEST_LENGTH];
176 SHA256_CTX ctx;
177
178 res = res; /* TODO */
179
180 SHA256_Init(&ctx);
181 SHA256_Update(&ctx, *datap, *sizep);
182 SHA256_Final(hash, &ctx);
183
184 nv_add_string(nv, "sha256", "checksum");
185 nv_add_uint8_array(nv, hash, sizeof(hash), "hash");
186
187 return (0);
188}
189
190static int
175 size_t *sizep, bool *freedatap __unused)
176{
177 unsigned char hash[SHA256_DIGEST_LENGTH];
178 SHA256_CTX ctx;
179
180 res = res; /* TODO */
181
182 SHA256_Init(&ctx);
183 SHA256_Update(&ctx, *datap, *sizep);
184 SHA256_Final(hash, &ctx);
185
186 nv_add_string(nv, "sha256", "checksum");
187 nv_add_uint8_array(nv, hash, sizeof(hash), "hash");
188
189 return (0);
190}
191
192static int
191checksum_recv(struct hast_resource *res, struct nv *nv, void **datap,
193checksum_recv(const struct hast_resource *res, struct nv *nv, void **datap,
192 size_t *sizep, bool *freedatap __unused)
193{
194 unsigned char chash[SHA256_DIGEST_LENGTH];
195 const unsigned char *rhash;
196 SHA256_CTX ctx;
197 const char *algo;
198 size_t size;
199

--- 31 unchanged lines hidden (view full) ---

231#endif /* HAVE_CRYPTO */
232
233/*
234 * Send the given nv structure via conn.
235 * We keep headers in nv structure and pass data in separate argument.
236 * There can be no data at all (data is NULL then).
237 */
238int
194 size_t *sizep, bool *freedatap __unused)
195{
196 unsigned char chash[SHA256_DIGEST_LENGTH];
197 const unsigned char *rhash;
198 SHA256_CTX ctx;
199 const char *algo;
200 size_t size;
201

--- 31 unchanged lines hidden (view full) ---

233#endif /* HAVE_CRYPTO */
234
235/*
236 * Send the given nv structure via conn.
237 * We keep headers in nv structure and pass data in separate argument.
238 * There can be no data at all (data is NULL then).
239 */
240int
239hast_proto_send(struct hast_resource *res, struct proto_conn *conn,
241hast_proto_send(const struct hast_resource *res, struct proto_conn *conn,
240 struct nv *nv, const void *data, size_t size)
241{
242 struct hast_main_header hdr;
243 struct ebuf *eb;
244 bool freedata;
245 void *dptr, *hptr;
246 size_t hsize;
247 int ret;

--- 40 unchanged lines hidden (view full) ---

288 ret = 0;
289end:
290 if (freedata)
291 free(dptr);
292 return (ret);
293}
294
295int
242 struct nv *nv, const void *data, size_t size)
243{
244 struct hast_main_header hdr;
245 struct ebuf *eb;
246 bool freedata;
247 void *dptr, *hptr;
248 size_t hsize;
249 int ret;

--- 40 unchanged lines hidden (view full) ---

290 ret = 0;
291end:
292 if (freedata)
293 free(dptr);
294 return (ret);
295}
296
297int
296hast_proto_recv_hdr(struct proto_conn *conn, struct nv **nvp)
298hast_proto_recv_hdr(const struct proto_conn *conn, struct nv **nvp)
297{
298 struct hast_main_header hdr;
299 struct nv *nv;
300 struct ebuf *eb;
301 void *hptr;
302
303 eb = NULL;
304 nv = NULL;

--- 25 unchanged lines hidden (view full) ---

330 return (0);
331fail:
332 if (eb != NULL)
333 ebuf_free(eb);
334 return (-1);
335}
336
337int
299{
300 struct hast_main_header hdr;
301 struct nv *nv;
302 struct ebuf *eb;
303 void *hptr;
304
305 eb = NULL;
306 nv = NULL;

--- 25 unchanged lines hidden (view full) ---

332 return (0);
333fail:
334 if (eb != NULL)
335 ebuf_free(eb);
336 return (-1);
337}
338
339int
338hast_proto_recv_data(struct hast_resource *res, struct proto_conn *conn,
340hast_proto_recv_data(const struct hast_resource *res, struct proto_conn *conn,
339 struct nv *nv, void *data, size_t size)
340{
341 unsigned int ii;
342 bool freedata;
343 size_t dsize;
344 void *dptr;
345 int ret;
346

--- 32 unchanged lines hidden (view full) ---

379end:
380if (ret < 0) printf("%s:%u %s\n", __func__, __LINE__, strerror(errno));
381 if (freedata)
382 free(dptr);
383 return (ret);
384}
385
386int
341 struct nv *nv, void *data, size_t size)
342{
343 unsigned int ii;
344 bool freedata;
345 size_t dsize;
346 void *dptr;
347 int ret;
348

--- 32 unchanged lines hidden (view full) ---

381end:
382if (ret < 0) printf("%s:%u %s\n", __func__, __LINE__, strerror(errno));
383 if (freedata)
384 free(dptr);
385 return (ret);
386}
387
388int
387hast_proto_recv(struct hast_resource *res, struct proto_conn *conn,
389hast_proto_recv(const struct hast_resource *res, struct proto_conn *conn,
388 struct nv **nvp, void *data, size_t size)
389{
390 struct nv *nv;
391 size_t dsize;
392 int ret;
393
394 ret = hast_proto_recv_hdr(conn, &nv);
395 if (ret < 0)

--- 12 unchanged lines hidden ---
390 struct nv **nvp, void *data, size_t size)
391{
392 struct nv *nv;
393 size_t dsize;
394 int ret;
395
396 ret = hast_proto_recv_hdr(conn, &nv);
397 if (ret < 0)

--- 12 unchanged lines hidden ---