1997-09-16 02:42:43 +02:00
|
|
|
/* Copyright (c) 1996 by Internet Software Consortium.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
|
|
|
|
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
|
|
|
|
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
|
|
|
|
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Copyright 1996 by the Massachusetts Institute of Technology.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this
|
|
|
|
* software and its documentation for any purpose and without
|
|
|
|
* fee is hereby granted, provided that the above copyright
|
|
|
|
* notice appear in all copies and that both that copyright
|
|
|
|
* notice and this permission notice appear in supporting
|
|
|
|
* documentation, and that the name of M.I.T. not be used in
|
|
|
|
* advertising or publicity pertaining to distribution of the
|
|
|
|
* software without specific, written prior permission.
|
|
|
|
* M.I.T. makes no representations about the suitability of
|
|
|
|
* this software for any purpose. It is provided "as is"
|
|
|
|
* without express or implied warranty.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* This file is part of the hesiod library. It implements the core
|
|
|
|
* portion of the hesiod resolver.
|
|
|
|
*
|
|
|
|
* This file is loosely based on an interim version of hesiod.c from
|
|
|
|
* the BIND IRS library, which was in turn based on an earlier version
|
|
|
|
* of this file. Extensive changes have been made on each step of the
|
|
|
|
* path.
|
|
|
|
*
|
|
|
|
* This implementation is not truly thread-safe at the moment because
|
|
|
|
* it uses res_send() and accesses _res.
|
|
|
|
*/
|
|
|
|
|
1999-08-05 03:33:18 +02:00
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
1997-09-16 02:42:43 +02:00
|
|
|
static const char rcsid[] = "$Id$";
|
1999-08-05 03:33:18 +02:00
|
|
|
#endif
|
1997-09-16 02:42:43 +02:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/nameser.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <resolv.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include "hesiod.h"
|
|
|
|
#include "hesiod_p.h"
|
|
|
|
|
|
|
|
/* A few operating systems don't define these. */
|
|
|
|
#ifndef C_HS
|
1998-02-20 19:45:36 +01:00
|
|
|
# define C_HS 4
|
1997-09-16 02:42:43 +02:00
|
|
|
#endif
|
|
|
|
#ifndef T_TXT
|
1998-02-20 19:45:36 +01:00
|
|
|
# define T_TXT 16
|
1997-09-16 02:42:43 +02:00
|
|
|
#endif
|
|
|
|
|
1998-02-20 19:45:36 +01:00
|
|
|
static int read_config_file (struct hesiod_p *ctx, const char *filename);
|
|
|
|
static char **get_txt_records (struct hesiod_p *ctx, int class,
|
|
|
|
const char *name);
|
1997-09-16 23:51:15 +02:00
|
|
|
#ifdef _LIBC
|
|
|
|
# define cistrcmp(s1, s2) strcasecmp (s1, s2)
|
|
|
|
#else
|
1998-02-20 19:45:36 +01:00
|
|
|
static int cistrcmp (const char *s1, const char *s2);
|
1997-09-16 23:51:15 +02:00
|
|
|
#endif
|
1997-09-16 02:42:43 +02:00
|
|
|
|
|
|
|
/* This function is called to initialize a hesiod_p. */
|
1998-02-20 19:45:36 +01:00
|
|
|
int
|
|
|
|
hesiod_init (void **context)
|
1997-09-16 02:42:43 +02:00
|
|
|
{
|
|
|
|
struct hesiod_p *ctx;
|
|
|
|
const char *p, *configname;
|
|
|
|
|
1998-02-20 19:45:36 +01:00
|
|
|
ctx = malloc (sizeof (struct hesiod_p));
|
1997-09-16 02:42:43 +02:00
|
|
|
if (ctx)
|
|
|
|
{
|
|
|
|
*context = ctx;
|
1998-02-20 19:45:36 +01:00
|
|
|
configname = __secure_getenv ("HESIOD_CONFIG");
|
1997-09-16 02:42:43 +02:00
|
|
|
if (!configname)
|
|
|
|
configname = SYSCONFDIR "/hesiod.conf";
|
1998-02-20 19:45:36 +01:00
|
|
|
if (read_config_file (ctx, configname) >= 0)
|
1997-09-16 02:42:43 +02:00
|
|
|
{
|
|
|
|
/* The default rhs can be overridden by an environment variable. */
|
1998-02-20 19:45:36 +01:00
|
|
|
p = __secure_getenv ("HES_DOMAIN");
|
1997-09-16 02:42:43 +02:00
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
if (ctx->rhs)
|
1998-02-20 19:45:36 +01:00
|
|
|
free (ctx->rhs);
|
|
|
|
ctx->rhs = malloc (strlen (p) + 2);
|
1997-09-16 02:42:43 +02:00
|
|
|
if (ctx->rhs)
|
|
|
|
{
|
|
|
|
*ctx->rhs = '.';
|
1998-02-20 19:45:36 +01:00
|
|
|
strcpy (ctx->rhs + 1, (*p == '.') ? p + 1 : p);
|
1997-09-16 02:42:43 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
__set_errno (ENOMEM);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
__set_errno (ENOMEM);
|
|
|
|
|
|
|
|
if (ctx->lhs)
|
1998-02-20 19:45:36 +01:00
|
|
|
free (ctx->lhs);
|
1997-09-16 02:42:43 +02:00
|
|
|
if (ctx->rhs)
|
1998-02-20 19:45:36 +01:00
|
|
|
free (ctx->rhs);
|
1997-09-16 02:42:43 +02:00
|
|
|
if (ctx)
|
1998-02-20 19:45:36 +01:00
|
|
|
free (ctx);
|
1997-09-16 02:42:43 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This function deallocates the hesiod_p. */
|
1998-02-20 19:45:36 +01:00
|
|
|
void
|
|
|
|
hesiod_end (void *context)
|
1997-09-16 02:42:43 +02:00
|
|
|
{
|
|
|
|
struct hesiod_p *ctx = (struct hesiod_p *) context;
|
|
|
|
|
1998-02-20 19:45:36 +01:00
|
|
|
free (ctx->rhs);
|
1997-09-16 02:42:43 +02:00
|
|
|
if (ctx->lhs)
|
1998-02-20 19:45:36 +01:00
|
|
|
free (ctx->lhs);
|
|
|
|
free (ctx);
|
1997-09-16 02:42:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This function takes a hesiod (name, type) and returns a DNS
|
|
|
|
* name which is to be resolved.
|
|
|
|
*/
|
1998-02-20 19:45:36 +01:00
|
|
|
char *
|
|
|
|
hesiod_to_bind (void *context, const char *name, const char *type)
|
1997-09-16 02:42:43 +02:00
|
|
|
{
|
|
|
|
struct hesiod_p *ctx = (struct hesiod_p *) context;
|
1997-09-16 23:51:15 +02:00
|
|
|
char bindname[MAXDNAME], *p, *endp, *ret, **rhs_list = NULL;
|
1997-09-16 02:42:43 +02:00
|
|
|
const char *rhs;
|
1997-09-16 23:51:15 +02:00
|
|
|
size_t len;
|
|
|
|
|
1998-02-20 19:45:36 +01:00
|
|
|
endp = stpcpy (bindname, name);
|
1997-09-16 02:42:43 +02:00
|
|
|
|
|
|
|
/* Find the right right hand side to use, possibly truncating bindname. */
|
1998-02-20 19:45:36 +01:00
|
|
|
p = strchr (bindname, '@');
|
1997-09-16 02:42:43 +02:00
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
*p++ = 0;
|
1998-02-20 19:45:36 +01:00
|
|
|
if (strchr (p, '.'))
|
1997-09-16 02:42:43 +02:00
|
|
|
rhs = name + (p - bindname);
|
|
|
|
else
|
|
|
|
{
|
1998-02-20 19:45:36 +01:00
|
|
|
rhs_list = hesiod_resolve (context, p, "rhs-extension");
|
1997-09-16 02:42:43 +02:00
|
|
|
if (rhs_list)
|
|
|
|
rhs = *rhs_list;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
__set_errno (ENOENT);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
1998-02-20 19:45:36 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
rhs = ctx->rhs;
|
1997-09-16 02:42:43 +02:00
|
|
|
|
|
|
|
/* See if we have enough room. */
|
1998-02-20 19:45:36 +01:00
|
|
|
len = (endp - bindname) + 1 + strlen (type);
|
1997-09-16 02:42:43 +02:00
|
|
|
if (ctx->lhs)
|
1998-02-20 19:45:36 +01:00
|
|
|
len += strlen (ctx->lhs) + ((ctx->lhs[0] != '.') ? 1 : 0);
|
|
|
|
len += strlen (rhs) + ((rhs[0] != '.') ? 1 : 0);
|
|
|
|
if (len > sizeof (bindname) - 1)
|
1997-09-16 02:42:43 +02:00
|
|
|
{
|
|
|
|
if (rhs_list)
|
1998-02-20 19:45:36 +01:00
|
|
|
hesiod_free_list (context, rhs_list);
|
1997-09-16 02:42:43 +02:00
|
|
|
__set_errno (EMSGSIZE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Put together the rest of the domain. */
|
1998-02-20 19:45:36 +01:00
|
|
|
endp = stpcpy (stpcpy (endp, "."), type);
|
1997-09-16 02:42:43 +02:00
|
|
|
if (ctx->lhs)
|
|
|
|
{
|
|
|
|
if (ctx->lhs[0] != '.')
|
1998-02-20 19:45:36 +01:00
|
|
|
endp = stpcpy (endp, ".");
|
|
|
|
endp = stpcpy (endp, ctx->lhs);
|
1997-09-16 02:42:43 +02:00
|
|
|
}
|
|
|
|
if (rhs[0] != '.')
|
1998-02-20 19:45:36 +01:00
|
|
|
endp = stpcpy (endp, ".");
|
|
|
|
endp = stpcpy (endp, rhs);
|
1997-09-16 02:42:43 +02:00
|
|
|
|
|
|
|
/* rhs_list is no longer needed, since we're done with rhs. */
|
|
|
|
if (rhs_list)
|
1998-02-20 19:45:36 +01:00
|
|
|
hesiod_free_list (context, rhs_list);
|
1997-09-16 02:42:43 +02:00
|
|
|
|
|
|
|
/* Make a copy of the result and return it to the caller. */
|
1998-02-20 19:45:36 +01:00
|
|
|
ret = malloc ((endp - bindname) + 1);
|
1997-09-16 02:42:43 +02:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
__set_errno (ENOMEM);
|
|
|
|
return NULL;
|
|
|
|
}
|
1998-02-20 19:45:36 +01:00
|
|
|
return strcpy (ret, bindname);
|
1997-09-16 02:42:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This is the core function. Given a hesiod name and type, it
|
|
|
|
* returns an array of strings returned by the resolver.
|
|
|
|
*/
|
1998-02-20 19:45:36 +01:00
|
|
|
char **
|
|
|
|
hesiod_resolve (void *context, const char *name, const char *type)
|
1997-09-16 02:42:43 +02:00
|
|
|
{
|
|
|
|
struct hesiod_p *ctx = (struct hesiod_p *) context;
|
|
|
|
char *bindname, **retvec;
|
|
|
|
|
1998-02-20 19:45:36 +01:00
|
|
|
bindname = hesiod_to_bind (context, name, type);
|
|
|
|
if (bindname == NULL)
|
1997-09-16 02:42:43 +02:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
retvec = get_txt_records(ctx, ctx->classes[0], bindname);
|
|
|
|
if (retvec == NULL && errno == ENOENT && ctx->classes[1])
|
1998-02-20 19:45:36 +01:00
|
|
|
retvec = get_txt_records (ctx, ctx->classes[1], bindname);
|
1997-09-16 02:42:43 +02:00
|
|
|
|
1998-02-20 19:45:36 +01:00
|
|
|
free (bindname);
|
1997-09-16 02:42:43 +02:00
|
|
|
return retvec;
|
|
|
|
}
|
|
|
|
|
1998-02-20 19:45:36 +01:00
|
|
|
void
|
|
|
|
hesiod_free_list (void *context, char **list)
|
1997-09-16 02:42:43 +02:00
|
|
|
{
|
|
|
|
char **p;
|
|
|
|
|
|
|
|
for (p = list; *p; p++)
|
1998-02-20 19:45:36 +01:00
|
|
|
free (*p);
|
|
|
|
free (list);
|
1997-09-16 02:42:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This function parses the /etc/hesiod.conf file. Returns 0 on success,
|
|
|
|
* -1 on failure. On failure, it might leave values in ctx->lhs or
|
|
|
|
* ctx->rhs which need to be freed by the caller. */
|
1998-02-20 19:45:36 +01:00
|
|
|
static int
|
|
|
|
read_config_file (struct hesiod_p *ctx, const char *filename)
|
1997-09-16 02:42:43 +02:00
|
|
|
{
|
|
|
|
char *key, *data, *p, **which;
|
|
|
|
char buf[MAXDNAME + 7];
|
|
|
|
int n;
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
/* Set default query classes. */
|
|
|
|
ctx->classes[0] = C_IN;
|
|
|
|
ctx->classes[1] = C_HS;
|
|
|
|
|
|
|
|
/* Try to open the configuration file. */
|
1998-02-20 19:45:36 +01:00
|
|
|
fp = fopen (filename, "r");
|
1998-05-25 12:01:47 +02:00
|
|
|
if (fp == NULL)
|
1997-09-16 02:42:43 +02:00
|
|
|
{
|
|
|
|
/* Use compiled in default domain names. */
|
1998-02-20 19:45:36 +01:00
|
|
|
ctx->lhs = malloc (strlen (DEF_LHS) + 1);
|
|
|
|
ctx->rhs = malloc (strlen (DEF_RHS) + 1);
|
1997-09-16 02:42:43 +02:00
|
|
|
if (ctx->lhs && ctx->rhs)
|
|
|
|
{
|
1998-02-20 19:45:36 +01:00
|
|
|
strcpy (ctx->lhs, DEF_LHS);
|
|
|
|
strcpy (ctx->rhs, DEF_RHS);
|
1997-09-16 02:42:43 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
__set_errno (ENOMEM);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->lhs = NULL;
|
|
|
|
ctx->rhs = NULL;
|
1998-02-20 19:45:36 +01:00
|
|
|
while (fgets (buf, sizeof (buf), fp) != NULL)
|
1997-09-16 02:42:43 +02:00
|
|
|
{
|
|
|
|
p = buf;
|
|
|
|
if (*p == '#' || *p == '\n' || *p == '\r')
|
|
|
|
continue;
|
1998-02-20 19:45:36 +01:00
|
|
|
while (*p == ' ' || *p == '\t')
|
|
|
|
++p;
|
1997-09-16 02:42:43 +02:00
|
|
|
key = p;
|
|
|
|
while(*p != ' ' && *p != '\t' && *p != '=')
|
1998-02-20 19:45:36 +01:00
|
|
|
++p;
|
1997-09-16 02:42:43 +02:00
|
|
|
*p++ = 0;
|
1997-09-16 23:51:15 +02:00
|
|
|
|
1998-02-20 19:45:36 +01:00
|
|
|
while (isspace (*p) || *p == '=')
|
|
|
|
++p;
|
1997-09-16 02:42:43 +02:00
|
|
|
data = p;
|
1998-02-20 19:45:36 +01:00
|
|
|
while (!isspace (*p))
|
|
|
|
++p;
|
1997-09-16 02:42:43 +02:00
|
|
|
*p = 0;
|
|
|
|
|
1998-02-20 19:45:36 +01:00
|
|
|
if (cistrcmp (key, "lhs") == 0 || cistrcmp (key, "rhs") == 0)
|
1997-09-16 02:42:43 +02:00
|
|
|
{
|
1998-02-20 19:45:36 +01:00
|
|
|
which = (strcmp (key, "lhs") == 0) ? &ctx->lhs : &ctx->rhs;
|
|
|
|
*which = strdup (data);
|
1997-09-16 02:42:43 +02:00
|
|
|
if (!*which)
|
|
|
|
{
|
|
|
|
__set_errno (ENOMEM);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
1998-02-20 19:45:36 +01:00
|
|
|
else if (cistrcmp (key, "classes") == 0)
|
1997-09-16 02:42:43 +02:00
|
|
|
{
|
|
|
|
n = 0;
|
|
|
|
while (*data && n < 2)
|
|
|
|
{
|
|
|
|
p = data;
|
|
|
|
while (*p && *p != ',')
|
1998-02-20 19:45:36 +01:00
|
|
|
++p;
|
1997-09-16 02:42:43 +02:00
|
|
|
if (*p)
|
|
|
|
*p++ = 0;
|
1998-02-20 19:45:36 +01:00
|
|
|
if (cistrcmp (data, "IN") == 0)
|
1997-09-16 02:42:43 +02:00
|
|
|
ctx->classes[n++] = C_IN;
|
1998-02-20 19:45:36 +01:00
|
|
|
else if (cistrcmp (data, "HS") == 0)
|
1997-09-16 02:42:43 +02:00
|
|
|
ctx->classes[n++] = C_HS;
|
|
|
|
data = p;
|
|
|
|
}
|
|
|
|
while (n < 2)
|
|
|
|
ctx->classes[n++] = 0;
|
|
|
|
}
|
|
|
|
}
|
1998-02-20 19:45:36 +01:00
|
|
|
fclose (fp);
|
1997-09-16 02:42:43 +02:00
|
|
|
|
|
|
|
if (!ctx->rhs || ctx->classes[0] == 0 || ctx->classes[0] == ctx->classes[1])
|
|
|
|
{
|
|
|
|
__set_errno (ENOEXEC);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
1997-09-16 23:51:15 +02:00
|
|
|
}
|
1997-09-16 02:42:43 +02:00
|
|
|
|
|
|
|
/* Given a DNS class and a DNS name, do a lookup for TXT records, and
|
|
|
|
* return a list of them.
|
|
|
|
*/
|
1998-02-20 19:45:36 +01:00
|
|
|
static char **
|
|
|
|
get_txt_records (struct hesiod_p *ctx, int qclass, const char *name)
|
1997-09-16 02:42:43 +02:00
|
|
|
{
|
|
|
|
HEADER *hp;
|
|
|
|
unsigned char qbuf[PACKETSZ], abuf[MAX_HESRESP], *p, *eom, *eor;
|
|
|
|
char *dst, **list;
|
|
|
|
int ancount, qdcount, i, j, n, skip, type, class, len;
|
|
|
|
|
|
|
|
/* Make sure the resolver is initialized. */
|
1998-02-20 19:45:36 +01:00
|
|
|
if ((_res.options & RES_INIT) == 0 && res_init () == -1)
|
1997-09-16 02:42:43 +02:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Construct the query. */
|
1998-02-20 19:45:36 +01:00
|
|
|
n = res_mkquery (QUERY, name, qclass, T_TXT, NULL, 0,
|
|
|
|
NULL, qbuf, PACKETSZ);
|
1997-09-16 02:42:43 +02:00
|
|
|
if (n < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Send the query. */
|
1998-02-20 19:45:36 +01:00
|
|
|
n = res_send (qbuf, n, abuf, MAX_HESRESP);
|
1997-09-16 02:42:43 +02:00
|
|
|
if (n < 0)
|
|
|
|
{
|
|
|
|
__set_errno (ECONNREFUSED);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse the header of the result. */
|
|
|
|
hp = (HEADER *) abuf;
|
1998-02-20 19:45:36 +01:00
|
|
|
ancount = ntohs (hp->ancount);
|
|
|
|
qdcount = ntohs (hp->qdcount);
|
|
|
|
p = abuf + sizeof (HEADER);
|
1997-09-16 02:42:43 +02:00
|
|
|
eom = abuf + n;
|
|
|
|
|
|
|
|
/* Skip questions, trying to get to the answer section which follows. */
|
1998-02-20 19:45:36 +01:00
|
|
|
for (i = 0; i < qdcount; ++i)
|
1997-09-16 02:42:43 +02:00
|
|
|
{
|
1998-02-20 19:45:36 +01:00
|
|
|
skip = dn_skipname (p, eom);
|
1997-09-16 02:42:43 +02:00
|
|
|
if (skip < 0 || p + skip + QFIXEDSZ > eom)
|
|
|
|
{
|
|
|
|
__set_errno (EMSGSIZE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
p += skip + QFIXEDSZ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate space for the text record answers. */
|
1998-02-20 19:45:36 +01:00
|
|
|
list = malloc ((ancount + 1) * sizeof(char *));
|
|
|
|
if (list == NULL)
|
1997-09-16 02:42:43 +02:00
|
|
|
{
|
|
|
|
__set_errno (ENOMEM);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse the answers. */
|
|
|
|
j = 0;
|
|
|
|
for (i = 0; i < ancount; i++)
|
|
|
|
{
|
|
|
|
/* Parse the header of this answer. */
|
1998-02-20 19:45:36 +01:00
|
|
|
skip = dn_skipname (p, eom);
|
1997-09-16 02:42:43 +02:00
|
|
|
if (skip < 0 || p + skip + 10 > eom)
|
|
|
|
break;
|
|
|
|
type = p[skip + 0] << 8 | p[skip + 1];
|
|
|
|
class = p[skip + 2] << 8 | p[skip + 3];
|
|
|
|
len = p[skip + 8] << 8 | p[skip + 9];
|
|
|
|
p += skip + 10;
|
|
|
|
if (p + len > eom)
|
|
|
|
{
|
|
|
|
__set_errno (EMSGSIZE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip entries of the wrong class and type. */
|
|
|
|
if (class != qclass || type != T_TXT)
|
|
|
|
{
|
|
|
|
p += len;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate space for this answer. */
|
1998-02-20 19:45:36 +01:00
|
|
|
list[j] = malloc (len);
|
1997-09-16 02:42:43 +02:00
|
|
|
if (!list[j])
|
|
|
|
{
|
|
|
|
__set_errno (ENOMEM);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
dst = list[j++];
|
|
|
|
|
|
|
|
/* Copy answer data into the allocated area. */
|
|
|
|
eor = p + len;
|
|
|
|
while (p < eor)
|
|
|
|
{
|
|
|
|
n = (unsigned char) *p++;
|
|
|
|
if (p + n > eor)
|
|
|
|
{
|
|
|
|
__set_errno (EMSGSIZE);
|
|
|
|
break;
|
|
|
|
}
|
1998-02-20 19:45:36 +01:00
|
|
|
dst = mempcpy (dst, p, n);
|
1997-09-16 02:42:43 +02:00
|
|
|
p += n;
|
|
|
|
}
|
|
|
|
if (p < eor)
|
|
|
|
{
|
|
|
|
__set_errno (EMSGSIZE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*dst = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we didn't terminate the loop normally, something went wrong. */
|
|
|
|
if (i < ancount)
|
|
|
|
{
|
|
|
|
for (i = 0; i < j; i++)
|
1998-02-20 19:45:36 +01:00
|
|
|
free (list[i]);
|
|
|
|
free (list);
|
1997-09-16 02:42:43 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (j == 0)
|
|
|
|
{
|
|
|
|
__set_errno (ENOENT);
|
1998-02-20 19:45:36 +01:00
|
|
|
free (list);
|
1997-09-16 02:42:43 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
list[j] = NULL;
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
1997-09-16 23:51:15 +02:00
|
|
|
#ifndef _LIBC
|
1998-02-20 19:45:36 +01:00
|
|
|
static int
|
|
|
|
cistrcmp (const char *s1, const char *s2)
|
1997-09-16 02:42:43 +02:00
|
|
|
{
|
|
|
|
while (*s1 && tolower(*s1) == tolower(*s2))
|
|
|
|
{
|
|
|
|
s1++;
|
|
|
|
s2++;
|
|
|
|
}
|
|
|
|
return tolower(*s1) - tolower(*s2);
|
|
|
|
}
|
1997-09-16 23:51:15 +02:00
|
|
|
#endif
|