Blame


1 13b2bc37 2022-10-23 stsp /*
2 13b2bc37 2022-10-23 stsp * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 13b2bc37 2022-10-23 stsp *
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.
7 13b2bc37 2022-10-23 stsp *
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.
15 13b2bc37 2022-10-23 stsp */
16 13b2bc37 2022-10-23 stsp
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>
20 13b2bc37 2022-10-23 stsp
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>
28 13b2bc37 2022-10-23 stsp
29 13b2bc37 2022-10-23 stsp #include <imsg.h>
30 13b2bc37 2022-10-23 stsp
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"
34 13b2bc37 2022-10-23 stsp
35 53bf0b54 2023-02-23 op #include "got_lib_hash.h"
36 13b2bc37 2022-10-23 stsp
37 13b2bc37 2022-10-23 stsp #include "gotd.h"
38 13b2bc37 2022-10-23 stsp #include "log.h"
39 13b2bc37 2022-10-23 stsp
40 13b2bc37 2022-10-23 stsp void
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)
43 13b2bc37 2022-10-23 stsp {
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];
47 13b2bc37 2022-10-23 stsp
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);
51 13b2bc37 2022-10-23 stsp
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);
54 13b2bc37 2022-10-23 stsp
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");
58 13b2bc37 2022-10-23 stsp goto done;
59 13b2bc37 2022-10-23 stsp }
60 13b2bc37 2022-10-23 stsp
61 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(ibuf);
62 13b2bc37 2022-10-23 stsp done:
63 13b2bc37 2022-10-23 stsp if (err)
64 13b2bc37 2022-10-23 stsp log_warnx("sending ACK: %s", err->msg);
65 13b2bc37 2022-10-23 stsp }
66 13b2bc37 2022-10-23 stsp
67 13b2bc37 2022-10-23 stsp void
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)
70 13b2bc37 2022-10-23 stsp {
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];
74 13b2bc37 2022-10-23 stsp
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);
78 13b2bc37 2022-10-23 stsp
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);
81 13b2bc37 2022-10-23 stsp
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");
85 13b2bc37 2022-10-23 stsp goto done;
86 13b2bc37 2022-10-23 stsp }
87 13b2bc37 2022-10-23 stsp
88 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(ibuf);
89 13b2bc37 2022-10-23 stsp done:
90 13b2bc37 2022-10-23 stsp if (err)
91 13b2bc37 2022-10-23 stsp log_warnx("sending NAK: %s", err->msg);
92 13b2bc37 2022-10-23 stsp }