lwconfig.c revision ae6860081ff6cd3c1ec8329c4deb2d0da4802116
1633838b8255282d10af15c5c84cee5a51466712Bob Halley/*
499b34cea04a46823d003d4c0520c8b03e8513cbBrian Wellington * Copyright (C) 2000, 2001 Internet Software Consortium.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * Permission to use, copy, modify, and distribute this software for any
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * purpose with or without fee is hereby granted, provided that the above
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * copyright notice and this permission notice appear in all copies.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
15a44745412679c30a6d022733925af70a38b715David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
15a44745412679c30a6d022733925af70a38b715David Lawrence * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
15a44745412679c30a6d022733925af70a38b715David Lawrence * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
15a44745412679c30a6d022733925af70a38b715David Lawrence * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
15a44745412679c30a6d022733925af70a38b715David Lawrence * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
15a44745412679c30a6d022733925af70a38b715David Lawrence * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
15a44745412679c30a6d022733925af70a38b715David Lawrence * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
15a44745412679c30a6d022733925af70a38b715David Lawrence * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1633838b8255282d10af15c5c84cee5a51466712Bob Halley */
94e25967cda41b886e33ec254b917d21df21a187Bob Halley
33f87146a856eb6c3dfd55a8ee9c173c94f82150Andreas Gustafsson/* $Id: lwconfig.c,v 1.32 2001/04/12 22:45:12 tale Exp $ */
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley/***
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley *** Module for parsing resolv.conf files.
94e25967cda41b886e33ec254b917d21df21a187Bob Halley ***
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence *** entry points are:
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews *** lwres_conf_init(lwres_context_t *ctx)
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence *** intializes data structure for subsequent config parsing.
94e25967cda41b886e33ec254b917d21df21a187Bob Halley ***
6e3a8256eed85f6704861d269ccfb35bdaeed5ffDavid Lawrence *** lwres_conf_parse(lwres_context_t *ctx, const char *filename)
6e3a8256eed85f6704861d269ccfb35bdaeed5ffDavid Lawrence *** parses a file and fills in the data structure.
df0f58959ed82a2a43ca8d816ce9592541df9f2fMark Andrews ***
33f87146a856eb6c3dfd55a8ee9c173c94f82150Andreas Gustafsson *** lwres_conf_print(lwres_context_t *ctx, FILE *fp)
4b87939256ede703385e9cab92d3c58d03c31098Mark Andrews *** prints the config data structure to the FILE.
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence ***
94e25967cda41b886e33ec254b917d21df21a187Bob Halley *** lwres_conf_clear(lwres_context_t *ctx)
364a82f7c25b62967678027043425201a5e5171aBob Halley *** frees up all the internal memory used by the config data
94e25967cda41b886e33ec254b917d21df21a187Bob Halley *** structure, returning it to the lwres_context_t.
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence ***
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence ***/
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence#include <config.h>
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence#include <assert.h>
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence#include <ctype.h>
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence#include <errno.h>
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence#include <stdlib.h>
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence#include <stdio.h>
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence#include <string.h>
df0f58959ed82a2a43ca8d816ce9592541df9f2fMark Andrews#include <unistd.h>
df0f58959ed82a2a43ca8d816ce9592541df9f2fMark Andrews
df0f58959ed82a2a43ca8d816ce9592541df9f2fMark Andrews#include <sys/types.h>
df0f58959ed82a2a43ca8d816ce9592541df9f2fMark Andrews
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley#include <lwres/lwbuffer.h>
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley#include <lwres/lwres.h>
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley#include <lwres/net.h>
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley#include <lwres/result.h>
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley#include "assert_p.h"
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley#include "context_p.h"
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews#if ! defined(NS_INADDRSZ)
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews#define NS_INADDRSZ 4
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews#endif
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews#if ! defined(NS_IN6ADDRSZ)
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews#define NS_IN6ADDRSZ 16
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews#endif
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrewsstatic lwres_result_t
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrewslwres_conf_parsenameserver(lwres_context_t *ctx, FILE *fp);
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrewsstatic lwres_result_t
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrewslwres_conf_parselwserver(lwres_context_t *ctx, FILE *fp);
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrewsstatic lwres_result_t
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrewslwres_conf_parsedomain(lwres_context_t *ctx, FILE *fp);
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrewsstatic lwres_result_t
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrewslwres_conf_parsesearch(lwres_context_t *ctx, FILE *fp);
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrewsstatic lwres_result_t
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrewslwres_conf_parsesortlist(lwres_context_t *ctx, FILE *fp);
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrewsstatic lwres_result_t
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halleylwres_conf_parseoption(lwres_context_t *ctx, FILE *fp);
3740b569ae76295b941d57a724a43beb75b533baBob Halley
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrencestatic void
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrencelwres_resetaddr(lwres_addr_t *addr);
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrencestatic lwres_result_t
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halleylwres_create_addr(const char *buff, lwres_addr_t *addr, int convert_zero);
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halleystatic int lwresaddr2af(int lwresaddrtype);
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halleystatic int
3740b569ae76295b941d57a724a43beb75b533baBob Halleylwresaddr2af(int lwresaddrtype)
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley{
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence int af = 0;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley switch (lwresaddrtype) {
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley case LWRES_ADDRTYPE_V4:
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley af = AF_INET;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley break;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley case LWRES_ADDRTYPE_V6:
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley af = AF_INET6;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley break;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley }
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley return (af);
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley}
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence/*
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence * Eat characters from FP until EOL or EOF. Returns EOF or '\n'
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence */
232fd751edcb5dd2b1fd2666e039efe83d2e2b55Michael Sawyerstatic int
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrenceeatline(FILE *fp) {
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence int ch;
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence ch = fgetc(fp);
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence while (ch != '\n' && ch != EOF)
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley ch = fgetc(fp);
3740b569ae76295b941d57a724a43beb75b533baBob Halley
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley return (ch);
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley}
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley/*
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley * Eats white space up to next newline or non-whitespace character (of
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley * EOF). Returns the last character read. Comments are considered white
3740b569ae76295b941d57a724a43beb75b533baBob Halley * space.
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley */
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrencestatic int
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halleyeatwhite(FILE *fp) {
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley int ch;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley ch = fgetc(fp);
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley while (ch != '\n' && ch != EOF && isspace((unsigned char)ch))
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley ch = fgetc(fp);
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
df0f58959ed82a2a43ca8d816ce9592541df9f2fMark Andrews if (ch == ';' || ch == '#')
4d6964d70a114b53a11a3bd778d9b8f5179a7934Bob Halley ch = eatline(fp);
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley
94e25967cda41b886e33ec254b917d21df21a187Bob Halley return (ch);
4b87939256ede703385e9cab92d3c58d03c31098Mark Andrews}
94e25967cda41b886e33ec254b917d21df21a187Bob Halley
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence/*
94e25967cda41b886e33ec254b917d21df21a187Bob Halley * Skip over any leading whitespace and then read in the next sequence of
4b87939256ede703385e9cab92d3c58d03c31098Mark Andrews * non-whitespace characters. In this context newline is not considered
4b87939256ede703385e9cab92d3c58d03c31098Mark Andrews * whitespace. Returns EOF on end-of-file, or the character
94e25967cda41b886e33ec254b917d21df21a187Bob Halley * that caused the reading to stop.
94e25967cda41b886e33ec254b917d21df21a187Bob Halley */
94e25967cda41b886e33ec254b917d21df21a187Bob Halleystatic int
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrencegetword(FILE *fp, char *buffer, size_t size) {
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence int ch;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence char *p = buffer;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence REQUIRE(buffer != NULL);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence REQUIRE(size > 0);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
df0f58959ed82a2a43ca8d816ce9592541df9f2fMark Andrews *p = '\0';
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews ch = eatwhite(fp);
df0f58959ed82a2a43ca8d816ce9592541df9f2fMark Andrews
df0f58959ed82a2a43ca8d816ce9592541df9f2fMark Andrews if (ch == EOF)
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence return (EOF);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
df0f58959ed82a2a43ca8d816ce9592541df9f2fMark Andrews do {
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence *p = '\0';
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (ch == EOF || isspace((unsigned char)ch))
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence break;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence else if ((size_t) (p - buffer) == size - 1)
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence return (EOF); /* Not enough space. */
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence *p++ = (char)ch;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley ch = fgetc(fp);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence } while (1);
94e25967cda41b886e33ec254b917d21df21a187Bob Halley
94e25967cda41b886e33ec254b917d21df21a187Bob Halley return (ch);
94e25967cda41b886e33ec254b917d21df21a187Bob Halley}
94e25967cda41b886e33ec254b917d21df21a187Bob Halley
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halleystatic void
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halleylwres_resetaddr(lwres_addr_t *addr) {
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley REQUIRE(addr != NULL);
4b87939256ede703385e9cab92d3c58d03c31098Mark Andrews
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley memset(addr->address, 0, LWRES_ADDR_MAXLEN);
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley addr->family = 0;
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley addr->length = 0;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence}
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halleystatic char *
4b87939256ede703385e9cab92d3c58d03c31098Mark Andrewslwres_strdup(lwres_context_t *ctx, const char *str) {
4b87939256ede703385e9cab92d3c58d03c31098Mark Andrews char *p;
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley REQUIRE(str != NULL);
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley REQUIRE(strlen(str) > 0);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence p = CTXMALLOC(strlen(str) + 1);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence if (p != NULL)
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence strcpy(p, str);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence return (p);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence}
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrewsvoid
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrewslwres_conf_init(lwres_context_t *ctx) {
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews int i;
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews lwres_conf_t *confdata;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence REQUIRE(ctx != NULL);
70ec7dd74103fa9e92a6d56a0e3b0fc30e17af0dMark Andrews confdata = &ctx->confdata;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence confdata->nsnext = 0;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence confdata->lwnext = 0;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence confdata->domainname = NULL;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence confdata->searchnxt = 0;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence confdata->sortlistnxt = 0;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence confdata->resdebug = 0;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence confdata->ndots = 1;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence confdata->no_tld_query = 0;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence for (i = 0 ; i < LWRES_CONFMAXNAMESERVERS ; i++)
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley lwres_resetaddr(&confdata->nameservers[i]);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence for (i = 0 ; i < LWRES_CONFMAXSEARCH ; i++)
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley confdata->search[i] = NULL;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley for (i = 0 ; i < LWRES_CONFMAXSORTLIST ; i++) {
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley lwres_resetaddr(&confdata->sortlist[i].addr);
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley lwres_resetaddr(&confdata->sortlist[i].mask);
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley }
afdb3abb9b06ed4070ac9021f1f4427b4cb3a286Bob Halley}
94e25967cda41b886e33ec254b917d21df21a187Bob Halley
3740b569ae76295b941d57a724a43beb75b533baBob Halleyvoid
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halleylwres_conf_clear(lwres_context_t *ctx) {
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence int i;
94e25967cda41b886e33ec254b917d21df21a187Bob Halley lwres_conf_t *confdata;
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley
94e25967cda41b886e33ec254b917d21df21a187Bob Halley REQUIRE(ctx != NULL);
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley confdata = &ctx->confdata;
94e25967cda41b886e33ec254b917d21df21a187Bob Halley
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley for (i = 0 ; i < confdata->nsnext ; i++)
94e25967cda41b886e33ec254b917d21df21a187Bob Halley lwres_resetaddr(&confdata->nameservers[i]);
bf6d2e39124ab3d51c253f7acad9a4abef059be6Bob Halley
94e25967cda41b886e33ec254b917d21df21a187Bob Halley if (confdata->domainname != NULL) {
94e25967cda41b886e33ec254b917d21df21a187Bob Halley CTXFREE(confdata->domainname,
94e25967cda41b886e33ec254b917d21df21a187Bob Halley strlen(confdata->domainname) + 1);
94e25967cda41b886e33ec254b917d21df21a187Bob Halley confdata->domainname = NULL;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence }
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley for (i = 0 ; i < confdata->searchnxt ; i++) {
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence if (confdata->search[i] != NULL) {
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence CTXFREE(confdata->search[i],
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence strlen(confdata->search[i]) + 1);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence confdata->search[i] = NULL;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence }
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence }
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence for (i = 0 ; i < LWRES_CONFMAXSORTLIST ; i++) {
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence lwres_resetaddr(&confdata->sortlist[i].addr);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence lwres_resetaddr(&confdata->sortlist[i].mask);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence }
94e25967cda41b886e33ec254b917d21df21a187Bob Halley
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley confdata->nsnext = 0;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley confdata->lwnext = 0;
c89ac488df58cf6a37918cd00236eedf015830f8Andreas Gustafsson confdata->domainname = NULL;
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley confdata->searchnxt = 0;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence confdata->sortlistnxt = 0;
94e25967cda41b886e33ec254b917d21df21a187Bob Halley confdata->resdebug = 0;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence confdata->ndots = 1;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence confdata->no_tld_query = 0;
94e25967cda41b886e33ec254b917d21df21a187Bob Halley}
94e25967cda41b886e33ec254b917d21df21a187Bob Halley
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrencestatic lwres_result_t
3740b569ae76295b941d57a724a43beb75b533baBob Halleylwres_conf_parsenameserver(lwres_context_t *ctx, FILE *fp) {
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley char word[LWRES_CONFMAXLINELEN];
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence int res;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence lwres_conf_t *confdata;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence confdata = &ctx->confdata;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence if (confdata->nsnext == LWRES_CONFMAXNAMESERVERS)
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence return (LWRES_R_SUCCESS);
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley res = getword(fp, word, sizeof(word));
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley if (strlen(word) == 0)
94e25967cda41b886e33ec254b917d21df21a187Bob Halley return (LWRES_R_FAILURE); /* Nothing on line. */
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence else if (res == ' ' || res == '\t')
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley res = eatwhite(fp);
4bed2e84a34b37259b85e5c092d51c122ef58c3cBob Halley
94e25967cda41b886e33ec254b917d21df21a187Bob Halley if (res != EOF && res != '\n')
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence return (LWRES_R_FAILURE); /* Extra junk on line. */
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
94e25967cda41b886e33ec254b917d21df21a187Bob Halley res = lwres_create_addr(word,
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence &confdata->nameservers[confdata->nsnext++], 1);
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence if (res != LWRES_R_SUCCESS)
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence return (res);
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence return (LWRES_R_SUCCESS);
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence}
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrencestatic lwres_result_t
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrencelwres_conf_parselwserver(lwres_context_t *ctx, FILE *fp) {
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence char word[LWRES_CONFMAXLINELEN];
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence int res;
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence lwres_conf_t *confdata;
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence confdata = &ctx->confdata;
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence if (confdata->lwnext == LWRES_CONFMAXLWSERVERS)
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence return (LWRES_R_SUCCESS);
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence res = getword(fp, word, sizeof(word));
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence if (strlen(word) == 0)
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence return (LWRES_R_FAILURE); /* Nothing on line. */
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence else if (res == ' ' || res == '\t')
4ad9b25e6ddf948ffb3b8198c5540d251f26c52eDavid Lawrence res = eatwhite(fp);
114d0d1642b5ede0ab154532159fe38c30762d82David Lawrence
114d0d1642b5ede0ab154532159fe38c30762d82David Lawrence if (res != EOF && res != '\n')
114d0d1642b5ede0ab154532159fe38c30762d82David Lawrence return (LWRES_R_FAILURE); /* Extra junk on line. */
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence res = lwres_create_addr(word,
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence &confdata->lwservers[confdata->lwnext++], 1);
114d0d1642b5ede0ab154532159fe38c30762d82David Lawrence if (res != LWRES_R_SUCCESS)
114d0d1642b5ede0ab154532159fe38c30762d82David Lawrence return (res);
114d0d1642b5ede0ab154532159fe38c30762d82David Lawrence
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence return (LWRES_R_SUCCESS);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence}
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrencestatic lwres_result_t
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrencelwres_conf_parsedomain(lwres_context_t *ctx, FILE *fp) {
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence char word[LWRES_CONFMAXLINELEN];
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence int res, i;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence lwres_conf_t *confdata;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence confdata = &ctx->confdata;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence res = getword(fp, word, sizeof(word));
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence if (strlen(word) == 0)
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence return (LWRES_R_FAILURE); /* Nothing else on line. */
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence else if (res == ' ' || res == '\t')
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence res = eatwhite(fp);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence if (res != EOF && res != '\n')
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence return (LWRES_R_FAILURE); /* Extra junk on line. */
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (confdata->domainname != NULL)
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence CTXFREE(confdata->domainname,
826c281a05cb89c9c28621937dc83fa676a5b207David Lawrence strlen(confdata->domainname) + 1); /* */
826c281a05cb89c9c28621937dc83fa676a5b207David Lawrence
826c281a05cb89c9c28621937dc83fa676a5b207David Lawrence /*
826c281a05cb89c9c28621937dc83fa676a5b207David Lawrence * Search and domain are mutually exclusive.
826c281a05cb89c9c28621937dc83fa676a5b207David Lawrence */
826c281a05cb89c9c28621937dc83fa676a5b207David Lawrence for (i = 0 ; i < LWRES_CONFMAXSEARCH ; i++) {
826c281a05cb89c9c28621937dc83fa676a5b207David Lawrence if (confdata->search[i] != NULL) {
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence CTXFREE(confdata->search[i],
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence strlen(confdata->search[i])+1);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence confdata->search[i] = NULL;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence }
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence }
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence confdata->searchnxt = 0;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence confdata->domainname = lwres_strdup(ctx, word);
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
826c281a05cb89c9c28621937dc83fa676a5b207David Lawrence if (confdata->domainname == NULL)
826c281a05cb89c9c28621937dc83fa676a5b207David Lawrence return (LWRES_R_FAILURE);
604a596b5f97f09f6a4de67fe2f076d3c939df33David Lawrence
826c281a05cb89c9c28621937dc83fa676a5b207David Lawrence return (LWRES_R_SUCCESS);
604a596b5f97f09f6a4de67fe2f076d3c939df33David Lawrence}
604a596b5f97f09f6a4de67fe2f076d3c939df33David Lawrence
604a596b5f97f09f6a4de67fe2f076d3c939df33David Lawrencestatic lwres_result_t
604a596b5f97f09f6a4de67fe2f076d3c939df33David Lawrencelwres_conf_parsesearch(lwres_context_t *ctx, FILE *fp) {
604a596b5f97f09f6a4de67fe2f076d3c939df33David Lawrence int idx, delim;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence char word[LWRES_CONFMAXLINELEN];
604a596b5f97f09f6a4de67fe2f076d3c939df33David Lawrence lwres_conf_t *confdata;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence confdata = &ctx->confdata;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence if (confdata->domainname != NULL) {
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence /*
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence * Search and domain are mutually exclusive.
114d0d1642b5ede0ab154532159fe38c30762d82David Lawrence */
114d0d1642b5ede0ab154532159fe38c30762d82David Lawrence CTXFREE(confdata->domainname,
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence strlen(confdata->domainname) + 1);
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence confdata->domainname = NULL;
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence }
114d0d1642b5ede0ab154532159fe38c30762d82David Lawrence
114d0d1642b5ede0ab154532159fe38c30762d82David Lawrence /*
114d0d1642b5ede0ab154532159fe38c30762d82David Lawrence * Remove any previous search definitions.
c8563aaf86c04f0e2284bcc8e444a0651c157ea0Andreas Gustafsson */
c8563aaf86c04f0e2284bcc8e444a0651c157ea0Andreas Gustafsson for (idx = 0 ; idx < LWRES_CONFMAXSEARCH ; idx++) {
c8563aaf86c04f0e2284bcc8e444a0651c157ea0Andreas Gustafsson if (confdata->search[idx] != NULL) {
c8563aaf86c04f0e2284bcc8e444a0651c157ea0Andreas Gustafsson CTXFREE(confdata->search[idx],
c8563aaf86c04f0e2284bcc8e444a0651c157ea0Andreas Gustafsson strlen(confdata->search[idx])+1);
c8563aaf86c04f0e2284bcc8e444a0651c157ea0Andreas Gustafsson confdata->search[idx] = NULL;
6184f9fc1e65ef131ea308a1a92882595bb1aeeaAndreas Gustafsson }
6184f9fc1e65ef131ea308a1a92882595bb1aeeaAndreas Gustafsson }
c8563aaf86c04f0e2284bcc8e444a0651c157ea0Andreas Gustafsson confdata->searchnxt = 0;
c8563aaf86c04f0e2284bcc8e444a0651c157ea0Andreas Gustafsson
c8563aaf86c04f0e2284bcc8e444a0651c157ea0Andreas Gustafsson delim = getword(fp, word, sizeof(word));
c8563aaf86c04f0e2284bcc8e444a0651c157ea0Andreas Gustafsson if (strlen(word) == 0)
c8563aaf86c04f0e2284bcc8e444a0651c157ea0Andreas Gustafsson return (LWRES_R_FAILURE); /* Nothing else on line. */
c8563aaf86c04f0e2284bcc8e444a0651c157ea0Andreas Gustafsson
idx = 0;
while (strlen(word) > 0) {
if (confdata->searchnxt == LWRES_CONFMAXSEARCH)
goto ignore; /* Too many domains. */
confdata->search[idx] = lwres_strdup(ctx, word);
if (confdata->search[idx] == NULL)
return (LWRES_R_FAILURE);
idx++;
confdata->searchnxt++;
ignore:
if (delim == EOF || delim == '\n')
break;
else
delim = getword(fp, word, sizeof(word));
}
return (LWRES_R_SUCCESS);
}
static lwres_result_t
lwres_create_addr(const char *buffer, lwres_addr_t *addr, int convert_zero) {
struct in_addr v4;
struct in6_addr v6;
if (lwres_net_aton(buffer, &v4) == 1) {
if (convert_zero) {
unsigned char zeroaddress[] = {0, 0, 0, 0};
unsigned char loopaddress[] = {127, 0, 0, 1};
if (memcmp(&v4, zeroaddress, 4) == 0)
memcpy(&v4, loopaddress, 4);
}
addr->family = LWRES_ADDRTYPE_V4;
addr->length = NS_INADDRSZ;
memcpy((void *)addr->address, &v4, NS_INADDRSZ);
} else if (lwres_net_pton(AF_INET6, buffer, &v6) == 1) {
addr->family = LWRES_ADDRTYPE_V6;
addr->length = NS_IN6ADDRSZ;
memcpy((void *)addr->address, &v6, NS_IN6ADDRSZ);
} else {
return (LWRES_R_FAILURE); /* Unrecognised format. */
}
return (LWRES_R_SUCCESS);
}
static lwres_result_t
lwres_conf_parsesortlist(lwres_context_t *ctx, FILE *fp) {
int delim, res, idx;
char word[LWRES_CONFMAXLINELEN];
char *p;
lwres_conf_t *confdata;
confdata = &ctx->confdata;
delim = getword(fp, word, sizeof(word));
if (strlen(word) == 0)
return (LWRES_R_FAILURE); /* Empty line after keyword. */
while (strlen(word) > 0) {
if (confdata->sortlistnxt == LWRES_CONFMAXSORTLIST)
return (LWRES_R_FAILURE); /* Too many values. */
p = strchr(word, '/');
if (p != NULL)
*p++ = '\0';
idx = confdata->sortlistnxt;
res = lwres_create_addr(word, &confdata->sortlist[idx].addr, 1);
if (res != LWRES_R_SUCCESS)
return (res);
if (p != NULL) {
res = lwres_create_addr(p,
&confdata->sortlist[idx].mask,
0);
if (res != LWRES_R_SUCCESS)
return (res);
} else {
/*
* Make up a mask.
*/
confdata->sortlist[idx].mask =
confdata->sortlist[idx].addr;
memset(&confdata->sortlist[idx].mask.address, 0xff,
confdata->sortlist[idx].addr.length);
}
confdata->sortlistnxt++;
if (delim == EOF || delim == '\n')
break;
else
delim = getword(fp, word, sizeof(word));
}
return (LWRES_R_SUCCESS);
}
static lwres_result_t
lwres_conf_parseoption(lwres_context_t *ctx, FILE *fp) {
int delim;
long ndots;
char *p;
char word[LWRES_CONFMAXLINELEN];
lwres_conf_t *confdata;
REQUIRE(ctx != NULL);
confdata = &ctx->confdata;
delim = getword(fp, word, sizeof(word));
if (strlen(word) == 0)
return (LWRES_R_FAILURE); /* Empty line after keyword. */
while (strlen(word) > 0) {
if (strcmp("debug", word) == 0) {
confdata->resdebug = 1;
} else if (strcmp("no_tld_query", word) == 0) {
confdata->no_tld_query = 1;
} else if (strncmp("ndots:", word, 6) == 0) {
ndots = strtol(word + 6, &p, 10);
if (*p != '\0') /* Bad string. */
return (LWRES_R_FAILURE);
if (ndots < 0 || ndots > 0xff) /* Out of range. */
return (LWRES_R_FAILURE);
confdata->ndots = (lwres_uint8_t)ndots;
}
if (delim == EOF || delim == '\n')
break;
else
delim = getword(fp, word, sizeof(word));
}
return (LWRES_R_SUCCESS);
}
lwres_result_t
lwres_conf_parse(lwres_context_t *ctx, const char *filename) {
FILE *fp = NULL;
char word[256];
lwres_result_t rval, ret;
lwres_conf_t *confdata;
int stopchar;
REQUIRE(ctx != NULL);
confdata = &ctx->confdata;
REQUIRE(filename != NULL);
REQUIRE(strlen(filename) > 0);
REQUIRE(confdata != NULL);
errno = 0;
if ((fp = fopen(filename, "r")) == NULL)
return (LWRES_R_FAILURE);
ret = LWRES_R_SUCCESS;
do {
stopchar = getword(fp, word, sizeof(word));
if (stopchar == EOF) {
rval = LWRES_R_SUCCESS;
break;
}
if (strlen(word) == 0)
rval = LWRES_R_SUCCESS;
else if (strcmp(word, "nameserver") == 0)
rval = lwres_conf_parsenameserver(ctx, fp);
else if (strcmp(word, "lwserver") == 0)
rval = lwres_conf_parselwserver(ctx, fp);
else if (strcmp(word, "domain") == 0)
rval = lwres_conf_parsedomain(ctx, fp);
else if (strcmp(word, "search") == 0)
rval = lwres_conf_parsesearch(ctx, fp);
else if (strcmp(word, "sortlist") == 0)
rval = lwres_conf_parsesortlist(ctx, fp);
else if (strcmp(word, "option") == 0)
rval = lwres_conf_parseoption(ctx, fp);
else {
/* unrecognised word. Ignore entire line */
rval = LWRES_R_SUCCESS;
stopchar = eatline(fp);
if (stopchar == EOF) {
break;
}
}
if (ret == LWRES_R_SUCCESS && rval != LWRES_R_SUCCESS)
ret = rval;
} while (1);
fclose(fp);
return (ret);
}
lwres_result_t
lwres_conf_print(lwres_context_t *ctx, FILE *fp) {
int i;
int af;
char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
const char *p;
lwres_conf_t *confdata;
lwres_addr_t tmpaddr;
REQUIRE(ctx != NULL);
confdata = &ctx->confdata;
REQUIRE(confdata->nsnext <= LWRES_CONFMAXNAMESERVERS);
for (i = 0 ; i < confdata->nsnext ; i++) {
af = lwresaddr2af(confdata->nameservers[i].family);
p = lwres_net_ntop(af, confdata->nameservers[i].address,
tmp, sizeof(tmp));
if (p != tmp)
return (LWRES_R_FAILURE);
fprintf(fp, "nameserver %s\n", tmp);
}
for (i = 0 ; i < confdata->lwnext ; i++) {
af = lwresaddr2af(confdata->lwservers[i].family);
p = lwres_net_ntop(af, confdata->lwservers[i].address,
tmp, sizeof(tmp));
if (p != tmp)
return (LWRES_R_FAILURE);
fprintf(fp, "lwserver %s\n", tmp);
}
if (confdata->domainname != NULL) {
fprintf(fp, "domain %s\n", confdata->domainname);
} else if (confdata->searchnxt > 0) {
REQUIRE(confdata->searchnxt <= LWRES_CONFMAXSEARCH);
fprintf(fp, "search");
for (i = 0 ; i < confdata->searchnxt ; i++)
fprintf(fp, " %s", confdata->search[i]);
fputc('\n', fp);
}
REQUIRE(confdata->sortlistnxt <= LWRES_CONFMAXSORTLIST);
if (confdata->sortlistnxt > 0) {
fputs("sortlist", fp);
for (i = 0 ; i < confdata->sortlistnxt ; i++) {
af = lwresaddr2af(confdata->sortlist[i].addr.family);
p = lwres_net_ntop(af,
confdata->sortlist[i].addr.address,
tmp, sizeof(tmp));
if (p != tmp)
return (LWRES_R_FAILURE);
fprintf(fp, " %s", tmp);
tmpaddr = confdata->sortlist[i].mask;
memset(&tmpaddr.address, 0xff, tmpaddr.length);
if (memcmp(&tmpaddr.address,
confdata->sortlist[i].mask.address,
confdata->sortlist[i].mask.length) != 0) {
af = lwresaddr2af(
confdata->sortlist[i].mask.family);
p = lwres_net_ntop
(af,
confdata->sortlist[i].mask.address,
tmp, sizeof(tmp));
if (p != tmp)
return (LWRES_R_FAILURE);
fprintf(fp, "/%s", tmp);
}
}
fputc('\n', fp);
}
if (confdata->resdebug)
fprintf(fp, "options debug\n");
if (confdata->ndots > 0)
fprintf(fp, "options ndots:%d\n", confdata->ndots);
if (confdata->no_tld_query)
fprintf(fp, "options no_tld_query\n");
return (LWRES_R_SUCCESS);
}
lwres_conf_t *
lwres_conf_get(lwres_context_t *ctx) {
REQUIRE(ctx != NULL);
return (&ctx->confdata);
}