1package Net::DNS::RR::TXT; 2# 3# $Id: TXT.pm 582 2006-04-25 07:12:19Z olaf $ 4# 5use strict; 6BEGIN { 7 eval { require bytes; } 8} 9use vars qw(@ISA $VERSION); 10 11use Text::ParseWords; 12 13@ISA = qw(Net::DNS::RR); 14$VERSION = (qw$LastChangedRevision: 582 $)[1]; 15 16sub new { 17 my ($class, $self, $data, $offset) = @_; 18 19 my $rdlength = $self->{'rdlength'} or return bless $self, $class; 20 my $end = $offset + $rdlength; 21 22 while ($offset < $end) { 23 my $strlen = unpack("\@$offset C", $$data); 24 ++$offset; 25 26 my $char_str = substr($$data, $offset, $strlen); 27 $offset += $strlen; 28 29 push(@{$self->{'char_str_list'}}, $char_str); 30 } 31 32 return bless $self, $class; 33} 34 35sub new_from_string { 36 my ( $class, $self, $rdata_string ) = @_ ; 37 38 bless $self, $class; 39 40 $self->_build_char_str_list($rdata_string); 41 42 return $self; 43} 44 45sub txtdata { 46 my $self = shift; 47 return join(' ', $self->char_str_list()); 48} 49 50sub rdatastr { 51 my $self = shift; 52 53 if ($self->char_str_list) { 54 return join(' ', map { 55 my $str = $_; 56 $str =~ s/"/\\"/g; 57 qq("$str"); 58 } @{$self->{'char_str_list'}}); 59 } 60 61 return ''; 62} 63 64sub _build_char_str_list { 65 my ($self, $rdata_string) = @_; 66 67 my @words; 68 69 @words= shellwords($rdata_string) if $rdata_string; 70 71 $self->{'char_str_list'} = []; 72 73 if (@words) { 74 foreach my $string (@words) { 75 $string =~ s/\\"/"/g; 76 push(@{$self->{'char_str_list'}}, $string); 77 } 78 } 79} 80 81sub char_str_list { 82 my $self = shift; 83 84 if (not $self->{'char_str_list'}) { 85 $self->_build_char_str_list( $self->{'txtdata'} ); 86 } 87 88 return @{$self->{'char_str_list'}}; # unquoted strings 89} 90 91sub rr_rdata { 92 my $self = shift; 93 my $rdata = ''; 94 95 foreach my $string ($self->char_str_list) { 96 $rdata .= pack("C", length $string ); 97 $rdata .= $string; 98 } 99 100 return $rdata; 101} 102 1031; 104__END__ 105 106=head1 NAME 107 108Net::DNS::RR::TXT - DNS TXT resource record 109 110=head1 SYNOPSIS 111 112C<use Net::DNS::RR>; 113 114=head1 DESCRIPTION 115 116Class for DNS Text (TXT) resource records. 117 118=head1 METHODS 119 120=head2 txtdata 121 122 print "txtdata = ", $rr->txtdata, "\n"; 123 124Returns the descriptive text as a single string, regardless of actual 125number of <character-string> elements. Of questionable value. Should 126be deprecated. 127 128Use C<< $txt->rdatastr() >> or C<< $txt->char_str_list() >> instead. 129 130 131=head2 char_str_list 132 133 print "Individual <character-string> list: \n\t", 134 join("\n\t", $rr->char_str_list()); 135 136Returns a list of the individual <character-string> elements, 137as unquoted strings. Used by TXT->rdatastr and TXT->rr_rdata. 138 139 140=head1 FEATURES 141 142The RR.pm module accepts semi-colons as a start of a comment. This is 143to allow the RR.pm to deal with RFC1035 specified zonefile format. 144 145For some applications of the TXT RR the semicolon is relevant, you 146will need to escape it on input. 147 148Also note that you should specify the several character strings 149separately. The easiest way to do so is to include the whole argument 150in single quotes and the several character strings in double 151quotes. Double quotes inside the character strings will need to be 152escaped. 153 154my $TXTrr=Net::DNS::RR->new('txt2.t.net-dns.org. 60 IN 155 TXT "Test1 \" \; more stuff" "Test2"'); 156 157would result in 158$TXTrr->char_str_list())[0] containing 'Test1 " ; more stuff' 159and 160$TXTrr->char_str_list())[1] containing 'Test2' 161 162 163=head1 COPYRIGHT 164 165Copyright (c) 1997-2002 Michael Fuhr. 166 167Portions Copyright (c) 2002-2004 Chris Reinhardt. 168Portions Copyright (c) 2005 Olaf Kolkman (NLnet Labs) 169 170All rights reserved. This program is free software; you may redistribute 171it and/or modify it under the same terms as Perl itself. 172 173=head1 SEE ALSO 174 175L<perl(1)>, L<Net::DNS>, L<Net::DNS::Resolver>, L<Net::DNS::Packet>, 176L<Net::DNS::Header>, L<Net::DNS::Question>, L<Net::DNS::RR>, 177RFC 1035 Section 3.3.14 178 179=cut 180