aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/DNS-LDNS/lib/DNS/LDNS/Resolver.pm
blob: 111e64f0b7ec71590ccbd4b67fd2636afc7ba3b5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package DNS::LDNS::Resolver;

use 5.008008;
use strict;
use warnings;

use DNS::LDNS ':all';

our $VERSION = '0.06';

sub new {
    my ($class, %args) = @_;
    
    my $file;
    my $status = &LDNS_STATUS_OK;

    if ($args{filename}) {
	unless (open FILE, $args{filename}) {
	    $DNS::LDNS::last_status = &LDNS_STATUS_FILE_ERR;
	    $DNS::LDNS::line_nr = 0;
	    return;
	}

	$file = \*FILE;
    }
    elsif ($args{file}) {
	$file = $args{file};
    }

    my $resolver;
    if ($file) {
	$resolver = _new_from_file($file, $status);
    }
    else {
	$resolver = _new();
    }

    if ($args{filename}) {
	close $file;
    }

    $DNS::LDNS::last_status = $status;
    if (!defined $resolver) {
	return;
    }

    return $resolver;
}

sub dnssec_anchors {
    my $self = shift;
    return DNS::LDNS::GC::own($self->_dnssec_anchors, $self);
}

sub push_dnssec_anchor {
    my ($self, $rr) = @_;
    
    _push_dnssec_anchor($self, my $copy = $rr->clone);
    DNS::LDNS::GC::own($copy, $self);
}

sub set_dnssec_anchors {
    my ($self, $l) = @_;
    DNS::LDNS::GC::disown(my $old = $self->dnssec_anchors);
    $self->_set_dnssec_anchors($l);
    DNS::LDNS::GC::own($l, $self);
    return $l;
}

sub domain {
    my $self = shift;
    return DNS::LDNS::GC::own($self->_domain, $self);
}

sub set_domain {
    my ($self, $dom) = @_;
    DNS::LDNS::GC::disown(my $old = $self->domain);
    _set_domain($self, my $copy = $dom->clone);
    DNS::LDNS::GC::own($copy, $self);
}

sub nameservers {
    my $self = shift;
    my $list = _nameservers($self);
    for my $r (@$list) {
	DNS::LDNS::GC::own($r, $self);
    }
    return wantarray ? @$list : $list;
}

sub push_nameserver {
    my ($self, $n) = @_;

    my $s = _push_nameserver($self, my $copy = $n->clone);
    DNS::LDNS::GC::own($copy, $self);
    $DNS::LDNS::last_status = $s;
    return $s;
}

sub pop_nameserver {
    my $self = shift;
    return DNS::LDNS::GC::own($self->_pop_nameserver);
}

sub push_searchlist {
    my ($self, $rd) = @_;

    _push_searchlist($self, my $copy = $rd->clone);
    DNS::LDNS::GC::own($copy, $self);
}

sub searchlist {
    my $self = shift;
    my $list = _searchlist($self);
    for my $r (@$list) {
	DNS::LDNS::GC::own($r, $self);
    }
    return wantarray ? @$list : $list;
}

sub timeout {
    my $self = shift;
    my $t = _timeout($self);
    return wantarray ? @$t : $t;
}

sub rtt {
    my $self = shift;
    my $list = _rtt($self);
    return wantarray ? @$list : $list;
}

sub set_rtt {
    my ($self, @rtt) = @_;
    # FIXME: Validate @rtt, existence, size
    _set_rtt($self, \@rtt);
}

sub fetch_valid_domain_keys {
    my ($self, $domain, $keys) = @_;

    my $status;
    my $trusted = _fetch_valid_domain_keys($self, $domain, $keys, $status);
    $DNS::LDNS::last_status = $status;
    if (!$trusted) {
	return;
    }

    return DNS::LDNS::GC::own($trusted, $self);
}

sub fetch_valid_domain_keys_time {
    my ($self, $domain, $keys, $checktime) = @_;

    my $status;
    my $trusted = _fetch_valid_domain_keys_time(
	$self, $domain, $keys, $checktime, $status);
    $DNS::LDNS::last_status = $status;
    if (!$trusted) {
	return;
    }

    return DNS::LDNS::GC::own($trusted, $self);
}

sub prepare_query_pkt {
    my ($self, $rdata, $type, $class, $flags) = @_;

    my $s = &LDNS_STATUS_OK;
    my $qry = _prepare_query_pkt($self, $rdata, $type, $class, $flags, $s);
    $DNS::LDNS::last_status = $s;
    if ($s != LDNS_STATUS_OK) {
	return;
    }
    return $qry;
}

sub send {
    my ($self, $rdata, $type, $class, $flags) = @_;

    my $s = &LDNS_STATUS_OK;
    my $ans = _send($self, $rdata, $type, $class, $flags, $s);
    $DNS::LDNS::last_status = $s;
    if ($s != LDNS_STATUS_OK) {
	return;
    }
    return $ans;
}

