Blame


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