Skip to content

Commit

Permalink
Make the query callbacks return the number of timeouts that happened …
Browse files Browse the repository at this point in the history
…during the execution of a query, and update documentation accordingly. (Patch from the Google tree.)
  • Loading branch information
sesse committed Sep 28, 2007
1 parent b669e17 commit 50ba81c
Show file tree
Hide file tree
Showing 19 changed files with 114 additions and 65 deletions.
6 changes: 4 additions & 2 deletions adig.c
Expand Up @@ -127,7 +127,8 @@ static const char *rcodes[] = {
"(unknown)", "(unknown)", "(unknown)", "(unknown)", "NOCHANGE"
};

static void callback(void *arg, int status, unsigned char *abuf, int alen);
static void callback(void *arg, int status, int timeouts,
unsigned char *abuf, int alen);
static const unsigned char *display_question(const unsigned char *aptr,
const unsigned char *abuf,
int alen);
Expand Down Expand Up @@ -294,7 +295,8 @@ int main(int argc, char **argv)
return 0;
}

static void callback(void *arg, int status, unsigned char *abuf, int alen)
static void callback(void *arg, int status, int timeouts,
unsigned char *abuf, int alen)
{
char *name = (char *) arg;
int id, qr, opcode, aa, tc, rd, ra, rcode;
Expand Down
4 changes: 2 additions & 2 deletions ahost.c
Expand Up @@ -47,7 +47,7 @@ struct in6_addr
};
#endif

static void callback(void *arg, int status, struct hostent *host);
static void callback(void *arg, int status, int timeouts, struct hostent *host);
static void usage(void);

int main(int argc, char **argv)
Expand Down Expand Up @@ -142,7 +142,7 @@ int main(int argc, char **argv)
return 0;
}

