Merge pull request #1735 from theoden8/minor-improvements

various minor fixes and improvements
This commit is contained in:
Azat Khuzhin 2024-11-02 20:04:49 +03:00 committed by GitHub
commit ffe913b9f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 45 additions and 30 deletions

32
evdns.c
View File

@ -5383,15 +5383,17 @@ evdns_cache_write(struct evdns_base *dns_base, char *nodename, struct evutil_add
log(EVDNS_LOG_DEBUG, "Ejecting old cache for %s", nodename); log(EVDNS_LOG_DEBUG, "Ejecting old cache for %s", nodename);
evdns_cache_free(cache); evdns_cache_free(cache);
} }
cache = mm_malloc(sizeof(struct evdns_cache)); if (res) {
cache = mm_calloc(1, sizeof(struct evdns_cache));
cache->base = dns_base; cache->base = dns_base;
cache->name = strdup(nodename); cache->name = mm_strdup(nodename);
cache->ai = evutil_dup_addrinfo_(res); cache->ai = evutil_dup_addrinfo_(res);
SPLAY_INSERT(evdns_tree, &cache->base->cache_root, cache); SPLAY_INSERT(evdns_tree, &cache->base->cache_root, cache);
evtimer_assign(&cache->ev_timeout, dns_base->event_base, evdns_ttl_expired, cache); evtimer_assign(&cache->ev_timeout, dns_base->event_base, evdns_ttl_expired, cache);
timerclear(&tv); timerclear(&tv);
tv.tv_sec = ttl; tv.tv_sec = ttl;
evtimer_add(&cache->ev_timeout, &tv); evtimer_add(&cache->ev_timeout, &tv);
}
EVDNS_UNLOCK(dns_base); EVDNS_UNLOCK(dns_base);
} }
@ -5425,7 +5427,7 @@ evdns_cache_lookup(struct evdns_base *base,
continue; continue;
ai_new = evutil_new_addrinfo_(e->ai_addr, e->ai_addrlen, hints); ai_new = evutil_new_addrinfo_(e->ai_addr, e->ai_addrlen, hints);
if (want_cname) { if (want_cname) {
ai_new->ai_canonname = strdup(e->ai_canonname); ai_new->ai_canonname = mm_strdup(e->ai_canonname);
} }
if (!ai_new) { if (!ai_new) {
n_found = 0; n_found = 0;
@ -5438,8 +5440,9 @@ evdns_cache_lookup(struct evdns_base *base,
EVDNS_UNLOCK(base); EVDNS_UNLOCK(base);
out: out:
if (n_found) { if (n_found) {
/* Note that we return an empty answer if we found entries for if (!ai) {
* this hostname but none were of the right address type. */ return EVUTIL_EAI_ADDRFAMILY;
}
*res = ai; *res = ai;
return 0; return 0;
} else { } else {
@ -5696,8 +5699,9 @@ evdns_getaddrinfo_fromhosts(struct evdns_base *base,
EVDNS_UNLOCK(base); EVDNS_UNLOCK(base);
out: out:
if (n_found) { if (n_found) {
/* Note that we return an empty answer if we found entries for if (!ai) {
* this hostname but none were of the right address type. */ return EVUTIL_EAI_ADDRFAMILY;
}
*res = ai; *res = ai;
return 0; return 0;
} else { } else {
@ -5761,16 +5765,20 @@ evdns_getaddrinfo(struct evdns_base *dns_base,
} }
/* If there is an entry in the hosts file, we should give it now. */ /* If there is an entry in the hosts file, we should give it now. */
if (!evdns_getaddrinfo_fromhosts(dns_base, nodename, &hints, port, &res)) { err = evdns_getaddrinfo_fromhosts(dns_base, nodename, &hints, port, &res);
cb(0, res, arg); if (!err || err == EVUTIL_EAI_ADDRFAMILY) {
cb(err, res, arg);
return NULL; return NULL;
} }
/* See if we have it in the cache */ /* See if we have it in the cache */
if (!dns_base->disable_cache && !evdns_cache_lookup(dns_base, nodename, &hints, port, &res)) { if (!dns_base->disable_cache) {
cb(0, res, arg); err = evdns_cache_lookup(dns_base, nodename, &hints, port, &res);
if (!err || err == EVUTIL_EAI_ADDRFAMILY) {
cb(err, res, arg);
return NULL; return NULL;
} }
}
/* Okay, things are serious now. We're going to need to actually /* Okay, things are serious now. We're going to need to actually
* launch a request. * launch a request.
@ -5788,7 +5796,7 @@ evdns_getaddrinfo(struct evdns_base *dns_base,
data->user_cb = cb; data->user_cb = cb;
data->user_data = arg; data->user_data = arg;
data->evdns_base = dns_base; data->evdns_base = dns_base;
data->nodename = strdup(nodename); data->nodename = mm_strdup(nodename);
want_cname = (hints.ai_flags & EVUTIL_AI_CANONNAME); want_cname = (hints.ai_flags & EVUTIL_AI_CANONNAME);

View File

@ -1785,10 +1785,10 @@ evutil_dup_addrinfo_(struct evutil_addrinfo *ai)
struct evutil_addrinfo *prev = NULL; struct evutil_addrinfo *prev = NULL;
for (; ai; ai = ai->ai_next) { for (; ai; ai = ai->ai_next) {
int len = sizeof(struct evutil_addrinfo) + ai->ai_addrlen; int len = sizeof(struct evutil_addrinfo) + ai->ai_addrlen;
struct evutil_addrinfo *n = calloc(1, len); struct evutil_addrinfo *n = mm_calloc(1, len);
memcpy(n, ai, len); memcpy(n, ai, len);
if (ai->ai_canonname) { if (ai->ai_canonname) {
n->ai_canonname = strdup(ai->ai_canonname); n->ai_canonname = mm_strdup(ai->ai_canonname);
} }
n->ai_addr = (struct sockaddr*)(((char*)n) + sizeof(struct evutil_addrinfo)); n->ai_addr = (struct sockaddr*)(((char*)n) + sizeof(struct evutil_addrinfo));
if (!first) { if (!first) {

9
http.c
View File

@ -845,6 +845,9 @@ evhttp_connection_fail_(struct evhttp_connection *evcon,
bufferevent_disable(evcon->bufev, EV_READ|EV_WRITE); bufferevent_disable(evcon->bufev, EV_READ|EV_WRITE);
error_cb = req->error_cb;
error_cb_arg = req->cb_arg;
if (evcon->flags & EVHTTP_CON_INCOMING) { if (evcon->flags & EVHTTP_CON_INCOMING) {
/* /*
* for incoming requests, there are two different * for incoming requests, there are two different
@ -856,11 +859,11 @@ evhttp_connection_fail_(struct evhttp_connection *evcon,
*/ */
if (evhttp_connection_incoming_fail(req, error) == -1) if (evhttp_connection_incoming_fail(req, error) == -1)
evhttp_connection_free(evcon); evhttp_connection_free(evcon);
if (error_cb != NULL)
error_cb(error, error_cb_arg);
return; return;
} }
error_cb = req->error_cb;
error_cb_arg = req->cb_arg;
/* when the request was canceled, the callback is not executed */ /* when the request was canceled, the callback is not executed */
if (error != EVREQ_HTTP_REQUEST_CANCEL) { if (error != EVREQ_HTTP_REQUEST_CANCEL) {
/* save the callback for later; the cb might free our object */ /* save the callback for later; the cb might free our object */
@ -5374,7 +5377,7 @@ evhttp_uri_free(struct evhttp_uri *uri)
} }
char * char *
evhttp_uri_join(struct evhttp_uri *uri, char *buf, size_t limit) evhttp_uri_join(const struct evhttp_uri *uri, char *buf, size_t limit)
{ {
struct evbuffer *tmp = 0; struct evbuffer *tmp = 0;
size_t joined_size = 0; size_t joined_size = 0;

View File

@ -819,6 +819,7 @@ struct evdns_getaddrinfo_request;
* - For ai_protocol, we only handle IPPROTO_TCP, IPPROTO_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. * - 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) * PF_INET), we will set addrinfo to NULL (e.g. queried with PF_INET6)
* and return EVUTIL_EAI_ADDRFAMILY.
* - Cache isn't hit when AI_CANONNAME is set but cached server response * - Cache isn't hit when AI_CANONNAME is set but cached server response
* doesn't contain CNAME. * doesn't contain CNAME.
* - If we can answer immediately (e.g. using hosts file, there is an error * - If we can answer immediately (e.g. using hosts file, there is an error

View File

@ -1473,7 +1473,7 @@ void evhttp_uri_free(struct evhttp_uri *uri);
* @see evhttp_uri_parse() * @see evhttp_uri_parse()
*/ */
EVENT2_EXPORT_SYMBOL EVENT2_EXPORT_SYMBOL
char *evhttp_uri_join(struct evhttp_uri *uri, char *buf, size_t limit); char *evhttp_uri_join(const struct evhttp_uri *uri, char *buf, size_t limit);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -2155,7 +2155,7 @@ test_getaddrinfo_async(void *arg)
tt_assert(!b_out[2].ai->ai_next); tt_assert(!b_out[2].ai->ai_next);
test_ai_eq(b_out[2].ai, "[b0b::f00d]:8002", SOCK_STREAM, IPPROTO_TCP); 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. */ /* 2.5: v6only.example.com cache lookup with PF_INET should return EVUTIL_EAI_ADDRFAMILY. */
hints.ai_family = PF_INET; hints.ai_family = PF_INET;
hints.ai_flags = 0; hints.ai_flags = 0;
evutil_freeaddrinfo(b_out[2].ai); // since this is reused evutil_freeaddrinfo(b_out[2].ai); // since this is reused
@ -2164,7 +2164,7 @@ test_getaddrinfo_async(void *arg)
&hints, gai_cb, &b_out[2]); &hints, gai_cb, &b_out[2]);
tt_assert(!r); tt_assert(!r);
// check // check
tt_int_op(b_out[2].err, ==, 0); tt_int_op(b_out[2].err, ==, EVUTIL_EAI_ADDRFAMILY);
tt_assert(!b_out[2].ai); tt_assert(!b_out[2].ai);
/* 3: v4assert.example.com should have been cached */ /* 3: v4assert.example.com should have been cached */
@ -2337,6 +2337,7 @@ gaic_launch(struct event_base *base, struct evdns_base *dns_base, unsigned i)
{ {
struct gaic_request_status *status = calloc(1,sizeof(*status)); struct gaic_request_status *status = calloc(1,sizeof(*status));
struct timeval tv = { 0, 0 }; struct timeval tv = { 0, 0 };
char nodename[256];
/// cancel via timer half of requests /// cancel via timer half of requests
if (i % 2) { if (i % 2) {
@ -2350,8 +2351,9 @@ gaic_launch(struct event_base *base, struct evdns_base *dns_base, unsigned i)
status->dns_base = dns_base; status->dns_base = dns_base;
event_assign(&status->cancel_event, base, -1, 0, gaic_cancel_request_cb, event_assign(&status->cancel_event, base, -1, 0, gaic_cancel_request_cb,
status); status);
snprintf(nodename, sizeof(nodename), "foobar-%u.bazquux.example.com", i);
status->request = evdns_getaddrinfo(dns_base, status->request = evdns_getaddrinfo(dns_base,
"foobar.bazquux.example.com", "80", NULL, gaic_getaddrinfo_cb, nodename, "80", NULL, gaic_getaddrinfo_cb,
status); status);
event_add(&status->cancel_event, &tv); event_add(&status->cancel_event, &tv);
++gaic_pending; ++gaic_pending;
@ -2547,7 +2549,8 @@ test_getaddrinfo_async_cancel_stress(void *ptr)
unsigned i; unsigned i;
base = event_base_new(); base = event_base_new();
dns_base = evdns_base_new(base, 0); /* if we keep hitting cache this test becomes unreliable */
dns_base = evdns_base_new(base, EVDNS_BASE_NO_CACHE);
memset(&sin, 0, sizeof(sin)); memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET; sin.sin_family = AF_INET;