getrrset.c revision 0c27b3fe77ac1d5094ba3521e8142d9e7973133f
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 2000-2005, 2007, 2012, 2014, 2016 Internet Systems Consortium, Inc. ("ISC")
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
70e5a7403f0e0a3bd292b8287c5fed5772c15270Automatic Updater/* $Id: getrrset.c,v 1.18 2007/06/19 23:47:22 tbox Exp $ */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * DESCRIPTION
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * lwres_getrrsetbyname() gets a set of resource records associated with
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * a hostname, class, and type. hostname is a pointer a to
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * null-terminated string. The flags field is currently unused and must
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * After a successful call to lwres_getrrsetbyname(), *res is a pointer
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * to an #rrsetinfo structure, containing a list of one or more #rdatainfo
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * structures containing resource records and potentially another list of
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * rdatainfo structures containing SIG resource records associated with
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * those records. The members #rri_rdclass and #rri_rdtype are copied from
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * the parameters. #rri_ttl and #rri_name are properties of the obtained
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * rrset. The resource records contained in #rri_rdatas and #rri_sigs are
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * in uncompressed DNS wire format. Properties of the rdataset are
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * represented in the #rri_flags bitfield. If the #RRSET_VALIDATED bit is
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * set, the data has been DNSSEC validated and the signatures verified.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * All of the information returned by lwres_getrrsetbyname() is
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * dynamically allocated: the rrsetinfo and rdatainfo structures, and the
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * canonical host name strings pointed to by the rrsetinfostructure.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * Memory allocated for the dynamically allocated structures created by a
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * successful call to lwres_getrrsetbyname() is released by
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * lwres_freerrset(). rrset is a pointer to a struct rrset created by a
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * call to lwres_getrrsetbyname().
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * The following structures are used:
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * struct rdatainfo {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * unsigned int rdi_length; // length of data
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * unsigned char *rdi_data; // record data
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * struct rrsetinfo {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * unsigned int rri_flags; // RRSET_VALIDATED...
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * unsigned int rri_rdclass; // class number
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * unsigned int rri_rdtype; // RR type number
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * unsigned int rri_ttl; // time to live
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * unsigned int rri_nrdatas; // size of rdatas array
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * unsigned int rri_nsigs; // size of sigs array
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * char *rri_name; // canonical name
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * struct rdatainfo *rri_rdatas; // individual records
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * struct rdatainfo *rri_sigs; // individual signatures
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * \section getrrset_return Return Values
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * lwres_getrrsetbyname() returns zero on success, and one of the
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * following error codes if an error occurred:
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * \li #ERRSET_NONAME: the name does not exist
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * \li #ERRSET_NODATA:
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * the name exists, but does not have data of the desired type
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * \li #ERRSET_NOMEMORY:
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * memory could not be allocated
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * \li #ERRSET_INVAL:
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * a parameter is invalid
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * \li #ERRSET_FAIL:
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * other failure
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington#include <lwres/netdb.h> /* XXX #include <netdb.h> */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * Structure to map results
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellingtonstatic unsigned int
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellingtonlwresult_to_result(lwres_result_t lwresult) {
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington case LWRES_R_SUCCESS: return (ERRSET_SUCCESS);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington case LWRES_R_NOMEMORY: return (ERRSET_NOMEMORY);
47ad4fad771b9c570bcf57793d6a931e0ca9856cAndreas Gustafsson case LWRES_R_NOTFOUND: return (ERRSET_NONAME);
47ad4fad771b9c570bcf57793d6a931e0ca9856cAndreas Gustafsson case LWRES_R_TYPENOTFOUND: return (ERRSET_NODATA);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington default: return (ERRSET_FAIL);
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson * malloc / calloc functions that guarantee to only
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson * return NULL if there is an error, like they used
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson * to before the ANSI C committee broke them.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*% Returns a set of resource records associated with a hostname, class, and type. hostname is a pointer a to null-terminated string. */
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellingtonlwres_getrrsetbyname(const char *hostname, unsigned int rdclass,
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington unsigned int i;
2103b1d4600dcfb0c28dde10c6c96a867f775f57Brian Wellington * Don't allow queries of class or type ANY
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwresult = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington (void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington * If any input flags were defined, lwflags would be set here
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington * based on them
5e2f4725611cf90870268a798409ce3ebe684474Brian Wellington lwresult = lwres_getrdatabyname(lwrctx, hostname,
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson rrset = sane_malloc(sizeof(struct rrsetinfo));
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson rrset->rri_name = sane_malloc(response->realnamelen + 1);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington strncpy(rrset->rri_name, response->realname, response->realnamelen);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington rrset->rri_name[response->realnamelen] = 0;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if ((response->flags & LWRDATA_VALIDATED) != 0)
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson rrset->rri_rdatas = sane_calloc(rrset->rri_nrdatas,
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington sizeof(struct rdatainfo));
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington rrset->rri_rdatas[i].rdi_length = response->rdatalen[i];
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson sane_malloc(rrset->rri_rdatas[i].rdi_length);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (rrset->rri_rdatas[i].rdi_data == NULL) {
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt memmove(rrset->rri_rdatas[i].rdi_data, response->rdatas[i],
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson rrset->rri_sigs = sane_calloc(rrset->rri_nsigs,
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson sizeof(struct rdatainfo));
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington rrset->rri_sigs[i].rdi_length = response->siglen[i];
f00075e753b20ece0b4daf68b29044e44c898d89Andreas Gustafsson sane_malloc(rrset->rri_sigs[i].rdi_length);
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt memmove(rrset->rri_sigs[i].rdi_data, response->sigs[i],
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_grbnresponse_free(lwrctx, &response);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_grbnresponse_free(lwrctx, &response);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*% Releases memory allocated for the dynamically allocated structures created by a successful call to lwres_getrrsetbyname(). */
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington unsigned int i;