Blob


1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
21 #include <event.h>
22 #include <limits.h>
23 #include <sha1.h>
24 #include <sha2.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <string.h>
29 #include <imsg.h>
31 #include "got_error.h"
32 #include "got_object.h"
34 #include "got_lib_hash.h"
36 #include "gotd.h"
37 #include "log.h"
39 void
40 gotd_imsg_send_ack(struct got_object_id *id, struct imsgbuf *ibuf,
41 uint32_t peerid, pid_t pid)
42 {
43 const struct got_error *err = NULL;
44 struct gotd_imsg_ack iack;
45 char hex[SHA1_DIGEST_STRING_LENGTH];
47 if (log_getverbose() > 0 &&
48 got_sha1_digest_to_str(id->hash, hex, sizeof(hex)))
49 log_debug("sending ACK for %s", hex);
51 memset(&iack, 0, sizeof(iack));
52 memcpy(iack.object_id, id->hash, SHA1_DIGEST_LENGTH);
54 if (imsg_compose(ibuf, GOTD_IMSG_ACK, peerid, pid, -1,
55 &iack, sizeof(iack)) == -1) {
56 err = got_error_from_errno("imsg_compose ACK");
57 goto done;
58 }
60 err = gotd_imsg_flush(ibuf);
61 done:
62 if (err)
63 log_warnx("sending ACK: %s", err->msg);
64 }
66 void
67 gotd_imsg_send_nak(struct got_object_id *id, struct imsgbuf *ibuf,
68 uint32_t peerid, pid_t pid)
69 {
70 const struct got_error *err = NULL;
71 struct gotd_imsg_nak inak;
72 char hex[SHA1_DIGEST_STRING_LENGTH];
74 if (log_getverbose() > 0 &&
75 got_sha1_digest_to_str(id->hash, hex, sizeof(hex)))
76 log_debug("sending NAK for %s", hex);
78 memset(&inak, 0, sizeof(inak));
79 memcpy(inak.object_id, id->hash, SHA1_DIGEST_LENGTH);
81 if (imsg_compose(ibuf, GOTD_IMSG_NAK, peerid, pid, -1,
82 &inak, sizeof(inak)) == -1) {
83 err = got_error_from_errno("imsg_compose NAK");
84 goto done;
85 }
87 err = gotd_imsg_flush(ibuf);
88 done:
89 if (err)
90 log_warnx("sending NAK: %s", err->msg);
91 }