/* * Lookup the first matching entry in the hostfile, either by address or by * name. Split the matching line into tokens in the "token" array and return * the number of tokens. */ static int hostent_file_match(FILE *f, int type, int family, int len, const char *data, char *addr, char **tokens, int ntokens) { int n, i; for(;;) { n = asr_parse_namedb_line(f, tokens, MAXTOKEN); if (n == -1) return (-1); if (type == ASR_GETHOSTBYNAME) { for (i = 1; i < n; i++) { if (strcasecmp(data, tokens[i])) continue; if (inet_pton(family, tokens[0], addr) == 1) return (n); } continue; } if (inet_pton(family, tokens[0], addr) == 1) if (memcmp(addr, data, len) == 0) return (n); } }
/* * Lookup the first matching entry in the hostfile, either by address or by * name depending on reqtype, and build a hostent from the line. */ static struct hostent_ext * hostent_file_match(FILE *f, int reqtype, int family, const char *data, int datalen) { char *tokens[MAXTOKEN], addr[16]; struct hostent_ext *h; int n, i; for (;;) { n = asr_parse_namedb_line(f, tokens, MAXTOKEN); if (n == -1) { errno = 0; /* ignore errors reading the file */ return (NULL); } if (reqtype == ASR_GETHOSTBYNAME) { for (i = 1; i < n; i++) { if (strcasecmp(data, tokens[i])) continue; if (inet_pton(family, tokens[0], addr) == 1) goto found; } } else { if (inet_pton(family, tokens[0], addr) == 1 && memcmp(addr, data, datalen) == 0) goto found; } } found: if ((h = hostent_alloc(family)) == NULL) return (NULL); if (hostent_set_cname(h, tokens[1], 0) == -1) goto fail; for (i = 2; i < n; i ++) if (hostent_add_alias(h, tokens[i], 0) == -1) goto fail; if (hostent_add_addr(h, addr, h->h.h_length) == -1) goto fail; return (h); fail: free(h); return (NULL); }
static int addrinfo_from_file(struct asr_query *as, int family, FILE *f) { char *tokens[MAXTOKEN], *c, buf[BUFSIZ + 1]; int n, i; union { struct sockaddr sa; struct sockaddr_in sain; struct sockaddr_in6 sain6; } u; for (;;) { n = asr_parse_namedb_line(f, tokens, MAXTOKEN, buf, sizeof(buf)); if (n == -1) break; /* ignore errors reading the file */ for (i = 1; i < n; i++) { if (strcasecmp(as->as.ai.hostname, tokens[i])) continue; if (asr_sockaddr_from_str(&u.sa, family, tokens[0]) == -1) continue; break; } if (i == n) continue; if (as->as.ai.hints.ai_flags & (AI_CANONNAME | AI_FQDN)) c = tokens[1]; else c = NULL; if (addrinfo_add(as, &u.sa, c)) return (-1); /* errno set */ } return (0); }