2 cbcc75fb 2021-03-17 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
17 cbcc75fb 2021-03-17 op #include "telescope.h"
19 cbcc75fb 2021-03-17 op #include <stdlib.h>
20 eb4388ee 2021-04-25 op #include <string.h>
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*);
27 cbcc75fb 2021-03-17 op hash_alloc(size_t len, void *d)
29 cbcc75fb 2021-03-17 op if ((d = malloc(len)) == NULL)
35 cbcc75fb 2021-03-17 op hash_calloc(size_t nmemb, size_t size, void *d)
37 cbcc75fb 2021-03-17 op if ((d = calloc(nmemb, size)) == NULL)
43 cbcc75fb 2021-03-17 op hash_free(void *ptr, void *d)
49 cbcc75fb 2021-03-17 op telescope_ohash_init(struct ohash *h, unsigned int sz, ptrdiff_t ko)
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,
58 cbcc75fb 2021-03-17 op ohash_init(h, sz, &info);
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)
64 eb4388ee 2021-04-25 op char buf[GEMINI_URL_LEN];
65 cbcc75fb 2021-03-17 op unsigned int slot;
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));
73 eb4388ee 2021-04-25 op slot = ohash_qlookup(h, buf);
74 cbcc75fb 2021-03-17 op return ohash_find(h, slot);
78 cbcc75fb 2021-03-17 op telescope_ohash_insert(struct ohash *h, struct tofu_entry *e)
80 cbcc75fb 2021-03-17 op unsigned int slot;
82 cbcc75fb 2021-03-17 op slot = ohash_qlookup(h, e->domain);
83 cbcc75fb 2021-03-17 op ohash_insert(h, slot, e);