lwconfig.c revision 6c6ad82aca98a80c0b557e31605d406e2e250901
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt/*
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt * Copyright (C) 2000 Internet Software Consortium.
c8175ece69d986ccd0671bc4d2571b247dfae177Automatic Updater *
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt * Permission to use, copy, modify, and distribute this software for any
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt * purpose with or without fee is hereby granted, provided that the above
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt * copyright notice and this permission notice appear in all copies.
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt *
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt * SOFTWARE.
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt */
9892bae7b760071b37881d4dc888ea4b4320a851Mark Andrews
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt/***
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt *** Module for parsing resolv.conf files.
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt ***
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt *** entry points are:
bbedadf76ab670b01887fb9b41097120ea4fdf14Evan Hunt *** lwres_conf_init(lwres_context_t *ctx, lwres_conf_t *confdata)
bbedadf76ab670b01887fb9b41097120ea4fdf14Evan Hunt *** intializes data structure for subsequent parsing.
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt ***
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt *** lwres_conf_parse(const char *filename, lwres_conf_t *confdata)
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt *** parses a file and fills in the data structure.
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt ***
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt *** lwres_conf_print(FILE *fp, lwres_conf_t *confdata)
0245f7725c40fd29637fbc83ee25bd84be25bfd2Evan Hunt *** prints the data structure to the FILE.
0245f7725c40fd29637fbc83ee25bd84be25bfd2Evan Hunt ***
0245f7725c40fd29637fbc83ee25bd84be25bfd2Evan Hunt *** lwres_conf_clear(lwres_conf_t *confdata)
0245f7725c40fd29637fbc83ee25bd84be25bfd2Evan Hunt *** frees up all the internal memory used by the data
0245f7725c40fd29637fbc83ee25bd84be25bfd2Evan Hunt *** structure, returning it to the lwres_context_t.
0245f7725c40fd29637fbc83ee25bd84be25bfd2Evan Hunt ***
0245f7725c40fd29637fbc83ee25bd84be25bfd2Evan Hunt ***/
0245f7725c40fd29637fbc83ee25bd84be25bfd2Evan Hunt
0245f7725c40fd29637fbc83ee25bd84be25bfd2Evan Hunt#include <config.h>
0245f7725c40fd29637fbc83ee25bd84be25bfd2Evan Hunt
0245f7725c40fd29637fbc83ee25bd84be25bfd2Evan Hunt#include <assert.h>
0245f7725c40fd29637fbc83ee25bd84be25bfd2Evan Hunt#include <ctype.h>
0245f7725c40fd29637fbc83ee25bd84be25bfd2Evan Hunt#include <errno.h>
0245f7725c40fd29637fbc83ee25bd84be25bfd2Evan Hunt#include <stdlib.h>
0245f7725c40fd29637fbc83ee25bd84be25bfd2Evan Hunt#include <stdio.h>
0245f7725c40fd29637fbc83ee25bd84be25bfd2Evan Hunt#include <string.h>
0245f7725c40fd29637fbc83ee25bd84be25bfd2Evan Hunt#include <unistd.h>
0245f7725c40fd29637fbc83ee25bd84be25bfd2Evan Hunt
0245f7725c40fd29637fbc83ee25bd84be25bfd2Evan Hunt#include <sys/types.h>
0245f7725c40fd29637fbc83ee25bd84be25bfd2Evan Hunt#include <sys/socket.h>
0245f7725c40fd29637fbc83ee25bd84be25bfd2Evan Hunt
0245f7725c40fd29637fbc83ee25bd84be25bfd2Evan Hunt#include <netinet/in.h>
0245f7725c40fd29637fbc83ee25bd84be25bfd2Evan Hunt
0245f7725c40fd29637fbc83ee25bd84be25bfd2Evan Hunt#include <lwres/lwbuffer.h>
0245f7725c40fd29637fbc83ee25bd84be25bfd2Evan Hunt#include <lwres/lwres.h>
0245f7725c40fd29637fbc83ee25bd84be25bfd2Evan Hunt#include <lwres/result.h>
0245f7725c40fd29637fbc83ee25bd84be25bfd2Evan Hunt
0245f7725c40fd29637fbc83ee25bd84be25bfd2Evan Hunt#include "assert_p.h"
e24ccb512c110d181e01f977196e518b0e72e451Mark Andrews#include "context_p.h"
e24ccb512c110d181e01f977196e518b0e72e451Mark Andrews
e24ccb512c110d181e01f977196e518b0e72e451Mark Andrews
e24ccb512c110d181e01f977196e518b0e72e451Mark Andrews#if ! defined(NS_INADDRSZ)
e24ccb512c110d181e01f977196e518b0e72e451Mark Andrews#define NS_INADDRSZ 4
e24ccb512c110d181e01f977196e518b0e72e451Mark Andrews#endif
e24ccb512c110d181e01f977196e518b0e72e451Mark Andrews
e24ccb512c110d181e01f977196e518b0e72e451Mark Andrews#if ! defined(NS_IN6ADDRSZ)
e24ccb512c110d181e01f977196e518b0e72e451Mark Andrews#define NS_IN6ADDRSZ 16
e24ccb512c110d181e01f977196e518b0e72e451Mark Andrews#endif
e24ccb512c110d181e01f977196e518b0e72e451Mark Andrews
ba88bcf08b965f65c07735efa2f675b8cbeb735aMark Andrewsextern int lwres_net_pton(int af, const char *src, void *dst);
ba88bcf08b965f65c07735efa2f675b8cbeb735aMark Andrewsextern const char *lwres_net_ntop(int af, const void *src, char *dst,
ba88bcf08b965f65c07735efa2f675b8cbeb735aMark Andrews size_t size);
ba88bcf08b965f65c07735efa2f675b8cbeb735aMark Andrews
ba88bcf08b965f65c07735efa2f675b8cbeb735aMark Andrewsstatic lwres_result_t
ba88bcf08b965f65c07735efa2f675b8cbeb735aMark Andrewslwres_conf_parsenameserver(lwres_context_t *ctx, FILE *fp);
e24ccb512c110d181e01f977196e518b0e72e451Mark Andrews
ba88bcf08b965f65c07735efa2f675b8cbeb735aMark Andrewsstatic lwres_result_t
ba88bcf08b965f65c07735efa2f675b8cbeb735aMark Andrewslwres_conf_parsedomain(lwres_context_t *ctx, FILE *fp);
ba88bcf08b965f65c07735efa2f675b8cbeb735aMark Andrews
ba88bcf08b965f65c07735efa2f675b8cbeb735aMark Andrewsstatic lwres_result_t
ba88bcf08b965f65c07735efa2f675b8cbeb735aMark Andrewslwres_conf_parsesearch(lwres_context_t *ctx, FILE *fp);
ba88bcf08b965f65c07735efa2f675b8cbeb735aMark Andrews
e24ccb512c110d181e01f977196e518b0e72e451Mark Andrewsstatic lwres_result_t
e24ccb512c110d181e01f977196e518b0e72e451Mark Andrewslwres_conf_parsesortlist(lwres_context_t *ctx, FILE *fp);
ba88bcf08b965f65c07735efa2f675b8cbeb735aMark Andrews
e24ccb512c110d181e01f977196e518b0e72e451Mark Andrewsstatic lwres_result_t
ba88bcf08b965f65c07735efa2f675b8cbeb735aMark Andrewslwres_conf_parseoption(lwres_context_t *ctx, FILE *fp);
e24ccb512c110d181e01f977196e518b0e72e451Mark Andrews
e24ccb512c110d181e01f977196e518b0e72e451Mark Andrewsstatic void
e24ccb512c110d181e01f977196e518b0e72e451Mark Andrewslwres_resetaddr(lwres_addr_t *addr);
e24ccb512c110d181e01f977196e518b0e72e451Mark Andrews
e24ccb512c110d181e01f977196e518b0e72e451Mark Andrewsstatic lwres_result_t
e24ccb512c110d181e01f977196e518b0e72e451Mark Andrewslwres_create_addr(const char *buff, lwres_addr_t *addr);
e24ccb512c110d181e01f977196e518b0e72e451Mark Andrews
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt/*
e24ccb512c110d181e01f977196e518b0e72e451Mark Andrews * Skip over any leading whitespace and then read in the next sequence of
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt * non-whitespace characters. Returnss EOF on end-of-file, or the character
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt * that caused the reading to stop.
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt */
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Huntstatic int
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Huntgetword(FILE *fp, char *buffer, size_t size) {
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt int ch;
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt char *p = buffer;
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt REQUIRE(buffer != NULL);
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt REQUIRE(size > 0);
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt *p = '\0';
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt ch = fgetc(fp);
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt while (ch != '\n' && ch != EOF && isspace((unsigned char)ch))
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt ch = fgetc(fp);
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt if (ch == EOF)
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt return (EOF);
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt do {
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt *p = '\0';
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt if (ch == EOF || isspace((unsigned char)ch))
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt break;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt else if ((size_t) (p - buffer) == size - 1)
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt return (EOF); /* Not enough space. */
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt *p++ = (char)ch;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt ch = fgetc(fp);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt } while (1);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt return (ch);
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt}
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Huntstatic void
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Huntlwres_resetaddr(lwres_addr_t *addr) {
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt REQUIRE(addr != NULL);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt memset(addr->address, 0, LWRES_ADDR_MAXLEN);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt addr->family = 0;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt addr->length = 0;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt}
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Huntstatic char *
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Huntlwres_strdup(lwres_context_t *ctx, const char *str) {
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt char *p;
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt REQUIRE(str != NULL);
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt REQUIRE(strlen(str) > 0);
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt p = CTXMALLOC(strlen(str) + 1);
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt if (p != NULL)
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt strcpy(p, str);
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt return (p);
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt}
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Huntvoid
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Huntlwres_conf_init(lwres_context_t *ctx) {
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt int i;
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt lwres_conf_t *confdata;
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt REQUIRE(ctx != NULL);
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt confdata = &ctx->confdata;
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt confdata->nsnext = 0;
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt confdata->domainname = NULL;
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt confdata->searchnxt = 0;
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt confdata->sortlistnxt = 0;
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt confdata->resdebug = 0;
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt confdata->ndots = 1;
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt confdata->no_tld_query = 0;
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt for (i = 0 ; i < LWRES_CONFMAXNAMESERVERS ; i++)
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt lwres_resetaddr(&confdata->nameservers[i]);
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt for (i = 0 ; i < LWRES_CONFMAXSEARCH ; i++)
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt confdata->search[i] = NULL;
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt for (i = 0 ; i < LWRES_CONFMAXSORTLIST ; i++) {
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt lwres_resetaddr(&confdata->sortlist[i].addr);
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt lwres_resetaddr(&confdata->sortlist[i].mask);
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt }
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt}
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Huntvoid
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Huntlwres_conf_clear(lwres_context_t *ctx) {
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt int i;
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt lwres_conf_t *confdata;
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt REQUIRE(ctx != NULL);
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt confdata = &ctx->confdata;
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt for (i = 0 ; i < confdata->nsnext ; i++)
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt lwres_resetaddr(&confdata->nameservers[i]);
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt if (confdata->domainname != NULL) {
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt CTXFREE(confdata->domainname,
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt strlen(confdata->domainname) + 1);
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt confdata->domainname = NULL;
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt }
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt for (i = 0 ; i < confdata->searchnxt ; i++) {
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt if (confdata->search[i] != NULL) {
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt CTXFREE(confdata->search[i],
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt strlen(confdata->search[i]) + 1);
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt confdata->search[i] = NULL;
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt }
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt }
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt for (i = 0 ; i < LWRES_CONFMAXSORTLIST ; i++) {
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt lwres_resetaddr(&confdata->sortlist[i].addr);
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt lwres_resetaddr(&confdata->sortlist[i].mask);
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt }
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt confdata->nsnext = 0;
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt confdata->domainname = NULL;
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt confdata->searchnxt = 0;
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt confdata->sortlistnxt = 0;
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt confdata->resdebug = 0;
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt confdata->ndots = 1;
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt confdata->no_tld_query = 0;
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt}
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Huntstatic lwres_result_t
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Huntlwres_conf_parsenameserver(lwres_context_t *ctx, FILE *fp) {
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt char word[LWRES_CONFMAXLINELEN];
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt int res;
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt lwres_conf_t *confdata;
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt confdata = &ctx->confdata;
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt if (confdata->nsnext == LWRES_CONFMAXNAMESERVERS)
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt return (LWRES_R_FAILURE);
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt res = getword(fp, word, sizeof(word));
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt if (strlen(word) == 0)
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt return (LWRES_R_FAILURE); /* Nothing on line. */
b47c020d5c635b662ac57e5485d266fd62c796c0Evan Hunt else if (res != EOF && res != '\n')
36b2d5f93c22b096c0417495f27ae0bdebf06ae1Evan Hunt return (LWRES_R_FAILURE); /* Extra junk on line. */
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt res = lwres_create_addr(word,
36b2d5f93c22b096c0417495f27ae0bdebf06ae1Evan Hunt &confdata->nameservers[confdata->nsnext++]);
36b2d5f93c22b096c0417495f27ae0bdebf06ae1Evan Hunt if (res != LWRES_R_SUCCESS)
36b2d5f93c22b096c0417495f27ae0bdebf06ae1Evan Hunt return (res);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt return (LWRES_R_SUCCESS);
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt}
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Huntstatic lwres_result_t
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Huntlwres_conf_parsedomain(lwres_context_t *ctx, FILE *fp) {
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt char word[LWRES_CONFMAXLINELEN];
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt int res, i;
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt lwres_conf_t *confdata;
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt confdata = &ctx->confdata;
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt res = getword(fp, word, sizeof(word));
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt if (strlen(word) == 0)
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt return (LWRES_R_FAILURE); /* Nothing else on line. */
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt else if (res != EOF && res != '\n')
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt return (LWRES_R_FAILURE); /* Extra junk on line. */
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt if (confdata->domainname != NULL)
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt CTXFREE(confdata->domainname,
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt strlen(confdata->domainname) + 1); /* */
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt /*
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt * Search and domain are mutually exclusive.
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt */
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt for (i = 0 ; i < LWRES_CONFMAXSEARCH ; i++) {
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt if (confdata->search[i] != NULL) {
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt CTXFREE(confdata->search[i],
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt strlen(confdata->search[i])+1);
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt confdata->search[i] = NULL;
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt }
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt }
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt confdata->searchnxt = 0;
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt confdata->domainname = lwres_strdup(ctx, word);
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt if (confdata->domainname == NULL)
79ce3a9e82384cc31fd6b86be8f3d1474fcfd9f4Evan Hunt return (LWRES_R_FAILURE);
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt return (LWRES_R_SUCCESS);
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt}
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Huntstatic lwres_result_t
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Huntlwres_conf_parsesearch(lwres_context_t *ctx, FILE *fp) {
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt int idx, delim;
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt char word[LWRES_CONFMAXLINELEN];
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt lwres_conf_t *confdata;
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt confdata = &ctx->confdata;
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt if (confdata->domainname != NULL) {
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt /*
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt * Search and domain are mutually exclusive.
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt */
e11a0c114cdaf8f7e7832e9f1a011138248093a6Evan Hunt CTXFREE(confdata->domainname,
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt strlen(confdata->domainname) + 1);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt confdata->domainname = NULL;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt }
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt /*
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt * Remove any previous search definitions.
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt */
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt for (idx = 0 ; idx < LWRES_CONFMAXSEARCH ; idx++) {
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt if (confdata->search[idx] != NULL) {
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt CTXFREE(confdata->search[idx],
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt strlen(confdata->search[idx])+1);
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt confdata->search[idx] = NULL;
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt }
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt }
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt confdata->searchnxt = 0;
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt delim = getword(fp, word, sizeof(word));
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt if (strlen(word) == 0)
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt return (LWRES_R_FAILURE); /* Nothing else on line. */
9c03f13e18c1b0c32f62391a17300378605bbc7bEvan Hunt
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt idx = 0;
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt while (strlen(word) > 0) {
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt if (confdata->searchnxt == LWRES_CONFMAXSEARCH)
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt return (LWRES_R_FAILURE); /* Too many domains. */
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt confdata->search[idx] = lwres_strdup(ctx, word);
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt if (confdata->search[idx] == NULL)
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt return (LWRES_R_FAILURE);
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt idx++;
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt confdata->searchnxt++;
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt if (delim == EOF || delim == '\n')
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt break;
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt else
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt delim = getword(fp, word, sizeof(word));
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt }
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt return (LWRES_R_SUCCESS);
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt}
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Huntstatic lwres_result_t
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Huntlwres_create_addr(const char *buffer, lwres_addr_t *addr) {
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt unsigned char addrbuff[NS_IN6ADDRSZ];
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt unsigned int len;
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt if (lwres_net_pton(AF_INET, buffer, &addrbuff) == 1) {
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt addr->family = LWRES_ADDRTYPE_V4;
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt addr->length = NS_INADDRSZ;
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt len = 4;
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt#if defined(AF_INET6)
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt } else if (lwres_net_pton(AF_INET6, buffer, &addrbuff) == 1) {
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt addr->family = LWRES_ADDRTYPE_V6;
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt addr->length = NS_IN6ADDRSZ;
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt len = 16;
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt#endif
61bcc232038f0a2cb77ed6269675fdc288f5ec98Evan Hunt } else {
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt return (LWRES_R_FAILURE); /* Unrecongnised format. */
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt }
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt memcpy((void *)addr->address, addrbuff, len);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt return (LWRES_R_SUCCESS);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt}
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Huntstatic lwres_result_t
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Huntlwres_conf_parsesortlist(lwres_context_t *ctx, FILE *fp) {
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt int delim, res, idx;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt char word[LWRES_CONFMAXLINELEN];
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt char *p;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt lwres_conf_t *confdata;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt confdata = &ctx->confdata;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt delim = getword(fp, word, sizeof(word));
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt if (strlen(word) == 0)
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt return (LWRES_R_FAILURE); /* Empty line after keyword. */
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt while (strlen(word) > 0) {
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt if (confdata->sortlistnxt == LWRES_CONFMAXSORTLIST)
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt return (LWRES_R_FAILURE); /* Too many values. */
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt p = strchr(word, '/');
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt if (p != NULL)
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt *p++ = '\0';
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt idx = confdata->sortlistnxt;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt res = lwres_create_addr(word, &confdata->sortlist[idx].addr);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt if (res != LWRES_R_SUCCESS)
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt return (res);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt if (p != NULL) {
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt res = lwres_create_addr(p,
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt &confdata->sortlist[idx].mask);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt if (res != LWRES_R_SUCCESS)
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt return (res);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt } else {
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt /*
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt * Make up a mask.
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt */
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt confdata->sortlist[idx].mask =
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt confdata->sortlist[idx].addr;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt memset(&confdata->sortlist[idx].mask, 0xff,
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt confdata->sortlist[idx].addr.length);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt }
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt confdata->sortlistnxt++;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt if (delim == EOF || delim == '\n')
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt break;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt else
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt delim = getword(fp, word, sizeof(word));
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt }
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt return (LWRES_R_SUCCESS);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt}
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Huntstatic lwres_result_t
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Huntlwres_conf_parseoption(lwres_context_t *ctx, FILE *fp) {
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt int delim;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt long ndots;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt char *p;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt char word[LWRES_CONFMAXLINELEN];
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt lwres_conf_t *confdata;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt REQUIRE(ctx != NULL);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt confdata = &ctx->confdata;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt delim = getword(fp, word, sizeof(word));
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt if (strlen(word) == 0)
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt return (LWRES_R_FAILURE); /* Empty line after keyword. */
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt while (strlen(word) > 0) {
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt if (strcmp("debug", word) == 0) {
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt confdata->resdebug = 1;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt } else if (strcmp("no_tld_query", word) == 0) {
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt confdata->no_tld_query = 1;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt } else if (strncmp("ndots:", word, 6) == 0) {
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt ndots = strtol(word + 6, &p, 10);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt if (*p != '\0') /* Bad string. */
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt return (LWRES_R_FAILURE);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt if (ndots < 0 || ndots > 0xff) /* Out of range. */
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt return (LWRES_R_FAILURE);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt confdata->ndots = ndots;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt }
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt if (delim == EOF || delim == '\n')
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt break;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt else
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt delim = getword(fp, word, sizeof(word));
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt }
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt return (LWRES_R_SUCCESS);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt}
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Huntlwres_result_t
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Huntlwres_conf_parse(lwres_context_t *ctx, const char *filename) {
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt FILE *fp = NULL;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt char word[256];
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt lwres_result_t rval;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt lwres_conf_t *confdata;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt REQUIRE(ctx != NULL);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt confdata = &ctx->confdata;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt REQUIRE(filename != NULL);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt REQUIRE(strlen(filename) > 0);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt REQUIRE(confdata != NULL);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt rval = LWRES_R_FAILURE; /* Make compiler happy. */
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt errno = 0;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt if ((fp = fopen(filename, "r")) == NULL)
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt return (LWRES_R_FAILURE);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt do {
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt (void)getword(fp, word, sizeof(word));
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt if (strlen(word) == 0) {
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt rval = LWRES_R_SUCCESS;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt break;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt }
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt if (strcmp(word, "nameserver") == 0)
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt rval = lwres_conf_parsenameserver(ctx, fp);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt else if (strcmp(word, "domain") == 0)
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt rval = lwres_conf_parsedomain(ctx, fp);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt else if (strcmp(word, "search") == 0)
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt rval = lwres_conf_parsesearch(ctx, fp);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt else if (strcmp(word, "sortlist") == 0)
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt rval = lwres_conf_parsesortlist(ctx, fp);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt else if (strcmp(word, "option") == 0)
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt rval = lwres_conf_parseoption(ctx, fp);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt } while (rval == LWRES_R_SUCCESS);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt fclose(fp);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt return (rval);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt}
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Huntlwres_result_t
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Huntlwres_conf_print(lwres_context_t *ctx, FILE *fp) {
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt int i;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt const char *p;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt lwres_conf_t *confdata;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt REQUIRE(ctx != NULL);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt confdata = &ctx->confdata;
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt REQUIRE(confdata->nsnext <= LWRES_CONFMAXNAMESERVERS);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt for (i = 0 ; i < confdata->nsnext ; i++) {
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt p = lwres_net_ntop(confdata->nameservers[i].family,
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt confdata->nameservers[i].address,
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt tmp, sizeof(tmp));
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt if (p != tmp)
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt return (LWRES_R_FAILURE);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt fprintf(fp, "nameserver %s\n", tmp);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt }
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt if (confdata->domainname != NULL) {
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt fprintf(fp, "domain %s\n", confdata->domainname);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt } else if (confdata->searchnxt > 0) {
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt REQUIRE(confdata->searchnxt <= LWRES_CONFMAXSEARCH);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt fprintf(fp, "search");
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt for (i = 0 ; i < confdata->searchnxt ; i++)
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt fprintf(fp, " %s", confdata->search[i]);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt fputc('\n', fp);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt }
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt REQUIRE(confdata->sortlistnxt <= LWRES_CONFMAXSORTLIST);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt if (confdata->sortlistnxt > 0) {
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt fputs("sortlist", fp);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt for (i = 0 ; i < confdata->sortlistnxt ; i++) {
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt p = lwres_net_ntop(confdata->sortlist[i].addr.family,
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt confdata->sortlist[i].addr.address,
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt tmp, sizeof(tmp));
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt if (p != tmp)
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt return (LWRES_R_FAILURE);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt fprintf(fp, " %s", tmp);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt if (confdata->sortlist[i].mask.length > 0) {
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt p = lwres_net_ntop
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt (confdata->sortlist[i].mask.family,
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt confdata->sortlist[i].mask.address,
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt tmp, sizeof(tmp));
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt if (p != tmp)
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt return (LWRES_R_FAILURE);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt fprintf(fp, "/%s", tmp);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt }
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt }
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt fputc('\n', fp);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt }
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt if (confdata->resdebug)
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt fprintf(fp, "options debug\n");
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt if (confdata->ndots > 0)
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt fprintf(fp, "options ndots:%d\n", confdata->ndots);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt if (confdata->no_tld_query)
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt fprintf(fp, "options no_tld_query\n");
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt return (LWRES_R_SUCCESS);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt}
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Huntlwres_conf_t *
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Huntlwres_conf_get(lwres_context_t *ctx) {
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt REQUIRE(ctx != NULL);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt return (&ctx->confdata);
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt}
75b8de87879ad017c9cd2ffc328e5d2391d16e99Evan Hunt