{
server = &channel->servers[i];
if (server->udp_socket != ARES_SOCKET_BAD)
- {
- FD_SET(server->udp_socket, read_fds);
- if (server->udp_socket >= nfds)
- nfds = server->udp_socket + 1;
- }
+ {
+ FD_SET(server->udp_socket, read_fds);
+ if (server->udp_socket >= nfds)
+ nfds = server->udp_socket + 1;
+ }
if (server->tcp_socket != ARES_SOCKET_BAD)
- {
- FD_SET(server->tcp_socket, read_fds);
- if (server->qhead)
- FD_SET(server->tcp_socket, write_fds);
- if (server->tcp_socket >= nfds)
- nfds = server->tcp_socket + 1;
- }
+ {
+ FD_SET(server->tcp_socket, read_fds);
+ if (server->qhead)
+ FD_SET(server->tcp_socket, write_fds);
+ if (server->tcp_socket >= nfds)
+ nfds = server->tcp_socket + 1;
+ }
}
return (int)nfds;
}
static void next_lookup(struct addr_query *aquery);
static void addr_callback(void *arg, int status, unsigned char *abuf,
- int alen);
+ int alen);
static void end_aquery(struct addr_query *aquery, int status,
- struct hostent *host);
+ struct hostent *host);
static int file_lookup(struct in_addr *addr, struct hostent **host);
void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen,
- int family, ares_host_callback callback, void *arg)
+ int family, ares_host_callback callback, void *arg)
{
struct addr_query *aquery;
for (p = aquery->remaining_lookups; *p; p++)
{
switch (*p)
- {
- case 'b':
- addr = ntohl(aquery->addr.s_addr);
- a1 = (int)((addr >> 24) & 0xff);
- a2 = (int)((addr >> 16) & 0xff);
- a3 = (int)((addr >> 8) & 0xff);
- a4 = (int)(addr & 0xff);
- sprintf(name, "%d.%d.%d.%d.in-addr.arpa", a4, a3, a2, a1);
- aquery->remaining_lookups = p + 1;
- ares_query(aquery->channel, name, C_IN, T_PTR, addr_callback,
- aquery);
- return;
- case 'f':
- status = file_lookup(&aquery->addr, &host);
- if (status != ARES_ENOTFOUND)
- {
- end_aquery(aquery, status, host);
- return;
- }
- break;
- }
+ {
+ case 'b':
+ addr = ntohl(aquery->addr.s_addr);
+ a1 = (int)((addr >> 24) & 0xff);
+ a2 = (int)((addr >> 16) & 0xff);
+ a3 = (int)((addr >> 8) & 0xff);
+ a4 = (int)(addr & 0xff);
+ sprintf(name, "%d.%d.%d.%d.in-addr.arpa", a4, a3, a2, a1);
+ aquery->remaining_lookups = p + 1;
+ ares_query(aquery->channel, name, C_IN, T_PTR, addr_callback,
+ aquery);
+ return;
+ case 'f':
+ status = file_lookup(&aquery->addr, &host);
+ if (status != ARES_ENOTFOUND)
+ {
+ end_aquery(aquery, status, host);
+ return;
+ }
+ break;
+ }
}
end_aquery(aquery, ARES_ENOTFOUND, NULL);
}
if (status == ARES_SUCCESS)
{
status = ares_parse_ptr_reply(abuf, alen, &aquery->addr,
- sizeof(struct in_addr), AF_INET, &host);
+ sizeof(struct in_addr), AF_INET, &host);
end_aquery(aquery, status, host);
}
else if (status == ARES_EDESTRUCTION)
}
static void end_aquery(struct addr_query *aquery, int status,
- struct hostent *host)
+ struct hostent *host)
{
aquery->callback(aquery->arg, status, host);
if (host)
char PATH_HOSTS[MAX_PATH];
if (IS_NT()) {
- char tmp[MAX_PATH];
- HKEY hkeyHosts;
-
- if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0, KEY_READ, &hkeyHosts)
- == ERROR_SUCCESS)
- {
- DWORD dwLength = MAX_PATH;
- RegQueryValueEx(hkeyHosts, DATABASEPATH, NULL, NULL, tmp,
- &dwLength);
- ExpandEnvironmentStrings(tmp, PATH_HOSTS, MAX_PATH);
- RegCloseKey(hkeyHosts);
- }
+ char tmp[MAX_PATH];
+ HKEY hkeyHosts;
+
+ if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0, KEY_READ, &hkeyHosts)
+ == ERROR_SUCCESS)
+ {
+ DWORD dwLength = MAX_PATH;
+ RegQueryValueEx(hkeyHosts, DATABASEPATH, NULL, NULL, tmp,
+ &dwLength);
+ ExpandEnvironmentStrings(tmp, PATH_HOSTS, MAX_PATH);
+ RegCloseKey(hkeyHosts);
+ }
}
else
GetWindowsDirectory(PATH_HOSTS, MAX_PATH);
while ((status = ares__get_hostent(fp, host)) == ARES_SUCCESS)
{
if (memcmp((*host)->h_addr, addr, sizeof(struct in_addr)) == 0)
- break;
+ break;
ares_free_hostent(*host);
}
fclose(fp);
static void next_lookup(struct host_query *hquery);
static void host_callback(void *arg, int status, unsigned char *abuf,
- int alen);
+ int alen);
static void end_hquery(struct host_query *hquery, int status,
- struct hostent *host);
+ struct hostent *host);
static int fake_hostent(const char *name, ares_host_callback callback,
- void *arg);
+ void *arg);
static int file_lookup(const char *name, struct hostent **host);
static void sort_addresses(struct hostent *host, struct apattern *sortlist,
- int nsort);
+ int nsort);
static int get_address_index(struct in_addr *addr, struct apattern *sortlist,
- int nsort);
+ int nsort);
void ares_gethostbyname(ares_channel channel, const char *name, int family,
- ares_host_callback callback, void *arg)
+ ares_host_callback callback, void *arg)
{
struct host_query *hquery;
for (p = hquery->remaining_lookups; *p; p++)
{
switch (*p)
- {
- case 'b':
- /* DNS lookup */
- hquery->remaining_lookups = p + 1;
- ares_search(hquery->channel, hquery->name, C_IN, T_A, host_callback,
- hquery);
- return;
-
- case 'f':
- /* Host file lookup */
- status = file_lookup(hquery->name, &host);
- if (status != ARES_ENOTFOUND)
- {
- end_hquery(hquery, status, host);
- return;
- }
- break;
- }
+ {
+ case 'b':
+ /* DNS lookup */
+ hquery->remaining_lookups = p + 1;
+ ares_search(hquery->channel, hquery->name, C_IN, T_A, host_callback,
+ hquery);
+ return;
+
+ case 'f':
+ /* Host file lookup */
+ status = file_lookup(hquery->name, &host);
+ if (status != ARES_ENOTFOUND)
+ {
+ end_hquery(hquery, status, host);
+ return;
+ }
+ break;
+ }
}
end_hquery(hquery, ARES_ENOTFOUND, NULL);
}
{
status = ares_parse_a_reply(abuf, alen, &host);
if (host && channel->nsort)
- sort_addresses(host, channel->sortlist, channel->nsort);
+ sort_addresses(host, channel->sortlist, channel->nsort);
end_hquery(hquery, status, host);
}
else if (status == ARES_EDESTRUCTION)
}
static void end_hquery(struct host_query *hquery, int status,
- struct hostent *host)
+ struct hostent *host)
{
hquery->callback(hquery->arg, status, host);
if (host)
* query immediately, and return true. Otherwise return false.
*/
static int fake_hostent(const char *name, ares_host_callback callback,
- void *arg)
+ void *arg)
{
struct in_addr addr;
struct hostent hostent;
for (p = name; *p; p++)
{
if (!isdigit((unsigned char)*p) && *p != '.')
- return 0;
+ return 0;
}
/* It also only looks like an IP address if it's non-zero-length and
#ifdef WIN32
char PATH_HOSTS[MAX_PATH];
if (IS_NT()) {
- char tmp[MAX_PATH];
- HKEY hkeyHosts;
-
- if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0, KEY_READ, &hkeyHosts)
- == ERROR_SUCCESS)
- {
- DWORD dwLength = MAX_PATH;
- RegQueryValueEx(hkeyHosts, DATABASEPATH, NULL, NULL, tmp,
- &dwLength);
- ExpandEnvironmentStrings(tmp, PATH_HOSTS, MAX_PATH);
- RegCloseKey(hkeyHosts);
- }
+ char tmp[MAX_PATH];
+ HKEY hkeyHosts;
+
+ if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0, KEY_READ, &hkeyHosts)
+ == ERROR_SUCCESS)
+ {
+ DWORD dwLength = MAX_PATH;
+ RegQueryValueEx(hkeyHosts, DATABASEPATH, NULL, NULL, tmp,
+ &dwLength);
+ ExpandEnvironmentStrings(tmp, PATH_HOSTS, MAX_PATH);
+ RegCloseKey(hkeyHosts);
+ }
}
else
GetWindowsDirectory(PATH_HOSTS, MAX_PATH);
while ((status = ares__get_hostent(fp, host)) == ARES_SUCCESS)
{
if (strcasecmp((*host)->h_name, name) == 0)
- break;
+ break;
for (alias = (*host)->h_aliases; *alias; alias++)
- {
- if (strcasecmp(*alias, name) == 0)
- break;
- }
+ {
+ if (strcasecmp(*alias, name) == 0)
+ break;
+ }
if (*alias)
- break;
+ break;
ares_free_hostent(*host);
}
fclose(fp);
}
static void sort_addresses(struct hostent *host, struct apattern *sortlist,
- int nsort)
+ int nsort)
{
struct in_addr a1, a2;
int i1, i2, ind1, ind2;
memcpy(&a1, host->h_addr_list[i1], sizeof(struct in_addr));
ind1 = get_address_index(&a1, sortlist, nsort);
for (i2 = i1 - 1; i2 >= 0; i2--)
- {
- memcpy(&a2, host->h_addr_list[i2], sizeof(struct in_addr));
- ind2 = get_address_index(&a2, sortlist, nsort);
- if (ind2 <= ind1)
- break;
- memcpy(host->h_addr_list[i2 + 1], &a2, sizeof(struct in_addr));
- }
+ {
+ memcpy(&a2, host->h_addr_list[i2], sizeof(struct in_addr));
+ ind2 = get_address_index(&a2, sortlist, nsort);
+ if (ind2 <= ind1)
+ break;
+ memcpy(host->h_addr_list[i2 + 1], &a2, sizeof(struct in_addr));
+ }
memcpy(host->h_addr_list[i2 + 1], &a1, sizeof(struct in_addr));
}
}
* if none of them match.
*/
static int get_address_index(struct in_addr *addr, struct apattern *sortlist,
- int nsort)
+ int nsort)
{
int i;
for (i = 0; i < nsort; i++)
{
if ((addr->s_addr & sortlist[i].mask.s_addr) == sortlist[i].addr.s_addr)
- break;
+ break;
}
return i;
}
#endif
static int init_by_options(ares_channel channel, struct ares_options *options,
- int optmask);
+ int optmask);
static int init_by_environment(ares_channel channel);
static int init_by_resolv_conf(ares_channel channel);
static int init_by_defaults(ares_channel channel);
static int config_lookup(ares_channel channel, const char *str,
const char *bindch, const char *filech);
static int config_nameserver(struct server_state **servers, int *nservers,
- char *str);
+ char *str);
static int config_sortlist(struct apattern **sortlist, int *nsort,
- const char *str);
+ const char *str);
static int set_search(ares_channel channel, const char *str);
static int set_options(ares_channel channel, const char *str);
static char *try_config(char *s, const char *opt);
}
int ares_init_options(ares_channel *channelptr, struct ares_options *options,
- int optmask)
+ int optmask)
{
ares_channel channel;
int i, status;
{
/* Something failed; clean up memory we may have allocated. */
if (channel->nservers != -1)
- free(channel->servers);
+ free(channel->servers);
if (channel->domains)
- {
- for (i = 0; i < channel->ndomains; i++)
- free(channel->domains[i]);
- free(channel->domains);
- }
+ {
+ for (i = 0; i < channel->ndomains; i++)
+ free(channel->domains[i]);
+ free(channel->domains);
+ }
if (channel->sortlist)
- free(channel->sortlist);
+ free(channel->sortlist);
if(channel->lookups)
free(channel->lookups);
free(channel);
}
static int init_by_options(ares_channel channel, struct ares_options *options,
- int optmask)
+ int optmask)
{
int i;
if ((optmask & ARES_OPT_SERVERS) && channel->nservers == -1)
{
channel->servers =
- malloc(options->nservers * sizeof(struct server_state));
+ malloc(options->nservers * sizeof(struct server_state));
if (!channel->servers && options->nservers != 0)
- return ARES_ENOMEM;
+ return ARES_ENOMEM;
for (i = 0; i < options->nservers; i++)
- channel->servers[i].addr = options->servers[i];
+ channel->servers[i].addr = options->servers[i];
channel->nservers = options->nservers;
}
{
channel->domains = malloc(options->ndomains * sizeof(char *));
if (!channel->domains && options->ndomains != 0)
- return ARES_ENOMEM;
+ return ARES_ENOMEM;
for (i = 0; i < options->ndomains; i++)
- {
- channel->ndomains = i;
- channel->domains[i] = strdup(options->domains[i]);
- if (!channel->domains[i])
- return ARES_ENOMEM;
- }
+ {
+ channel->ndomains = i;
+ channel->domains[i] = strdup(options->domains[i]);
+ if (!channel->domains[i])
+ return ARES_ENOMEM;
+ }
channel->ndomains = options->ndomains;
}
{
channel->lookups = strdup(options->lookups);
if (!channel->lookups)
- return ARES_ENOMEM;
+ return ARES_ENOMEM;
}
return ARES_SUCCESS;
{
status = set_search(channel, localdomain);
if (status != ARES_SUCCESS)
- return status;
+ return status;
}
res_options = getenv("RES_OPTIONS");
{
status = set_options(channel, res_options);
if (status != ARES_SUCCESS)
- return status;
+ return status;
}
return ARES_SUCCESS;
On Windows 9X, the DNS server can be found in:
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\VxD\MSTCP\NameServer
- On Windows NT/2000/XP/2003:
+ On Windows NT/2000/XP/2003:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NameServer
- or
+ or
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\DhcpNameServer
- or
+ or
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\{AdapterID}\
NameServer
- or
+ or
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\{AdapterID}\
DhcpNameServer
*/
/* If nobody specified servers, try a local named. */
channel->servers = malloc(sizeof(struct server_state));
if (!channel->servers)
- return ARES_ENOMEM;
+ return ARES_ENOMEM;
channel->servers[0].addr.s_addr = htonl(INADDR_LOOPBACK);
channel->nservers = 1;
}
* or set it to empty if the hostname isn't helpful.
*/
if (gethostname(hostname, sizeof(hostname)) == -1
- || !strchr(hostname, '.'))
- {
- channel->domains = malloc(0);
- channel->ndomains = 0;
- }
+ || !strchr(hostname, '.'))
+ {
+ channel->domains = malloc(0);
+ channel->ndomains = 0;
+ }
else
- {
- channel->domains = malloc(sizeof(char *));
- if (!channel->domains)
- return ARES_ENOMEM;
- channel->ndomains = 0;
- channel->domains[0] = strdup(strchr(hostname, '.') + 1);
- if (!channel->domains[0])
- return ARES_ENOMEM;
- channel->ndomains = 1;
- }
+ {
+ channel->domains = malloc(sizeof(char *));
+ if (!channel->domains)
+ return ARES_ENOMEM;
+ channel->ndomains = 0;
+ channel->domains[0] = strdup(strchr(hostname, '.') + 1);
+ if (!channel->domains[0])
+ return ARES_ENOMEM;
+ channel->ndomains = 1;
+ }
}
if (channel->nsort == -1)
{
channel->lookups = strdup("fb");
if (!channel->lookups)
- return ARES_ENOMEM;
+ return ARES_ENOMEM;
}
return ARES_SUCCESS;
while (*p)
{
if ((*p == *bindch || *p == *filech) && l < lookups + 2) {
- if (*p == *bindch) *l++ = 'b';
+ if (*p == *bindch) *l++ = 'b';
else *l++ = 'f';
}
while (*p && !isspace((unsigned char)*p) && (*p != ','))
- p++;
+ p++;
while (*p && (isspace((unsigned char)*p) || (*p == ',')))
- p++;
+ p++;
}
*l = 0;
channel->lookups = strdup(lookups);
}
static int config_nameserver(struct server_state **servers, int *nservers,
- char *str)
+ char *str)
{
struct in_addr addr;
struct server_state *newserv;
}
static int config_sortlist(struct apattern **sortlist, int *nsort,
- const char *str)
+ const char *str)
{
struct apattern pat, *newsort;
const char *q;
{
q = str;
while (*q && *q != '/' && *q != ';' && !isspace((unsigned char)*q))
- q++;
+ q++;
if (ip_addr(str, (int)(q - str), &pat.addr) == 0)
- {
- /* We have a pattern address; now determine the mask. */
- if (*q == '/')
- {
- str = q + 1;
- while (*q && *q != ';' && !isspace((unsigned char)*q))
- q++;
- if (ip_addr(str, (int)(q - str), &pat.mask) != 0)
- natural_mask(&pat);
- }
- else
- natural_mask(&pat);
-
- /* Add this pattern to our list. */
- newsort = realloc(*sortlist, (*nsort + 1) * sizeof(struct apattern));
- if (!newsort)
- return ARES_ENOMEM;
- newsort[*nsort] = pat;
- *sortlist = newsort;
- (*nsort)++;
- }
+ {
+ /* We have a pattern address; now determine the mask. */
+ if (*q == '/')
+ {
+ str = q + 1;
+ while (*q && *q != ';' && !isspace((unsigned char)*q))
+ q++;
+ if (ip_addr(str, (int)(q - str), &pat.mask) != 0)
+ natural_mask(&pat);
+ }
+ else
+ natural_mask(&pat);
+
+ /* Add this pattern to our list. */
+ newsort = realloc(*sortlist, (*nsort + 1) * sizeof(struct apattern));
+ if (!newsort)
+ return ARES_ENOMEM;
+ newsort[*nsort] = pat;
+ *sortlist = newsort;
+ (*nsort)++;
+ }
else
- {
- while (*q && *q != ';' && !isspace((unsigned char)*q))
- q++;
- }
+ {
+ while (*q && *q != ';' && !isspace((unsigned char)*q))
+ q++;
+ }
str = q;
while (isspace((unsigned char)*str))
- str++;
+ str++;
}
return ARES_SUCCESS;
while (*p)
{
while (*p && !isspace((unsigned char)*p))
- p++;
+ p++;
while (isspace((unsigned char)*p))
- p++;
+ p++;
n++;
}
channel->ndomains = n;
q = p;
while (*q && !isspace((unsigned char)*q))
- q++;
+ q++;
channel->domains[n] = malloc(q - p + 1);
if (!channel->domains[n])
- return ARES_ENOMEM;
+ return ARES_ENOMEM;
memcpy(channel->domains[n], p, q - p);
channel->domains[n][q - p] = 0;
p = q;
while (isspace((unsigned char)*p))
- p++;
+ p++;
n++;
}
channel->ndomains = n;
{
q = p;
while (*q && !isspace((unsigned char)*q))
- q++;
+ q++;
val = try_option(p, q, "ndots:");
if (val && channel->ndots == -1)
- channel->ndots = atoi(val);
+ channel->ndots = atoi(val);
val = try_option(p, q, "retrans:");
if (val && channel->timeout == -1)
- channel->timeout = atoi(val);
+ channel->timeout = atoi(val);
val = try_option(p, q, "retry:");
if (val && channel->tries == -1)
- channel->tries = atoi(val);
+ channel->tries = atoi(val);
p = q;
while (isspace((unsigned char)*p))
- p++;
+ p++;
}
return ARES_SUCCESS;