sub send_pkt {
    my ($self, $qry) = @_;

    my $s = &LDNS_STATUS_OK;
    my $ans = _send_pkt($self, $qry, $s);
    $DNS::LDNS::last_status = $s;
    if ($s != LDNS_STATUS_OK) {
	return;
    }
    return $ans;
}

sub verify_trusted {
    my ($self, $rrset, $rrsigs, $validating_keys) = @_;
    my $s = _verify_trusted($self, $rrset, $rrsigs, $validating_keys);
    $DNS::LDNS::last_status = $s;
    return $s;
}

sub verify_trusted_time {
    my ($self, $rrset, $rrsigs, $check_time, $validating_keys) = @_;
    my $s = _verify_trusted_time($self, $rrset, $rrsigs, $check_time, 
			    $validating_keys);
    $DNS::LDNS::last_status = $s;
    return $s;
}

sub DESTROY {
    DNS::LDNS::GC::free($_[0]);
}

1;
__END__

=head1 NAME

DNS::LDNS::Resolver - DNS resolver

=head1 SYNOPSIS

  use DNS::LDNS ':all'

  my r = new DNS::LDNS::Resolver(filename => '/my/resolv.conf')
  my r = new DNS::LDNS::Resolver(file => \*FILE)
  my r = new DNS::LDNS::Resolver

  bool = r->dnssec
  r->set_dnssec(bool)

  bool = r->dnssec_cd        # Resolver sets the CD bit
  r->set_dnssec_cd(bool)

  port = r->port
  r->set_port(port)

  bool = r->recursive
  r->set_recursive(bool)

  bool = r->debug
  r->set_debug(bool)

  count = r->retry
  r->set_retry(count)

  count = r->retrans
  r->set_retrans(count)

  bool = r->fallback          # Resolver truncation fallback mechanism
  r->set_fallback(bool)

  bool = r->ip6
  r->set_ip6(bool)

  size = r->edns_udp_size
  r->set_edns_udp_size(size)

  bool = r->usevc             # Use virtual circuit (TCP)
  r->set_usevc(bool)

  r->fail
  r->set_fail

  r->defnames
  r->set_defnames

  r->dnsrch
  r->set_dnsrch

  r->igntc
  r->set_igntc

  bool = r->random            # randomize nameserver before usage
  r->set_random(bool)

  rrlist = r->dnssec_anchors  # list of trusted DNSSEC anchors
  r->push_dnssec_anchor(rr)
  r->set_dnssec_anchors(rrlist)

  rdata = r->domain           # Domain to add to relative queries
  r->set_domain(rdata)

  @rdata = r->nameservers
  count = r->nameserver_count
  r->push_nameserver(rdata)
  rdata = r->pop_nameserver
  r->nameservers_randomize

  str = r->tsig_keyname
  r->set_tsig_keyname(str)

  str = r->tsig_algorithm
  r->set_tsig_algorithm(str)

  str = r->tsig_keydata
  r->set_tsig_keydata(str)

  count = r->searchlist_count
  r->push_searchlist(rdata)
  @rdata = r->searchlist

  @times = r->rtt              # Round trip times
  r->set_rtt(@rtt)
  time = r->nameserver_rtt(pos)
  r->set_nameserver_rtt(pos, time)

  (sec, usec) = r->timeout
  r->set_timeout(sec, usec)

  # DNSSec validation
  rrlist = r->fetch_valid_domain_keys(domain, keys)
  rrlist = r->fetch_valid_domain_keys_time(domain, keys, checktime)
  rrlist = r->validate_domain_ds(domain, keys)
  rrlist = r->validate_domain_ds_time(domain, keys, checktime)
  rrlist = r->validate_domain_dnskey(domain, keys)
  rrlist = r->validate_domain_dnskey_time(domain, keys, checktime)
  status = r->verify_trusted(rrset, rrsigs, validation_keys)
  status = r->verify_trusted_time(rrset, rrsigs, checktime, validation_keys)
  bool = r->trusted_key(keys, trusted_keys)
  chain = r->build_data_chain(qflags, dataset, pkt, orig_rr)

  # Query
  pkt = r->query(rdata, type, class, flags)
  pkt = r->search(rdata, type, class, flags)
  query = r->prepare_query_pkt(rdata, type, class, flags)
  answer = r->send(rdata, type, class, flags)
  answer = r->send_pkt(query)
  rrlist = r->get_rr_list_addr_by_name(name, class, flags)
  rrlist = r->get_rr_list_name_by_addr(addr, class, flags)

=head1 SEE ALSO

http://www.nlnetlabs.nl/projects/ldns

=head1 AUTHOR

Erik Pihl Ostlyngen, E<lt>erik.ostlyngen@uninett.noE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2013 by UNINETT Norid AS

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.14.2 or,
at your option, any later version of Perl 5 you may have available.

=cut