| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Net::BitTorrent::Protocol::BEP23; | 
| 2 | 3 |  |  | 3 |  | 494 | use strict; | 
|  | 3 |  |  |  |  | 4 |  | 
|  | 3 |  |  |  |  | 71 |  | 
| 3 | 3 |  |  | 3 |  | 9 | use warnings; | 
|  | 3 |  |  |  |  | 3 |  | 
|  | 3 |  |  |  |  | 62 |  | 
| 4 | 3 |  |  | 3 |  | 10 | use Carp qw[carp]; | 
|  | 3 |  |  |  |  | 3 |  | 
|  | 3 |  |  |  |  | 155 |  | 
| 5 |  |  |  |  |  |  | our $VERSION = "1.5.3"; | 
| 6 | 3 |  |  | 3 |  | 10 | use vars qw[@EXPORT_OK %EXPORT_TAGS]; | 
|  | 3 |  |  |  |  | 23 |  | 
|  | 3 |  |  |  |  | 119 |  | 
| 7 | 3 |  |  | 3 |  | 9 | use Exporter qw[]; | 
|  | 3 |  |  |  |  | 3 |  | 
|  | 3 |  |  |  |  | 858 |  | 
| 8 |  |  |  |  |  |  | *import = *import = *Exporter::import; | 
| 9 |  |  |  |  |  |  | @EXPORT_OK = qw[compact_ipv4 uncompact_ipv4]; | 
| 10 |  |  |  |  |  |  | %EXPORT_TAGS = (all => [@EXPORT_OK], bencode => [@EXPORT_OK]); | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | sub compact_ipv4 { | 
| 13 | 4 |  |  | 4 | 1 | 397 | my (@peers) = @_; | 
| 14 | 4 |  |  |  |  | 3 | my $return; | 
| 15 |  |  |  |  |  |  | my %seen; | 
| 16 | 4 |  |  |  |  | 8 | PEER: for my $peer (@peers) { | 
| 17 | 5 | 50 |  |  |  | 9 | next if not $peer; | 
| 18 | 5 |  |  |  |  | 7 | my ($ip, $port) = @$peer; | 
| 19 | 5 | 100 |  |  |  | 17 | next if $seen{$ip . ':' . $port}++; | 
| 20 | 4 | 50 |  |  |  | 26 | if ($ip | 
|  |  | 50 |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | !~ m[^(?:(?:(?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.]?){4})$]) | 
| 22 | 0 |  |  |  |  | 0 | {   carp 'Invalid IPv4 address: ' . $ip; | 
| 23 |  |  |  |  |  |  | } | 
| 24 |  |  |  |  |  |  | elsif ($port > 2**16) { | 
| 25 | 0 |  |  |  |  | 0 | carp 'Port number beyond ephemeral range: ' . $port; | 
| 26 |  |  |  |  |  |  | } | 
| 27 |  |  |  |  |  |  | else { | 
| 28 | 4 |  |  |  |  | 30 | $return .= pack 'C4n', | 
| 29 |  |  |  |  |  |  | ($ip =~ m[^([\d]+)\.([\d]+)\.([\d]+)\.([\d]+)$]), | 
| 30 |  |  |  |  |  |  | int $port; | 
| 31 |  |  |  |  |  |  | } | 
| 32 |  |  |  |  |  |  | } | 
| 33 | 4 |  |  |  |  | 34 | return $return; | 
| 34 |  |  |  |  |  |  | } | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | sub uncompact_ipv4 { | 
| 37 |  |  |  |  |  |  | return $_[0] ? | 
| 38 |  |  |  |  |  |  | map { | 
| 39 | 5 | 100 |  | 5 | 1 | 29 | my (@h) = unpack 'C4n', $_; | 
|  | 5 |  |  |  |  | 13 |  | 
| 40 | 5 |  |  |  |  | 58 | [sprintf('%s.%s.%s.%s', @h), $h[-1]] | 
| 41 |  |  |  |  |  |  | } $_[0] =~ m[(.{6})]g | 
| 42 |  |  |  |  |  |  | : (); | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  | 1; | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | =pod | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | =head1 NAME | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | Net::BitTorrent::Protocol::BEP23 - Utility functions for BEP23: Tracker Returns Compact Peer Lists | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | =head1 Importing From Net::BitTorrent::Protocol::BEP23 | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | By default, nothing is exported. | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | You may import any of the following or use one or more of these tag: | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | =over | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | =item C<:all> | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | Imports the tracker response-related functions | 
| 63 |  |  |  |  |  |  | L and | 
| 64 |  |  |  |  |  |  | L. | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | =back | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | =head1 Functions | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | =over | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | =item C | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | Compacts a list of IPv4:port strings into a single string. | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | A compact peer is 6 bytes; the first four bytes are the host (in network byte | 
| 77 |  |  |  |  |  |  | order), the last two bytes are the port (again, in network byte order). | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | =item C | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | Inflates a compacted string of peers and returns a list of IPv4:port strings. | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | =back | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | =head1 See Also | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | =over | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | =item BEP 23: Tracker Returns Compact Peer Lists | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | http://bittorrent.org/beps/bep_0023.html | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | =back | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | =head1 Author | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | Sanko Robinson  - http://sankorobinson.com/ | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | CPAN ID: SANKO | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | =head1 License and Legal | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | Copyright (C) 2008-2012 by Sanko Robinson | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | This program is free software; you can redistribute it and/or modify it under | 
| 106 |  |  |  |  |  |  | the terms of | 
| 107 |  |  |  |  |  |  | L. | 
| 108 |  |  |  |  |  |  | See the F file included with this distribution or | 
| 109 |  |  |  |  |  |  | L | 
| 110 |  |  |  |  |  |  | for clarification. | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | When separated from the distribution, all original POD documentation is | 
| 113 |  |  |  |  |  |  | covered by the | 
| 114 |  |  |  |  |  |  | L. | 
| 115 |  |  |  |  |  |  | See the | 
| 116 |  |  |  |  |  |  | L. | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | Neither this module nor the L is affiliated with BitTorrent, | 
| 119 |  |  |  |  |  |  | Inc. | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | =cut |