4 * function that operate on specific rr types
6 * (c) NLnet Labs, 2004-2006
7 * See the file LICENSE for the license
11 * These come strait from perldoc Net::DNS::RR::xxx
12 * first the read variant, then the write. This is
16 #include <ldns/config.h>
18 #include <ldns/ldns.h>
24 * return a specific rdf
25 * \param[in] type type of RR
26 * \param[in] rr the rr itself
27 * \param[in] pos at which postion to get it
28 * \return the rdf sought
31 ldns_rr_function(ldns_rr_type type
, const ldns_rr
*rr
, size_t pos
)
33 if (!rr
|| ldns_rr_get_type(rr
) != type
) {
36 return ldns_rr_rdf(rr
, pos
);
41 * \param[in] type type of RR
42 * \param[in] rr the rr itself
43 * \param[in] rdf the rdf to set
44 * \param[in] pos at which postion to set it
45 * \return true or false
48 ldns_rr_set_function(ldns_rr_type type
, ldns_rr
*rr
, ldns_rdf
*rdf
, size_t pos
)
51 if (!rr
|| ldns_rr_get_type(rr
) != type
) {
54 pop
= ldns_rr_set_rdf(rr
, rdf
, pos
);
55 ldns_rdf_deep_free(pop
);
61 ldns_rr_a_address(const ldns_rr
*r
)
63 /* 2 types to check, cannot use the macro */
64 if (!r
|| (ldns_rr_get_type(r
) != LDNS_RR_TYPE_A
&&
65 ldns_rr_get_type(r
) != LDNS_RR_TYPE_AAAA
)) {
68 return ldns_rr_rdf(r
, 0);
72 ldns_rr_a_set_address(ldns_rr
*r
, ldns_rdf
*f
)
74 /* 2 types to check, cannot use the macro... */
76 if (!r
|| (ldns_rr_get_type(r
) != LDNS_RR_TYPE_A
&&
77 ldns_rr_get_type(r
) != LDNS_RR_TYPE_AAAA
)) {
80 pop
= ldns_rr_set_rdf(r
, f
, 0);
91 ldns_rr_ns_nsdname(const ldns_rr
*r
)
93 return ldns_rr_function(LDNS_RR_TYPE_NS
, r
, 0);
98 ldns_rr_mx_preference(const ldns_rr
*r
)
100 return ldns_rr_function(LDNS_RR_TYPE_MX
, r
, 0);
104 ldns_rr_mx_exchange(const ldns_rr
*r
)
106 return ldns_rr_function(LDNS_RR_TYPE_MX
, r
, 1);
111 ldns_rr_rrsig_typecovered(const ldns_rr
*r
)
113 return ldns_rr_function(LDNS_RR_TYPE_RRSIG
, r
, 0);
117 ldns_rr_rrsig_set_typecovered(ldns_rr
*r
, ldns_rdf
*f
)
119 return ldns_rr_set_function(LDNS_RR_TYPE_RRSIG
, r
, f
, 0);
123 ldns_rr_rrsig_algorithm(const ldns_rr
*r
)
125 return ldns_rr_function(LDNS_RR_TYPE_RRSIG
, r
, 1);
129 ldns_rr_rrsig_set_algorithm(ldns_rr
*r
, ldns_rdf
*f
)
131 return ldns_rr_set_function(LDNS_RR_TYPE_RRSIG
, r
, f
, 1);
135 ldns_rr_rrsig_labels(const ldns_rr
*r
)
137 return ldns_rr_function(LDNS_RR_TYPE_RRSIG
, r
, 2);
141 ldns_rr_rrsig_set_labels(ldns_rr
*r
, ldns_rdf
*f
)
143 return ldns_rr_set_function(LDNS_RR_TYPE_RRSIG
, r
, f
, 2);
147 ldns_rr_rrsig_origttl(const ldns_rr
*r
)
149 return ldns_rr_function(LDNS_RR_TYPE_RRSIG
, r
, 3);
153 ldns_rr_rrsig_set_origttl(ldns_rr
*r
, ldns_rdf
*f
)
155 return ldns_rr_set_function(LDNS_RR_TYPE_RRSIG
, r
, f
, 3);
159 ldns_rr_rrsig_expiration(const ldns_rr
*r
)
161 return ldns_rr_function(LDNS_RR_TYPE_RRSIG
, r
, 4);
165 ldns_rr_rrsig_set_expiration(ldns_rr
*r
, ldns_rdf
*f
)
167 return ldns_rr_set_function(LDNS_RR_TYPE_RRSIG
, r
, f
, 4);
171 ldns_rr_rrsig_inception(const ldns_rr
*r
)
173 return ldns_rr_function(LDNS_RR_TYPE_RRSIG
, r
, 5);
177 ldns_rr_rrsig_set_inception(ldns_rr
*r
, ldns_rdf
*f
)
179 return ldns_rr_set_function(LDNS_RR_TYPE_RRSIG
, r
, f
, 5);
183 ldns_rr_rrsig_keytag(const ldns_rr
*r
)
185 return ldns_rr_function(LDNS_RR_TYPE_RRSIG
, r
, 6);
189 ldns_rr_rrsig_set_keytag(ldns_rr
*r
, ldns_rdf
*f
)
191 return ldns_rr_set_function(LDNS_RR_TYPE_RRSIG
, r
, f
, 6);
195 ldns_rr_rrsig_signame(const ldns_rr
*r
)
197 return ldns_rr_function(LDNS_RR_TYPE_RRSIG
, r
, 7);
201 ldns_rr_rrsig_set_signame(ldns_rr
*r
, ldns_rdf
*f
)
203 return ldns_rr_set_function(LDNS_RR_TYPE_RRSIG
, r
, f
, 7);
207 ldns_rr_rrsig_sig(const ldns_rr
*r
)
209 return ldns_rr_function(LDNS_RR_TYPE_RRSIG
, r
, 8);
213 ldns_rr_rrsig_set_sig(ldns_rr
*r
, ldns_rdf
*f
)
215 return ldns_rr_set_function(LDNS_RR_TYPE_RRSIG
, r
, f
, 8);
220 ldns_rr_dnskey_flags(const ldns_rr
*r
)
222 return ldns_rr_function(LDNS_RR_TYPE_DNSKEY
, r
, 0);
226 ldns_rr_dnskey_set_flags(ldns_rr
*r
, ldns_rdf
*f
)
228 return ldns_rr_set_function(LDNS_RR_TYPE_DNSKEY
, r
, f
, 0);
232 ldns_rr_dnskey_protocol(const ldns_rr
*r
)
234 return ldns_rr_function(LDNS_RR_TYPE_DNSKEY
, r
, 1);
238 ldns_rr_dnskey_set_protocol(ldns_rr
*r
, ldns_rdf
*f
)
240 return ldns_rr_set_function(LDNS_RR_TYPE_DNSKEY
, r
, f
, 1);
244 ldns_rr_dnskey_algorithm(const ldns_rr
*r
)
246 return ldns_rr_function(LDNS_RR_TYPE_DNSKEY
, r
, 2);
250 ldns_rr_dnskey_set_algorithm(ldns_rr
*r
, ldns_rdf
*f
)
252 return ldns_rr_set_function(LDNS_RR_TYPE_DNSKEY
, r
, f
, 2);
256 ldns_rr_dnskey_key(const ldns_rr
*r
)
258 return ldns_rr_function(LDNS_RR_TYPE_DNSKEY
, r
, 3);
262 ldns_rr_dnskey_set_key(ldns_rr
*r
, ldns_rdf
*f
)
264 return ldns_rr_set_function(LDNS_RR_TYPE_DNSKEY
, r
, f
, 3);
268 ldns_rr_dnskey_key_size_raw(const unsigned char* keydata
,
270 const ldns_algorithm alg
)
279 switch ((ldns_signing_algorithm
)alg
) {
281 case LDNS_SIGN_DSA_NSEC3
:
289 case LDNS_SIGN_RSAMD5
:
290 case LDNS_SIGN_RSASHA1
:
291 case LDNS_SIGN_RSASHA1_NSEC3
:
293 case LDNS_SIGN_RSASHA256
:
294 case LDNS_SIGN_RSASHA512
:
297 if (keydata
[0] == 0) {
300 memmove(&int16
, keydata
+ 1, 2);
302 return (len
- exp
- 3)*8;
308 return (len
-exp
-1)*8;
315 case LDNS_SIGN_ECC_GOST
:
319 case LDNS_SIGN_ECDSAP256SHA256
:
321 case LDNS_SIGN_ECDSAP384SHA384
:
324 case LDNS_SIGN_HMACMD5
:
332 ldns_rr_dnskey_key_size(const ldns_rr
*key
)
334 if (!key
|| !ldns_rr_dnskey_key(key
)
335 || !ldns_rr_dnskey_algorithm(key
)) {
338 return ldns_rr_dnskey_key_size_raw((unsigned char*)ldns_rdf_data(ldns_rr_dnskey_key(key
)),
339 ldns_rdf_size(ldns_rr_dnskey_key(key
)),
340 ldns_rdf2native_int8(ldns_rr_dnskey_algorithm(key
))
344 uint32_t ldns_soa_serial_identity(uint32_t ATTR_UNUSED(unused
), void *data
)
346 return (uint32_t) (intptr_t) data
;
349 uint32_t ldns_soa_serial_increment(uint32_t s
, void *ATTR_UNUSED(unused
))
351 return ldns_soa_serial_increment_by(s
, (void *)1);
354 uint32_t ldns_soa_serial_increment_by(uint32_t s
, void *data
)
356 return s
+ (intptr_t) data
;
359 uint32_t ldns_soa_serial_datecounter(uint32_t s
, void *data
)
364 time_t t
= data
? (time_t) (intptr_t) data
: ldns_time(NULL
);
366 (void) strftime(s_str
, 11, "%Y%m%d00", localtime_r(&t
, &tm
));
367 new_s
= (int32_t) atoi(s_str
);
368 return new_s
- ((int32_t) s
) <= 0 ? s
+1 : ((uint32_t) new_s
);
371 uint32_t ldns_soa_serial_unixtime(uint32_t s
, void *data
)
373 int32_t new_s
= data
? (int32_t) (intptr_t) data
374 : (int32_t) ldns_time(NULL
);
375 return new_s
- ((int32_t) s
) <= 0 ? s
+1 : ((uint32_t) new_s
);
379 ldns_rr_soa_increment(ldns_rr
*soa
)
381 ldns_rr_soa_increment_func_data(soa
, ldns_soa_serial_increment
, NULL
);
385 ldns_rr_soa_increment_func(ldns_rr
*soa
, ldns_soa_serial_increment_func_t f
)
387 ldns_rr_soa_increment_func_data(soa
, f
, NULL
);
391 ldns_rr_soa_increment_func_data(ldns_rr
*soa
,
392 ldns_soa_serial_increment_func_t f
, void *data
)
394 ldns_rdf
*prev_soa_serial_rdf
;
395 if ( !soa
|| !f
|| ldns_rr_get_type(soa
) != LDNS_RR_TYPE_SOA
396 || !ldns_rr_rdf(soa
, 2)) {
399 prev_soa_serial_rdf
= ldns_rr_set_rdf(
401 , ldns_native2rdf_int32(
403 , (*f
)( ldns_rdf2native_int32(
410 LDNS_FREE(prev_soa_serial_rdf
);
414 ldns_rr_soa_increment_func_int(ldns_rr
*soa
,
415 ldns_soa_serial_increment_func_t f
, int data
)
417 ldns_rr_soa_increment_func_data(soa
, f
, (void *) (intptr_t) data
);