File Coverage

blib/lib/Net/BitTorrent/Protocol/BEP10.pm
Criterion Covered Total %
statement 26 26 100.0
branch 6 6 100.0
condition 7 9 77.7
subroutine 6 6 100.0
pod 2 2 100.0
total 47 49 95.9


line stmt bran cond sub pod time code
1             package Net::BitTorrent::Protocol::BEP10;
2             our $VERSION = "1.5.1";
3 2     2   508 use Carp qw[carp];
  2         2  
  2         84  
4 2     2   283 use Net::BitTorrent::Protocol::BEP03::Bencode qw[:all];
  2         3  
  2         201  
5 2     2   8 use vars qw[@EXPORT_OK %EXPORT_TAGS];
  2         2  
  2         67  
6 2     2   6 use Exporter qw[];
  2         2  
  2         530  
7             *import = *import = *Exporter::import;
8             %EXPORT_TAGS = (build => [qw[ build_extended ]],
9             parse => [qw[ parse_extended ]],
10             types => [qw[ $EXTENDED ]]
11             );
12             @EXPORT_OK = sort map { @$_ = sort @$_; @$_ } values %EXPORT_TAGS;
13             $EXPORT_TAGS{'all'} = \@EXPORT_OK;
14              
15             # Type
16             our $EXTENDED = 20;
17              
18             # Build function
19             sub build_extended ($$) {
20 10     10 1 1100 my ($msgID, $data) = @_;
21 10 100 66     64 if ((!defined $msgID) || ($msgID !~ m[^\d+$])) {
22 3         324 carp sprintf
23             '%s::build_extended() requires a message id parameter',
24             __PACKAGE__;
25 3         80 return;
26             }
27 7 100 100     33 if ((!$data) || (ref($data) ne 'HASH')) {
28 4         299 carp sprintf '%s::build_extended() requires a payload', __PACKAGE__;
29 4         24 return;
30             }
31 3         9 my $packet = pack('ca*', $msgID, bencode($data));
32 3         31 return pack('Nca*', length($packet) + 1, 20, $packet);
33             }
34              
35             # Parsing function
36             sub parse_extended ($) {
37 3     3 1 5 my ($packet) = @_;
38 3 100 66     14 if ((!$packet) || (!length($packet))) { return; }
  1         3  
39 2         7 my ($id, $payload) = unpack('ca*', $packet);
40 2         8 return ([$id, scalar bdecode($payload)]);
41             }
42             1;
43              
44             =pod
45              
46             =head1 NAME
47              
48             Net::BitTorrent::Protocol::BEP23 - Packet Utilities for BEP10: Extension Protocol
49              
50             =head1 Synopsis
51              
52             use Net::BitTorrent::Protocol::BEP10 qw[all];
53             my $index = build_extended(
54             build_extended(
55             0,
56             {m => {'ut_pex' => 1, "\xC2\xB5T_PEX" => 2},
57             p => 30,
58             reqq => 30,
59             v => "Net::BitTorrent r0.30",
60             yourip => "\x7F\0\0\1",
61             }
62             )
63             );
64              
65             =head1 Description
66              
67             The intention of this protocol is to provide a simple and thin transport for
68             extensions to the bittorrent protocol. Supporting this protocol makes it easy
69             to add new extensions without interfering with the standard BitTorrent
70             protocol or clients that don't support this extension or the one you want to
71             add.
72              
73             =head1 Importing from Net::BitTorrent::Protocol::BEP10
74              
75             There are three tags available for import. To get them all in one go, use the
76             C<:all> tag.
77              
78             =over
79              
80             =item C<:types>
81              
82             Packet types
83              
84             For more on what these packets actually mean, see the Extension Protocol spec.
85             This is a list of the currently supported packet types.
86              
87             =over
88              
89             =item C<$EXTENDED>
90              
91             =back
92              
93             =item C<:build>
94              
95             These create packets ready-to-send to remote peers. See
96             L.
97              
98             =item C<:parse>
99              
100             These are used to parse unknown data into sensible packets. The same packet
101             types we can build, we can also parse. See
102             L.
103              
104             =back
105              
106             =head1 Building Functions
107              
108             =over
109              
110             =item C
111              
112             Creates an extended protocol packet.
113              
114             C<$msgID> should be C<0> if you are creating a handshake packet, C<< >0 >> if
115             an extended message as specified by the handshake is being created.
116              
117             C<$data> should be a HashRef of appropriate data.
118              
119             =back
120              
121             =head1 Parsing Functions
122              
123             These are the parsing counterparts for the C functions.
124              
125             When the packet is invalid, a hash reference is returned with a single key:
126             C. The value is a string describing what went wrong.
127              
128             Return values for valid packets are explained below.
129              
130             =over
131              
132             =item C
133              
134             Returns an integer ID and a HashRef containing the packet's payload.
135              
136             =back
137              
138             =head1 See Also
139              
140             http://bittorrent.org/beps/bep_0010.html - Fast Extension
141              
142             =head1 Author
143              
144             Sanko Robinson - http://sankorobinson.com/
145              
146             CPAN ID: SANKO
147              
148             =head1 License and Legal
149              
150             Copyright (C) 2008-2012 by Sanko Robinson
151              
152             This program is free software; you can redistribute it and/or modify it under
153             the terms of
154             L.
155             See the F file included with this distribution or
156             L
157             for clarification.
158              
159             When separated from the distribution, all original POD documentation is
160             covered by the
161             L.
162             See the
163             L.
164              
165             Neither this module nor the L is affiliated with BitTorrent,
166             Inc.
167              
168             =cut