Newer
Older
if (channel->flags == -1)
channel->flags = 0;
if (channel->timeout == -1)
channel->timeout = DEFAULT_TIMEOUT;
if (channel->tries == -1)
channel->tries = DEFAULT_TRIES;
if (channel->ndots == -1)
channel->ndots = 1;
Daniel Stenberg
committed
if (channel->rotate == -1)
channel->rotate = 0;
if (channel->udp_port == -1)
channel->udp_port = htons(NAMESERVER_PORT);
if (channel->tcp_port == -1)
channel->tcp_port = htons(NAMESERVER_PORT);
Daniel Stenberg
committed
if (channel->nservers == -1) {
/* If nobody specified servers, try a local named. */
channel->servers = malloc(sizeof(struct server_state));
if (!channel->servers) {
rc = ARES_ENOMEM;
goto error;
channel->servers[0].addr.family = AF_INET;
channel->servers[0].addr.addrV4.s_addr = htonl(INADDR_LOOPBACK);
Daniel Stenberg
committed
channel->nservers = 1;
}
Daniel Stenberg
committed
#ifdef ENAMETOOLONG
#define toolong(x) (x == -1) && ((ENAMETOOLONG == errno) || (EINVAL == errno))
#else
#define toolong(x) (x == -1) && (EINVAL == errno)
#endif
Daniel Stenberg
committed
if (channel->ndomains == -1) {
/* Derive a default domain search list from the kernel hostname,
* or set it to empty if the hostname isn't helpful.
*/
size_t len = 64;
int res;
channel->ndomains = 0; /* default to none */
Daniel Stenberg
committed
#ifdef HAVE_GETHOSTNAME
Daniel Stenberg
committed
if(!hostname) {
rc = ARES_ENOMEM;
goto error;
Daniel Stenberg
committed
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
do {
res = gethostname(hostname, len);
if(toolong(res)) {
char *p;
len *= 2;
p = realloc(hostname, len);
if(!p) {
rc = ARES_ENOMEM;
goto error;
}
hostname = p;
continue;
}
else if(res) {
rc = ARES_EBADNAME;
goto error;
}
} while(0);
dot = strchr(hostname, '.');
if (dot) {
Daniel Stenberg
committed
/* a dot was found */
channel->domains = malloc(sizeof(char *));
if (!channel->domains) {
rc = ARES_ENOMEM;
goto error;
}
channel->domains[0] = strdup(dot + 1);
Daniel Stenberg
committed
if (!channel->domains[0]) {
rc = ARES_ENOMEM;
goto error;
}
channel->ndomains = 1;
Daniel Stenberg
committed
}
Daniel Stenberg
committed
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
if (channel->nsort == -1) {
channel->sortlist = NULL;
channel->nsort = 0;
}
if (!channel->lookups) {
channel->lookups = strdup("fb");
if (!channel->lookups)
rc = ARES_ENOMEM;
}
error:
if(rc) {
if(channel->servers)
free(channel->servers);
if(channel->domains && channel->domains[0])
free(channel->domains[0]);
if(channel->domains)
free(channel->domains);
if(channel->lookups)
free(channel->lookups);
}
if(hostname)
free(hostname);
return rc;
#if !defined(WIN32) && !defined(WATT32)
static int config_domain(ares_channel channel, char *str)
{
char *q;
/* Set a single search domain. */
q = str;
return set_search(channel, str);
}
#if defined(__INTEL_COMPILER) && (__INTEL_COMPILER == 910) && \
defined(__OPTIMIZE__) && defined(__unix__) && defined(__i386__)
/* workaround icc 9.1 optimizer issue */
# define vqualifier volatile
#else
# define vqualifier
#endif
Daniel Stenberg
committed
static int config_lookup(ares_channel channel, const char *str,
const char *bindch, const char *filech)
{
char lookups[3], *l;
/* Set the lookup order. Only the first letter of each work
* is relevant, and it has to be "b" for DNS or "f" for the
* host file. Ignore everything else.
*/
l = lookups;
p = str;
while (*p)
{
Daniel Stenberg
committed
if ((*p == *bindch || *p == *filech) && l < lookups + 2) {
Daniel Stenberg
committed
else *l++ = 'f';
}
while (*p && !ISSPACE(*p) && (*p != ','))
while (*p && (ISSPACE(*p) || (*p == ',')))
channel->lookups = strdup(lookups);
return (channel->lookups) ? ARES_SUCCESS : ARES_ENOMEM;
}
#endif /* !WIN32 & !WATT32 */
#ifndef WATT32
static int config_nameserver(struct server_state **servers, int *nservers,
struct server_state *newserv;
/* On Windows, there may be more than one nameserver specified in the same
*/
{
/* Skip whitespace and commas. */
while (*p && (ISSPACE(*p) || (*p == ',')))
p++;
if (!*p)
/* No more input, done. */
break;
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
/* Advance past this address. */
while (*p && !ISSPACE(*p) && (*p != ','))
p++;
if (*p)
/* Null terminate this address. */
*p++ = '\0';
else
/* Reached end of input, done when this address is processed. */
p = NULL;
/* Convert textual address to binary format. */
if (ares_inet_pton(AF_INET, txtaddr, &host.addrV4) == 1)
host.family = AF_INET;
else if (ares_inet_pton(AF_INET6, txtaddr, &host.addrV6) == 1)
host.family = AF_INET6;
else
continue;
/* Resize servers state array. */
newserv = realloc(*servers, (*nservers + 1) *
sizeof(struct server_state));
if (!newserv)
return ARES_ENOMEM;
/* Store address data. */
newserv[*nservers].addr.family = host.family;
if (host.family == AF_INET)
memcpy(&newserv[*nservers].addr.addrV4, &host.addrV4,
sizeof(host.addrV4));
else
memcpy(&newserv[*nservers].addr.addrV6, &host.addrV6,
sizeof(host.addrV6));
/* Update arguments. */
*servers = newserv;
*nservers += 1;
}
return ARES_SUCCESS;
}
#ifndef WIN32
static int config_sortlist(struct apattern **sortlist, int *nsort,
struct apattern pat;
const char *q;
/* Add sortlist entries. */
while (*str && *str != ';')
{
int bits;
char ipbuf[16], ipbufpfx[32];
/* Find just the IP */
while (*q && *q != '/' && *q != ';' && !ISSPACE(*q))
memcpy(ipbuf, str, (int)(q-str));
ipbuf[(int)(q-str)] = '\0';
/* Find the prefix */
if (*q == '/')
{
const char *str2 = q+1;
while (*q && *q != ';' && !ISSPACE(*q))
q++;
memcpy(ipbufpfx, str, (int)(q-str));
ipbufpfx[(int)(q-str)] = '\0';
str = str2;
}
else
ipbufpfx[0] = '\0';
/* Lets see if it is CIDR */
/* First we'll try IPv6 */
if ((bits = ares_inet_net_pton(AF_INET6, ipbufpfx[0] ? ipbufpfx : ipbuf,
&pat.addrV6,
sizeof(pat.addrV6))) > 0)
{
pat.type = PATTERN_CIDR;
pat.family = AF_INET6;
if (!sortlist_alloc(sortlist, nsort, &pat))
return ARES_ENOMEM;
}
if (ipbufpfx[0] &&
(bits = ares_inet_net_pton(AF_INET, ipbufpfx, &pat.addrV4,
sizeof(pat.addrV4))) > 0)
{
pat.type = PATTERN_CIDR;
pat.family = AF_INET;
if (!sortlist_alloc(sortlist, nsort, &pat))
return ARES_ENOMEM;
}
/* See if it is just a regular IP */
else if (ip_addr(ipbuf, (int)(q-str), &pat.addrV4) == 0)
if (ipbufpfx[0])
memcpy(ipbuf, str, (int)(q-str));
ipbuf[(int)(q-str)] = '\0';
if (ip_addr(ipbuf, (int)(q - str), &pat.mask.addr4) != 0)
natural_mask(&pat);
}
else
natural_mask(&pat);
pat.family = AF_INET;
pat.type = PATTERN_MASK;
if (!sortlist_alloc(sortlist, nsort, &pat))
while (*q && *q != ';' && !ISSPACE(*q))
}
return ARES_SUCCESS;
}
#endif /* !WIN32 */
#endif /* !WATT32 */
static int set_search(ares_channel channel, const char *str)
{
int n;
const char *p, *q;
Daniel Stenberg
committed
if(channel->ndomains != -1) {
/* if we already have some domains present, free them first */
for(n=0; n < channel->ndomains; n++)
free(channel->domains[n]);
free(channel->domains);
channel->domains = NULL;
Daniel Stenberg
committed
channel->ndomains = -1;
}
/* Count the domains given. */
n = 0;
p = str;
while (*p)
{
if (!n)
{
channel->ndomains = 0;
return ARES_SUCCESS;
}
channel->domains = malloc(n * sizeof(char *));
if (!channel->domains)
return ARES_ENOMEM;
/* Now copy the domains. */
n = 0;
p = str;
while (*p)
{
channel->ndomains = n;
q = p;
channel->domains[n] = malloc(q - p + 1);
if (!channel->domains[n])
memcpy(channel->domains[n], p, q - p);
channel->domains[n][q - p] = 0;
p = q;
n++;
}
channel->ndomains = n;
return ARES_SUCCESS;
}
static int set_options(ares_channel channel, const char *str)
{
const char *p, *q, *val;
p = str;
while (*p)
{
q = p;
val = try_option(p, q, "ndots:");
if (val && channel->ndots == -1)
val = try_option(p, q, "retrans:");
if (val && channel->timeout == -1)
val = try_option(p, q, "retry:");
if (val && channel->tries == -1)
Daniel Stenberg
committed
val = try_option(p, q, "rotate");
if (val && channel->rotate == -1)
channel->rotate = 1;
}
return ARES_SUCCESS;
}
static const char *try_option(const char *p, const char *q, const char *opt)
{
size_t len = strlen(opt);
return ((size_t)(q - p) >= len && !strncmp(p, opt, len)) ? &p[len] : NULL;
}
#if !defined(WIN32) && !defined(WATT32)
static char *try_config(char *s, const char *opt)
if (!s || !opt)
/* no line or no option */
return NULL;
/* trim line comment */
/* trim trailing whitespace */
/* skip leading whitespace */
if (!*p)
/* empty line */
return NULL;
if ((len = strlen(opt)) == 0)
/* empty option */
return NULL;
if (strncmp(p, opt, len) != 0)
/* line and option do not match */
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
/* skip over given option name */
p += len;
if (!*p)
/* no option value */
return NULL;
if ((opt[len-1] != ':') && (opt[len-1] != '=') && !ISSPACE(*p))
/* whitespace between option name and value is mandatory
for given option names which do not end with ':' or '=' */
return NULL;
/* skip over whitespace */
while (*p && ISSPACE(*p))
p++;
if (!*p)
/* no option value */
return NULL;
/* return pointer to option value */
return p;
}
static int sortlist_alloc(struct apattern **sortlist, int *nsort,
struct apattern *pat)
{
struct apattern *newsort;
newsort = realloc(*sortlist, (*nsort + 1) * sizeof(struct apattern));
if (!newsort)
return 0;
newsort[*nsort] = *pat;
*sortlist = newsort;
(*nsort)++;
return 1;
}
static int ip_addr(const char *ipbuf, int len, struct in_addr *addr)
{
/* Four octets and three periods yields at most 15 characters. */
if (len > 15)
return -1;
addr->s_addr = inet_addr(ipbuf);
if (addr->s_addr == INADDR_NONE && strcmp(ipbuf, "255.255.255.255") != 0)
return -1;
return 0;
}
static void natural_mask(struct apattern *pat)
{
struct in_addr addr;
/* Store a host-byte-order copy of pat in a struct in_addr. Icky,
* but portable.
*/
addr.s_addr = ntohl(pat->addrV4.s_addr);
/* This is out of date in the CIDR world, but some people might
* still rely on it.
*/
if (IN_CLASSA(addr.s_addr))
pat->mask.addr4.s_addr = htonl(IN_CLASSA_NET);
else if (IN_CLASSB(addr.s_addr))
pat->mask.addr4.s_addr = htonl(IN_CLASSB_NET);
pat->mask.addr4.s_addr = htonl(IN_CLASSC_NET);
#endif /* !WIN32 && !WATT32 */
/* initialize an rc4 key. If possible a cryptographically secure random key
is generated using a suitable function (for example win32's RtlGenRandom as
described in
http://blogs.msdn.com/michael_howard/archive/2005/01/14/353379.aspx
otherwise the code defaults to cross-platform albeit less secure mechanism
using rand
*/
static void randomize_key(unsigned char* key,int key_data_len)
{
int randomized = 0;
Daniel Stenberg
committed
int counter=0;
#ifdef WIN32
Yang Tse
committed
if (ares_fpSystemFunction036)
Yang Tse
committed
res = (*ares_fpSystemFunction036) (key, key_data_len);
if (res)
randomized = 1;
}
Daniel Stenberg
committed
#else /* !WIN32 */
#ifdef RANDOM_FILE
FILE *f = fopen(RANDOM_FILE, "rb");
if(f) {
Daniel Stenberg
committed
counter = fread(key, 1, key_data_len, f);
Daniel Stenberg
committed
fclose(f);
}
#endif
Daniel Stenberg
committed
#endif /* WIN32 */
if ( !randomized ) {
Daniel Stenberg
committed
for (;counter<key_data_len;counter++)
key[counter]=(unsigned char)(rand() % 256);
}
}
static int init_id_key(rc4_key* key,int key_data_len)
{
unsigned char index1;
unsigned char index2;
unsigned char* state;
short counter;
unsigned char *key_data_ptr = 0;
key_data_ptr = calloc(1,key_data_len);
if (!key_data_ptr)
return ARES_ENOMEM;
state = &key->state[0];
for(counter = 0; counter < 256; counter++)
/* unnecessary AND but it keeps some compilers happier */
state[counter] = (unsigned char)(counter & 0xff);
key->x = 0;
key->y = 0;
index1 = 0;
index2 = 0;
for(counter = 0; counter < 256; counter++)
{
index2 = (unsigned char)((key_data_ptr[index1] + state[counter] +
index2) % 256);
ARES_SWAP_BYTE(&state[counter], &state[index2]);
index1 = (unsigned char)((index1 + 1) % key_data_len);
}
free(key_data_ptr);
return ARES_SUCCESS;
}
unsigned short ares__generate_new_id(rc4_key* key)
unsigned short r=0;
ares__rc4(key, (unsigned char *)&r, sizeof(r));
return r;
}
Daniel Stenberg
committed
void ares_set_socket_callback(ares_channel channel,
ares_sock_create_callback cb,
void *data)
{
channel->sock_create_cb = cb;
channel->sock_create_cb_data = data;
}
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
void ares__init_servers_state(ares_channel channel)
{
struct server_state *server;
int i;
for (i = 0; i < channel->nservers; i++)
{
server = &channel->servers[i];
server->udp_socket = ARES_SOCKET_BAD;
server->tcp_socket = ARES_SOCKET_BAD;
server->tcp_connection_generation = ++channel->tcp_connection_generation;
server->tcp_lenbuf_pos = 0;
server->tcp_buffer_pos = 0;
server->tcp_buffer = NULL;
server->tcp_length = 0;
server->qhead = NULL;
server->qtail = NULL;
ares__init_list_head(&server->queries_to_server);
server->channel = channel;
server->is_broken = 0;
}
}