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