static void callback(void *arg, int status, struct hostent *host)
static void callback(void *arg, int status, int timeouts, struct hostent *host)
{
char **p;

Expand Down
8 changes: 4 additions & 4 deletions ares.h
Expand Up @@ -193,11 +193,11 @@ struct timeval;
struct sockaddr;
struct ares_channeldata;
typedef struct ares_channeldata *ares_channel;
typedef void (*ares_callback)(void *arg, int status, unsigned char *abuf,
int alen);
typedef void (*ares_host_callback)(void *arg, int status,
typedef void (*ares_callback)(void *arg, int status, int timeouts,
unsigned char *abuf, int alen);
typedef void (*ares_host_callback)(void *arg, int status, int timeouts,
struct hostent *hostent);
typedef void (*ares_nameinfo_callback)(void *arg, int status,
typedef void (*ares_nameinfo_callback)(void *arg, int status, int timeouts,
char *node, char *service);

int ares_init(ares_channel *channelptr);
Expand Down
2 changes: 1 addition & 1 deletion ares_cancel.c
Expand Up @@ -31,7 +31,7 @@ void ares_cancel(ares_channel channel)
for (query = channel->queries; query; query = next)
{
next = query->next;
query->callback(query->arg, ARES_ETIMEOUT, NULL, 0);
query->callback(query->arg, ARES_ETIMEOUT, 0, NULL, 0);
free(query->tcpbuf);
free(query->server_info);
free(query);
Expand Down
2 changes: 1 addition & 1 deletion ares_destroy.c
Expand Up @@ -62,7 +62,7 @@ void ares_destroy(ares_channel channel)
while (channel->queries) {
query = channel->queries;
channel->queries = query->next;
query->callback(query->arg, ARES_EDESTRUCTION, NULL, 0);
query->callback(query->arg, ARES_EDESTRUCTION, 0, NULL, 0);
if (query->tcpbuf)
free(query->tcpbuf);
if (query->server_info)
Expand Down
7 changes: 6 additions & 1 deletion ares_gethostbyaddr.3
Expand Up @@ -22,7 +22,7 @@ ares_gethostbyaddr \- Initiate a host query by address
.B #include <ares.h>
.PP
.B typedef void (*ares_host_callback)(void *\fIarg\fP, int \fIstatus\fP,
.B struct hostent *\fIhostent\fP)
.B int \fItimeouts\fP, struct hostent *\fIhostent\fP)
.PP
.B void ares_gethostbyaddr(ares_channel \fIchannel\fP, const void *\fIaddr\fP,
.B int \fIaddrlen\fP, int \fIfamily\fP, ares_host_callback \fIcallback\fP,
Expand Down Expand Up @@ -76,6 +76,11 @@ The name service channel
.I channel
is being destroyed; the query will not be completed.
.PP
The callback argument
.I timeouts
reports how many times a query timed out during the execution of the
given request.
.PP
On successful completion of the query, the callback argument
.I hostent
points to a
Expand Down
18 changes: 11 additions & 7 deletions ares_gethostbyaddr.c
Expand Up @@ -49,11 +49,12 @@ struct addr_query {
void *arg;

const char *remaining_lookups;
int timeouts;
};

static void next_lookup(struct addr_query *aquery);
static void addr_callback(void *arg, int status, unsigned char *abuf,
int alen);
static void addr_callback(void *arg, int status, int timeouts,
unsigned char *abuf, int alen);
static void end_aquery(struct addr_query *aquery, int status,
struct hostent *host);
static int file_lookup(union ares_addr *addr, int family, struct hostent **host);
Expand All @@ -65,21 +66,21 @@ void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen,

if (family != AF_INET && family != AF_INET6)
{
callback(arg, ARES_ENOTIMP, NULL);
callback(arg, ARES_ENOTIMP, 0, NULL);
return;
}

if ((family == AF_INET && addrlen != sizeof(struct in_addr)) ||
(family == AF_INET6 && addrlen != sizeof(struct in6_addr)))
{
callback(arg, ARES_ENOTIMP, NULL);
callback(arg, ARES_ENOTIMP, 0, NULL);
return;
}

aquery = malloc(sizeof(struct addr_query));
if (!aquery)
{
callback(arg, ARES_ENOMEM, NULL);
callback(arg, ARES_ENOMEM, 0, NULL);
return;
}
aquery->channel = channel;
Expand All @@ -91,6 +92,7 @@ void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen,
aquery->callback = callback;
aquery->arg = arg;
aquery->remaining_lookups = channel->lookups;
aquery->timeouts = 0;

next_lookup(aquery);
}
Expand Down Expand Up @@ -151,11 +153,13 @@ static void next_lookup(struct addr_query *aquery)
end_aquery(aquery, ARES_ENOTFOUND, NULL);
}

static void addr_callback(void *arg, int status, unsigned char *abuf, int alen)
static void addr_callback(void *arg, int status, int timeouts,
unsigned char *abuf, int alen)
{
struct addr_query *aquery = (struct addr_query *) arg;
struct hostent *host;

aquery->timeouts += timeouts;
if (status == ARES_SUCCESS)
{
if (aquery->family == AF_INET)
Expand All @@ -175,7 +179,7 @@ static void addr_callback(void *arg, int status, unsigned char *abuf, int alen)
static void end_aquery(struct addr_query *aquery, int status,
struct hostent *host)
{
aquery->callback(aquery->arg, status, host);
aquery->callback(aquery->arg, status, aquery->timeouts, host);
if (host)
ares_free_hostent(host);
free(aquery);
Expand Down
7 changes: 6 additions & 1 deletion ares_gethostbyname.3
Expand Up @@ -22,7 +22,7 @@ ares_gethostbyname \- Initiate a host query by name
.B #include <ares.h>
.PP
.B typedef void (*ares_host_callback)(void *\fIarg\fP, int \fIstatus\fP,
.B struct hostent *\fIhostent\fP)
.B int \fItimeouts\fP, struct hostent *\fIhostent\fP)
.PP
.B void ares_gethostbyname(ares_channel \fIchannel\fP, const char *\fIname\fP,
.B int \fIfamily\fP, ares_host_callback \fIcallback\fP, void *\fIarg\fP)
Expand Down Expand Up @@ -80,6 +80,11 @@ The name service channel
.I channel
is being destroyed; the query will not be completed.
.PP
The callback argument
.I timeouts
reports how many times a query timed out during the execution of the
given request.
.PP
On successful completion of the query, the callback argument
.I hostent
points to a
Expand Down
24 changes: 13 additions & 11 deletions ares_gethostbyname.c
Expand Up @@ -54,11 +54,12 @@ struct host_query {
void *arg;
int family;
const char *remaining_lookups;
int timeouts;
};

static void next_lookup(struct host_query *hquery);
static void host_callback(void *arg, int status, unsigned char *abuf,
int alen);
static void host_callback(void *arg, int status, int timeouts,
unsigned char *abuf, int alen);
static void end_hquery(struct host_query *hquery, int status,
struct hostent *host);
static int fake_hostent(const char *name, int family, ares_host_callback callback,
Expand All @@ -81,7 +82,7 @@ void ares_gethostbyname(ares_channel channel, const char *name, int family,
/* Right now we only know how to look up Internet addresses. */
if (family != AF_INET && family != AF_INET6)
{
callback(arg, ARES_ENOTIMP, NULL);
callback(arg, ARES_ENOTIMP, 0, NULL);
return;
}

Expand All @@ -92,7 +93,7 @@ void ares_gethostbyname(ares_channel channel, const char *name, int family,
hquery = malloc(sizeof(struct host_query));
if (!hquery)
{
callback(arg, ARES_ENOMEM, NULL);
callback(arg, ARES_ENOMEM, 0, NULL);
return;
}
hquery->channel = channel;
Expand All @@ -101,12 +102,13 @@ void ares_gethostbyname(ares_channel channel, const char *name, int family,
if (!hquery->name)
{
free(hquery);
callback(arg, ARES_ENOMEM, NULL);
callback(arg, ARES_ENOMEM, 0, NULL);
return;
}
hquery->callback = callback;
hquery->arg = arg;
hquery->remaining_lookups = channel->lookups;
hquery->timeouts = 0;

/* Start performing lookups according to channel->lookups. */
next_lookup(hquery);
Expand Down Expand Up @@ -144,15 +146,16 @@ static void next_lookup(struct host_query *hquery)
break;
}
}
end_hquery(hquery, ARES_ENOTFOUND, NULL);
}

static void host_callback(void *arg, int status, unsigned char *abuf, int alen)
static void host_callback(void *arg, int status, int timeouts,
unsigned char *abuf, int alen)
{
struct host_query *hquery = (struct host_query *) arg;
ares_channel channel = hquery->channel;
struct hostent *host;

hquery->timeouts += timeouts;
if (status == ARES_SUCCESS)
{
if (hquery->family == AF_INET)
Expand Down Expand Up @@ -185,7 +188,7 @@ static void host_callback(void *arg, int status, unsigned char *abuf, int alen)
static void end_hquery(struct host_query *hquery, int status,
struct hostent *host)
{
hquery->callback(hquery->arg, status, host);
hquery->callback(hquery->arg, status, hquery->timeouts, host);
if (host)
ares_free_hostent(host);
free(hquery->name);
Expand Down Expand Up @@ -227,7 +230,7 @@ static int fake_hostent(const char *name, int family, ares_host_callback callbac
hostent.h_name = strdup(name);
if (!hostent.h_name)
{
callback(arg, ARES_ENOMEM, NULL);
callback(arg, ARES_ENOMEM, 0, NULL);
return 1;
}

Expand All @@ -236,7 +239,7 @@ static int fake_hostent(const char *name, int family, ares_host_callback callbac
hostent.h_aliases = aliases;
hostent.h_addrtype = family;
hostent.h_addr_list = addrs;
callback(arg, ARES_SUCCESS, &hostent);
callback(arg, ARES_SUCCESS, 0, &hostent);

free((char *)(hostent.h_name));
return 1;
Expand Down Expand Up @@ -416,4 +419,3 @@ static int get6_address_index(struct in6_addr *addr, struct apattern *sortlist,
}
return i;
}

7 changes: 6 additions & 1 deletion ares_getnameinfo.3
Expand Up @@ -22,7 +22,7 @@ ares_getnameinfo \- Address-to-nodename translation in protocol-independent mann
.B #include <ares.h>
.PP
.B typedef void (*ares_nameinfo_callback)(void *\fIarg\fP, int \fIstatus\fP,
.B char *\fInode\fP, char *\fIservice\fP)
.B int \fItimeouts\fP, char *\fInode\fP, char *\fIservice\fP)
.PP
.B void ares_getnameinfo(ares_channel \fIchannel\fP, const struct sockaddr *\fIsa\fP,
.B socklen_t \fIsalen\fP, int \fIflags\fP, ares_nameinfo_callback \fIcallback\fP,
Expand Down Expand Up @@ -120,6 +120,11 @@ The
.I flags
parameter contains an illegal value.
.PP
The callback argument
.I timeouts
reports how many times a query timed out during the execution of the
given request.
.PP
On successful completion of the query, the callback argument
.I node
contains a string representing the hostname (assuming
Expand Down

0 comments on commit 50ba81c

Please sign in to comment.