lwconfig.c revision 10a6f640ed599cbe4a8b98c46b71a61d24e5bbe7
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley/*
a7038d1a0513c8e804937ebc95fc9cb3a46c04f5Mark Andrews * Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC")
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Copyright (C) 2000-2003 Internet Software Consortium.
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley *
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * Permission to use, copy, modify, and/or distribute this software for any
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * purpose with or without fee is hereby granted, provided that the above
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * copyright notice and this permission notice appear in all copies.
15a44745412679c30a6d022733925af70a38b715David Lawrence *
15a44745412679c30a6d022733925af70a38b715David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
15a44745412679c30a6d022733925af70a38b715David Lawrence * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15a44745412679c30a6d022733925af70a38b715David Lawrence * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
15a44745412679c30a6d022733925af70a38b715David Lawrence * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15a44745412679c30a6d022733925af70a38b715David Lawrence * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15a44745412679c30a6d022733925af70a38b715David Lawrence * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15a44745412679c30a6d022733925af70a38b715David Lawrence * PERFORMANCE OF THIS SOFTWARE.
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley */
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
80b782f356f0692c11b4e52e8dd46ec41704e5a2Mark Andrews/* $Id: lwconfig.c,v 1.47 2008/12/17 19:19:29 jinmei Exp $ */
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
110d1702731f42dd620879c1d765ebe91f3920ceMichael Graff/*! \file */
110d1702731f42dd620879c1d765ebe91f3920ceMichael Graff
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley/**
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * Module for parsing resolv.conf files.
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence *
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews * lwres_conf_init() creates an empty lwres_conf_t structure for
364a82f7c25b62967678027043425201a5e5171aBob Halley * lightweight resolver context ctx.
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley *
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * lwres_conf_clear() frees up all the internal memory used by that
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley * lwres_conf_t structure in resolver context ctx.
54f959d12b5a1f9315fbf6a776c6d349316e9686Bob Halley *
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * lwres_conf_parse() opens the file filename and parses it to initialise
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * the resolver context ctx's lwres_conf_t structure.
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley *
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * lwres_conf_print() prints the lwres_conf_t structure for resolver
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * context ctx to the FILE fp.
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley *
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * \section lwconfig_return Return Values
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley *
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * lwres_conf_parse() returns #LWRES_R_SUCCESS if it successfully read and
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * parsed filename. It returns #LWRES_R_FAILURE if filename could not be
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * opened or contained incorrect resolver statements.
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley *
078d49b63324f01d98301ee21671abee0c41fcdeBob Halley * lwres_conf_print() returns #LWRES_R_SUCCESS unless an error occurred
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * when converting the network addresses to a numeric host address
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * string. If this happens, the function returns #LWRES_R_FAILURE.
904a5734375869ffb504ed8cde6b68cafadb6d64Bob Halley *
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * \section lwconfig_see See Also
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley *
ca67883a666bdf314d3da958d5195e7215b1f797Bob Halley * stdio(3), \link resolver resolver \endlink
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley *
b12f0228b32775ee688ed21ddbf3a116c1adfb43Michael Graff * \section files Files
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley *
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * /etc/resolv.conf
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley */
80b782f356f0692c11b4e52e8dd46ec41704e5a2Mark Andrews
e496615043400500492fa7b891c515c8e7cb7d08Bob Halley#include <config.h>
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#include <assert.h>
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#include <ctype.h>
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#include <errno.h>
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#include <stdlib.h>
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#include <stdio.h>
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#include <string.h>
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#include <unistd.h>
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
078d49b63324f01d98301ee21671abee0c41fcdeBob Halley#include <lwres/lwbuffer.h>
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#include <lwres/lwres.h>
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence#include <lwres/net.h>
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#include <lwres/result.h>
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
904a5734375869ffb504ed8cde6b68cafadb6d64Bob Halley#include "assert_p.h"
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#include "context_p.h"
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
ca67883a666bdf314d3da958d5195e7215b1f797Bob Halley
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley#if ! defined(NS_INADDRSZ)
b12f0228b32775ee688ed21ddbf3a116c1adfb43Michael Graff#define NS_INADDRSZ 4
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#endif
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#if ! defined(NS_IN6ADDRSZ)
80b782f356f0692c11b4e52e8dd46ec41704e5a2Mark Andrews#define NS_IN6ADDRSZ 16
e496615043400500492fa7b891c515c8e7cb7d08Bob Halley#endif
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleystatic lwres_result_t
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleylwres_conf_parsenameserver(lwres_context_t *ctx, FILE *fp);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleystatic lwres_result_t
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleylwres_conf_parselwserver(lwres_context_t *ctx, FILE *fp);
2cd0c38115b1efb043ed3104c0d08e51ceade0d7Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleystatic lwres_result_t
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleylwres_conf_parsedomain(lwres_context_t *ctx, FILE *fp);
078d49b63324f01d98301ee21671abee0c41fcdeBob Halley
0180ccf72c79b98eb8ee5abbb7331aec6951dd9fBob Halleystatic lwres_result_t
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleylwres_conf_parsesearch(lwres_context_t *ctx, FILE *fp);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleystatic lwres_result_t
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleylwres_conf_parsesortlist(lwres_context_t *ctx, FILE *fp);
904a5734375869ffb504ed8cde6b68cafadb6d64Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleystatic lwres_result_t
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleylwres_conf_parseoption(lwres_context_t *ctx, FILE *fp);
ca67883a666bdf314d3da958d5195e7215b1f797Bob Halley
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halleystatic void
b12f0228b32775ee688ed21ddbf3a116c1adfb43Michael Grafflwres_resetaddr(lwres_addr_t *addr);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleystatic lwres_result_t
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleylwres_create_addr(const char *buff, lwres_addr_t *addr, int convert_zero);
80b782f356f0692c11b4e52e8dd46ec41704e5a2Mark Andrews
e496615043400500492fa7b891c515c8e7cb7d08Bob Halleystatic int lwresaddr2af(int lwresaddrtype);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halleystatic int
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halleylwresaddr2af(int lwresaddrtype)
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley{
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley int af = 0;
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence switch (lwresaddrtype) {
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley case LWRES_ADDRTYPE_V4:
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley af = AF_INET;
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley break;
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley case LWRES_ADDRTYPE_V6:
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley af = AF_INET6;
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley break;
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley }
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley return (af);
e02884167b7c969b56413f76c48c3802c4dca14dAndreas Gustafsson}
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff/*!
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley * Eat characters from FP until EOL or EOF. Returns EOF or '\n'
e02884167b7c969b56413f76c48c3802c4dca14dAndreas Gustafsson */
e02884167b7c969b56413f76c48c3802c4dca14dAndreas Gustafssonstatic int
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graffeatline(FILE *fp) {
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley int ch;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley ch = fgetc(fp);
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley while (ch != '\n' && ch != EOF)
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley ch = fgetc(fp);
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley return (ch);
e02884167b7c969b56413f76c48c3802c4dca14dAndreas Gustafsson}
e02884167b7c969b56413f76c48c3802c4dca14dAndreas Gustafsson
e02884167b7c969b56413f76c48c3802c4dca14dAndreas Gustafsson
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley/*!
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley * Eats white space up to next newline or non-whitespace character (of
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley * EOF). Returns the last character read. Comments are considered white
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley * space.
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley */
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halleystatic int
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halleyeatwhite(FILE *fp) {
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley int ch;
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley ch = fgetc(fp);
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley while (ch != '\n' && ch != EOF && isspace((unsigned char)ch))
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley ch = fgetc(fp);
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley
e02884167b7c969b56413f76c48c3802c4dca14dAndreas Gustafsson if (ch == ';' || ch == '#')
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley ch = eatline(fp);
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley return (ch);
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley}
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley/*!
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley * Skip over any leading whitespace and then read in the next sequence of
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley * non-whitespace characters. In this context newline is not considered
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley * whitespace. Returns EOF on end-of-file, or the character
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley * that caused the reading to stop.
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley */
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halleystatic int
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halleygetword(FILE *fp, char *buffer, size_t size) {
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley int ch;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley char *p = buffer;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley REQUIRE(buffer != NULL);
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley REQUIRE(size > 0U);
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley *p = '\0';
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley ch = eatwhite(fp);
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley if (ch == EOF)
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley return (EOF);
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley do {
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley *p = '\0';
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley if (ch == EOF || isspace((unsigned char)ch))
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley break;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley else if ((size_t) (p - buffer) == size - 1)
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley return (EOF); /* Not enough space. */
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley *p++ = (char)ch;
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley ch = fgetc(fp);
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley } while (1);
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley return (ch);
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley}
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halleystatic void
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halleylwres_resetaddr(lwres_addr_t *addr) {
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley REQUIRE(addr != NULL);
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley memset(addr->address, 0, LWRES_ADDR_MAXLEN);
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley addr->family = 0;
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley addr->length = 0;
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley}
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halleystatic char *
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halleylwres_strdup(lwres_context_t *ctx, const char *str) {
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley char *p;
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley REQUIRE(str != NULL);
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley REQUIRE(strlen(str) > 0U);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley p = CTXMALLOC(strlen(str) + 1);
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley if (p != NULL)
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley strcpy(p, str);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley return (p);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley}
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley/*% intializes data structure for subsequent config parsing. */
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleyvoid
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleylwres_conf_init(lwres_context_t *ctx) {
078d49b63324f01d98301ee21671abee0c41fcdeBob Halley int i;
0180ccf72c79b98eb8ee5abbb7331aec6951dd9fBob Halley lwres_conf_t *confdata;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley REQUIRE(ctx != NULL);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley confdata = &ctx->confdata;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff confdata->nsnext = 0;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley confdata->lwnext = 0;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley confdata->domainname = NULL;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley confdata->searchnxt = 0;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley confdata->sortlistnxt = 0;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley confdata->resdebug = 0;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley confdata->ndots = 1;
078d49b63324f01d98301ee21671abee0c41fcdeBob Halley confdata->no_tld_query = 0;
0180ccf72c79b98eb8ee5abbb7331aec6951dd9fBob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley for (i = 0; i < LWRES_CONFMAXNAMESERVERS; i++)
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley lwres_resetaddr(&confdata->nameservers[i]);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley for (i = 0; i < LWRES_CONFMAXSEARCH; i++)
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley confdata->search[i] = NULL;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley for (i = 0; i < LWRES_CONFMAXSORTLIST; i++) {
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley lwres_resetaddr(&confdata->sortlist[i].addr);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley lwres_resetaddr(&confdata->sortlist[i].mask);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley }
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley}
078d49b63324f01d98301ee21671abee0c41fcdeBob Halley
0180ccf72c79b98eb8ee5abbb7331aec6951dd9fBob Halley/*% Frees up all the internal memory used by the config data structure, returning it to the lwres_context_t. */
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleyvoid
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleylwres_conf_clear(lwres_context_t *ctx) {
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley int i;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley lwres_conf_t *confdata;
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley REQUIRE(ctx != NULL);
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews confdata = &ctx->confdata;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson for (i = 0; i < confdata->nsnext; i++)
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson lwres_resetaddr(&confdata->nameservers[i]);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson if (confdata->domainname != NULL) {
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson CTXFREE(confdata->domainname,
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson strlen(confdata->domainname) + 1);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson confdata->domainname = NULL;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson }
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson for (i = 0; i < confdata->searchnxt; i++) {
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson if (confdata->search[i] != NULL) {
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley CTXFREE(confdata->search[i],
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews strlen(confdata->search[i]) + 1);
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews confdata->search[i] = NULL;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews }
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews }
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington for (i = 0; i < LWRES_CONFMAXSORTLIST; i++) {
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley lwres_resetaddr(&confdata->sortlist[i].addr);
88a6fef4944a00d8350ffd8b64ef58c694b8335eMark Andrews lwres_resetaddr(&confdata->sortlist[i].mask);
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff }
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews confdata->nsnext = 0;
8af4b7042d84e0699f7c1de306526098030b74a9Mark Andrews confdata->lwnext = 0;
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff confdata->domainname = NULL;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley confdata->searchnxt = 0;
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley confdata->sortlistnxt = 0;
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley confdata->resdebug = 0;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson confdata->ndots = 1;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley confdata->no_tld_query = 0;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews}
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleystatic lwres_result_t
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleylwres_conf_parsenameserver(lwres_context_t *ctx, FILE *fp) {
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley char word[LWRES_CONFMAXLINELEN];
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley int res;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley lwres_conf_t *confdata;
54f959d12b5a1f9315fbf6a776c6d349316e9686Bob Halley lwres_addr_t address;
ccbfddc70ef38263daca312d29bb8c5077e24785Bob Halley
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson confdata = &ctx->confdata;
54f959d12b5a1f9315fbf6a776c6d349316e9686Bob Halley
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley if (confdata->nsnext == LWRES_CONFMAXNAMESERVERS)
89d8adb6663b13435ff9ae1eb53e45da7fa79275Bob Halley return (LWRES_R_SUCCESS);
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley res = getword(fp, word, sizeof(word));
89d8adb6663b13435ff9ae1eb53e45da7fa79275Bob Halley if (strlen(word) == 0U)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff return (LWRES_R_FAILURE); /* Nothing on line. */
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley else if (res == ' ' || res == '\t')
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley res = eatwhite(fp);
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley if (res != EOF && res != '\n')
b2ca6fd3a8293440b4d263723525396059cf2400Brian Wellington return (LWRES_R_FAILURE); /* Extra junk on line. */
b2ca6fd3a8293440b4d263723525396059cf2400Brian Wellington
b2ca6fd3a8293440b4d263723525396059cf2400Brian Wellington res = lwres_create_addr(word, &address, 1);
b2ca6fd3a8293440b4d263723525396059cf2400Brian Wellington if (res == LWRES_R_SUCCESS &&
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington ((address.family == LWRES_ADDRTYPE_V4 && ctx->use_ipv4 == 1) ||
89d8adb6663b13435ff9ae1eb53e45da7fa79275Bob Halley (address.family == LWRES_ADDRTYPE_V6 && ctx->use_ipv6 == 1))) {
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley confdata->nameservers[confdata->nsnext++] = address;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley }
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff return (LWRES_R_SUCCESS);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff}
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halleystatic lwres_result_t
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halleylwres_conf_parselwserver(lwres_context_t *ctx, FILE *fp) {
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson char word[LWRES_CONFMAXLINELEN];
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson int res;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson lwres_conf_t *confdata;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson confdata = &ctx->confdata;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson if (confdata->lwnext == LWRES_CONFMAXLWSERVERS)
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson return (LWRES_R_SUCCESS);
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson res = getword(fp, word, sizeof(word));
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson if (strlen(word) == 0U)
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson return (LWRES_R_FAILURE); /* Nothing on line. */
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson else if (res == ' ' || res == '\t')
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson res = eatwhite(fp);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson if (res != EOF && res != '\n')
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson return (LWRES_R_FAILURE); /* Extra junk on line. */
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson res = lwres_create_addr(word,
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson &confdata->lwservers[confdata->lwnext++], 1);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson if (res != LWRES_R_SUCCESS)
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson return (res);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson return (LWRES_R_SUCCESS);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson}
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafssonstatic lwres_result_t
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrewslwres_conf_parsedomain(lwres_context_t *ctx, FILE *fp) {
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley char word[LWRES_CONFMAXLINELEN];
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson int res, i;
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley lwres_conf_t *confdata;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley confdata = &ctx->confdata;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley res = getword(fp, word, sizeof(word));
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews if (strlen(word) == 0U)
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson return (LWRES_R_FAILURE); /* Nothing else on line. */
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson else if (res == ' ' || res == '\t')
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson res = eatwhite(fp);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson if (res != EOF && res != '\n')
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews return (LWRES_R_FAILURE); /* Extra junk on line. */
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews if (confdata->domainname != NULL)
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews CTXFREE(confdata->domainname,
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews strlen(confdata->domainname) + 1); /* */
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews /*
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews * Search and domain are mutually exclusive.
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews */
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews for (i = 0; i < LWRES_CONFMAXSEARCH; i++) {
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews if (confdata->search[i] != NULL) {
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews CTXFREE(confdata->search[i],
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews strlen(confdata->search[i])+1);
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews confdata->search[i] = NULL;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews }
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews }
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews confdata->searchnxt = 0;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews confdata->domainname = lwres_strdup(ctx, word);
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson if (confdata->domainname == NULL)
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley return (LWRES_R_FAILURE);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley return (LWRES_R_SUCCESS);
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews}
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrewsstatic lwres_result_t
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrewslwres_conf_parsesearch(lwres_context_t *ctx, FILE *fp) {
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews int idx, delim;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson char word[LWRES_CONFMAXLINELEN];
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews lwres_conf_t *confdata;
abc93ada82b37282122f8162f48761eb99736571Mark Andrews
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews confdata = &ctx->confdata;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews if (confdata->domainname != NULL) {
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson /*
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson * Search and domain are mutually exclusive.
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson */
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson CTXFREE(confdata->domainname,
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley strlen(confdata->domainname) + 1);
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley confdata->domainname = NULL;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews }
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews /*
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews * Remove any previous search definitions.
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews */
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews for (idx = 0; idx < LWRES_CONFMAXSEARCH; idx++) {
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews if (confdata->search[idx] != NULL) {
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley CTXFREE(confdata->search[idx],
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley strlen(confdata->search[idx])+1);
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley confdata->search[idx] = NULL;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson }
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews }
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley confdata->searchnxt = 0;
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff delim = getword(fp, word, sizeof(word));
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence if (strlen(word) == 0U)
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff return (LWRES_R_FAILURE); /* Nothing else on line. */
8af4b7042d84e0699f7c1de306526098030b74a9Mark Andrews
8af4b7042d84e0699f7c1de306526098030b74a9Mark Andrews idx = 0;
94a08e09db3dc844b6ee4841c368a2d7074a9c3fAndreas Gustafsson while (strlen(word) > 0U) {
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff if (confdata->searchnxt == LWRES_CONFMAXSEARCH)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff goto ignore; /* Too many domains. */
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff confdata->search[idx] = lwres_strdup(ctx, word);
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley if (confdata->search[idx] == NULL)
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff return (LWRES_R_FAILURE);
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff idx++;
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence confdata->searchnxt++;
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley ignore:
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley if (delim == EOF || delim == '\n')
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews break;
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff else
904a5734375869ffb504ed8cde6b68cafadb6d64Bob Halley delim = getword(fp, word, sizeof(word));
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley }
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff return (LWRES_R_SUCCESS);
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff}
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graffstatic lwres_result_t
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Grafflwres_create_addr(const char *buffer, lwres_addr_t *addr, int convert_zero) {
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff struct in_addr v4;
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff struct in6_addr v6;
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence if (lwres_net_aton(buffer, &v4) == 1) {
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff if (convert_zero) {
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley unsigned char zeroaddress[] = {0, 0, 0, 0};
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson unsigned char loopaddress[] = {127, 0, 0, 1};
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews if (memcmp(&v4, zeroaddress, 4) == 0)
43b8fb75704f290a48fc96461140b42b2488348dMark Andrews memcpy(&v4, loopaddress, 4);
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley }
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews addr->family = LWRES_ADDRTYPE_V4;
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff addr->length = NS_INADDRSZ;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff memcpy((void *)addr->address, &v4, NS_INADDRSZ);
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley
13eaef2df27683658868c3828c18880d7419a16aDavid Lawrence } else if (lwres_net_pton(AF_INET6, buffer, &v6) == 1) {
13eaef2df27683658868c3828c18880d7419a16aDavid Lawrence addr->family = LWRES_ADDRTYPE_V6;
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff addr->length = NS_IN6ADDRSZ;
13eaef2df27683658868c3828c18880d7419a16aDavid Lawrence memcpy((void *)addr->address, &v6, NS_IN6ADDRSZ);
13eaef2df27683658868c3828c18880d7419a16aDavid Lawrence } else {
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews return (LWRES_R_FAILURE); /* Unrecognised format. */
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews }
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley return (LWRES_R_SUCCESS);
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley}
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graffstatic lwres_result_t
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halleylwres_conf_parsesortlist(lwres_context_t *ctx, FILE *fp) {
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley int delim, res, idx;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson char word[LWRES_CONFMAXLINELEN];
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley char *p;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson lwres_conf_t *confdata;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff confdata = &ctx->confdata;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley delim = getword(fp, word, sizeof(word));
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff if (strlen(word) == 0U)
ccbfddc70ef38263daca312d29bb8c5077e24785Bob Halley return (LWRES_R_FAILURE); /* Empty line after keyword. */
ccbfddc70ef38263daca312d29bb8c5077e24785Bob Halley
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff while (strlen(word) > 0U) {
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley if (confdata->sortlistnxt == LWRES_CONFMAXSORTLIST)
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley return (LWRES_R_FAILURE); /* Too many values. */
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews
8af4b7042d84e0699f7c1de306526098030b74a9Mark Andrews p = strchr(word, '/');
8af4b7042d84e0699f7c1de306526098030b74a9Mark Andrews if (p != NULL)
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews *p++ = '\0';
92296c744ef9ef36de499eb10b3e27800d8b24a5Mark Andrews
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews idx = confdata->sortlistnxt;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews res = lwres_create_addr(word, &confdata->sortlist[idx].addr, 1);
13eaef2df27683658868c3828c18880d7419a16aDavid Lawrence if (res != LWRES_R_SUCCESS)
13eaef2df27683658868c3828c18880d7419a16aDavid Lawrence return (res);
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley if (p != NULL) {
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley res = lwres_create_addr(p,
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley &confdata->sortlist[idx].mask,
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley 0);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley if (res != LWRES_R_SUCCESS)
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews return (res);
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews } else {
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews /*
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews * Make up a mask.
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews */
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews confdata->sortlist[idx].mask =
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews confdata->sortlist[idx].addr;
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews memset(&confdata->sortlist[idx].mask.address, 0xff,
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews confdata->sortlist[idx].addr.length);
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews }
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington confdata->sortlistnxt++;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews if (delim == EOF || delim == '\n')
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews break;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews else
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews delim = getword(fp, word, sizeof(word));
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews }
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews return (LWRES_R_SUCCESS);
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews}
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrewsstatic lwres_result_t
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrewslwres_conf_parseoption(lwres_context_t *ctx, FILE *fp) {
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews int delim;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews long ndots;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews char *p;
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington char word[LWRES_CONFMAXLINELEN];
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington lwres_conf_t *confdata;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews REQUIRE(ctx != NULL);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson confdata = &ctx->confdata;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson delim = getword(fp, word, sizeof(word));
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson if (strlen(word) == 0U)
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson return (LWRES_R_FAILURE); /* Empty line after keyword. */
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson while (strlen(word) > 0U) {
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson if (strcmp("debug", word) == 0) {
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews confdata->resdebug = 1;
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington } else if (strcmp("no_tld_query", word) == 0) {
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson confdata->no_tld_query = 1;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson } else if (strncmp("ndots:", word, 6) == 0) {
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff ndots = strtol(word + 6, &p, 10);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley if (*p != '\0') /* Bad string. */
d981ca645597116d227a48bf37cc5edc061c854dBob Halley return (LWRES_R_FAILURE);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley if (ndots < 0 || ndots > 0xff) /* Out of range. */
88a6fef4944a00d8350ffd8b64ef58c694b8335eMark Andrews return (LWRES_R_FAILURE);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff confdata->ndots = (lwres_uint8_t)ndots;
d981ca645597116d227a48bf37cc5edc061c854dBob Halley }
d981ca645597116d227a48bf37cc5edc061c854dBob Halley
d981ca645597116d227a48bf37cc5edc061c854dBob Halley if (delim == EOF || delim == '\n')
d981ca645597116d227a48bf37cc5edc061c854dBob Halley break;
d981ca645597116d227a48bf37cc5edc061c854dBob Halley else
d981ca645597116d227a48bf37cc5edc061c854dBob Halley delim = getword(fp, word, sizeof(word));
d981ca645597116d227a48bf37cc5edc061c854dBob Halley }
d981ca645597116d227a48bf37cc5edc061c854dBob Halley
d981ca645597116d227a48bf37cc5edc061c854dBob Halley return (LWRES_R_SUCCESS);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley}
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
d981ca645597116d227a48bf37cc5edc061c854dBob Halley/*% parses a file and fills in the data structure. */
d981ca645597116d227a48bf37cc5edc061c854dBob Halleylwres_result_t
d981ca645597116d227a48bf37cc5edc061c854dBob Halleylwres_conf_parse(lwres_context_t *ctx, const char *filename) {
d981ca645597116d227a48bf37cc5edc061c854dBob Halley FILE *fp = NULL;
d981ca645597116d227a48bf37cc5edc061c854dBob Halley char word[256];
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff lwres_result_t rval, ret;
d981ca645597116d227a48bf37cc5edc061c854dBob Halley lwres_conf_t *confdata;
43b8fb75704f290a48fc96461140b42b2488348dMark Andrews int stopchar;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
d981ca645597116d227a48bf37cc5edc061c854dBob Halley REQUIRE(ctx != NULL);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff confdata = &ctx->confdata;
d981ca645597116d227a48bf37cc5edc061c854dBob Halley
d981ca645597116d227a48bf37cc5edc061c854dBob Halley REQUIRE(filename != NULL);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff REQUIRE(strlen(filename) > 0U);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley REQUIRE(confdata != NULL);
dc570b92f6cc60def4207733c7a194fbb69a4399Michael Sawyer
errno = 0;
if ((fp = fopen(filename, "r")) == NULL)
return (LWRES_R_NOTFOUND);
ret = LWRES_R_SUCCESS;
do {
stopchar = getword(fp, word, sizeof(word));
if (stopchar == EOF) {
rval = LWRES_R_SUCCESS;
break;
}
if (strlen(word) == 0U)
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, "options") == 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);
}
/*% Prints the config data structure to the FILE. */
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);
}
/*% Returns a pointer to the current config structure. */
lwres_conf_t *
lwres_conf_get(lwres_context_t *ctx) {
REQUIRE(ctx != NULL);
return (&ctx->confdata);
}