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