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 <stdint.h>
24 #include <stdio.h>
25 #include <string.h>
27 #include <imsg.h>
29 #include "got_error.h"
30 #include "got_object.h"
32 #include "got_lib_hash.h"
34 #include "gotd.h"
35 #include "log.h"
37 void
38 gotd_imsg_send_ack(struct got_object_id *id, struct imsgbuf *ibuf,
39 uint32_t peerid, pid_t pid)
40 {
41 const struct got_error *err = NULL;
42 struct gotd_imsg_ack iack;
43 char hex[SHA1_DIGEST_STRING_LENGTH];
45 if (log_getverbose() > 0 &&
46 got_sha1_digest_to_str(id->sha1, hex, sizeof(hex)))
47 log_debug("sending ACK for %s", hex);
49 memset(&iack, 0, sizeof(iack));
50 memcpy(iack.object_id, id->sha1, SHA1_DIGEST_LENGTH);
52 if (imsg_compose(ibuf, GOTD_IMSG_ACK, peerid, pid, -1,
53 &iack, sizeof(iack)) == -1) {
54 err = got_error_from_errno("imsg_compose ACK");
55 goto done;
56 }
58 err = gotd_imsg_flush(ibuf);
59 done:
60 if (err)
61 log_warnx("sending ACK: %s", err->msg);
62 }
64 void
65 gotd_imsg_send_nak(struct got_object_id *id, struct imsgbuf *ibuf,
66 uint32_t peerid, pid_t pid)
67 {
68 const struct got_error *err = NULL;
69 struct gotd_imsg_nak inak;
70 char hex[SHA1_DIGEST_STRING_LENGTH];
72 if (log_getverbose() > 0 &&
73 got_sha1_digest_to_str(id->sha1, hex, sizeof(hex)))
74 log_debug("sending NAK for %s", hex);
76 memset(&inak, 0, sizeof(inak));
77 memcpy(inak.object_id, id->sha1, SHA1_DIGEST_LENGTH);
79 if (imsg_compose(ibuf, GOTD_IMSG_NAK, peerid, pid, -1,
80 &inak, sizeof(inak)) == -1) {
81 err = got_error_from_errno("imsg_compose NAK");
82 goto done;
83 }
85 err = gotd_imsg_flush(ibuf);
86 done:
87 if (err)
88 log_warnx("sending NAK: %s", err->msg);
89 }