From e30b215f3410794243ea47ea9d1edccafbf4a6cf Mon Sep 17 00:00:00 2001 From: Kirill Rd Date: Thu, 24 Oct 2024 06:28:53 +0000 Subject: [PATCH] Add caching for evdns (#1717) No evdns will do caching by default (with respect to TTL), to disable this set EVDNS_BASE_NO_CACHE There are also helpers for manually manage the cache: - evdns_cache_write() - evdns_cache_lookup() Initial PR: #571 Fixes: #1715 Co-authored-by: Greg Hazel Co-authored-by: Keith Moore --- CMakeLists.txt | 4 +- Makefile.am | 1 + compat/sys/tree.h | 677 +++++++++++++++++++++++++++++++++++++++++++ evdns.c | 160 +++++++++- evutil.c | 23 ++ include/event2/dns.h | 40 ++- test/regress_dns.c | 133 ++++++++- util-internal.h | 2 + 8 files changed, 1030 insertions(+), 10 deletions(-) create mode 100644 compat/sys/tree.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 5fdc6345..7d31d69b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -466,6 +466,7 @@ else() sys/ioctl.h sys/mman.h sys/queue.h + sys/tree.h sys/select.h sys/sendfile.h sys/uio.h @@ -839,7 +840,8 @@ set(HDR_PRIVATE openssl-compat.h evconfig-private.h sha1.h - compat/sys/queue.h) + compat/sys/queue.h + compat/sys/tree.h) set(HDR_COMPAT include/evdns.h diff --git a/Makefile.am b/Makefile.am index 9ce8bdd3..75a58a67 100644 --- a/Makefile.am +++ b/Makefile.am @@ -323,6 +323,7 @@ noinst_HEADERS += \ bufferevent-internal.h \ changelist-internal.h \ compat/sys/queue.h \ + compat/sys/tree.h \ defer-internal.h \ epolltable-internal.h \ evbuffer-internal.h \ diff --git a/compat/sys/tree.h b/compat/sys/tree.h new file mode 100644 index 00000000..6a3381ca --- /dev/null +++ b/compat/sys/tree.h @@ -0,0 +1,677 @@ +/* $OpenBSD: tree.h,v 1.7 2002/10/17 21:51:54 art Exp $ */ +/* + * Copyright 2002 Niels Provos + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _SYS_TREE_H_ +#define _SYS_TREE_H_ + +/* + * This file defines data structures for different types of trees: + * splay trees and red-black trees. + * + * A splay tree is a self-organizing data structure. Every operation + * on the tree causes a splay to happen. The splay moves the requested + * node to the root of the tree and partly rebalances it. + * + * This has the benefit that request locality causes faster lookups as + * the requested nodes move to the top of the tree. On the other hand, + * every lookup causes memory writes. + * + * The Balance Theorem bounds the total access time for m operations + * and n inserts on an initially empty tree as O((m + n)lg n). The + * amortized cost for a sequence of m accesses to a splay tree is O(lg n); + * + * A red-black tree is a binary search tree with the node color as an + * extra attribute. It fulfills a set of conditions: + * - every search path from the root to a leaf consists of the + * same number of black nodes, + * - each red node (except for the root) has a black parent, + * - each leaf node is black. + * + * Every operation on a red-black tree is bounded as O(lg n). + * The maximum height of a red-black tree is 2lg (n+1). + */ + +#define SPLAY_HEAD(name, type) \ +struct name { \ + struct type *sph_root; /* root of the tree */ \ +} + +#define SPLAY_INITIALIZER(root) \ + { NULL } + +#define SPLAY_INIT(root) do { \ + (root)->sph_root = NULL; \ +} while (0) + +#define SPLAY_ENTRY(type) \ +struct { \ + struct type *spe_left; /* left element */ \ + struct type *spe_right; /* right element */ \ +} + +#define SPLAY_LEFT(elm, field) (elm)->field.spe_left +#define SPLAY_RIGHT(elm, field) (elm)->field.spe_right +#define SPLAY_ROOT(head) (head)->sph_root +#define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == NULL) + +/* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */ +#define SPLAY_ROTATE_RIGHT(head, tmp, field) do { \ + SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \ + SPLAY_RIGHT(tmp, field) = (head)->sph_root; \ + (head)->sph_root = tmp; \ +} while (0) + +#define SPLAY_ROTATE_LEFT(head, tmp, field) do { \ + SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \ + SPLAY_LEFT(tmp, field) = (head)->sph_root; \ + (head)->sph_root = tmp; \ +} while (0) + +#define SPLAY_LINKLEFT(head, tmp, field) do { \ + SPLAY_LEFT(tmp, field) = (head)->sph_root; \ + tmp = (head)->sph_root; \ + (head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \ +} while (0) + +#define SPLAY_LINKRIGHT(head, tmp, field) do { \ + SPLAY_RIGHT(tmp, field) = (head)->sph_root; \ + tmp = (head)->sph_root; \ + (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \ +} while (0) + +#define SPLAY_ASSEMBLE(head, node, left, right, field) do { \ + SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \ + SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\ + SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \ + SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \ +} while (0) + +/* Generates prototypes and inline functions */ + +#define SPLAY_PROTOTYPE(name, type, field, cmp) \ +void name##_SPLAY(struct name *, struct type *); \ +void name##_SPLAY_MINMAX(struct name *, int); \ +struct type *name##_SPLAY_INSERT(struct name *, struct type *); \ +struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \ + \ +/* Finds the node with the same key as elm */ \ +static __inline struct type * \ +name##_SPLAY_FIND(struct name *head, struct type *elm) \ +{ \ + if (SPLAY_EMPTY(head)) \ + return(NULL); \ + name##_SPLAY(head, elm); \ + if ((cmp)(elm, (head)->sph_root) == 0) \ + return (head->sph_root); \ + return (NULL); \ +} \ + \ +static __inline struct type * \ +name##_SPLAY_NEXT(struct name *head, struct type *elm) \ +{ \ + name##_SPLAY(head, elm); \ + if (SPLAY_RIGHT(elm, field) != NULL) { \ + elm = SPLAY_RIGHT(elm, field); \ + while (SPLAY_LEFT(elm, field) != NULL) { \ + elm = SPLAY_LEFT(elm, field); \ + } \ + } else \ + elm = NULL; \ + return (elm); \ +} \ + \ +static __inline struct type * \ +name##_SPLAY_MIN_MAX(struct name *head, int val) \ +{ \ + name##_SPLAY_MINMAX(head, val); \ + return (SPLAY_ROOT(head)); \ +} + +/* Main splay operation. + * Moves node close to the key of elm to top + */ +#define SPLAY_GENERATE(name, type, field, cmp) \ +struct type * \ +name##_SPLAY_INSERT(struct name *head, struct type *elm) \ +{ \ + if (SPLAY_EMPTY(head)) { \ + SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL; \ + } else { \ + int __comp; \ + name##_SPLAY(head, elm); \ + __comp = (cmp)(elm, (head)->sph_root); \ + if(__comp < 0) { \ + SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\ + SPLAY_RIGHT(elm, field) = (head)->sph_root; \ + SPLAY_LEFT((head)->sph_root, field) = NULL; \ + } else if (__comp > 0) { \ + SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\ + SPLAY_LEFT(elm, field) = (head)->sph_root; \ + SPLAY_RIGHT((head)->sph_root, field) = NULL; \ + } else \ + return ((head)->sph_root); \ + } \ + (head)->sph_root = (elm); \ + return (NULL); \ +} \ + \ +struct type * \ +name##_SPLAY_REMOVE(struct name *head, struct type *elm) \ +{ \ + struct type *__tmp; \ + if (SPLAY_EMPTY(head)) \ + return (NULL); \ + name##_SPLAY(head, elm); \ + if ((cmp)(elm, (head)->sph_root) == 0) { \ + if (SPLAY_LEFT((head)->sph_root, field) == NULL) { \ + (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\ + } else { \ + __tmp = SPLAY_RIGHT((head)->sph_root, field); \ + (head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\ + name##_SPLAY(head, elm); \ + SPLAY_RIGHT((head)->sph_root, field) = __tmp; \ + } \ + return (elm); \ + } \ + return (NULL); \ +} \ + \ +void \ +name##_SPLAY(struct name *head, struct type *elm) \ +{ \ + struct type __node, *__left, *__right, *__tmp; \ + int __comp; \ +\ + SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\ + __left = __right = &__node; \ +\ + while ((__comp = (cmp)(elm, (head)->sph_root))) { \ + if (__comp < 0) { \ + __tmp = SPLAY_LEFT((head)->sph_root, field); \ + if (__tmp == NULL) \ + break; \ + if ((cmp)(elm, __tmp) < 0){ \ + SPLAY_ROTATE_RIGHT(head, __tmp, field); \ + if (SPLAY_LEFT((head)->sph_root, field) == NULL)\ + break; \ + } \ + SPLAY_LINKLEFT(head, __right, field); \ + } else if (__comp > 0) { \ + __tmp = SPLAY_RIGHT((head)->sph_root, field); \ + if (__tmp == NULL) \ + break; \ + if ((cmp)(elm, __tmp) > 0){ \ + SPLAY_ROTATE_LEFT(head, __tmp, field); \ + if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\ + break; \ + } \ + SPLAY_LINKRIGHT(head, __left, field); \ + } \ + } \ + SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \ +} \ + \ +/* Splay with either the minimum or the maximum element \ + * Used to find minimum or maximum element in tree. \ + */ \ +void name##_SPLAY_MINMAX(struct name *head, int __comp) \ +{ \ + struct type __node, *__left, *__right, *__tmp; \ +\ + SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\ + __left = __right = &__node; \ +\ + while (1) { \ + if (__comp < 0) { \ + __tmp = SPLAY_LEFT((head)->sph_root, field); \ + if (__tmp == NULL) \ + break; \ + if (__comp < 0){ \ + SPLAY_ROTATE_RIGHT(head, __tmp, field); \ + if (SPLAY_LEFT((head)->sph_root, field) == NULL)\ + break; \ + } \ + SPLAY_LINKLEFT(head, __right, field); \ + } else if (__comp > 0) { \ + __tmp = SPLAY_RIGHT((head)->sph_root, field); \ + if (__tmp == NULL) \ + break; \ + if (__comp > 0) { \ + SPLAY_ROTATE_LEFT(head, __tmp, field); \ + if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\ + break; \ + } \ + SPLAY_LINKRIGHT(head, __left, field); \ + } \ + } \ + SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \ +} + +#define SPLAY_NEGINF -1 +#define SPLAY_INF 1 + +#define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y) +#define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y) +#define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y) +#define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y) +#define SPLAY_MIN(name, x) (SPLAY_EMPTY(x) ? NULL \ + : name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF)) +#define SPLAY_MAX(name, x) (SPLAY_EMPTY(x) ? NULL \ + : name##_SPLAY_MIN_MAX(x, SPLAY_INF)) + +#define SPLAY_FOREACH(x, name, head) \ + for ((x) = SPLAY_MIN(name, head); \ + (x) != NULL; \ + (x) = SPLAY_NEXT(name, head, x)) + +/* Macros that define a red-back tree */ +#define RB_HEAD(name, type) \ +struct name { \ + struct type *rbh_root; /* root of the tree */ \ +} + +#define RB_INITIALIZER(root) \ + { NULL } + +#define RB_INIT(root) do { \ + (root)->rbh_root = NULL; \ +} while (0) + +#define RB_BLACK 0 +#define RB_RED 1 +#define RB_ENTRY(type) \ +struct { \ + struct type *rbe_left; /* left element */ \ + struct type *rbe_right; /* right element */ \ + struct type *rbe_parent; /* parent element */ \ + int rbe_color; /* node color */ \ +} + +#define RB_LEFT(elm, field) (elm)->field.rbe_left +#define RB_RIGHT(elm, field) (elm)->field.rbe_right +#define RB_PARENT(elm, field) (elm)->field.rbe_parent +#define RB_COLOR(elm, field) (elm)->field.rbe_color +#define RB_ROOT(head) (head)->rbh_root +#define RB_EMPTY(head) (RB_ROOT(head) == NULL) + +#define RB_SET(elm, parent, field) do { \ + RB_PARENT(elm, field) = parent; \ + RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \ + RB_COLOR(elm, field) = RB_RED; \ +} while (0) + +#define RB_SET_BLACKRED(black, red, field) do { \ + RB_COLOR(black, field) = RB_BLACK; \ + RB_COLOR(red, field) = RB_RED; \ +} while (0) + +#ifndef RB_AUGMENT +#define RB_AUGMENT(x) +#endif + +#define RB_ROTATE_LEFT(head, elm, tmp, field) do { \ + (tmp) = RB_RIGHT(elm, field); \ + if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field))) { \ + RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \ + } \ + RB_AUGMENT(elm); \ + if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \ + if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \ + RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \ + else \ + RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \ + } else \ + (head)->rbh_root = (tmp); \ + RB_LEFT(tmp, field) = (elm); \ + RB_PARENT(elm, field) = (tmp); \ + RB_AUGMENT(tmp); \ + if ((RB_PARENT(tmp, field))) \ + RB_AUGMENT(RB_PARENT(tmp, field)); \ +} while (0) + +#define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \ + (tmp) = RB_LEFT(elm, field); \ + if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field))) { \ + RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \ + } \ + RB_AUGMENT(elm); \ + if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \ + if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \ + RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \ + else \ + RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \ + } else \ + (head)->rbh_root = (tmp); \ + RB_RIGHT(tmp, field) = (elm); \ + RB_PARENT(elm, field) = (tmp); \ + RB_AUGMENT(tmp); \ + if ((RB_PARENT(tmp, field))) \ + RB_AUGMENT(RB_PARENT(tmp, field)); \ +} while (0) + +/* Generates prototypes and inline functions */ +#define RB_PROTOTYPE(name, type, field, cmp) \ +void name##_RB_INSERT_COLOR(struct name *, struct type *); \ +void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *);\ +struct type *name##_RB_REMOVE(struct name *, struct type *); \ +struct type *name##_RB_INSERT(struct name *, struct type *); \ +struct type *name##_RB_FIND(struct name *, struct type *); \ +struct type *name##_RB_NEXT(struct type *); \ +struct type *name##_RB_MINMAX(struct name *, int); \ + \ + +/* Main rb operation. + * Moves node close to the key of elm to top + */ +#define RB_GENERATE(name, type, field, cmp) \ +void \ +name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \ +{ \ + struct type *parent, *gparent, *tmp; \ + while ((parent = RB_PARENT(elm, field)) && \ + RB_COLOR(parent, field) == RB_RED) { \ + gparent = RB_PARENT(parent, field); \ + if (parent == RB_LEFT(gparent, field)) { \ + tmp = RB_RIGHT(gparent, field); \ + if (tmp && RB_COLOR(tmp, field) == RB_RED) { \ + RB_COLOR(tmp, field) = RB_BLACK; \ + RB_SET_BLACKRED(parent, gparent, field);\ + elm = gparent; \ + continue; \ + } \ + if (RB_RIGHT(parent, field) == elm) { \ + RB_ROTATE_LEFT(head, parent, tmp, field);\ + tmp = parent; \ + parent = elm; \ + elm = tmp; \ + } \ + RB_SET_BLACKRED(parent, gparent, field); \ + RB_ROTATE_RIGHT(head, gparent, tmp, field); \ + } else { \ + tmp = RB_LEFT(gparent, field); \ + if (tmp && RB_COLOR(tmp, field) == RB_RED) { \ + RB_COLOR(tmp, field) = RB_BLACK; \ + RB_SET_BLACKRED(parent, gparent, field);\ + elm = gparent; \ + continue; \ + } \ + if (RB_LEFT(parent, field) == elm) { \ + RB_ROTATE_RIGHT(head, parent, tmp, field);\ + tmp = parent; \ + parent = elm; \ + elm = tmp; \ + } \ + RB_SET_BLACKRED(parent, gparent, field); \ + RB_ROTATE_LEFT(head, gparent, tmp, field); \ + } \ + } \ + RB_COLOR(head->rbh_root, field) = RB_BLACK; \ +} \ + \ +void \ +name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \ +{ \ + struct type *tmp; \ + while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \ + elm != RB_ROOT(head)) { \ + if (RB_LEFT(parent, field) == elm) { \ + tmp = RB_RIGHT(parent, field); \ + if (RB_COLOR(tmp, field) == RB_RED) { \ + RB_SET_BLACKRED(tmp, parent, field); \ + RB_ROTATE_LEFT(head, parent, tmp, field);\ + tmp = RB_RIGHT(parent, field); \ + } \ + if ((RB_LEFT(tmp, field) == NULL || \ + RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\ + (RB_RIGHT(tmp, field) == NULL || \ + RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\ + RB_COLOR(tmp, field) = RB_RED; \ + elm = parent; \ + parent = RB_PARENT(elm, field); \ + } else { \ + if (RB_RIGHT(tmp, field) == NULL || \ + RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\ + struct type *oleft; \ + if ((oleft = RB_LEFT(tmp, field)))\ + RB_COLOR(oleft, field) = RB_BLACK;\ + RB_COLOR(tmp, field) = RB_RED; \ + RB_ROTATE_RIGHT(head, tmp, oleft, field);\ + tmp = RB_RIGHT(parent, field); \ + } \ + RB_COLOR(tmp, field) = RB_COLOR(parent, field);\ + RB_COLOR(parent, field) = RB_BLACK; \ + if (RB_RIGHT(tmp, field)) \ + RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\ + RB_ROTATE_LEFT(head, parent, tmp, field);\ + elm = RB_ROOT(head); \ + break; \ + } \ + } else { \ + tmp = RB_LEFT(parent, field); \ + if (RB_COLOR(tmp, field) == RB_RED) { \ + RB_SET_BLACKRED(tmp, parent, field); \ + RB_ROTATE_RIGHT(head, parent, tmp, field);\ + tmp = RB_LEFT(parent, field); \ + } \ + if ((RB_LEFT(tmp, field) == NULL || \ + RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\ + (RB_RIGHT(tmp, field) == NULL || \ + RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\ + RB_COLOR(tmp, field) = RB_RED; \ + elm = parent; \ + parent = RB_PARENT(elm, field); \ + } else { \ + if (RB_LEFT(tmp, field) == NULL || \ + RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\ + struct type *oright; \ + if ((oright = RB_RIGHT(tmp, field)))\ + RB_COLOR(oright, field) = RB_BLACK;\ + RB_COLOR(tmp, field) = RB_RED; \ + RB_ROTATE_LEFT(head, tmp, oright, field);\ + tmp = RB_LEFT(parent, field); \ + } \ + RB_COLOR(tmp, field) = RB_COLOR(parent, field);\ + RB_COLOR(parent, field) = RB_BLACK; \ + if (RB_LEFT(tmp, field)) \ + RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\ + RB_ROTATE_RIGHT(head, parent, tmp, field);\ + elm = RB_ROOT(head); \ + break; \ + } \ + } \ + } \ + if (elm) \ + RB_COLOR(elm, field) = RB_BLACK; \ +} \ + \ +struct type * \ +name##_RB_REMOVE(struct name *head, struct type *elm) \ +{ \ + struct type *child, *parent, *old = elm; \ + int color; \ + if (RB_LEFT(elm, field) == NULL) \ + child = RB_RIGHT(elm, field); \ + else if (RB_RIGHT(elm, field) == NULL) \ + child = RB_LEFT(elm, field); \ + else { \ + struct type *left; \ + elm = RB_RIGHT(elm, field); \ + while ((left = RB_LEFT(elm, field))) \ + elm = left; \ + child = RB_RIGHT(elm, field); \ + parent = RB_PARENT(elm, field); \ + color = RB_COLOR(elm, field); \ + if (child) \ + RB_PARENT(child, field) = parent; \ + if (parent) { \ + if (RB_LEFT(parent, field) == elm) \ + RB_LEFT(parent, field) = child; \ + else \ + RB_RIGHT(parent, field) = child; \ + RB_AUGMENT(parent); \ + } else \ + RB_ROOT(head) = child; \ + if (RB_PARENT(elm, field) == old) \ + parent = elm; \ + (elm)->field = (old)->field; \ + if (RB_PARENT(old, field)) { \ + if (RB_LEFT(RB_PARENT(old, field), field) == old)\ + RB_LEFT(RB_PARENT(old, field), field) = elm;\ + else \ + RB_RIGHT(RB_PARENT(old, field), field) = elm;\ + RB_AUGMENT(RB_PARENT(old, field)); \ + } else \ + RB_ROOT(head) = elm; \ + RB_PARENT(RB_LEFT(old, field), field) = elm; \ + if (RB_RIGHT(old, field)) \ + RB_PARENT(RB_RIGHT(old, field), field) = elm; \ + if (parent) { \ + left = parent; \ + do { \ + RB_AUGMENT(left); \ + } while ((left = RB_PARENT(left, field))); \ + } \ + goto color; \ + } \ + parent = RB_PARENT(elm, field); \ + color = RB_COLOR(elm, field); \ + if (child) \ + RB_PARENT(child, field) = parent; \ + if (parent) { \ + if (RB_LEFT(parent, field) == elm) \ + RB_LEFT(parent, field) = child; \ + else \ + RB_RIGHT(parent, field) = child; \ + RB_AUGMENT(parent); \ + } else \ + RB_ROOT(head) = child; \ +color: \ + if (color == RB_BLACK) \ + name##_RB_REMOVE_COLOR(head, parent, child); \ + return (old); \ +} \ + \ +/* Inserts a node into the RB tree */ \ +struct type * \ +name##_RB_INSERT(struct name *head, struct type *elm) \ +{ \ + struct type *tmp; \ + struct type *parent = NULL; \ + int comp = 0; \ + tmp = RB_ROOT(head); \ + while (tmp) { \ + parent = tmp; \ + comp = (cmp)(elm, parent); \ + if (comp < 0) \ + tmp = RB_LEFT(tmp, field); \ + else if (comp > 0) \ + tmp = RB_RIGHT(tmp, field); \ + else \ + return (tmp); \ + } \ + RB_SET(elm, parent, field); \ + if (parent != NULL) { \ + if (comp < 0) \ + RB_LEFT(parent, field) = elm; \ + else \ + RB_RIGHT(parent, field) = elm; \ + RB_AUGMENT(parent); \ + } else \ + RB_ROOT(head) = elm; \ + name##_RB_INSERT_COLOR(head, elm); \ + return (NULL); \ +} \ + \ +/* Finds the node with the same key as elm */ \ +struct type * \ +name##_RB_FIND(struct name *head, struct type *elm) \ +{ \ + struct type *tmp = RB_ROOT(head); \ + int comp; \ + while (tmp) { \ + comp = cmp(elm, tmp); \ + if (comp < 0) \ + tmp = RB_LEFT(tmp, field); \ + else if (comp > 0) \ + tmp = RB_RIGHT(tmp, field); \ + else \ + return (tmp); \ + } \ + return (NULL); \ +} \ + \ +struct type * \ +name##_RB_NEXT(struct type *elm) \ +{ \ + if (RB_RIGHT(elm, field)) { \ + elm = RB_RIGHT(elm, field); \ + while (RB_LEFT(elm, field)) \ + elm = RB_LEFT(elm, field); \ + } else { \ + if (RB_PARENT(elm, field) && \ + (elm == RB_LEFT(RB_PARENT(elm, field), field))) \ + elm = RB_PARENT(elm, field); \ + else { \ + while (RB_PARENT(elm, field) && \ + (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\ + elm = RB_PARENT(elm, field); \ + elm = RB_PARENT(elm, field); \ + } \ + } \ + return (elm); \ +} \ + \ +struct type * \ +name##_RB_MINMAX(struct name *head, int val) \ +{ \ + struct type *tmp = RB_ROOT(head); \ + struct type *parent = NULL; \ + while (tmp) { \ + parent = tmp; \ + if (val < 0) \ + tmp = RB_LEFT(tmp, field); \ + else \ + tmp = RB_RIGHT(tmp, field); \ + } \ + return (parent); \ +} + +#define RB_NEGINF -1 +#define RB_INF 1 + +#define RB_INSERT(name, x, y) name##_RB_INSERT(x, y) +#define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y) +#define RB_FIND(name, x, y) name##_RB_FIND(x, y) +#define RB_NEXT(name, x, y) name##_RB_NEXT(y) +#define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF) +#define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF) + +#define RB_FOREACH(x, name, head) \ + for ((x) = RB_MIN(name, head); \ + (x) != NULL; \ + (x) = name##_RB_NEXT(x)) + +#endif /* _SYS_TREE_H_ */ diff --git a/evdns.c b/evdns.c index 2f119923..c70b726d 100644 --- a/evdns.c +++ b/evdns.c @@ -74,6 +74,7 @@ #include #include #include +#include #ifdef _WIN32 #include #include @@ -82,6 +83,7 @@ #define _WIN32_IE 0x400 #endif #include +#define strcasecmp strcmpi #endif #include "event2/buffer.h" @@ -422,11 +424,14 @@ struct evdns_base { TAILQ_HEAD(hosts_list, hosts_entry) hostsdb; + SPLAY_HEAD(evdns_tree, evdns_cache) cache_root; + #ifndef EVENT__DISABLE_THREAD_SUPPORT void *lock; #endif int disable_when_inactive; + int disable_cache; /* Maximum timeout between two probe packets * will change `global_nameserver_probe_initial_timeout` @@ -447,6 +452,14 @@ struct hosts_entry { char hostname[1]; }; +struct evdns_cache { + SPLAY_ENTRY(evdns_cache) node; + char *name; + struct evutil_addrinfo *ai; + struct event ev_timeout; + struct evdns_base *base; +}; + static struct evdns_base *current_base = NULL; struct evdns_base * @@ -495,7 +508,7 @@ static void incoming_conn_cb(struct evconnlistener *listener, evutil_socket_t fd static int strtoint(const char *const str); #ifdef EVENT__DISABLE_THREAD_SUPPORT -#define EVDNS_LOCK(base) EVUTIL_NIL_STMT_ +#define EVDNS_LOCK(base) EVUTIL_NIL_CONDITION_(base) #define EVDNS_UNLOCK(base) EVUTIL_NIL_STMT_ #define ASSERT_LOCKED(base) EVUTIL_NIL_STMT_ #else @@ -2026,7 +2039,7 @@ evdns_request_data_build(const struct evdns_base *base, APPEND16(class); if (EDNS_ENABLED(base)) { - /* The OPT pseudo-RR format + /* The OPT pseudo-RR format * (https://tools.ietf.org/html/rfc6891#section-6.1.2) * +------------+--------------+------------------------------+ * | Field Name | Field Type | Description | @@ -4865,11 +4878,13 @@ evdns_base_new(struct event_base *event_base, int flags) base->global_tcp_idle_timeout.tv_sec = CLIENT_IDLE_CONN_TIMEOUT; TAILQ_INIT(&base->hostsdb); + SPLAY_INIT(&base->cache_root); #define EVDNS_BASE_ALL_FLAGS ( \ EVDNS_BASE_INITIALIZE_NAMESERVERS | \ EVDNS_BASE_DISABLE_WHEN_INACTIVE | \ EVDNS_BASE_NAMESERVERS_NO_DEFAULT | \ + EVDNS_BASE_NO_CACHE | \ 0) if (flags & ~EVDNS_BASE_ALL_FLAGS) { @@ -4902,6 +4917,8 @@ evdns_base_new(struct event_base *event_base, int flags) } } + base->disable_cache = flags & EVDNS_BASE_NO_CACHE; + EVDNS_UNLOCK(base); return base; } @@ -4957,6 +4974,25 @@ evdns_nameserver_free(struct nameserver *server) mm_free(server); } +static int +evdns_cache_compare(struct evdns_cache *a, struct evdns_cache *b) +{ + return strcasecmp(a->name, b->name); +} + +SPLAY_PROTOTYPE(evdns_tree, evdns_cache, node, evdns_cache_compare); +SPLAY_GENERATE(evdns_tree, evdns_cache, node, evdns_cache_compare); + +static void +evdns_cache_free(struct evdns_cache *cache) +{ + SPLAY_REMOVE(evdns_tree, &cache->base->cache_root, cache); + mm_free(cache->name); + evtimer_del(&cache->ev_timeout); + evutil_freeaddrinfo(cache->ai); + mm_free(cache); +} + static void evdns_base_free_and_unlock(struct evdns_base *base, int fail_requests) { @@ -5012,6 +5048,10 @@ evdns_base_free_and_unlock(struct evdns_base *base, int fail_requests) mm_free(base->req_heads); + while (!SPLAY_EMPTY(&base->cache_root)) { + evdns_cache_free(SPLAY_ROOT(&base->cache_root)); + } + EVDNS_UNLOCK(base); EVTHREAD_FREE_LOCK(base->lock, EVTHREAD_LOCKTYPE_RECURSIVE); @@ -5165,6 +5205,8 @@ struct evdns_getaddrinfo_request { /* Copy of the modified 'hints' data that we'll use to build * answers. */ struct evutil_addrinfo hints; + /* The original requested nodename */ + char *nodename; /* The callback to invoke when we're done */ evdns_getaddrinfo_cb user_cb; /* User-supplied data to give to the callback. */ @@ -5182,6 +5224,7 @@ struct evdns_getaddrinfo_request { /* If we have one request answered and one request still inflight, * then this field holds the answer from the first request... */ struct evutil_addrinfo *pending_result; + int pending_result_ttl; /* And this event is a timeout that will tell us to cancel the second * request if it's taking a long time. */ struct event timeout; @@ -5229,6 +5272,7 @@ free_getaddrinfo_request(struct evdns_getaddrinfo_request *data) if (data->cname_result) mm_free(data->cname_result); event_del(&data->timeout); + mm_free(data->nodename); mm_free(data); return; } @@ -5313,6 +5357,98 @@ evdns_result_is_answer(int result) result != DNS_ERR_SERVERFAILED && result != DNS_ERR_CANCEL); } +static void +evdns_ttl_expired(evutil_socket_t fd, short what, void *arg) +{ + struct evdns_cache *cache = arg; + struct evdns_base *base = cache->base; + log(EVDNS_LOG_DEBUG, "Expiring cache for %s", cache->name); + EVDNS_LOCK(base); + evdns_cache_free(cache); + EVDNS_UNLOCK(base); +} + +void +evdns_cache_write(struct evdns_base *dns_base, char *nodename, struct evutil_addrinfo *res, int ttl) +{ + struct timeval tv; + struct evdns_cache *cache; + struct evdns_cache find; + + log(EVDNS_LOG_DEBUG, "Writing cache for %s", nodename); + EVDNS_LOCK(dns_base); + find.name = (char *)nodename; + cache = SPLAY_FIND(evdns_tree, &dns_base->cache_root, &find); + if (cache) { + log(EVDNS_LOG_DEBUG, "Ejecting old cache for %s", nodename); + evdns_cache_free(cache); + } + cache = mm_malloc(sizeof(struct evdns_cache)); + cache->base = dns_base; + cache->name = strdup(nodename); + cache->ai = evutil_dup_addrinfo_(res); + SPLAY_INSERT(evdns_tree, &cache->base->cache_root, cache); + evtimer_assign(&cache->ev_timeout, dns_base->event_base, evdns_ttl_expired, cache); + timerclear(&tv); + tv.tv_sec = ttl; + evtimer_add(&cache->ev_timeout, &tv); + EVDNS_UNLOCK(dns_base); +} + +int +evdns_cache_lookup(struct evdns_base *base, + const char *nodename, struct evutil_addrinfo *hints, ev_uint16_t port, + struct evutil_addrinfo **res) +{ + int n_found = 0; + struct evdns_cache *cache; + struct evdns_cache find; + struct evutil_addrinfo *ai = NULL; + int want_cname = hints->ai_flags & EVUTIL_AI_CANONNAME; + int f = hints->ai_family; + + log(EVDNS_LOG_DEBUG, "Looking in cache for %s", nodename); + EVDNS_LOCK(base); + find.name = (char *)nodename; + cache = SPLAY_FIND(evdns_tree, &base->cache_root, &find); + if (cache) { + struct evutil_addrinfo *e = cache->ai; + log(EVDNS_LOG_DEBUG, "Found cache for %s", cache->name); + for (; e; e = e->ai_next) { + struct evutil_addrinfo *ai_new; + // an existing record might not have the canonname + if (want_cname && e->ai_canonname == NULL) + continue; + ++n_found; + if ((e->ai_addr->sa_family == AF_INET && f == PF_INET6) || + (e->ai_addr->sa_family == AF_INET6 && f == PF_INET)) + continue; + ai_new = evutil_new_addrinfo_(e->ai_addr, e->ai_addrlen, hints); + if (want_cname) { + ai_new->ai_canonname = strdup(e->ai_canonname); + } + if (!ai_new) { + n_found = 0; + goto out; + } + sockaddr_setport(ai_new->ai_addr, port); + ai = evutil_addrinfo_append_(ai, ai_new); + } + } + EVDNS_UNLOCK(base); +out: + if (n_found) { + /* Note that we return an empty answer if we found entries for + * this hostname but none were of the right address type. */ + *res = ai; + return 0; + } else { + if (ai) + evutil_freeaddrinfo(ai); + return -1; + } +} + static void evdns_getaddrinfo_gotresolve(int result, char type, int count, int ttl, void *addresses, void *arg) @@ -5404,6 +5540,9 @@ evdns_getaddrinfo_gotresolve(int result, char type, int count, /* If we have an answer waiting, and we weren't * canceled, ignore this error. */ add_cname_to_reply(data, data->pending_result); + if (data->evdns_base && !data->evdns_base->disable_cache) { + evdns_cache_write(data->evdns_base, data->nodename, data->pending_result, data->pending_result_ttl); + } data->user_cb(0, data->pending_result, data->user_data); data->pending_result = NULL; } else { @@ -5481,10 +5620,12 @@ evdns_getaddrinfo_gotresolve(int result, char type, int count, /* XXXX handle failure from set_timeout */ evdns_getaddrinfo_set_timeout(data->evdns_base, data); data->pending_result = res; + data->pending_result_ttl = ttl; return; } else { /* The other request is done or never started; append its * results (if any) and return them. */ + int res_ttl = ttl; if (data->pending_result) { if (req->type == DNS_IPv4_A) res = evutil_addrinfo_append_(res, @@ -5492,11 +5633,15 @@ evdns_getaddrinfo_gotresolve(int result, char type, int count, else res = evutil_addrinfo_append_( data->pending_result, res); + res_ttl = data->pending_result_ttl; data->pending_result = NULL; } /* Call the user callback. */ add_cname_to_reply(data, res); + if (data->evdns_base && !data->evdns_base->disable_cache) { + evdns_cache_write(data->evdns_base, data->nodename, res, res_ttl); + } data->user_cb(0, res, data->user_data); /* Free data. */ @@ -5529,7 +5674,7 @@ evdns_getaddrinfo_fromhosts(struct evdns_base *base, { int n_found = 0; struct hosts_entry *e; - struct evutil_addrinfo *ai=NULL; + struct evutil_addrinfo *ai = NULL; int f = hints->ai_family; EVDNS_LOCK(base); @@ -5621,6 +5766,12 @@ evdns_getaddrinfo(struct evdns_base *dns_base, return NULL; } + /* See if we have it in the cache */ + if (!dns_base->disable_cache && !evdns_cache_lookup(dns_base, nodename, &hints, port, &res)) { + cb(0, res, arg); + return NULL; + } + /* Okay, things are serious now. We're going to need to actually * launch a request. */ @@ -5637,6 +5788,7 @@ evdns_getaddrinfo(struct evdns_base *dns_base, data->user_cb = cb; data->user_data = arg; data->evdns_base = dns_base; + data->nodename = strdup(nodename); want_cname = (hints.ai_flags & EVUTIL_AI_CANONNAME); @@ -5688,7 +5840,7 @@ evdns_getaddrinfo(struct evdns_base *dns_base, if (started) { return data; } else { - mm_free(data); + free_getaddrinfo_request(data); cb(EVUTIL_EAI_FAIL, NULL, arg); return NULL; } diff --git a/evutil.c b/evutil.c index 72b87ce9..8b87ed8c 100644 --- a/evutil.c +++ b/evutil.c @@ -1778,6 +1778,29 @@ evutil_freeaddrinfo(struct evutil_addrinfo *ai) #endif } +struct evutil_addrinfo * +evutil_dup_addrinfo_(struct evutil_addrinfo *ai) +{ + struct evutil_addrinfo *first = NULL; + struct evutil_addrinfo *prev = NULL; + for (; ai; ai = ai->ai_next) { + int len = sizeof(struct evutil_addrinfo) + ai->ai_addrlen; + struct evutil_addrinfo *n = calloc(1, len); + memcpy(n, ai, len); + if (ai->ai_canonname) { + n->ai_canonname = strdup(ai->ai_canonname); + } + n->ai_addr = (struct sockaddr*)(((char*)n) + sizeof(struct evutil_addrinfo)); + if (!first) { + first = n; + } else { + prev->ai_next = n; + } + prev = n; + } + return first; +} + static evdns_getaddrinfo_fn evdns_getaddrinfo_impl = NULL; static evdns_getaddrinfo_cancel_fn evdns_getaddrinfo_cancel_impl = NULL; diff --git a/include/event2/dns.h b/include/event2/dns.h index 2494324c..73af0ac4 100644 --- a/include/event2/dns.h +++ b/include/event2/dns.h @@ -253,6 +253,9 @@ struct event_base; /** Flag for evdns_base_new: process resolv.conf. */ #define EVDNS_BASE_INITIALIZE_NAMESERVERS 1 +/** Flag for evdns_base_new: disable caching of DNS responses by default + * for async resolver. */ +#define EVDNS_BASE_NO_CACHE 0x10 /** Flag for evdns_base_new: Do not prevent the libevent event loop from * exiting when we have no active dns requests. */ #define EVDNS_BASE_DISABLE_WHEN_INACTIVE 0x8000 @@ -285,7 +288,8 @@ struct event_base; @param event_base the event base to associate the dns client with @param flags any of EVDNS_BASE_INITIALIZE_NAMESERVERS| - EVDNS_BASE_DISABLE_WHEN_INACTIVE|EVDNS_BASE_NAMESERVERS_NO_DEFAULT + EVDNS_BASE_DISABLE_WHEN_INACTIVE|EVDNS_BASE_NAMESERVERS_NO_DEFAULT| + EVDNS_BASE_NO_CACHE @return evdns_base object if successful, or NULL if an error occurred. @see evdns_base_free() */ @@ -317,6 +321,29 @@ void evdns_base_free(struct evdns_base *base, int fail_requests); EVENT2_EXPORT_SYMBOL void evdns_base_clear_host_addresses(struct evdns_base *base); +/** + Write an entry to the evdns cache + + @param dns_base the evdns base to add the entry to + @param nodename the DNS name + @param res the address information associated with the DNS name + @param ttl the time to live associated with the address information + */ +EVENT2_EXPORT_SYMBOL +void evdns_cache_write(struct evdns_base *dns_base, char *nodename, struct evutil_addrinfo *res, int ttl); + +/** + Lookup an entry from the evdns cache + + @param base the evdns base associated with the cache + @param nodename the DNS name for which information is being sought + @param hints see man getaddrinfo() + @param port used to fill in port numbers in the resulting address list + @param res pointer to an evutil_addrinfo struct for result + */ +EVENT2_EXPORT_SYMBOL +int evdns_cache_lookup(struct evdns_base *base, const char *nodename, struct evutil_addrinfo *hints, ev_uint16_t port, struct evutil_addrinfo **res); + /** Convert a DNS error code to a string. @@ -776,8 +803,9 @@ struct evdns_getaddrinfo_request; /** Make a non-blocking getaddrinfo request using the dns_base in 'dns_base'. * * If we can answer the request immediately (with an error or not!), then we - * invoke cb immediately and return NULL. Otherwise we return - * an evdns_getaddrinfo_request and invoke cb later. + * invoke cb immediately and return NULL. This can happen e.g. if the + * requested address is in the hosts file, or cached, or invalid. Otherwise + * we return an evdns_getaddrinfo_request and invoke cb later. * * When the callback is invoked, we pass as its first argument the error code * that getaddrinfo would return (or 0 for no error). As its second argument, @@ -789,6 +817,12 @@ struct evdns_getaddrinfo_request; * - The AI_V4MAPPED and AI_ALL flags are not currently implemented. * - For ai_socktype, we only handle SOCKTYPE_STREAM, SOCKTYPE_UDP, and 0. * - For ai_protocol, we only handle IPPROTO_TCP, IPPROTO_UDP, and 0. + * - If we cached a response exclusively for a different address type (e.g. + * PF_INET), we will set addrinfo to NULL (e.g. queried with PF_INET6) + * - Cache isn't hit when AI_CANONNAME is set but cached server response + * doesn't contain CNAME. + * - If we can answer immediately (e.g. using hosts file, there is an error + * or when cache is hit), we invoke the call back and return NULL. */ EVENT2_EXPORT_SYMBOL struct evdns_getaddrinfo_request *evdns_getaddrinfo( diff --git a/test/regress_dns.c b/test/regress_dns.c index 31e945f2..d3919820 100644 --- a/test/regress_dns.c +++ b/test/regress_dns.c @@ -1749,7 +1749,7 @@ test_getaddrinfo_async(void *arg) struct basic_test_data *data = arg; struct evutil_addrinfo hints, *a; struct gai_outcome local_outcome; - struct gai_outcome a_out[13]; + struct gai_outcome a_out[13], b_out[13]; unsigned i; struct evdns_getaddrinfo_request *r; char buf[128]; @@ -1759,6 +1759,7 @@ test_getaddrinfo_async(void *arg) struct evdns_base *dns_base; memset(a_out, 0, sizeof(a_out)); + memset(b_out, 0, sizeof(b_out)); memset(&local_outcome, 0, sizeof(local_outcome)); dns_base = evdns_base_new(data->base, 0); @@ -2026,7 +2027,7 @@ test_getaddrinfo_async(void *arg) */ - n_gai_results_pending = 12; + n_gai_results_pending = 13; exit_base_on_no_pending_results = data->base; event_base_dispatch(data->base); @@ -2120,6 +2121,130 @@ test_getaddrinfo_async(void *arg) test_ai_eq(a_out[12].ai, "18.52.86.120:8000", SOCK_STREAM, IPPROTO_TCP); tt_str_op(a_out[12].ai->ai_canonname, ==, HOST_NAME_MAX_NAME); + /* 3. Let's make sure the results are all cached */ + + n_gai_results_pending = 13; + + /* 0: both.example.com should have been replaced (evicted) in cache with no canonname */ + memset(&hints, 0, sizeof(hints)); + hints.ai_family = PF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = EVUTIL_AI_CANONNAME; + r = evdns_getaddrinfo(dns_base, "both.example.com", "8000", + &hints, gai_cb, &b_out[0]); + tt_assert(r); + + /* 1: v4only.example.com should have been cached, but CNAME was not replied with and the client wants CNAME. */ + // XXX ideally, cache should hit with no CNAME if AI_CANONNAME on the previous call didn't obtain it with this flag. + hints.ai_flags = AI_CANONNAME; + r = evdns_getaddrinfo(dns_base, "v4only.example.com", "8001", + &hints, gai_cb, &b_out[1]); + tt_assert(r); + + /* 2: v6only.example.com should have been cached */ + hints.ai_family = PF_INET6; + hints.ai_flags = 0; + r = evdns_getaddrinfo(dns_base, "v6only.example.com", "8002", + &hints, gai_cb, &b_out[2]); + tt_assert(!r); + // check + tt_int_op(b_out[2].err, ==, 0); + tt_assert(b_out[2].ai); + tt_assert(!b_out[2].ai->ai_next); + test_ai_eq(b_out[2].ai, "[b0b::f00d]:8002", SOCK_STREAM, IPPROTO_TCP); + + /* 2.5: v6only.example.com cache lookup with PF_INET should return NULL addressinfo. */ + hints.ai_family = PF_INET; + hints.ai_flags = 0; + evutil_freeaddrinfo(b_out[2].ai); // since this is reused + ++n_gai_results_pending; + r = evdns_getaddrinfo(dns_base, "v6only.example.com", "8002", + &hints, gai_cb, &b_out[2]); + tt_assert(!r); + // check + tt_int_op(b_out[2].err, ==, 0); + tt_assert(!b_out[2].ai); + + /* 3: v4assert.example.com should have been cached */ + hints.ai_family = PF_INET; + r = evdns_getaddrinfo(dns_base, "v4assert.example.com", "8003", + &hints, gai_cb, &b_out[3]); + tt_assert(!r); + // check + tt_int_op(b_out[3].err, ==, 0); + tt_assert(b_out[3].ai); + tt_assert(!b_out[3].ai->ai_next); + test_ai_eq(b_out[3].ai, "18.52.86.120:8003", SOCK_STREAM, IPPROTO_TCP); + + /* 4: v6assert.example.com should have been cached. */ + hints.ai_family = PF_INET6; + r = evdns_getaddrinfo(dns_base, "v6assert.example.com", "8004", + &hints, gai_cb, &b_out[4]); + tt_assert(!r); + /* check */ + tt_int_op(b_out[4].err, ==, 0); + tt_assert(b_out[4].ai); + tt_assert(!b_out[4].ai->ai_next); + test_ai_eq(b_out[4].ai, "[b0b::f00d]:8004", SOCK_STREAM, IPPROTO_TCP); + + /* 5: NEXIST shouldn't be cached, as it is instant. */ + hints.ai_family = PF_INET; + r = evdns_getaddrinfo(dns_base, "nosuchplace.example.com", "8005", + &hints, gai_cb, &b_out[5]); + tt_assert(r); + + /* 6: NEXIST shouldn't be cached. */ + hints.ai_family = PF_UNSPEC; + r = evdns_getaddrinfo(dns_base, "nosuchplace.example.com", "8006", + &hints, gai_cb, &b_out[6]); + tt_assert(r); + + /* 7: v6timeout.example.com timed out and therefore shouldn't be in cache. */ + hints.ai_family = PF_UNSPEC; + r = evdns_getaddrinfo(dns_base, "v6timeout.example.com", "8007", + &hints, gai_cb, &b_out[7]); + tt_assert(r); + + /* 8: v6timeout-nonexist.example.com produced NEXIST and shouldn't be cached */ + hints.ai_family = PF_UNSPEC; + r = evdns_getaddrinfo(dns_base, "v6timeout-nonexist.example.com", + "8008", &hints, gai_cb, &b_out[8]); + tt_assert(r); + + /* 9: AI_ADDRCONFIG should at least not crash. */ + hints.ai_flags |= EVUTIL_AI_ADDRCONFIG; + r = evdns_getaddrinfo(dns_base, "both.example.com", + "8009", &hints, gai_cb, &b_out[9]); + tt_assert(!r); + + /* 10: v4timeout.example.com shouldn't cache as it didn't succeed. */ + hints.ai_family = PF_UNSPEC; + hints.ai_flags = 0; + r = evdns_getaddrinfo(dns_base, "v4timeout.example.com", "8010", + &hints, gai_cb, &b_out[10]); + tt_assert(r); + + /* 11: timeout.example.com: shouldn't have cached as it was cancelled. */ + r = evdns_getaddrinfo(dns_base, "all-timeout.example.com", "8011", + &hints, gai_cb, &b_out[11]); + tt_assert(r); + + /* 12: HOST_NAME_MAX_NAME should've cached and match the original value */ + hints.ai_family = PF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = EVUTIL_AI_CANONNAME; + r = evdns_getaddrinfo(dns_base, "long.example.com", "8000", + &hints, gai_cb, &b_out[12]); + tt_assert(!r); + //check + tt_int_op(b_out[12].err, ==, 0); + tt_assert(b_out[12].ai); + tt_assert(!b_out[12].ai->ai_next); + test_ai_eq(b_out[12].ai, "18.52.86.120:8000", SOCK_STREAM, IPPROTO_TCP); + tt_str_op(b_out[12].ai->ai_canonname, ==, HOST_NAME_MAX_NAME); + + exit_base_on_no_pending_results = data->base; + event_base_dispatch(data->base); end: if (local_outcome.ai) @@ -2128,6 +2253,10 @@ end: if (a_out[i].ai) evutil_freeaddrinfo(a_out[i].ai); } + for (i = 0; i < ARRAY_SIZE(b_out); ++i) { + if (b_out[i].ai) + evutil_freeaddrinfo(b_out[i].ai); + } if (port) evdns_close_server_port(port); if (dns_base) diff --git a/util-internal.h b/util-internal.h index 8cd08497..5094a710 100644 --- a/util-internal.h +++ b/util-internal.h @@ -429,6 +429,8 @@ EVENT2_EXPORT_SYMBOL struct evutil_addrinfo *evutil_new_addrinfo_(struct sockaddr *sa, ev_socklen_t socklen, const struct evutil_addrinfo *hints); EVENT2_EXPORT_SYMBOL +struct evutil_addrinfo *evutil_dup_addrinfo_(struct evutil_addrinfo *ai); +EVENT2_EXPORT_SYMBOL struct evutil_addrinfo *evutil_addrinfo_append_(struct evutil_addrinfo *first, struct evutil_addrinfo *append); EVENT2_EXPORT_SYMBOL