File: Packet.pm

package info (click to toggle)
libzonemaster-perl 8.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 72,256 kB
  • sloc: perl: 16,941; makefile: 16
file content (293 lines) | stat: -rw-r--r-- 7,156 bytes parent folder | download
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
package Zonemaster::Engine::Packet;

use v5.16.0;
use warnings;

use version; our $VERSION = version->declare("v1.0.5");

use Class::Accessor 'antlers';
use Carp qw( confess );
use Zonemaster::Engine::Util;

has 'packet' => (
    is  => 'ro',
    isa => 'Zonemaster::LDNS::Packet',
);

sub new {
    my $proto = shift;
    my $class = ref $proto || $proto;
    my $attrs = shift;

    my $packet = delete $attrs->{packet};
    if ( %$attrs ) {
        confess "unexpected arguments: " . join ', ', sort keys %$attrs;
    }

    return Class::Accessor::new( $class, { packet => $packet } );
}

sub timestamp    { my ( $self, $time )    = @_; return $self->packet->timestamp( $time       // () ); }
sub querytime    { my ( $self, $value )   = @_; return $self->packet->querytime( $value      // () ); }
sub id           { my ( $self, $id )      = @_; return $self->packet->id( $id                // () ); }
sub opcode       { my ( $self, $string )  = @_; return $self->packet->opcode( $string        // () ); }
sub rcode        { my ( $self, $string )  = @_; return $self->packet->rcode( $string         // () ); }
sub edns_version { my ( $self, $version ) = @_; return $self->packet->edns_version( $version // () ); }

sub type       { my ( $self ) = @_; return $self->packet->type; }
sub string     { my ( $self ) = @_; return $self->packet->string; }
sub data       { my ( $self ) = @_; return $self->packet->data; }
sub aa         { my ( $self ) = @_; return $self->packet->aa; }
sub do         { my ( $self ) = @_; return $self->packet->do; }
sub ra         { my ( $self ) = @_; return $self->packet->ra; }
sub tc         { my ( $self ) = @_; return $self->packet->tc; }
sub question   { my ( $self ) = @_; return $self->packet->question; }
sub authority  { my ( $self ) = @_; return $self->packet->authority; }
sub answer     { my ( $self ) = @_; return $self->packet->answer; }
sub additional { my ( $self ) = @_; return $self->packet->additional; }
sub edns_size  { my ( $self ) = @_; return $self->packet->edns_size; }
sub edns_rcode { my ( $self ) = @_; return $self->packet->edns_rcode; }
sub edns_data  { my ( $self ) = @_; return $self->packet->edns_data; }
sub edns_z     { my ( $self ) = @_; return $self->packet->edns_z; }
sub has_edns   { my ( $self ) = @_; return $self->packet->has_edns; }

sub unique_push {
    my ( $self, $section, $rr ) = @_;
    return $self->packet->unique_push( $section, $rr );
}

sub no_such_record {
    my ( $self ) = @_;

    if ( $self->type eq 'nodata' ) {
        my ( $q ) = $self->question;
        Zonemaster::Engine::Util::info( NO_SUCH_RECORD => { name => Zonemaster::Engine::Util::name( $q->name ), type => $q->type } );

        return 1;
    }
    else {
        return;
    }
}

sub no_such_name {
    my ( $self ) = @_;

    if ( $self->type eq 'nxdomain' ) {
        my ( $q ) = $self->question;
        info( NO_SUCH_NAME => { name => name( $q->name ), type => $q->type } );

        return 1;
    }
    else {
        return;
    }
}

sub is_redirect {
    my ( $self ) = @_;

    if ( $self->type eq 'referral' ) {
        my ( $q ) = $self->question;
        my ( $a ) = $self->authority;
        Zonemaster::Engine::Util::info(
            IS_REDIRECT => {
                name => Zonemaster::Engine::DNSName->from_string( $q->name ),
                type => $q->type,
                to   => Zonemaster::Engine::DNSName->from_string( $a->name )
            }
        );

        return 1;
    }
    else {
        return;
    }
} ## end sub is_redirect

sub get_records {
    my ( $self, $type, @section ) = @_;
    @section = qw(answer authority additional) if !@section;
    my %sec = map { lc( $_ ) => 1 } @section;
    my @raw;
    $type = uc( $type );

    if ( $sec{'answer'} ) {
        push @raw, grep { $_->type eq $type } $self->packet->answer;
    }

    if ( $sec{'authority'} ) {
        push @raw, grep { $_->type eq $type } $self->packet->authority;
    }

    if ( $sec{'additional'} ) {
        push @raw, grep { $_->type eq $type } $self->packet->additional;
    }

    return @raw;
} ## end sub get_records

sub get_records_for_name {
    my ( $self, $type, $name, @section ) = @_;

    # Make sure $name is a Zonemaster::Engine::DNSName
    $name = name( $name );

    return grep { name( $_->name ) eq $name } $self->get_records( $type, @section );
}

sub has_rrs_of_type_for_name {
    my ( $self, $type, $name, @section ) = @_;

    # Make sure $name is a Zonemaster::Engine::DNSName
    $name = name( $name );

    return ( grep { name( $_->name ) eq $name } $self->get_records( $type, @section ) ) > 0;
}

sub answerfrom {
    my ( $self, @args ) = @_;

    if ( @args ) {
        $self->packet->answerfrom( @args );
    }

    my $from = $self->packet->answerfrom // '<unknown>';

    return $from;
}

sub TO_JSON {
    my ( $self ) = @_;

    return { 'Zonemaster::Engine::Packet' => $self->packet };
}

1;

=head1 NAME

Zonemaster::Engine::Packet - wrapping object for L<Zonemaster::LDNS::Packet> objects

=head1 SYNOPSIS

    my $packet = $ns->query('iis.se', 'NS');
    my @rrs = $packet->get_records('ns');

=head1 ATTRIBUTES

=over

=item packet

Holds the L<Zonemaster::LDNS::Packet> the object is wrapping.

=back

=head1 CONSTRUCTORS

=over

=item new

Construct a new instance.

=back

=head1 METHODS

=over

=item no_such_record

Returns true if the packet represents an existing DNS node lacking any records of the requested type.

=item no_such_name

Returns true if the packet represents a nonexistent DNS node.

=item is_redirect

Returns true if the packet is a redirect to another set of nameservers.

=item get_records($type[, @section])

Returns the L<Zonemaster::LDNS::RR> objects of the requested type in the packet.
If the optional C<@section> argument is given, and is a list of C<answer>,
C<authority> and C<additional>, only RRs from those sections are returned.

=item get_records_for_name($type, $name[, @section])

Returns all L<Zonemaster::LDNS::RR> objects for the given name in the packet.
If the optional C<@section> argument is given, and is a list of C<answer>,
C<authority> and C<additional>, only RRs from those sections are returned.

=item has_rrs_of_type_for_name($type, $name[, @section])

Returns true if the packet holds any RRs of the specified type for the given name.
If the optional C<@section> argument is given, and is a list of C<answer>,
C<authority> and C<additional>, only RRs from those sections are returned.

=item answerfrom

Wrapper for the underlying packet method, that replaces undefined values with the string C<E<lt>unknownE<gt>>.

=item TO_JSON

Support method for L<JSON> to be able to serialize these objects.

=back

=head1 METHODS PASSED THROUGH

These methods are passed through transparently to the underlying L<Zonemaster::LDNS::Packet> object.

=over

=item data

=item rcode

=item aa

=item ra

=item tc

=item question

=item answer

=item authority

=item additional

=item string

=item unique_push

=item timestamp

=item type

=item edns_size

=item edns_rcode

=item edns_version

=item edns_z

=item edns_data

=item has_edns

=item id

=item querytime

=item do

=item opcode

=back