Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include "compat.h"
19 #include <stdlib.h>
20 #include <string.h>
22 #include "telescope.h"
23 #include "utils.h"
25 void
26 tofu_init(struct ohash *h, unsigned int sz, ptrdiff_t ko)
27 {
28 struct ohash_info info = {
29 .key_offset = ko,
30 .calloc = hash_calloc,
31 .free = hash_free,
32 .alloc = hash_alloc,
33 };
35 ohash_init(h, sz, &info);
36 }
38 struct tofu_entry *
39 tofu_lookup(struct ohash *h, const char *domain, const char *port)
40 {
41 char buf[GEMINI_URL_LEN];
42 unsigned int slot;
44 strlcpy(buf, domain, sizeof(buf));
45 if (port != NULL && *port != '\0' && strcmp(port, "1965")) {
46 strlcat(buf, ":", sizeof(buf));
47 strlcat(buf, port, sizeof(buf));
48 }
50 slot = ohash_qlookup(h, buf);
51 return ohash_find(h, slot);
52 }
54 void
55 tofu_add(struct ohash *h, struct tofu_entry *e)
56 {
57 unsigned int slot;
59 slot = ohash_qlookup(h, e->domain);
60 ohash_insert(h, slot, e);
61 }
63 void
64 tofu_update(struct ohash *h, struct tofu_entry *e)
65 {
66 struct tofu_entry *t;
68 if ((t = tofu_lookup(h, e->domain, NULL)) == NULL)
69 tofu_add(h, e);
70 else {
71 strlcpy(t->hash, e->hash, sizeof(t->hash));
72 t->verified = e->verified;
73 free(e);
74 }
75 }
77 void
78 tofu_temp_trust(struct ohash *h, const char *host, const char *port,
79 const char *hash)
80 {
81 struct tofu_entry *e;
83 if ((e = calloc(1, sizeof(*e))) == NULL)
84 abort();
86 strlcpy(e->domain, host, sizeof(e->domain));
87 if (*port != '\0' && strcmp(port, "1965")) {
88 strlcat(e->domain, ":", sizeof(e->domain));
89 strlcat(e->domain, port, sizeof(e->domain));
90 }
91 strlcpy(e->hash, hash, sizeof(e->hash));
92 e->verified = -1;
94 tofu_update(h, e);
95 }