static int init_by_defaults(ares_channel channel) { char *hostname = NULL; int rc = ARES_SUCCESS; #ifdef HAVE_GETHOSTNAME char *dot; #endif 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; 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); 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); channel->nservers = 1; } #if defined(USE_WINSOCK) #define toolong(x) (x == -1) && (SOCKERRNO == WSAEFAULT) #elif defined(ENAMETOOLONG) #define toolong(x) (x == -1) && ((SOCKERRNO == ENAMETOOLONG) || \ (SOCKERRNO == EINVAL)) #else #define toolong(x) (x == -1) && (SOCKERRNO == EINVAL) #endif 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. */ #ifndef HAVE_GETHOSTNAME channel->ndomains = 0; /* default to none */ #else GETHOSTNAME_TYPE_ARG2 lenv = 64; size_t len = 64; int res; channel->ndomains = 0; /* default to none */ hostname = malloc(len); if(!hostname) { rc = ARES_ENOMEM; goto error; } do { res = gethostname(hostname, lenv); if(toolong(res)) { char *p; len *= 2; lenv *= 2; p = realloc(hostname, len); if(!p) { rc = ARES_ENOMEM; goto error; } hostname = p; continue; } else if(res) { rc = ARES_EBADNAME; goto error; } } WHILE_FALSE; dot = strchr(hostname, '.'); if (dot) { /* a dot was found */ channel->domains = malloc(sizeof(char *)); if (!channel->domains) { rc = ARES_ENOMEM; goto error; } channel->domains[0] = strdup(dot + 1); if (!channel->domains[0]) { rc = ARES_ENOMEM; goto error; } channel->ndomains = 1; } #endif }
static int init_by_defaults(ares_channel channel) { char *hostname = NULL; int rc = ARES_SUCCESS; #ifdef HAVE_GETHOSTNAME char *dot; #endif 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; 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); 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); channel->nservers = 1; } #ifdef ENAMETOOLONG #define toolong(x) (x == -1) && ((ENAMETOOLONG == errno) || (EINVAL == errno)) #else #define toolong(x) (x == -1) && (EINVAL == errno) #endif 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 */ #ifdef HAVE_GETHOSTNAME hostname = malloc(len); if(!hostname) { rc = ARES_ENOMEM; goto error; } 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) { /* a dot was found */ channel->domains = malloc(sizeof(char *)); if (!channel->domains) { rc = ARES_ENOMEM; goto error; } channel->domains[0] = strdup(dot + 1); if (!channel->domains[0]) { rc = ARES_ENOMEM; goto error; } channel->ndomains = 1; } #endif } 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; }