make dns lookups (and thus getaddrinfo) cancellable
This commit is contained in:
parent
bf027ca5e0
commit
12d547b262
1 changed files with 11 additions and 4 deletions
|
|
@ -19,6 +19,11 @@
|
||||||
#define PACKET_MAX 512
|
#define PACKET_MAX 512
|
||||||
#define PTR_MAX (64 + sizeof ".in-addr.arpa")
|
#define PTR_MAX (64 + sizeof ".in-addr.arpa")
|
||||||
|
|
||||||
|
static void cleanup(void *p)
|
||||||
|
{
|
||||||
|
close((intptr_t)p);
|
||||||
|
}
|
||||||
|
|
||||||
int __dns_doqueries(unsigned char *dest, const char *name, int *rr, int rrcnt)
|
int __dns_doqueries(unsigned char *dest, const char *name, int *rr, int rrcnt)
|
||||||
{
|
{
|
||||||
time_t t0 = time(0);
|
time_t t0 = time(0);
|
||||||
|
|
@ -44,6 +49,8 @@ int __dns_doqueries(unsigned char *dest, const char *name, int *rr, int rrcnt)
|
||||||
int id;
|
int id;
|
||||||
int cs;
|
int cs;
|
||||||
|
|
||||||
|
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
|
||||||
|
|
||||||
/* Construct query template - RR and ID will be filled later */
|
/* Construct query template - RR and ID will be filled later */
|
||||||
if (strlen(name)-1 >= 254U) return EAI_NONAME;
|
if (strlen(name)-1 >= 254U) return EAI_NONAME;
|
||||||
q[2] = q[5] = 1;
|
q[2] = q[5] = 1;
|
||||||
|
|
@ -81,8 +88,6 @@ int __dns_doqueries(unsigned char *dest, const char *name, int *rr, int rrcnt)
|
||||||
sl = sizeof sa.sin;
|
sl = sizeof sa.sin;
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
|
|
||||||
|
|
||||||
/* Get local address and open/bind a socket */
|
/* Get local address and open/bind a socket */
|
||||||
sa.sin.sin_family = family;
|
sa.sin.sin_family = family;
|
||||||
fd = socket(family, SOCK_DGRAM, 0);
|
fd = socket(family, SOCK_DGRAM, 0);
|
||||||
|
|
@ -96,6 +101,9 @@ int __dns_doqueries(unsigned char *dest, const char *name, int *rr, int rrcnt)
|
||||||
pfd.fd = fd;
|
pfd.fd = fd;
|
||||||
pfd.events = POLLIN;
|
pfd.events = POLLIN;
|
||||||
|
|
||||||
|
pthread_cleanup_push(cleanup, (void *)(intptr_t)fd);
|
||||||
|
pthread_setcancelstate(cs, 0);
|
||||||
|
|
||||||
/* Loop until we timeout; break early on success */
|
/* Loop until we timeout; break early on success */
|
||||||
for (; time(0)-t0 < TIMEOUT; ) {
|
for (; time(0)-t0 < TIMEOUT; ) {
|
||||||
|
|
||||||
|
|
@ -143,8 +151,7 @@ int __dns_doqueries(unsigned char *dest, const char *name, int *rr, int rrcnt)
|
||||||
if (got+failed == rrcnt) break;
|
if (got+failed == rrcnt) break;
|
||||||
}
|
}
|
||||||
out:
|
out:
|
||||||
close(fd);
|
pthread_cleanup_pop(1);
|
||||||
pthread_setcancelstate(cs, 0);
|
|
||||||
|
|
||||||
/* Return the number of results, or an error code if none */
|
/* Return the number of results, or an error code if none */
|
||||||
if (got) return got;
|
if (got) return got;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue