| 1 | package Net::Gnutella::Packet::Reply; | ||||||||||||||||||
| 2 | 1 | ▶ | use Socket qw(inet_ntoa inet_aton); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 3 | 1 | ▶ | use Carp; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 1 | ▶ | use vars qw/$VERSION $AUTOLOAD/; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | 1 | $VERSION = $VERSION = "0.1"; | |||||||||||||||||
| 8 | |||||||||||||||||||
| 9 | # Use AUTOHANDLER to supply generic attribute methods | ||||||||||||||||||
| 10 | # | ||||||||||||||||||
| 11 | 0 | ▶ | sub AUTOLOAD { | ||||||||||||||||
|
Subroutine
AUTOLOAD: not called CC=4 SCAR=30.0
| |||||||||||||||||||
| 12 | 0 | my $self = shift; | |||||||||||||||||
| 13 | 0 | my $attr = $AUTOLOAD; | |||||||||||||||||
| 14 | 0 | $attr =~ s/.*:://; | |||||||||||||||||
| 15 | 0 | ▶ | return unless $attr =~ /[^A-Z]/; # skip DESTROY and all-cap methods | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 16 | 0 | ▶ | croak sprintf "invalid attribute method: %s->%s()", ref($self), $attr unless exists $self->{_attr}->{lc $attr}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 17 | 0 | ▶ | $self->{_attr}->{lc $attr} = shift if @_; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 18 | 0 | return $self->{_attr}->{lc $attr}; | |||||||||||||||||
| 19 | } | ||||||||||||||||||
| 20 | |||||||||||||||||||
| 21 | 0 | ▶ | sub new { | ||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||
| 22 | 0 | my $proto = shift; | |||||||||||||||||
| 23 | 0 | my %args = @_; | |||||||||||||||||
| 24 | |||||||||||||||||||
| 25 | 0 | my $self = { | |||||||||||||||||
| 26 | _attr => { | ||||||||||||||||||
| 27 | msgid => [], | ||||||||||||||||||
| 28 | ttl => 7, | ||||||||||||||||||
| 29 | hops => 1, | ||||||||||||||||||
| 30 | function => 129, | ||||||||||||||||||
| 31 | |||||||||||||||||||
| 32 | ip => [], | ||||||||||||||||||
| 33 | port => 0, | ||||||||||||||||||
| 34 | speed => 0, | ||||||||||||||||||
| 35 | results => [], | ||||||||||||||||||
| 36 | guid => [], | ||||||||||||||||||
| 37 | }, | ||||||||||||||||||
| 38 | }; | ||||||||||||||||||
| 39 | |||||||||||||||||||
| 40 | 0 | bless $self, $proto; | |||||||||||||||||
| 41 | |||||||||||||||||||
| 42 | 0 | foreach my $key (keys %args) { | |||||||||||||||||
| 43 | 0 | my $lkey = lc $key; | |||||||||||||||||
| 44 | |||||||||||||||||||
| 45 | 0 | $self->$lkey($args{$key}); | |||||||||||||||||
| 46 | } | ||||||||||||||||||
| 47 | |||||||||||||||||||
| 48 | 0 | return $self; | |||||||||||||||||
| 49 | } | ||||||||||||||||||
| 50 | |||||||||||||||||||
| 51 | 0 | ▶ | sub ip { | ||||||||||||||||
|
Subroutine
ip: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||
| 52 | 0 | my $self = shift; | |||||||||||||||||
| 53 | |||||||||||||||||||
| 54 | 0 | ▶ | if (@_) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 55 | 0 | ▶ | if (ref($_[0]) eq 'ARRAY') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 56 | 0 | $self->{_attr}->{ip} = $_[0]; | |||||||||||||||||
| 57 | } elsif ($_[0] =~ /^[\d.]+$/) { | ||||||||||||||||||
| 58 | 0 | $self->{_attr}->{ip} = [ split(/\./, $_[0]) ]; | |||||||||||||||||
| 59 | } elsif ($_[0] =~ /\D/) { | ||||||||||||||||||
| 60 | 0 | $self->{_attr}->{ip} = [ split(/\./, inet_ntoa(inet_aton($_[0]))) ]; | |||||||||||||||||
| 61 | } | ||||||||||||||||||
| 62 | } | ||||||||||||||||||
| 63 | |||||||||||||||||||
| 64 | 0 | return @{ $self->{_attr}->{ip} }; | |||||||||||||||||
| 65 | } | ||||||||||||||||||
| 66 | |||||||||||||||||||
| 67 | 0 | ▶ | sub ip_as_string { | ||||||||||||||||
|
Subroutine
ip_as_string: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||
| 68 | 0 | my $self = shift; | |||||||||||||||||
| 69 | |||||||||||||||||||
| 70 | 0 | return join(".", @{ $self->{_attr}->{ip} }); | |||||||||||||||||
| 71 | } | ||||||||||||||||||
| 72 | |||||||||||||||||||
| 73 | 0 | ▶ | sub parse { | ||||||||||||||||
|
Subroutine
parse: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||
| 74 | 0 | my $self = shift; | |||||||||||||||||
| 75 | 0 | my $data = shift; | |||||||||||||||||
| 76 | |||||||||||||||||||
| 77 | 0 | my $count = unpack("C", substr($data, 0, 1, '')); | |||||||||||||||||
| 78 | 0 | my $port = unpack("S", substr($data, 0, 2, '')); | |||||||||||||||||
| 79 | 0 | my @ip = unpack("C4", substr($data, 0, 4, '')); | |||||||||||||||||
| 80 | 0 | my $speed = unpack("L", substr($data, 0, 4, '')); | |||||||||||||||||
| 81 | 0 | my @set; | |||||||||||||||||
| 82 | |||||||||||||||||||
| 83 | 0 | ▶ | for (my $i = 0; $i < $count; $i++) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 84 | 0 | my $index = unpack("L", substr($data, 0, 4, '')); | |||||||||||||||||
| 85 | 0 | my $size = unpack("L", substr($data, 0, 4, '')); | |||||||||||||||||
| 86 | 0 | my $name = substr($data, 0, index($data, "\x00\x00"), ''); | |||||||||||||||||
| 87 | 0 | my $extra; | |||||||||||||||||
| 88 | |||||||||||||||||||
| 89 | 0 | ▶ | if (index($name, "\x00") != -1) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 90 | 0 | $extra = $name; | |||||||||||||||||
| 91 | 0 | $name = substr($extra, 0, index($extra, "\x00"), ''); | |||||||||||||||||
| 92 | |||||||||||||||||||
| 93 | 0 | substr($extra, 0, 1, ''); | |||||||||||||||||
| 94 | } | ||||||||||||||||||
| 95 | |||||||||||||||||||
| 96 | 0 | substr($data, 0, 2, ''); | |||||||||||||||||
| 97 | |||||||||||||||||||
| 98 | 0 | push @set, [ $index, $size, $name, $extra ]; | |||||||||||||||||
| 99 | } | ||||||||||||||||||
| 100 | |||||||||||||||||||
| 101 | 0 | my @guid = unpack("L4", substr($data, 0, 16, '')); | |||||||||||||||||
| 102 | |||||||||||||||||||
| 103 | 0 | $self->port($port); | |||||||||||||||||
| 104 | 0 | $self->ip(\@ip); | |||||||||||||||||
| 105 | 0 | $self->speed($speed); | |||||||||||||||||
| 106 | 0 | $self->results(\@set); | |||||||||||||||||
| 107 | 0 | $self->guid(\@guid); | |||||||||||||||||
| 108 | } | ||||||||||||||||||
| 109 | |||||||||||||||||||
| 110 | 0 | ▶ | sub format { | ||||||||||||||||
|
Subroutine
format: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||
| 111 | 0 | my $self = shift; | |||||||||||||||||
| 112 | 0 | my $data; | |||||||||||||||||
| 113 | |||||||||||||||||||
| 114 | 0 | my $results = $self->results; | |||||||||||||||||
| 115 | |||||||||||||||||||
| 116 | 0 | $data .= pack("C", scalar @$results); | |||||||||||||||||
| 117 | 0 | $data .= pack("S", $self->port); | |||||||||||||||||
| 118 | 0 | $data .= pack("C4", $self->ip); | |||||||||||||||||
| 119 | 0 | $data .= pack("L", $self->speed); | |||||||||||||||||
| 120 | |||||||||||||||||||
| 121 | 0 | foreach my $res (@$results) { | |||||||||||||||||
| 122 | 0 | $data .= pack("L", $res->[0]); | |||||||||||||||||
| 123 | 0 | $data .= pack("L", $res->[1]); | |||||||||||||||||
| 124 | 0 | $data .= $res->[2]; | |||||||||||||||||
| 125 | 0 | ▶ | $data .= $data =~ /\x00\x00$/ ? "" : "\x00\x00"; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 126 | } | ||||||||||||||||||
| 127 | |||||||||||||||||||
| 128 | 0 | $data .= pack("L4", @{ $self->guid }); | |||||||||||||||||
| 129 | |||||||||||||||||||
| 130 | 0 | return $data; | |||||||||||||||||
| 131 | } | ||||||||||||||||||
| 132 | |||||||||||||||||||
| 133 | 1 | 1; | |||||||||||||||||