struct nwrap_he {
struct nwrap_cache *cache;
- struct nwrap_entdata *list;
- struct nwrap_vector entdata;
+ struct nwrap_vector entries;
int num;
int idx;
"Error parsing NSS_WRAPPER_MAX_HOSTENTS "
"value or value is too small. "
"Using default value: %lu.",
- max_hostents);
+ (unsigned long)max_hostents);
} else {
max_hostents = max_hostents_tmp;
}
}
/* Initialize hash table */
NWRAP_LOG(NWRAP_LOG_DEBUG,
- "Initializing hash table of size %lu items.", max_hostents);
+ "Initializing hash table of size %lu items.",
+ (unsigned long)max_hostents);
if (hcreate(max_hostents) == 0) {
NWRAP_LOG(NWRAP_LOG_ERROR,
"Failed to initialize hash table");
return false;
}
- el_new = nwrap_entlist_init(ed);
- if (el_new == NULL) {
- return false;
- }
for (cursor = el; cursor->next != NULL; cursor = cursor->next)
{
if (cursor->ed == ed) {
- free(el_new);
return false;
}
}
if (cursor->ed == ed) {
- free(el_new);
+ return false;
+ }
+
+ el_new = nwrap_entlist_init(ed);
+ if (el_new == NULL) {
return false;
}
aliases_count += 1;
}
- nwrap_vector_add_item(&(nwrap_he->entdata), (void *const)ed);
+ nwrap_vector_add_item(&(nwrap_he->entries), (void *const)ed);
ed->aliases_count = aliases_count;
/* Inventarize item */
struct nwrap_entdata *ed;
size_t i;
- nwrap_vector_foreach (ed, nwrap_he->entdata, i)
+ nwrap_vector_foreach (ed, nwrap_he->entries, i)
{
SAFE_FREE(ed->nwrap_addrdata.items);
SAFE_FREE(ed->ht.h_aliases);
SAFE_FREE(ed);
}
- SAFE_FREE(nwrap_he->entdata.items);
- nwrap_he->entdata.count = nwrap_he->entdata.capacity = 0;
+ SAFE_FREE(nwrap_he->entries.items);
+ nwrap_he->entries.count = nwrap_he->entries.capacity = 0;
nwrap_he->num = 0;
nwrap_he->idx = 0;
static int nwrap_files_getaddrinfo(const char *name,
unsigned short port,
const struct addrinfo *hints,
- struct addrinfo **ai,
- struct addrinfo **ai_tail)
+ struct addrinfo **ai)
{
struct nwrap_entlist *el;
struct hostent *he;
- struct addrinfo *ai_new = NULL;
struct addrinfo *ai_head = NULL;
struct addrinfo *ai_cur = NULL;
char *h_name_lower;
size_t name_len;
char canon_name[DNS_NAME_MAX] = { 0 };
bool skip_canonname = false;
- ENTRY e = { 0 };
+ ENTRY e = {
+ .key = NULL,
+ };
ENTRY *e_p = NULL;
int rc;
bool ok;
for (el = (struct nwrap_entlist *)e_p->data; el != NULL; el = el->next)
{
int rc2;
+ struct addrinfo *ai_new = NULL;
he = &(el->ed->ht);
}
*ai = ai_head;
- *ai_tail = ai_new;
return rc;
}
return NULL;
}
- nwrap_vector_foreach(ed, nwrap_he_global.entdata, i)
+ nwrap_vector_foreach(ed, nwrap_he_global.entries, i)
{
he = &(ed->ht);
if (he->h_addrtype != type) {
return NULL;
}
- he = &((struct nwrap_entdata *)nwrap_he_global.entdata.items[nwrap_he_global.idx++])->ht;
+ he = &((struct nwrap_entdata *)nwrap_he_global.entries.items[nwrap_he_global.idx++])->ht;
NWRAP_LOG(NWRAP_LOG_DEBUG, "return hosts[%s]", he->h_name);
struct addrinfo **res)
{
struct addrinfo *ai = NULL;
- struct addrinfo *ai_tail;
unsigned short port = 0;
struct {
int family;
return EAI_ADDRFAMILY;
}
- rc = nwrap_files_getaddrinfo(node, port, hints, &ai, &ai_tail);
+ rc = nwrap_files_getaddrinfo(node, port, hints, &ai);
if (rc != 0) {
int ret;
struct addrinfo *p = NULL;
return rc;
}
+ /*
+ * If the socktype was not specified, duplicate
+ * each ai returned, so that we have variants for
+ * both UDP and TCP.
+ */
if (hints->ai_socktype == 0) {
- /* Add second ai */
- struct addrinfo *ai_head = ai;
- struct addrinfo *ai_tmp;
- struct addrinfo *ai_new_tail = ai_tail;
-
- /* Add at least one more struct */
- do {
- /* CHECKS! */
- ai_tmp = malloc(sizeof(struct addrinfo));
- memcpy(ai_tmp, ai_head, sizeof(struct addrinfo));
- ai_tmp->ai_next = NULL;
+ struct addrinfo *ai_cur;
- /* We need a deep copy or freeaddrinfo() will blow up */
- if (ai_head->ai_canonname != NULL) {
- ai_tmp->ai_canonname =
- strdup(ai_head->ai_canonname);
+ /* freeaddrinfo() frees ai_canonname and ai so allocate them */
+ for (ai_cur = ai; ai_cur != NULL; ai_cur = ai_cur->ai_next) {
+ struct addrinfo *ai_new;
+
+ /* duplicate the current entry */
+
+ ai_new = malloc(sizeof(struct addrinfo));
+ if (ai_new == NULL) {
+ freeaddrinfo(ai);
+ return EAI_MEMORY;
}
- /* ai_head should point inside hints. */
- ai_tmp->ai_addr = ai_head->ai_addr;
- if (ai_head->ai_flags == 0) {
- ai_tmp->ai_flags = hints->ai_flags;
+ memcpy(ai_new, ai_cur, sizeof(struct addrinfo));
+ ai_new->ai_next = NULL;
+
+ /* We need a deep copy or freeaddrinfo() will blow up */
+ if (ai_cur->ai_canonname != NULL) {
+ ai_new->ai_canonname =
+ strdup(ai_cur->ai_canonname);
}
- if (ai_head->ai_socktype == SOCK_DGRAM) {
- ai_tmp->ai_socktype = SOCK_STREAM;
- } else if (ai_head->ai_socktype == SOCK_STREAM) {
- ai_tmp->ai_socktype = SOCK_DGRAM;
+
+ if (ai_cur->ai_socktype == SOCK_DGRAM) {
+ ai_new->ai_socktype = SOCK_STREAM;
+ } else if (ai_cur->ai_socktype == SOCK_STREAM) {
+ ai_new->ai_socktype = SOCK_DGRAM;
}
- if (ai_head->ai_socktype == SOCK_DGRAM) {
- ai_tmp->ai_protocol = IPPROTO_UDP;
- } else if (ai_head->ai_socktype == SOCK_STREAM) {
- ai_tmp->ai_protocol = IPPROTO_TCP;
+ if (ai_cur->ai_protocol == IPPROTO_TCP) {
+ ai_new->ai_protocol = IPPROTO_UDP;
+ } else if (ai_cur->ai_protocol == IPPROTO_UDP) {
+ ai_new->ai_protocol = IPPROTO_TCP;
}
- ai_new_tail->ai_next = ai_tmp;
- ai_new_tail = ai_tmp;
+ /* now insert the new entry */
- if (ai_head == ai_tail) {
- break;
- }
- ai_head = ai_head->ai_next;
- } while (1);
+ ai_new->ai_next = ai_cur->ai_next;
+ ai_cur->ai_next = ai_new;
+
+ /* and move on (don't duplicate the new entry) */
+
+ ai_cur = ai_new;
+ }
}
*res = ai;