lwconfig.c revision f9c862673848361c4361d143ac4c5d60f239d29b
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews * Copyright (C) 2000 Internet Software Consortium.
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 * 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 *** Module for parsing resolv.conf files.
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews *** entry points are:
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews *** lwres_conf_init(lwres_conf_t *confdata)
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews *** intializes data structure for subsequent parsing.
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 *** lwres_conf_print(FILE *fp, lwres_conf_t *confdata)
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews *** prints the data structure to the FILE.
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 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 Andrewsstatic int lwres_conf_parsenameserver(lwres_context_t *ctx, FILE *fp,
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrewsstatic int lwres_conf_parsedomain(lwres_context_t *ctx, FILE *fp,
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrewsstatic int lwres_conf_parsesearch(lwres_context_t *ctx, FILE *fp,
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrewsstatic int lwres_conf_parsesortlist(lwres_context_t *ctx, FILE *fp,
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrewsstatic int lwres_conf_parseoption(lwres_context_t *ctx, FILE *fp,
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrewsstatic void lwres_resetaddr(lwres_context_t *ctx, lwres_addr_t *addr,
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrewsstatic int lwres_create_addr(lwres_context_t *ctx, const char *buff,
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 } else if ((size_t) (p - buffer) == size - 1) {
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews *p++ = (char)ch;
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews } while (1);
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrewslwres_resetaddr(lwres_context_t *ctx, lwres_addr_t *addr, int freeit)
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrewsstatic char *
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrewslwres_strdup(lwres_context_t *ctx, const char *str)
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews for (i = 0 ; i < LWRES_CONFMAXNAMESERVERS ; i++) {
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews lwres_resetaddr(NULL, &confdata->nameservers[i], 0);
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews for (i = 0 ; i < LWRES_CONFMAXSEARCH ; i++) {
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 Andrewslwres_conf_clear(lwres_context_t *ctx, lwres_conf_t *confdata)
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews lwres_resetaddr(ctx, &confdata->nameservers[i], 1);
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 Andrewslwres_conf_parsenameserver(lwres_context_t *ctx, FILE *fp,
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews if (confdata->nsnext == LWRES_CONFMAXNAMESERVERS) {
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews return (LWRES_R_FAILURE); /* nothing on line */
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews return (LWRES_R_FAILURE); /* extra junk on line */
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrewslwres_conf_parsedomain(lwres_context_t *ctx, FILE *fp, lwres_conf_t *confdata)
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews return (LWRES_R_FAILURE); /* nothing else on line */
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews return (LWRES_R_FAILURE); /* extra junk on line */
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews /* search and domain are mutually exclusive */
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews for (i = 0 ; i < LWRES_CONFMAXSEARCH ; i++) {
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews confdata->domainname = lwres_strdup(ctx, word);
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrewslwres_conf_parsesearch(lwres_context_t *ctx, FILE *fp,
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews /* search and domain are mutually exclusive */
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews /* remove any previous search definitions. */
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews for (idx = 0 ; idx < LWRES_CONFMAXSEARCH ; idx++) {
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews return (LWRES_R_FAILURE); /* nothing else on line */
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews if (confdata->searchnxt == LWRES_CONFMAXSEARCH) {
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews return (LWRES_R_FAILURE); /* too many domains */
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews confdata->search[idx] = lwres_strdup(ctx, word);
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrewslwres_create_addr(lwres_context_t *ctx, const char *buffer, lwres_addr_t *addr)
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews if (lwres_net_pton(AF_INET, buffer, &addrbuff) == 1) {
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews } else if (lwres_net_pton(AF_INET6, buffer, &addrbuff) == 1) {
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews return (LWRES_R_FAILURE); /* unrecongnised format */
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrewslwres_conf_parsesortlist(lwres_context_t *ctx, FILE *fp,
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews return (LWRES_R_FAILURE); /* empty line after keyword */
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews if (confdata->sortlistnxt == LWRES_CONFMAXSORTLIST) {
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews return (LWRES_R_FAILURE); /* too many values. */
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews *p++ = '\0';
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrewslwres_conf_parseoption(lwres_context_t *ctx, FILE *fp,
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews return (LWRES_R_FAILURE); /* empty line after keyword */
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews } else if (strcmp("no_tld_query", word) == 0) {
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrewslwres_conf_parse(lwres_context_t *ctx, const char *filename,
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews rval = LWRES_R_FAILURE; /* Make compiler happy. */
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews rval = lwres_conf_parsenameserver(ctx, fp, confdata);
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews rval = lwres_conf_parsedomain(ctx, fp, confdata);
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews rval = lwres_conf_parsesearch(ctx, fp, confdata);
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews rval = lwres_conf_parsesortlist(ctx, fp, confdata);
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews rval = lwres_conf_parseoption(ctx, fp, confdata);
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrewslwres_conf_print(FILE *fp, lwres_conf_t *confdata)
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews const char *p;
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews REQUIRE(confdata->nsnext <= LWRES_CONFMAXNAMESERVERS);
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews p = lwres_net_ntop(confdata->nameservers[i].family,
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews fprintf(fp, "domain %s\n", confdata->domainname);
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews REQUIRE(confdata->searchnxt <= LWRES_CONFMAXSEARCH);
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews REQUIRE(confdata->sortlistnxt <= LWRES_CONFMAXSORTLIST);
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews for (i = 0 ; i < confdata->sortlistnxt ; i++) {
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews p = lwres_net_ntop(confdata->sortlist[i].addr.family,
af850c4120c5bee9462de4def85d0b4c1b583963Mark Andrews fprintf(fp, "options ndots:%d\n", confdata->ndots);