Blame


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