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