| 1 | package Net::DNS::ZoneParse::Parser::NetDNSZoneParser; | ||
| 2 | |||
| 3 | use 5.008000; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use vars qw($VERSION); | ||
| 7 | |||
| 8 | use Net::DNS; | ||
| 9 | use Net::DNS::Zone::Parser; | ||
| 10 | use IO::File; | ||
| 11 | use POSIX qw(tmpnam); | ||
| 12 | |||
| 13 | $VERSION = 0.10; | ||
| 14 | |||
| 15 | =pod | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | Net::DNS::ZoneParse::Parser::NetDNSZoneParser - Glue for Net::DNS::ZoneParse | ||
| 20 | to use Net::DNS::Zone::Parser | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | NetDNSZoneParser uses Net::DNS::Zone::Parser as parsing engine. This can | ||
| 25 | perform to different tasks. On the one hand it is a pre-parser, helping | ||
| 26 | other parsers to read zonefile, on the other hand it can be used to generate | ||
| 27 | the corresponding Records directly. | ||
| 28 | |||
| 29 | =head2 EXPORT | ||
| 30 | |||
| 31 | =head3 parse | ||
| 32 | |||
| 33 | $rr = Net::DNS::ZoneParse::Parser::NetDNSZoneParser->parse($param) | ||
| 34 | |||
| 35 | This will be called by Net::DNS::ZoneParse. | ||
| 36 | The parameters filehandle will be modified to point to the preparsed | ||
| 37 | zone. | ||
| 38 | If NetDNSZoneParser is called with CREATE_RR set, the Records read will be | ||
| 39 | returned. | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | sub parse { | ||
| 44 | my ($self, $param) = @_; | ||
| 45 | my $oldfh; | ||
| 46 | unless($param->{file}) { | ||
| 47 | $oldfh = $param->{fh}; | ||
| 48 | do { $param->{file} = tmpnam(); } | ||
| 49 | until $param->{fh} = IO::File->new($param->{file}, | ||
| 50 | O_RDWR|O_CREAT|O_EXCL); | ||
| 51 | my $fh = $param->{fh}; | ||
| 52 | while(<$oldfh>) { | ||
| 53 | printf $fh $_; | ||
| 54 | } | ||
| 55 | seek($param->{fh}, 0, 0); | ||
| 56 | } | ||
| 57 | my $parser = Net::DNS::Zone::Parser->new(); | ||
| 58 | $param->{parser_arg}->{ORIGIN} = $param->{origin}; | ||
| 59 | $parser->read($param->{file}, $param->{parser_arg}); | ||
| 60 | if($oldfh) { | ||
| 61 | close($param->{fh}); | ||
| 62 | unlink($param->{file}); | ||
| 63 | delete($param->{file}); | ||
| 64 | } | ||
| 65 | $param->{fh} = $parser->get_io; | ||
| 66 | $param->{origin} = $parser->get_origin; | ||
| 67 | return $param->{parser_arg}->{CREATE_RR}?$parser->get_array: undef(); | ||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | =pod | ||
| 72 | |||
| 73 | =head1 SEE ALSO | ||
| 74 | |||
| 75 | Net::DNS::ZoneParse | ||
| 76 | Net::DNS::Zone::Parser | ||
| 77 | |||
| 78 | =head1 AUTHOR | ||
| 79 | |||
| 80 | Benjamin Tietz E<lt>benjamin@micronet24.deE<gt> | ||
| 81 | |||
| 82 | =head1 COPYRIGHT | ||
| 83 | |||
| 84 | Copyright (C) 2010 by Benjamin Tietz | ||
| 85 | |||
| 86 | This library is free software; you can redistribute it and/or modify | ||
| 87 | it under the same terms as Perl itself, either Perl version 5.10.0 or, | ||
| 88 | at your option, any later version of Perl 5 you may have available. | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | 1; |