| 1 | package POE::Filter::MDNS; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'POE::Filter::DNS'; | ||
| 7 | |||
| 8 | use Net::DNS::Packet; | ||
| 9 | |||
| 10 | sub get_one { | ||
| 11 | my $self = shift; | ||
| 12 | |||
| 13 | # This is a hack to make Net::DNS::Packet pass the UTF-8 data through, rather than escaping it all. | ||
| 14 | no warnings 'redefine'; | ||
| 15 | # local $SIG{__WARN__} = sub { stuff }; # a cute way to prevent -W from still emitting warnings | ||
| 16 | local *Net::DNS::Packet::dn_expand = \&dn_expand; | ||
| 17 | |||
| 18 | return $self->SUPER::get_one(@_); | ||
| 19 | } | ||
| 20 | |||
| 21 | sub dn_expand { | ||
| 22 | my ($packet, $offset) = @_; # $seen from $_[2] for debugging | ||
| 23 | my $name = ""; | ||
| 24 | my $len; | ||
| 25 | my $packetlen = length $$packet; | ||
| 26 | my $int16sz = Net::DNS::INT16SZ(); | ||
| 27 | |||
| 28 | # Debugging | ||
| 29 | # warn "USING PURE PERL dn_expand()\n"; | ||
| 30 | #if ($seen->{$offset}) { | ||
| 31 | # die "dn_expand: loop: offset=$offset (seen = ", | ||
| 32 | # join(",", keys %$seen), ")\n"; | ||
| 33 | #} | ||
| 34 | #$seen->{$offset} = 1; | ||
| 35 | |||
| 36 | while (1) { | ||
| 37 | return (undef, undef) if $packetlen < ($offset + 1); | ||
| 38 | |||
| 39 | $len = unpack("\@$offset C", $$packet); | ||
| 40 | |||
| 41 | if ($len == 0) { | ||
| 42 | $offset++; | ||
| 43 | last; | ||
| 44 | } | ||
| 45 | elsif (($len & 0xc0) == 0xc0) { | ||
| 46 | return (undef, undef) | ||
| 47 | if $packetlen < ($offset + $int16sz); | ||
| 48 | |||
| 49 | my $ptr = unpack("\@$offset n", $$packet); | ||
| 50 | $ptr &= 0x3fff; | ||
| 51 | my($name2) = dn_expand($packet, $ptr); # pass $seen for debugging | ||
| 52 | |||
| 53 | return (undef, undef) unless defined $name2; | ||
| 54 | |||
| 55 | $name .= $name2; | ||
| 56 | $offset += $int16sz; | ||
| 57 | last; | ||
| 58 | } | ||
| 59 | else { | ||
| 60 | $offset++; | ||
| 61 | |||
| 62 | return (undef, undef) | ||
| 63 | if $packetlen < ($offset + $len); | ||
| 64 | |||
| 65 | my $elem = substr($$packet, $offset, $len); | ||
| 66 | |||
| 67 | $name .= $elem . "."; | ||
| 68 | |||
| 69 | $offset += $len; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | $name =~ s/\.$//; | ||
| 74 | return ($name, $offset); | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | 1; |