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 e83f12a6 2023-02-12 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 13b2bc37 2022-10-23 stsp
34 4123af3c 2023-02-12 op #include "got_lib_hash.h"
35 13b2bc37 2022-10-23 stsp
36 13b2bc37 2022-10-23 stsp #include "gotd.h"
37 13b2bc37 2022-10-23 stsp #include "log.h"
38 13b2bc37 2022-10-23 stsp
39 13b2bc37 2022-10-23 stsp void
40 13b2bc37 2022-10-23 stsp gotd_imsg_send_ack(struct got_object_id *id, struct imsgbuf *ibuf,
41 13b2bc37 2022-10-23 stsp uint32_t peerid, pid_t pid)
42 13b2bc37 2022-10-23 stsp {
43 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
44 13b2bc37 2022-10-23 stsp struct gotd_imsg_ack iack;
45 13b2bc37 2022-10-23 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
46 13b2bc37 2022-10-23 stsp
47 13b2bc37 2022-10-23 stsp if (log_getverbose() > 0 &&
48 a41a7d4a 2023-02-12 op got_sha1_digest_to_str(id->hash, hex, sizeof(hex)))
49 13b2bc37 2022-10-23 stsp log_debug("sending ACK for %s", hex);
50 13b2bc37 2022-10-23 stsp
51 13b2bc37 2022-10-23 stsp memset(&iack, 0, sizeof(iack));
52 a41a7d4a 2023-02-12 op memcpy(iack.object_id, id->hash, SHA1_DIGEST_LENGTH);
53 13b2bc37 2022-10-23 stsp
54 13b2bc37 2022-10-23 stsp if (imsg_compose(ibuf, GOTD_IMSG_ACK, peerid, pid, -1,
55 13b2bc37 2022-10-23 stsp &iack, sizeof(iack)) == -1) {
56 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_compose ACK");
57 13b2bc37 2022-10-23 stsp goto done;
58 13b2bc37 2022-10-23 stsp }
59 13b2bc37 2022-10-23 stsp
60 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(ibuf);
61 13b2bc37 2022-10-23 stsp done:
62 13b2bc37 2022-10-23 stsp if (err)
63 13b2bc37 2022-10-23 stsp log_warnx("sending ACK: %s", err->msg);
64 13b2bc37 2022-10-23 stsp }
65 13b2bc37 2022-10-23 stsp
66 13b2bc37 2022-10-23 stsp void
67 13b2bc37 2022-10-23 stsp gotd_imsg_send_nak(struct got_object_id *id, struct imsgbuf *ibuf,
68 13b2bc37 2022-10-23 stsp uint32_t peerid, pid_t pid)
69 13b2bc37 2022-10-23 stsp {
70 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
71 13b2bc37 2022-10-23 stsp struct gotd_imsg_nak inak;
72 13b2bc37 2022-10-23 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
73 13b2bc37 2022-10-23 stsp
74 13b2bc37 2022-10-23 stsp if (log_getverbose() > 0 &&
75 a41a7d4a 2023-02-12 op got_sha1_digest_to_str(id->hash, hex, sizeof(hex)))
76 13b2bc37 2022-10-23 stsp log_debug("sending NAK for %s", hex);
77 13b2bc37 2022-10-23 stsp
78 13b2bc37 2022-10-23 stsp memset(&inak, 0, sizeof(inak));
79 a41a7d4a 2023-02-12 op memcpy(inak.object_id, id->hash, SHA1_DIGEST_LENGTH);
80 13b2bc37 2022-10-23 stsp
81 13b2bc37 2022-10-23 stsp if (imsg_compose(ibuf, GOTD_IMSG_NAK, peerid, pid, -1,
82 13b2bc37 2022-10-23 stsp &inak, sizeof(inak)) == -1) {
83 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_compose NAK");
84 13b2bc37 2022-10-23 stsp goto done;
85 13b2bc37 2022-10-23 stsp }
86 13b2bc37 2022-10-23 stsp
87 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(ibuf);
88 13b2bc37 2022-10-23 stsp done:
89 13b2bc37 2022-10-23 stsp if (err)
90 13b2bc37 2022-10-23 stsp log_warnx("sending NAK: %s", err->msg);
91 13b2bc37 2022-10-23 stsp }