/build/ldns-Ls6SRU/ldns-1.8.4/update.c
Go to the documentation of this file.
/* update.c
*
* Functions for RFC 2136 Dynamic Update
*
* Copyright (c) 2005-2008, NLnet Labs. All rights reserved.
*
* See LICENSE for the license.
*/
#include <ldns/config.h>
#include <ldns/ldns.h>
#include <strings.h>
#include <stdlib.h>
#include <limits.h>
/*
* RFC 2136 sections mapped to RFC 1035:
* zone/ZO -- QD/question
* prerequisites/PR -- AN/answers
* updates/UP -- NS/authority records
* additional data/AD -- AR/additional records
*/
ldns_pkt *
ldns_update_pkt_new(ldns_rdf *zone_rdf, ldns_rr_class c,
const ldns_rr_list *pr_rrlist, const ldns_rr_list *up_rrlist, const ldns_rr_list *ad_rrlist)
{
ldns_pkt *p;
if (!zone_rdf || !up_rrlist) {
return NULL;
}
if (c == 0) {
c = LDNS_RR_CLASS_IN;
}
/* Create packet, fill in Zone Section. */
p = ldns_pkt_query_new(zone_rdf, LDNS_RR_TYPE_SOA, c, LDNS_RD);
if (!p) {
return NULL;
}
zone_rdf = NULL; /* No longer safe to use. */
ldns_pkt_set_opcode(p, LDNS_PACKET_UPDATE);
ldns_rr_list_deep_free(p->_authority);
ldns_pkt_set_authority(p, ldns_rr_list_clone(up_rrlist));
ldns_update_set_upcount(p, ldns_rr_list_rr_count(up_rrlist));
if (pr_rrlist) {
ldns_rr_list_deep_free(p->_answer); /*XXX access function */
ldns_pkt_set_answer(p, ldns_rr_list_clone(pr_rrlist));
ldns_update_set_prcount(p, ldns_rr_list_rr_count(pr_rrlist));
}
if (ad_rrlist) {
ldns_rr_list_deep_free(p->_additional);
ldns_pkt_set_additional(p, ldns_rr_list_clone(ad_rrlist));
ldns_update_set_adcount(p, ldns_rr_list_rr_count(ad_rrlist));
}
return p;
}
ldns_status
ldns_update_pkt_tsig_add(ldns_pkt *p, const ldns_resolver *r)
{
#ifdef HAVE_SSL
uint16_t fudge = 300; /* Recommended fudge. [RFC2845 6.4] */
if (ldns_resolver_tsig_keyname(r) && ldns_resolver_tsig_keydata(r))
return ldns_pkt_tsig_sign(p, ldns_resolver_tsig_keyname(r),
ldns_resolver_tsig_keydata(r), fudge,
ldns_resolver_tsig_algorithm(r), NULL);
#else
/* do nothing */
(void)p;
(void)r;
#endif /* HAVE_SSL */
/* No TSIG to do. */
return LDNS_STATUS_OK;
}
/* Move to higher.c or similar? */
/* XXX doc */
ldns_status
ldns_update_soa_mname(ldns_rdf *zone, ldns_resolver *r,
ldns_rr_class c, ldns_rdf **mname)
{
ldns_rr *soa_rr;
ldns_pkt *query, *resp;
/* Nondestructive, so clone 'zone' here */
query = ldns_pkt_query_new(ldns_rdf_clone(zone), LDNS_RR_TYPE_SOA,
c, LDNS_RD);
if (!query) {
return LDNS_STATUS_ERR;
}
ldns_pkt_set_random_id(query);
if (ldns_resolver_send_pkt(&resp, r, query) != LDNS_STATUS_OK) {
ldns_pkt_free(query);
return LDNS_STATUS_ERR;
}
ldns_pkt_free(query);
if (!resp) {
return LDNS_STATUS_ERR;
}
/* Expect a SOA answer. */
*mname = NULL;
while ((soa_rr = ldns_rr_list_pop_rr(ldns_pkt_answer(resp)))) {
if (ldns_rr_get_type(soa_rr) != LDNS_RR_TYPE_SOA
|| ldns_rr_rdf(soa_rr, 0) == NULL)
continue;
/* [RFC1035 3.3.13] */
*mname = ldns_rdf_clone(ldns_rr_rdf(soa_rr, 0));
break;
}
ldns_pkt_free(resp);
return *mname ? LDNS_STATUS_OK : LDNS_STATUS_ERR;
}
/* Try to get zone and MNAME from SOA queries. */
ldns_status
ldns_update_soa_zone_mname(const char *fqdn, ldns_resolver *r,
ldns_rr_class c, ldns_rdf **zone_rdf, ldns_rdf **mname_rdf)
{
ldns_rr *soa_rr, *rr;
ldns_rdf *soa_zone = NULL, *soa_mname = NULL;
ldns_rdf *ipaddr, *fqdn_rdf, *tmp;
ldns_rdf **nslist;
ldns_pkt *query, *resp;
ldns_resolver *tmp_r;
size_t i;
/*
* XXX Ok, this cannot be the best way to find this...?
* XXX (I run into weird cache-related stuff here)
*/
/* Step 1 - first find a nameserver that should know *something* */
fqdn_rdf = ldns_dname_new_frm_str(fqdn);
query = ldns_pkt_query_new(fqdn_rdf, LDNS_RR_TYPE_SOA, c, LDNS_RD);
if (!query) {
return LDNS_STATUS_ERR;
}
fqdn_rdf = NULL;
ldns_pkt_set_random_id(query);
if (ldns_resolver_send_pkt(&resp, r, query) != LDNS_STATUS_OK) {
ldns_pkt_free(query);
return LDNS_STATUS_ERR;
}
ldns_pkt_free(query);
if (!resp) {
return LDNS_STATUS_ERR;
}
/* XXX Is it safe to only look in authority section here? */
while ((soa_rr = ldns_rr_list_pop_rr(ldns_pkt_authority(resp)))) {
if (ldns_rr_get_type(soa_rr) != LDNS_RR_TYPE_SOA
|| ldns_rr_rdf(soa_rr, 0) == NULL)
continue;
/* [RFC1035 3.3.13] */
soa_mname = ldns_rdf_clone(ldns_rr_rdf(soa_rr, 0));
break;
}
ldns_pkt_free(resp);
if (!soa_rr) {
return LDNS_STATUS_ERR;
}
/* Step 2 - find SOA MNAME IP address, add to resolver */
query = ldns_pkt_query_new(soa_mname, LDNS_RR_TYPE_A, c, LDNS_RD);
if (!query) {
return LDNS_STATUS_ERR;
}
soa_mname = NULL;
ldns_pkt_set_random_id(query);
if (ldns_resolver_send_pkt(&resp, r, query) != LDNS_STATUS_OK) {
ldns_pkt_free(query);
return LDNS_STATUS_ERR;
}
ldns_pkt_free(query);
if (!resp) {
return LDNS_STATUS_ERR;
}
if (ldns_pkt_ancount(resp) == 0) {
ldns_pkt_free(resp);
return LDNS_STATUS_ERR;
}
/* XXX There may be more than one answer RR here. */
rr = ldns_rr_list_pop_rr(ldns_pkt_answer(resp));
ipaddr = ldns_rr_rdf(rr, 0);
/* Put the SOA mname IP first in the nameserver list. */
if (!(tmp_r = ldns_resolver_clone(r))) {
return LDNS_STATUS_MEM_ERR;
}
nslist = ldns_resolver_nameservers(tmp_r);
for (i = 0; i < ldns_resolver_nameserver_count(tmp_r); i++) {
if (ldns_rdf_compare(ipaddr, nslist[i]) == 0) {
if (i) {
tmp = nslist[0];
nslist[0] = nslist[i];
nslist[i] = tmp;
}
break;
}
}
if (i >= ldns_resolver_nameserver_count(tmp_r)) {
/* SOA mname was not part of the resolver so add it first. */
(void) ldns_resolver_push_nameserver(tmp_r, ipaddr);
nslist = ldns_resolver_nameservers(tmp_r);
i = ldns_resolver_nameserver_count(tmp_r) - 1;
tmp = nslist[0];
nslist[0] = nslist[i];
nslist[i] = tmp;
}
ldns_pkt_free(resp);
/* Make sure to ask the first in the list, i.e SOA mname */
ldns_resolver_set_random(tmp_r, false);
/* Step 3 - Redo SOA query, sending to SOA MNAME directly. */
fqdn_rdf = ldns_dname_new_frm_str(fqdn);
query = ldns_pkt_query_new(fqdn_rdf, LDNS_RR_TYPE_SOA, c, LDNS_RD);
if (!query) {
ldns_resolver_free(tmp_r);
return LDNS_STATUS_ERR;
}
fqdn_rdf = NULL;
ldns_pkt_set_random_id(query);
if (ldns_resolver_send_pkt(&resp, tmp_r, query) != LDNS_STATUS_OK) {
ldns_pkt_free(query);
ldns_resolver_free(tmp_r);
return LDNS_STATUS_ERR;
}
ldns_resolver_free(tmp_r);
ldns_pkt_free(query);
if (!resp) {
return LDNS_STATUS_ERR;
}
/* XXX Is it safe to only look in authority section here, too? */
while ((soa_rr = ldns_rr_list_pop_rr(ldns_pkt_authority(resp)))) {
if (ldns_rr_get_type(soa_rr) != LDNS_RR_TYPE_SOA
|| ldns_rr_rdf(soa_rr, 0) == NULL)
continue;
/* [RFC1035 3.3.13] */
soa_mname = ldns_rdf_clone(ldns_rr_rdf(soa_rr, 0));
soa_zone = ldns_rdf_clone(ldns_rr_owner(soa_rr));
break;
}
ldns_pkt_free(resp);
if (!soa_rr) {
return LDNS_STATUS_ERR;
}
/* That seems to have worked, pass results to caller. */
*zone_rdf = soa_zone;
*mname_rdf = soa_mname;
return LDNS_STATUS_OK;
}
/*
* ldns_update_{get,set}_{zo,pr,up,ad}count
*/
uint16_t
ldns_update_zocount(const ldns_pkt *p)
{
return ldns_pkt_qdcount(p);
}
uint16_t
ldns_update_prcount(const ldns_pkt *p)
{
return ldns_pkt_ancount(p);
}
uint16_t
ldns_update_upcount(const ldns_pkt *p)
{
return ldns_pkt_nscount(p);
}
uint16_t
ldns_update_ad(const ldns_pkt *p)
{
return ldns_pkt_arcount(p);
}
void
ldns_update_set_zo(ldns_pkt *p, uint16_t v)
{
ldns_pkt_set_qdcount(p, v);
}
void
ldns_update_set_prcount(ldns_pkt *p, uint16_t v)
{
ldns_pkt_set_ancount(p, v);
}
void
ldns_update_set_upcount(ldns_pkt *p, uint16_t v)
{
ldns_pkt_set_nscount(p, v);
}
void
ldns_update_set_adcount(ldns_pkt *p, uint16_t v)
{
ldns_pkt_set_arcount(p, v);
}
config.h
ldns_dname_new_frm_str
ldns_rdf * ldns_dname_new_frm_str(const char *str)
creates a new dname rdf from a string.
Definition dname.c:268
LDNS_STATUS_ERR
@ LDNS_STATUS_ERR
Definition error.h:37
LDNS_STATUS_MEM_ERR
@ LDNS_STATUS_MEM_ERR
Definition error.h:34
LDNS_STATUS_OK
@ LDNS_STATUS_OK
Definition error.h:26
ldns_status
enum ldns_enum_status ldns_status
Definition error.h:148
ldns_pkt_free
void ldns_pkt_free(ldns_pkt *packet)
frees the packet structure and all data that it contains.
Definition packet.c:897
ldns_pkt_set_additional
void ldns_pkt_set_additional(ldns_pkt *p, ldns_rr_list *rr)
directly set the additional section
Definition packet.c:509
ldns_pkt_authority
ldns_rr_list * ldns_pkt_authority(const ldns_pkt *p)
Return the packet's authority section.
Definition packet.c:136
ldns_pkt_set_authority
void ldns_pkt_set_authority(ldns_pkt *p, ldns_rr_list *rr)
directly set the authority section
Definition packet.c:527
ldns_pkt_query_new
ldns_pkt * ldns_pkt_query_new(ldns_rdf *rr_name, ldns_rr_type rr_type, ldns_rr_class rr_class, uint16_t flags)
creates a packet with a query in it for the given name, type and class.
Definition packet.c:1150
ldns_pkt_ancount
uint16_t ldns_pkt_ancount(const ldns_pkt *p)
Return the packet's an count.
Definition packet.c:106
ldns_pkt_nscount
uint16_t ldns_pkt_nscount(const ldns_pkt *p)
Return the packet's ns count.
Definition packet.c:112
ldns_pkt_answer
ldns_rr_list * ldns_pkt_answer(const ldns_pkt *p)
Return the packet's answer section.
Definition packet.c:130
ldns_pkt
struct ldns_struct_pkt ldns_pkt
Definition packet.h:272
LDNS_PACKET_UPDATE
@ LDNS_PACKET_UPDATE
Definition packet.h:51
ldns_pkt_set_answer
void ldns_pkt_set_answer(ldns_pkt *p, ldns_rr_list *rr)
directly set the answer section
Definition packet.c:521
ldns_pkt_set_qdcount
void ldns_pkt_set_qdcount(ldns_pkt *p, uint16_t c)
Set the packet's qd count.
Definition packet.c:563
ldns_pkt_qdcount
uint16_t ldns_pkt_qdcount(const ldns_pkt *p)
Return the packet's qd count.
Definition packet.c:100
ldns_pkt_set_arcount
void ldns_pkt_set_arcount(ldns_pkt *p, uint16_t c)
Set the packet's arcount.
Definition packet.c:581
ldns_pkt_set_opcode
void ldns_pkt_set_opcode(ldns_pkt *p, ldns_pkt_opcode c)
Set the packet's opcode.
Definition packet.c:551
ldns_pkt_arcount
uint16_t ldns_pkt_arcount(const ldns_pkt *p)
Return the packet's ar count.
Definition packet.c:118
ldns_pkt_set_random_id
void ldns_pkt_set_random_id(ldns_pkt *p)
Set the packet's id to a random value.
Definition packet.c:477
ldns_pkt_set_ancount
void ldns_pkt_set_ancount(ldns_pkt *p, uint16_t c)
Set the packet's an count.
Definition packet.c:569
LDNS_RD
#define LDNS_RD
Definition packet.h:30
ldns_pkt_set_nscount
void ldns_pkt_set_nscount(ldns_pkt *p, uint16_t c)
Set the packet's ns count.
Definition packet.c:575
ldns_rdf_clone
ldns_rdf * ldns_rdf_clone(const ldns_rdf *rd)
clones a rdf structure.
Definition rdata.c:222
ldns_rdf
struct ldns_struct_rdf ldns_rdf
Definition rdata.h:205
ldns_rdf_compare
int ldns_rdf_compare(const ldns_rdf *rd1, const ldns_rdf *rd2)
compares two rdf's on their wire formats.
Definition rdata.c:657
ldns_resolver_tsig_keydata
const char * ldns_resolver_tsig_keydata(const ldns_resolver *r)
Return the tsig keydata as used by the nameserver.
Definition resolver.c:219
ldns_resolver_tsig_keyname
const char * ldns_resolver_tsig_keyname(const ldns_resolver *r)
Return the tsig keyname as used by the nameserver.
Definition resolver.c:207
ldns_resolver_nameserver_count
size_t ldns_resolver_nameserver_count(const ldns_resolver *r)
How many nameserver are configured in the resolver.
Definition resolver.c:114
ldns_resolver_free
void ldns_resolver_free(ldns_resolver *res)
Frees the allocated space for this resolver.
Definition resolver.c:998
ldns_resolver_clone
ldns_resolver * ldns_resolver_clone(ldns_resolver *r)
Clone a resolver.
Definition resolver.c:665
ldns_resolver_nameservers
ldns_rdf ** ldns_resolver_nameservers(const ldns_resolver *r)
Return the configured nameserver ip address.
Definition resolver.c:108
ldns_resolver
struct ldns_struct_resolver ldns_resolver
Definition resolver.h:147
ldns_resolver_push_nameserver
ldns_status ldns_resolver_push_nameserver(ldns_resolver *r, const ldns_rdf *n)
Push a new nameserver to the resolver.
Definition resolver.c:288
ldns_resolver_set_random
void ldns_resolver_set_random(ldns_resolver *r, signed char b)
Should the nameserver list be randomized before each use.
Definition resolver.c:601
ldns_resolver_tsig_algorithm
const char * ldns_resolver_tsig_algorithm(const ldns_resolver *r)
Return the tsig algorithm as used by the nameserver.
Definition resolver.c:213
ldns_resolver_send_pkt
ldns_status ldns_resolver_send_pkt(ldns_pkt **answer, ldns_resolver *r, ldns_pkt *query_pkt)
Send the given packet to a nameserver.
Definition resolver.c:1171
ldns_rr_list_deep_free
void ldns_rr_list_deep_free(ldns_rr_list *rr_list)
frees an rr_list structure and all rrs contained therein.
Definition rr.c:1024
ldns_rr
struct ldns_struct_rr ldns_rr
Definition rr.h:337
ldns_rr_list_pop_rr
ldns_rr * ldns_rr_list_pop_rr(ldns_rr_list *rr_list)
pops the last rr from an rrlist.
Definition rr.c:1181
LDNS_RR_TYPE_A
@ LDNS_RR_TYPE_A
a host address
Definition rr.h:80
LDNS_RR_TYPE_SOA
@ LDNS_RR_TYPE_SOA
marks the start of a zone of authority
Definition rr.h:90
ldns_rr_rdf
ldns_rdf * ldns_rr_rdf(const ldns_rr *rr, size_t nr)
returns the rdata field member counter.
Definition rr.c:913
ldns_rr_list_rr_count
size_t ldns_rr_list_rr_count(const ldns_rr_list *rr_list)
returns the number of rr's in an rr_list.
Definition rr.c:961
ldns_rr_get_type
ldns_rr_type ldns_rr_get_type(const ldns_rr *rr)
returns the type of the rr.
Definition rr.c:947
ldns_rr_list
struct ldns_struct_rr_list ldns_rr_list
Definition rr.h:351
ldns_rr_class
enum ldns_enum_rr_class ldns_rr_class
Definition rr.h:61
ldns_rr_list_clone
ldns_rr_list * ldns_rr_list_clone(const ldns_rr_list *rrlist)
clones an rrlist.
Definition rr.c:1435
ldns_rr_owner
ldns_rdf * ldns_rr_owner(const ldns_rr *rr)
returns the owner name of an rr structure.
Definition rr.c:923
LDNS_RR_CLASS_IN
@ LDNS_RR_CLASS_IN
the Internet
Definition rr.h:47
ldns_struct_pkt::_authority
ldns_rr_list * _authority
Authority section.
Definition packet.h:268
ldns_struct_pkt::_answer
ldns_rr_list * _answer
Answer section.
Definition packet.h:266
ldns_struct_pkt::_additional
ldns_rr_list * _additional
Additional section.
Definition packet.h:270
ldns_pkt_tsig_sign
ldns_status ldns_pkt_tsig_sign(ldns_pkt *pkt, const char *key_name, const char *key_data, uint16_t fudge, const char *algorithm_name, const ldns_rdf *query_mac)
creates a tsig rr for the given packet and key.
Definition tsig.c:376
ldns_update_ad
uint16_t ldns_update_ad(const ldns_pkt *p)
Get the zo count.
Definition update.c:298
ldns_update_set_zo
void ldns_update_set_zo(ldns_pkt *p, uint16_t v)
Set the zo count.
Definition update.c:304
ldns_update_set_adcount
void ldns_update_set_adcount(ldns_pkt *p, uint16_t v)
Set the ad count.
Definition update.c:322
ldns_update_upcount
uint16_t ldns_update_upcount(const ldns_pkt *p)
Get the zo count.
Definition update.c:292
ldns_update_zocount
uint16_t ldns_update_zocount(const ldns_pkt *p)
Get the zo count.
Definition update.c:280
ldns_update_soa_zone_mname
ldns_status ldns_update_soa_zone_mname(const char *fqdn, ldns_resolver *r, ldns_rr_class c, ldns_rdf **zone_rdf, ldns_rdf **mname_rdf)
Definition update.c:130
ldns_update_prcount
uint16_t ldns_update_prcount(const ldns_pkt *p)
Get the zo count.
Definition update.c:286
ldns_update_pkt_new
ldns_pkt * ldns_update_pkt_new(ldns_rdf *zone_rdf, ldns_rr_class c, const ldns_rr_list *pr_rrlist, const ldns_rr_list *up_rrlist, const ldns_rr_list *ad_rrlist)
create an update packet from zone name, class and the rr lists
Definition update.c:27
ldns_update_set_prcount
void ldns_update_set_prcount(ldns_pkt *p, uint16_t v)
Set the pr count.
Definition update.c:310
ldns_update_set_upcount
void ldns_update_set_upcount(ldns_pkt *p, uint16_t v)
Set the up count.
Definition update.c:316
ldns_update_pkt_tsig_add
ldns_status ldns_update_pkt_tsig_add(ldns_pkt *p, const ldns_resolver *r)
add tsig credentials to a packet from a resolver
Definition update.c:70
ldns_update_soa_mname
ldns_status ldns_update_soa_mname(ldns_rdf *zone, ldns_resolver *r, ldns_rr_class c, ldns_rdf **mname)
Definition update.c:90