From cfe4051aaffd4ff5e7a1b55b023840042896dbd6 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Thu, 6 Jul 2023 11:00:58 +0000 Subject: Add s6dns_hosts reading functions Signed-off-by: Laurent Bercot --- src/libs6dns/s6dns_hosts_name.c | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/libs6dns/s6dns_hosts_name.c (limited to 'src/libs6dns/s6dns_hosts_name.c') diff --git a/src/libs6dns/s6dns_hosts_name.c b/src/libs6dns/s6dns_hosts_name.c new file mode 100644 index 0000000..af2030b --- /dev/null +++ b/src/libs6dns/s6dns_hosts_name.c @@ -0,0 +1,43 @@ +/* ISC license. */ + +#include +#include + +#include +#include +#include + +#include + +#include + +int s6dns_hosts_name_r (cdb const *c, char const *ip, stralloc *sa, genalloc *ga, int is6) +{ + int r ; + cdb_data data ; + int sawn = !sa->s ; + int gawn = !genalloc_s(size_t, ga) ; + size_t sabase = sa->len ; + size_t gabase = genalloc_len(size_t, ga) ; + size_t i = sabase ; + char tmp[19] = "p4:" ; + if (!c->map) return 0 ; + if (is6) tmp[1] = '6' ; + memcpy(tmp + 3, ip, is6 ? 16 : 4) ; + r = cdb_find(c, &data, tmp, 7) ; + if (r <= 0) return r ; + if (!data.len) return 0 ; + if (data.s[data.len - 1]) return (errno = EPROTO, -1) ; + if (!stralloc_catb(sa, data.s, data.len)) return -1 ; + while (i < sa->len) + { + if (!genalloc_catb(size_t, ga, &i, 1)) goto err ; + i += strlen(sa->s + i) + 1 ; + } + return genalloc_len(size_t, ga) - gabase ; + + err: + if (gawn) genalloc_free(size_t, ga) ; else genalloc_setlen(size_t, ga, 0) ; + if (sawn) stralloc_free(sa) ; else sa->len = 0 ; + return -1 ; +} -- cgit v1.2.3