lwconfig.c revision 03968c0f5eed4e5c1e17f7df5948acf9aa4870ee
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews/*
5fa46bc91672ef5737aee6f99763161511566c24Tinderbox User * Copyright (C) 2000 Internet Software Consortium.
fcb54ce0a4f7377486df5bec83b3aa4711bf4131Mark Andrews *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Permission to use, copy, modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * copyright notice and this permission notice appear in all copies.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * SOFTWARE.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
28a8f5b0de57d269cf2845c69cb6abe18cbd3b3aMark Andrews/* $Id: lwconfig.c,v 1.21 2000/07/07 18:28:58 bwelling Exp $ */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/***
0c310d16b05ee94743d33f6920907edee6084fc8Michael Graff *** Module for parsing resolv.conf files.
0c310d16b05ee94743d33f6920907edee6084fc8Michael Graff ***
de153390f5a1f6d4fa86af91d4cae772d9846ca0Mark Andrews *** entry points are:
0c310d16b05ee94743d33f6920907edee6084fc8Michael Graff *** lwres_conf_init(lwres_context_t *ctx)
822f6cdabb1edd44472c7a758b5cae71376fa9beBrian Wellington *** intializes data structure for subsequent config parsing.
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington ***
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews *** lwres_conf_parse(lwres_context_t *ctx, const char *filename)
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence *** parses a file and fills in the data structure.
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson ***
25a66b4e41e2b0a2af4840749bac80ae78c678bfMark Andrews *** lwres_conf_print(lwres_context_t *ctx, FILE *fp)
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence *** prints the config data structure to the FILE.
21f1794606dce19928cf455029e173321f166380Mark Andrews ***
973a19342597823f111fce6a8cd5adfd0e2e7c0dMark Andrews *** lwres_conf_clear(lwres_context_t *ctx)
0c310d16b05ee94743d33f6920907edee6084fc8Michael Graff *** frees up all the internal memory used by the config data
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence *** structure, returning it to the lwres_context_t.
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence ***
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence ***/
28b863e609ff2d97b78663b46894494cfa2ea411Mark Andrews
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <config.h>
21825a8d005ccc2dfaf12889bf9eef3413555277Brian Wellington
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉#include <assert.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <ctype.h>
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#include <errno.h>
a98551ef592e9be6008e0141ceeb32efd586c5efMark Andrews#include <stdlib.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <stdio.h>
a98551ef592e9be6008e0141ceeb32efd586c5efMark Andrews#include <string.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <unistd.h>
54c26ab21c61c6d6b1e484bb88dc3ac263845d17Mark Andrews
3ddd92da6651bc72aa79a04195ad389d86fd1a66Andreas Gustafsson#include <sys/types.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <sys/socket.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#include <netinet/in.h>
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#include <lwres/lwbuffer.h>
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#include <lwres/lwres.h>
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#include <lwres/net.h>
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#include <lwres/result.h>
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews
c46f10e4a1702191b003cf8f8fc5059c15d29c48Mark Andrews#include "assert_p.h"
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#include "context_p.h"
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#if ! defined(NS_INADDRSZ)
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#define NS_INADDRSZ 4
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#endif
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews
eb6bd543c7d072efdca509eb17f8f301c1467b53Mark Andrews#if ! defined(NS_IN6ADDRSZ)
deaaf94332abbfdb3aff53675546acfed16e5eb6Mark Andrews#define NS_IN6ADDRSZ 16
c46f10e4a1702191b003cf8f8fc5059c15d29c48Mark Andrews#endif
c46f10e4a1702191b003cf8f8fc5059c15d29c48Mark Andrews
0b056755b2f423ba5f6adac8f7851d78f7d11437David Lawrence
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrenceextern int lwres_net_pton(int af, const char *src, void *dst);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsextern const char *lwres_net_ntop(int af, const void *src, char *dst,
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews size_t size);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencestatic lwres_result_t
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencelwres_conf_parsenameserver(lwres_context_t *ctx, FILE *fp);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
0b056755b2f423ba5f6adac8f7851d78f7d11437David Lawrencestatic lwres_result_t
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencelwres_conf_parsedomain(lwres_context_t *ctx, FILE *fp);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencestatic lwres_result_t
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencelwres_conf_parsesearch(lwres_context_t *ctx, FILE *fp);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencestatic lwres_result_t
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencelwres_conf_parsesortlist(lwres_context_t *ctx, FILE *fp);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
0b056755b2f423ba5f6adac8f7851d78f7d11437David Lawrencestatic lwres_result_t
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencelwres_conf_parseoption(lwres_context_t *ctx, FILE *fp);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencestatic void
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencelwres_resetaddr(lwres_addr_t *addr);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencestatic lwres_result_t
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencelwres_create_addr(const char *buff, lwres_addr_t *addr);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencestatic int lwresaddr2af(int lwresaddrtype);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic int
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewslwresaddr2af(int lwresaddrtype)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews{
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews int af = 0;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews switch (lwresaddrtype) {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein case LWRES_ADDRTYPE_V4:
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence af = AF_INET;
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson break;
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson case LWRES_ADDRTYPE_V6:
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson af = AF_INET6;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein break;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein }
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater return (af);
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson}
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence/*
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence * Eat characters from FP until EOL or EOF. Returns EOF or '\n'
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence */
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrencestatic int
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrenceeatline(FILE *fp) {
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence int ch;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence ch = fgetc(fp);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont while (ch != '\n' && ch != EOF)
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont ch = fgetc(fp);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont return (ch);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont}
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont/*
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont * Eats white space up to next newline or non-whitespace character (of
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence * EOF). Returns the last character read. Comments are considered white
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence * space.
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence */
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrencestatic int
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrenceeatwhite(FILE *fp) {
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence int ch;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence ch = fgetc(fp);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence while (ch != '\n' && ch != EOF && isspace((unsigned char)ch))
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington ch = fgetc(fp);
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington if (ch == ';' || ch == '#')
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence ch = eatline(fp);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence return (ch);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence}
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence/*
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence * Skip over any leading whitespace and then read in the next sequence of
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence * non-whitespace characters. In this context newline is not considered
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence * whitespace. Returns EOF on end-of-file, or the character
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence * that caused the reading to stop.
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence */
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrencestatic int
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrencegetword(FILE *fp, char *buffer, size_t size) {
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence int ch;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence char *p = buffer;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence REQUIRE(buffer != NULL);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence REQUIRE(size > 0);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence *p = '\0';
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence ch = eatwhite(fp);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence if (ch == EOF)
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence return (EOF);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews do {
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews *p = '\0';
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence if (ch == EOF || isspace((unsigned char)ch))
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence break;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence else if ((size_t) (p - buffer) == size - 1)
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence return (EOF); /* Not enough space. */
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence *p++ = (char)ch;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence ch = fgetc(fp);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence } while (1);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence return (ch);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence}
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrencestatic void
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrencelwres_resetaddr(lwres_addr_t *addr) {
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence REQUIRE(addr != NULL);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson memset(addr->address, 0, LWRES_ADDR_MAXLEN);
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson addr->family = 0;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence addr->length = 0;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence}
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencestatic char *
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencelwres_strdup(lwres_context_t *ctx, const char *str) {
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence char *p;
fd4810861c0c0ccb9aebde94e9d289442b2630dbMark Andrews
fd4810861c0c0ccb9aebde94e9d289442b2630dbMark Andrews REQUIRE(str != NULL);
fd4810861c0c0ccb9aebde94e9d289442b2630dbMark Andrews REQUIRE(strlen(str) > 0);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence p = CTXMALLOC(strlen(str) + 1);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence if (p != NULL)
7ab0e69f61e61e81d489c95c7ebd981e74e7ef16Andreas Gustafsson strcpy(p, str);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews return (p);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews}
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrewsvoid
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrewslwres_conf_init(lwres_context_t *ctx) {
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews int i;
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews lwres_conf_t *confdata;
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews REQUIRE(ctx != NULL);
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews confdata = &ctx->confdata;
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews confdata->nsnext = 0;
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews confdata->domainname = NULL;
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews confdata->searchnxt = 0;
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews confdata->sortlistnxt = 0;
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews confdata->resdebug = 0;
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews confdata->ndots = 1;
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews confdata->no_tld_query = 0;
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews for (i = 0 ; i < LWRES_CONFMAXNAMESERVERS ; i++)
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews lwres_resetaddr(&confdata->nameservers[i]);
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews for (i = 0 ; i < LWRES_CONFMAXSEARCH ; i++)
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews confdata->search[i] = NULL;
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews
b6a0341bcb113e93bd0bc41a9f9a1fc117444da6Mark Andrews for (i = 0 ; i < LWRES_CONFMAXSORTLIST ; i++) {
b6a0341bcb113e93bd0bc41a9f9a1fc117444da6Mark Andrews lwres_resetaddr(&confdata->sortlist[i].addr);
aa05bbdef7f7827dde158dcc913f4dade84c8511Brian Wellington lwres_resetaddr(&confdata->sortlist[i].mask);
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews }
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews}
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrewsvoid
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrewslwres_conf_clear(lwres_context_t *ctx) {
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews int i;
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews lwres_conf_t *confdata;
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews REQUIRE(ctx != NULL);
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews confdata = &ctx->confdata;
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews for (i = 0 ; i < confdata->nsnext ; i++)
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews lwres_resetaddr(&confdata->nameservers[i]);
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews if (confdata->domainname != NULL) {
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews CTXFREE(confdata->domainname,
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews strlen(confdata->domainname) + 1);
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews confdata->domainname = NULL;
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews }
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews for (i = 0 ; i < confdata->searchnxt ; i++) {
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews if (confdata->search[i] != NULL) {
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews CTXFREE(confdata->search[i],
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews strlen(confdata->search[i]) + 1);
f6407f9a0b890bebbfd5f738d9c4aef3d3315fe9Michael Graff confdata->search[i] = NULL;
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews }
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews }
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews
ffe74cc719aa0f10c38fbc1f2f3ea7db0960cb8fMark Andrews for (i = 0 ; i < LWRES_CONFMAXSORTLIST ; i++) {
ffe74cc719aa0f10c38fbc1f2f3ea7db0960cb8fMark Andrews lwres_resetaddr(&confdata->sortlist[i].addr);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews lwres_resetaddr(&confdata->sortlist[i].mask);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
bed8e84810a80dad3d37870be927d1dfd015f480Mark Andrews confdata->nsnext = 0;
bed8e84810a80dad3d37870be927d1dfd015f480Mark Andrews confdata->domainname = NULL;
bed8e84810a80dad3d37870be927d1dfd015f480Mark Andrews confdata->searchnxt = 0;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews confdata->sortlistnxt = 0;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews confdata->resdebug = 0;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews confdata->ndots = 1;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews confdata->no_tld_query = 0;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews}
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic lwres_result_t
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewslwres_conf_parsenameserver(lwres_context_t *ctx, FILE *fp) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews char word[LWRES_CONFMAXLINELEN];
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews int res;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews lwres_conf_t *confdata;
d981ca645597116d227a48bf37cc5edc061c854dBob Halley
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews confdata = &ctx->confdata;
e6bd97dded968f82e26b270842b789bff7bca422Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (confdata->nsnext == LWRES_CONFMAXNAMESERVERS)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (LWRES_R_SUCCESS);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews res = getword(fp, word, sizeof(word));
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrews if (strlen(word) == 0)
368b37b616234fce3d23099eb180f1dd38e1fb62Mark Andrews return (LWRES_R_FAILURE); /* Nothing on line. */
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrews else if (res == ' ' || res == '\t')
cabaeca9ae5b98c80586b91e89cf552e17a75a9bBrian Wellington res = eatwhite(fp);
cabaeca9ae5b98c80586b91e89cf552e17a75a9bBrian Wellington
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrews if (res != EOF && res != '\n')
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews return (LWRES_R_FAILURE); /* Extra junk on line. */
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrews
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrews res = lwres_create_addr(word,
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrews &confdata->nameservers[confdata->nsnext++]);
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrews if (res != LWRES_R_SUCCESS)
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrews return (res);
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrews
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrews return (LWRES_R_SUCCESS);
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrews}
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrewsstatic lwres_result_t
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrewslwres_conf_parsedomain(lwres_context_t *ctx, FILE *fp) {
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews char word[LWRES_CONFMAXLINELEN];
29c818c7d40fc8898b062903ec703851328a4deaMark Andrews int res, i;
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews lwres_conf_t *confdata;
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews
cabaeca9ae5b98c80586b91e89cf552e17a75a9bBrian Wellington confdata = &ctx->confdata;
cabaeca9ae5b98c80586b91e89cf552e17a75a9bBrian Wellington
cabaeca9ae5b98c80586b91e89cf552e17a75a9bBrian Wellington res = getword(fp, word, sizeof(word));
f1cae4bcb7ee3060d893f5ab3ba55c1820bf3e4aBrian Wellington if (strlen(word) == 0)
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews return (LWRES_R_FAILURE); /* Nothing else on line. */
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews else if (res == ' ' || res == '\t')
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews res = eatwhite(fp);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews
29c818c7d40fc8898b062903ec703851328a4deaMark Andrews if (res != EOF && res != '\n')
29c818c7d40fc8898b062903ec703851328a4deaMark Andrews return (LWRES_R_FAILURE); /* Extra junk on line. */
29c818c7d40fc8898b062903ec703851328a4deaMark Andrews
29c818c7d40fc8898b062903ec703851328a4deaMark Andrews if (confdata->domainname != NULL)
29c818c7d40fc8898b062903ec703851328a4deaMark Andrews CTXFREE(confdata->domainname,
29c818c7d40fc8898b062903ec703851328a4deaMark Andrews strlen(confdata->domainname) + 1); /* */
29c818c7d40fc8898b062903ec703851328a4deaMark Andrews
29c818c7d40fc8898b062903ec703851328a4deaMark Andrews /*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Search and domain are mutually exclusive.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews for (i = 0 ; i < LWRES_CONFMAXSEARCH ; i++) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (confdata->search[i] != NULL) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews CTXFREE(confdata->search[i],
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence strlen(confdata->search[i])+1);
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews confdata->search[i] = NULL;
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews confdata->searchnxt = 0;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
735ca24fa61ad9cd0285776c2fc4c14032f8f194Mark Andrews confdata->domainname = lwres_strdup(ctx, word);
735ca24fa61ad9cd0285776c2fc4c14032f8f194Mark Andrews
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews if (confdata->domainname == NULL)
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews return (LWRES_R_FAILURE);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
d981ca645597116d227a48bf37cc5edc061c854dBob Halley return (LWRES_R_SUCCESS);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley}
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic lwres_result_t
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewslwres_conf_parsesearch(lwres_context_t *ctx, FILE *fp) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews int idx, delim;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews char word[LWRES_CONFMAXLINELEN];
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews lwres_conf_t *confdata;
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews confdata = &ctx->confdata;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews if (confdata->domainname != NULL) {
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews /*
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * Search and domain are mutually exclusive.
90e303b114e56db5809fdd19805243457fa43cd9Olafur Gudmundsson */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews CTXFREE(confdata->domainname,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews strlen(confdata->domainname) + 1);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews confdata->domainname = NULL;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews /*
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews * Remove any previous search definitions.
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews */
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews for (idx = 0 ; idx < LWRES_CONFMAXSEARCH ; idx++) {
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews if (confdata->search[idx] != NULL) {
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews CTXFREE(confdata->search[idx],
735ca24fa61ad9cd0285776c2fc4c14032f8f194Mark Andrews strlen(confdata->search[idx])+1);
735ca24fa61ad9cd0285776c2fc4c14032f8f194Mark Andrews confdata->search[idx] = NULL;
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews }
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews }
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews confdata->searchnxt = 0;
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews delim = getword(fp, word, sizeof(word));
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews if (strlen(word) == 0)
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews return (LWRES_R_FAILURE); /* Nothing else on line. */
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews idx = 0;
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews while (strlen(word) > 0) {
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews if (confdata->searchnxt == LWRES_CONFMAXSEARCH)
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews goto ignore; /* Too many domains. */
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews confdata->search[idx] = lwres_strdup(ctx, word);
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews if (confdata->search[idx] == NULL)
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews return (LWRES_R_FAILURE);
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews idx++;
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews confdata->searchnxt++;
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews ignore:
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews if (delim == EOF || delim == '\n')
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews break;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews else
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews delim = getword(fp, word, sizeof(word));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (LWRES_R_SUCCESS);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley}
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrewsstatic lwres_result_t
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrencelwres_create_addr(const char *buffer, lwres_addr_t *addr) {
19d365e4448f1782611280b020987988b7ac3210Mark Andrews unsigned char addrbuff[NS_IN6ADDRSZ];
f1cae4bcb7ee3060d893f5ab3ba55c1820bf3e4aBrian Wellington unsigned int len;
19d365e4448f1782611280b020987988b7ac3210Mark Andrews
19d365e4448f1782611280b020987988b7ac3210Mark Andrews if (lwres_net_pton(AF_INET, buffer, &addrbuff) == 1) {
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews addr->family = LWRES_ADDRTYPE_V4;
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews addr->length = NS_INADDRSZ;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews len = 4;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews } else if (lwres_net_pton(AF_INET6, buffer, &addrbuff) == 1) {
d981ca645597116d227a48bf37cc5edc061c854dBob Halley addr->family = LWRES_ADDRTYPE_V6;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews addr->length = NS_IN6ADDRSZ;
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews len = 16;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews } else {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (LWRES_R_FAILURE); /* Unrecognised format. */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews memcpy((void *)addr->address, addrbuff, len);
19d365e4448f1782611280b020987988b7ac3210Mark Andrews
19d365e4448f1782611280b020987988b7ac3210Mark Andrews return (LWRES_R_SUCCESS);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews}
19d365e4448f1782611280b020987988b7ac3210Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic lwres_result_t
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewslwres_conf_parsesortlist(lwres_context_t *ctx, FILE *fp) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews int delim, res, idx;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews char word[LWRES_CONFMAXLINELEN];
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff char *p;
d981ca645597116d227a48bf37cc5edc061c854dBob Halley lwres_conf_t *confdata;
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews confdata = &ctx->confdata;
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews delim = getword(fp, word, sizeof(word));
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (strlen(word) == 0)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (LWRES_R_FAILURE); /* Empty line after keyword. */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews while (strlen(word) > 0) {
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews if (confdata->sortlistnxt == LWRES_CONFMAXSORTLIST)
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews return (LWRES_R_FAILURE); /* Too many values. */
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews p = strchr(word, '/');
19d365e4448f1782611280b020987988b7ac3210Mark Andrews if (p != NULL)
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews *p++ = '\0';
f1cae4bcb7ee3060d893f5ab3ba55c1820bf3e4aBrian Wellington
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews idx = confdata->sortlistnxt;
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews res = lwres_create_addr(word, &confdata->sortlist[idx].addr);
7e9d637131516486630290d36c4c0db544cb700eMark Andrews if (res != LWRES_R_SUCCESS)
7e9d637131516486630290d36c4c0db544cb700eMark Andrews return (res);
19d365e4448f1782611280b020987988b7ac3210Mark Andrews
6bebabb376b93e7d12f53a253b197a3fc0e0b001Andreas Gustafsson if (p != NULL) {
6bebabb376b93e7d12f53a253b197a3fc0e0b001Andreas Gustafsson res = lwres_create_addr(p,
6bebabb376b93e7d12f53a253b197a3fc0e0b001Andreas Gustafsson &confdata->sortlist[idx].mask);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (res != LWRES_R_SUCCESS)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (res);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews } else {
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews /*
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews * Make up a mask.
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews confdata->sortlist[idx].mask =
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews confdata->sortlist[idx].addr;
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews memset(&confdata->sortlist[idx].mask.address, 0xff,
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews confdata->sortlist[idx].addr.length);
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews }
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews confdata->sortlistnxt++;
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews if (delim == EOF || delim == '\n')
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews break;
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews else
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews delim = getword(fp, word, sizeof(word));
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews }
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews return (LWRES_R_SUCCESS);
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews}
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrewsstatic lwres_result_t
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrewslwres_conf_parseoption(lwres_context_t *ctx, FILE *fp) {
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews int delim;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence long ndots;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence char *p;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence char word[LWRES_CONFMAXLINELEN];
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff lwres_conf_t *confdata;
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews REQUIRE(ctx != NULL);
ad7209ea70d346527ffdcda335153831341d9dcfAndreas Gustafsson confdata = &ctx->confdata;
7d8cdd869ed2162a5befda7cc1600136110f54d6Mark Andrews
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews delim = getword(fp, word, sizeof(word));
d981ca645597116d227a48bf37cc5edc061c854dBob Halley if (strlen(word) == 0)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (LWRES_R_FAILURE); /* Empty line after keyword. */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff while (strlen(word) > 0) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (strcmp("debug", word) == 0) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews confdata->resdebug = 1;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews } else if (strcmp("no_tld_query", word) == 0) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews confdata->no_tld_query = 1;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews } else if (strncmp("ndots:", word, 6) == 0) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews ndots = strtol(word + 6, &p, 10);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff if (*p != '\0') /* Bad string. */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (LWRES_R_FAILURE);
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington if (ndots < 0 || ndots > 0xff) /* Out of range. */
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews return (LWRES_R_FAILURE);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff confdata->ndots = (lwres_uint8_t)ndots;
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews }
f0a5bb8f86631ce638cb2b6c65bbb9bcf9b0cdc0Bob Halley
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews if (delim == EOF || delim == '\n')
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews break;
19d365e4448f1782611280b020987988b7ac3210Mark Andrews else
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews delim = getword(fp, word, sizeof(word));
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence }
693ddf84daa745a0ea8ca311a8154dfa03eabc43Andreas Gustafsson
693ddf84daa745a0ea8ca311a8154dfa03eabc43Andreas Gustafsson return (LWRES_R_SUCCESS);
693ddf84daa745a0ea8ca311a8154dfa03eabc43Andreas Gustafsson}
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrewslwres_result_t
693ddf84daa745a0ea8ca311a8154dfa03eabc43Andreas Gustafssonlwres_conf_parse(lwres_context_t *ctx, const char *filename) {
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews FILE *fp = NULL;
693ddf84daa745a0ea8ca311a8154dfa03eabc43Andreas Gustafsson char word[256];
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews lwres_result_t rval;
19d365e4448f1782611280b020987988b7ac3210Mark Andrews lwres_conf_t *confdata;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews int stopchar;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews REQUIRE(ctx != NULL);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence confdata = &ctx->confdata;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff REQUIRE(filename != NULL);
f0a5bb8f86631ce638cb2b6c65bbb9bcf9b0cdc0Bob Halley REQUIRE(strlen(filename) > 0);
f0a5bb8f86631ce638cb2b6c65bbb9bcf9b0cdc0Bob Halley REQUIRE(confdata != NULL);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews errno = 0;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if ((fp = fopen(filename, "r")) == NULL)
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews return (LWRES_R_FAILURE);
7c0378745269fe49a05904935afc42b85528f53aDavid Lawrence
7c0378745269fe49a05904935afc42b85528f53aDavid Lawrence do {
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews stopchar = getword(fp, word, sizeof(word));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (stopchar == EOF) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews rval = LWRES_R_SUCCESS;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews break;
7e4cda9965e2edf2ec43c57967eec8eff7061ab0Andreas Gustafsson }
7e4cda9965e2edf2ec43c57967eec8eff7061ab0Andreas Gustafsson
7e4cda9965e2edf2ec43c57967eec8eff7061ab0Andreas Gustafsson if (strlen(word) == 0)
7e4cda9965e2edf2ec43c57967eec8eff7061ab0Andreas Gustafsson rval = LWRES_R_SUCCESS;
7e4cda9965e2edf2ec43c57967eec8eff7061ab0Andreas Gustafsson else if (strcmp(word, "nameserver") == 0)
3a16668468060842e847ea6556e80e1405d35cd6Brian Wellington rval = lwres_conf_parsenameserver(ctx, fp);
7e4cda9965e2edf2ec43c57967eec8eff7061ab0Andreas Gustafsson else if (strcmp(word, "domain") == 0)
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington rval = lwres_conf_parsedomain(ctx, fp);
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews else if (strcmp(word, "search") == 0)
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington rval = lwres_conf_parsesearch(ctx, fp);
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington else if (strcmp(word, "sortlist") == 0)
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington rval = lwres_conf_parsesortlist(ctx, fp);
942d1a339b1fe617f7d17d66cb5fccce798d15aeBrian Wellington else if (strcmp(word, "option") == 0)
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington rval = lwres_conf_parseoption(ctx, fp);
7e9d637131516486630290d36c4c0db544cb700eMark Andrews else {
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington /* unrecognised word. Ignore entire line */
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews rval = LWRES_R_SUCCESS;
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews stopchar = eatline(fp);
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews if (stopchar == EOF) {
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews break;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington }
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington }
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington } while (rval == LWRES_R_SUCCESS);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington fclose(fp);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington return (rval);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington}
de5247ae1683ce145662180ee50272d2214a0232Andreas Gustafsson
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellingtonlwres_result_t
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellingtonlwres_conf_print(lwres_context_t *ctx, FILE *fp) {
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington int i;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington int af;
5eb91bd90e3ad3426e5e3213031556a737cf3809Mark Andrews char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington const char *p;
f16495732753175e4a9fc144323a12fdcc29b561Brian Wellington lwres_conf_t *confdata;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington lwres_addr_t tmpaddr;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater REQUIRE(ctx != NULL);
e086935752b6e2f51ef2985fee21ccfff617b115David Lawrence confdata = &ctx->confdata;
e086935752b6e2f51ef2985fee21ccfff617b115David Lawrence
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington REQUIRE(confdata->nsnext <= LWRES_CONFMAXNAMESERVERS);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington for (i = 0 ; i < confdata->nsnext ; i++) {
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington af = lwresaddr2af(confdata->nameservers[i].family);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington p = lwres_net_ntop(af, confdata->nameservers[i].address,
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington tmp, sizeof(tmp));
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington if (p != tmp)
7e4cda9965e2edf2ec43c57967eec8eff7061ab0Andreas Gustafsson return (LWRES_R_FAILURE);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington fprintf(fp, "nameserver %s\n", tmp);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington }
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington if (confdata->domainname != NULL) {
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington fprintf(fp, "domain %s\n", confdata->domainname);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington } else if (confdata->searchnxt > 0) {
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington REQUIRE(confdata->searchnxt <= LWRES_CONFMAXSEARCH);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington fprintf(fp, "search");
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington for (i = 0 ; i < confdata->searchnxt ; i++)
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington fprintf(fp, " %s", confdata->search[i]);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington fputc('\n', fp);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington }
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington REQUIRE(confdata->sortlistnxt <= LWRES_CONFMAXSORTLIST);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
d981ca645597116d227a48bf37cc5edc061c854dBob Halley if (confdata->sortlistnxt > 0) {
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews fputs("sortlist", fp);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews for (i = 0 ; i < confdata->sortlistnxt ; i++) {
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews af = lwresaddr2af(confdata->sortlist[i].addr.family);
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff p = lwres_net_ntop(af,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews confdata->sortlist[i].addr.address,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews tmp, sizeof(tmp));
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews if (p != tmp)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews return (LWRES_R_FAILURE);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews fprintf(fp, " %s", tmp);
20b20b23948b90cb2f7d7f402da99d09f837efd0David Lawrence
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence tmpaddr = confdata->sortlist[i].mask;
8dd5237a27e2e824d18f835dc711573aeb23a173Mark Andrews memset(&tmpaddr.address, 0xff, tmpaddr.length);
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (memcmp(&tmpaddr.address,
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence confdata->sortlist[i].mask.address,
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews confdata->sortlist[i].mask.length) != 0) {
f1cae4bcb7ee3060d893f5ab3ba55c1820bf3e4aBrian Wellington af = lwresaddr2af(
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews confdata->sortlist[i].mask.family);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews p = lwres_net_ntop
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews (af,
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews confdata->sortlist[i].mask.address,
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews tmp, sizeof(tmp));
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews if (p != tmp)
19d365e4448f1782611280b020987988b7ac3210Mark Andrews return (LWRES_R_FAILURE);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews fprintf(fp, "/%s", tmp);
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews }
228c679d7a269423019f7c528db92e855f08240bMark Andrews }
b6a0341bcb113e93bd0bc41a9f9a1fc117444da6Mark Andrews fputc('\n', fp);
228c679d7a269423019f7c528db92e855f08240bMark Andrews }
228c679d7a269423019f7c528db92e855f08240bMark Andrews
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington if (confdata->resdebug)
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington fprintf(fp, "options debug\n");
228c679d7a269423019f7c528db92e855f08240bMark Andrews
228c679d7a269423019f7c528db92e855f08240bMark Andrews if (confdata->ndots > 0)
228c679d7a269423019f7c528db92e855f08240bMark Andrews fprintf(fp, "options ndots:%d\n", confdata->ndots);
c51f2c9a529b2130a8058cb7a32bd284fa6cfdb9Mark Andrews
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington if (confdata->no_tld_query)
228c679d7a269423019f7c528db92e855f08240bMark Andrews fprintf(fp, "options no_tld_query\n");
228c679d7a269423019f7c528db92e855f08240bMark Andrews
c46f10e4a1702191b003cf8f8fc5059c15d29c48Mark Andrews return (LWRES_R_SUCCESS);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington}
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellingtonlwres_conf_t *
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellingtonlwres_conf_get(lwres_context_t *ctx) {
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington REQUIRE(ctx != NULL);
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews return (&ctx->confdata);
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews}
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews