| 1 | # P2P::pDonkey::Met.pm | ||
| 2 | # | ||
| 3 | # Copyright (c) 2003-2004 Alexey klimkin <klimkin at cpan.org>. | ||
| 4 | # All rights reserved. | ||
| 5 | # This program is free software; you can redistribute it and/or | ||
| 6 | # modify it under the same terms as Perl itself. | ||
| 7 | # | ||
| 8 | package P2P::pDonkey::Met; | ||
| 9 | |||
| 10 | 1 | ▶ | use 5.006; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | |||
| 14 | require Exporter; | ||
| 15 | |||
| 16 | our $VERSION = '0.05'; | ||
| 17 | |||
| 18 | our @ISA = qw(Exporter); | ||
| 19 | |||
| 20 | # Items to export into callers namespace by default. Note: do not export | ||
| 21 | # names by default without a very good reason. Use EXPORT_OK instead. | ||
| 22 | # Do not simply export all your public functions/methods/constants. | ||
| 23 | |||
| 24 | # This allows declaration use P2P::pDonkey ':all'; | ||
| 25 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||
| 26 | # will save memory. | ||
| 27 | our %EXPORT_TAGS = ( 'all' => [ qw( | ||
| 28 | MT_KNOWNMET MT_PARTMET MT_SERVERMET | ||
| 29 | |||
| 30 | unpackServerDesc packServerDesc printServerDesc makeServerDesc | ||
| 31 | unpackServerDescList packServerDescList printServerDescList | ||
| 32 | unpackServerDescListU packServerDescListU printServerDescListU | ||
| 33 | unpackServerMet packServerMet printServerMet | ||
| 34 | readServerMet writeServerMet | ||
| 35 | |||
| 36 | unpackPartMet packPartMet printPartMet | ||
| 37 | readPartMet writePartMet | ||
| 38 | |||
| 39 | unpackKnownMet packKnownMet printKnownMet | ||
| 40 | readKnownMet writeKnownMet | ||
| 41 | |||
| 42 | unpackPrefMet packPrefMet printPrefMet | ||
| 43 | readPrefMet writePrefMet | ||
| 44 | |||
| 45 | readFile writeFile | ||
| 46 | ) ], | ||
| 47 | 'server' => [ qw( | ||
| 48 | unpackServerDesc packServerDesc printServerDesc makeServerDesc | ||
| 49 | unpackServerDescList packServerDescList printServerDescList | ||
| 50 | unpackServerDescListU packServerDescListU printServerDescListU | ||
| 51 | unpackServerMet packServerMet printServerMet | ||
| 52 | readServerMet writeServerMet | ||
| 53 | ) ], | ||
| 54 | 'part' => [ qw( | ||
| 55 | unpackPartMet packPartMet printPartMet | ||
| 56 | readPartMet writePartMet | ||
| 57 | ) ], | ||
| 58 | 'known' => [ qw( | ||
| 59 | unpackKnownMet packKnownMet printKnownMet | ||
| 60 | readKnownMet writeKnownMet | ||
| 61 | ) ], | ||
| 62 | 'pref' => [ qw( | ||
| 63 | unpackPrefMet packPrefMet printPrefMet | ||
| 64 | readPrefMet writePrefMet | ||
| 65 | ) ] ); | ||
| 66 | |||
| 67 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 68 | |||
| 69 | our @EXPORT = qw( | ||
| 70 | |||
| 71 | ); | ||
| 72 | |||
| 73 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 74 | 1 | ▶ | use Data::Hexdumper; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 75 | 0 | ▶ | use P2P::pDonkey::Meta ':all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 76 | use P2P::pDonkey::Util qw( ip2addr ); | ||
| 77 | |||
| 78 | my $debug = 0; | ||
| 79 | |||
| 80 | # Preloaded methods go here. | ||
| 81 | |||
| 82 | use constant MT_SERVERMET => 0x0e; | ||
| 83 | use constant MT_PARTMET => 0xe1; | ||
| 84 | use constant MT_KNOWNMET => 0x0e; | ||
| 85 | |||
| 86 | |||
| 87 | sub readFile { | ||
| 88 | my ($fname, $tag) = @_; | ||
| 89 | my ($handle, $buf); | ||
| 90 | my $rs = $/; | ||
| 91 | undef $/; | ||
| 92 | open($handle, "<$fname") | ||
| 93 | or warn "Can't open '$fname': $!\n" | ||
| 94 | and $/ = $rs | ||
| 95 | and return; | ||
| 96 | binmode($handle); | ||
| 97 | if ($tag) { | ||
| 98 | if (read($handle, $buf, 1) != 1 || unpack('C',$buf) != $tag) { | ||
| 99 | warn "File '$fname' without tag!\n"; | ||
| 100 | close($handle); | ||
| 101 | $/ = $rs; | ||
| 102 | return; | ||
| 103 | } | ||
| 104 | $buf .= <$handle>; | ||
| 105 | } else { | ||
| 106 | $buf = <$handle>; | ||
| 107 | } | ||
| 108 | close($handle); | ||
| 109 | $/ = $rs; | ||
| 110 | return \$buf; | ||
| 111 | }; | ||
| 112 | |||
| 113 | sub writeFile($$) { | ||
| 114 | my ($fname, $buf) = @_; | ||
| 115 | my $handle; | ||
| 116 | open($handle, ">$fname") or warn "Can't open `$fname': $!\n" and return; | ||
| 117 | binmode($handle); | ||
| 118 | print $handle $$buf; | ||
| 119 | close($handle); | ||
| 120 | return 1; | ||
| 121 | }; | ||
| 122 | |||
| 123 | # ----------------------------------------------------------------------------- | ||
| 124 | # server.met | ||
| 125 | |||
| 126 | sub unpackServerDesc { | ||
| 127 | my ($ip, $port, $meta); | ||
| 128 | ($ip, $port) = &unpackAddr or return; | ||
| 129 | $meta = &unpackMetaListU or return; | ||
| 130 | # Hash is for compatibility with Info structure | ||
| 131 | return {Hash => '01234567012345670123456701234567', IP => $ip, Port => $port, Meta => $meta}; | ||
| 132 | } | ||
| 133 | sub packServerDesc { | ||
| 134 | my ($d) = @_; | ||
| 135 | return packAddr($d) . packMetaListU($d->{Meta}); | ||
| 136 | } | ||
| 137 | sub printServerDesc { | ||
| 138 | my ($d) = @_; | ||
| 139 | printAddr($d); | ||
| 140 | print "\n"; | ||
| 141 | printMetaListU($d->{Meta}); | ||
| 142 | } | ||
| 143 | sub makeServerDesc { | ||
| 144 | my ($ip, $port, $name, $desc, $nusers, $nfiles, $preference) = @_; | ||
| 145 | defined($ip) && defined($port) or confess "Specify ip and port of server!"; | ||
| 146 | $name or $name = ''; | ||
| 147 | $desc or $desc = ''; | ||
| 148 | $preference or $preference = 0; | ||
| 149 | my %meta; | ||
| 150 | tie %meta, "Tie::IxHash"; | ||
| 151 | $meta{Name} = makeMeta(TT_NAME, $name); | ||
| 152 | $meta{Description} = makeMeta(TT_DESCRIPTION, $desc); | ||
| 153 | $meta{IP} = makeMeta(TT_IP, $ip); | ||
| 154 | $meta{Port} = makeMeta(TT_PORT, $port); | ||
| 155 | $meta{users} = makeMeta(TT_UNDEFINED, $nusers, "users", VT_INTEGER) if defined $nusers; | ||
| 156 | $meta{files} = makeMeta(TT_UNDEFINED, $nfiles, "files", VT_INTEGER) if defined $nfiles; | ||
| 157 | $meta{Preference} = makeMeta(TT_PREFERENCE, $preference); | ||
| 158 | return {Hash => '01234567012345670123456701234567', IP => $ip, Port => $port, Meta => \%meta}; | ||
| 159 | } | ||
| 160 | |||
| 161 | sub unpackServerDescList { | ||
| 162 | my ($n, @l, $d); | ||
| 163 | defined($n = &unpackD) or return; | ||
| 164 | @l = (); | ||
| 165 | while ($n--) { | ||
| 166 | $d = &unpackServerDesc or return; | ||
| 167 | push @l, $d; | ||
| 168 | } | ||
| 169 | return \@l; | ||
| 170 | } | ||
| 171 | sub packServerDescList { | ||
| 172 | my ($l) = @_; | ||
| 173 | my ($res); | ||
| 174 | $res = packD(scalar @$l); | ||
| 175 | foreach my $d (@$l) { | ||
| 176 | $res .= packServerDesc($d); | ||
| 177 | } | ||
| 178 | return $res; | ||
| 179 | } | ||
| 180 | sub printServerDescList { | ||
| 181 | foreach my $d (@{$_[0]}) { | ||
| 182 | printServerDesc($d); | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | sub unpackServerDescListU { | ||
| 187 | my ($n, %l, $d); | ||
| 188 | tie %l, "Tie::IxHash"; | ||
| 189 | defined($n = &unpackD) or return; | ||
| 190 | %l = (); | ||
| 191 | while ($n--) { | ||
| 192 | $d = &unpackServerDesc or return; | ||
| 193 | $l{idAddr($d)} = $d; | ||
| 194 | } | ||
| 195 | return \%l; | ||
| 196 | } | ||
| 197 | sub packServerDescListU { | ||
| 198 | my ($res, $d); | ||
| 199 | my $n = 0; | ||
| 200 | $res = ''; | ||
| 201 | while ((undef, $d) = each %{$_[0]}) { | ||
| 202 | $res .= packServerDesc($d); | ||
| 203 | $n++; | ||
| 204 | } | ||
| 205 | return packD($n) . $res; | ||
| 206 | } | ||
| 207 | sub printServerDescListU { | ||
| 208 | my $d; | ||
| 209 | while ((undef, $d) = each %{$_[0]}) { | ||
| 210 | printServerDesc($d); | ||
| 211 | } | ||
| 212 | } | ||
| 213 | |||
| 214 | sub unpackServerMet { | ||
| 215 | &unpackB == MT_SERVERMET or return; | ||
| 216 | return &unpackServerDescListU; | ||
| 217 | } | ||
| 218 | sub packServerMet { | ||
| 219 | return packB(MT_SERVERMET) . &packServerDescListU; | ||
| 220 | } | ||
| 221 | sub printServerMet { | ||
| 222 | &printServerDescListU; | ||
| 223 | } | ||
| 224 | |||
| 225 | # parse server.met file && create hash | ||
| 226 | sub readServerMet { | ||
| 227 | my ($fname) = @_; | ||
| 228 | my ($off, $buf, $res); | ||
| 229 | $buf = readFile($fname, MT_SERVERMET) or return; | ||
| 230 | $off = 0; | ||
| 231 | $res = unpackServerMet($$buf, $off); | ||
| 232 | if ($res && $off != length $$buf) { | ||
| 233 | warn "Unhandled bytes at the end:\n", hexdump(data=>$$buf, start_position=>$off); | ||
| 234 | } | ||
| 235 | return $res; | ||
| 236 | } | ||
| 237 | |||
| 238 | sub writeServerMet { | ||
| 239 | my ($fname, $servers) = @_; | ||
| 240 | my $buf = packServerMet($servers); | ||
| 241 | return writeFile($fname, \$buf); | ||
| 242 | } | ||
| 243 | |||
| 244 | # ----------------------------------------------------------------------------- | ||
| 245 | # .part.met | ||
| 246 | |||
| 247 | sub unpackPartMet { | ||
| 248 | my $v = &unpackB; | ||
| 249 | $v == MT_PARTMET or return; | ||
| 250 | return &unpackFileInfo; | ||
| 251 | } | ||
| 252 | sub packPartMet { | ||
| 253 | return packB(MT_PARTMET) . &packFileInfo; | ||
| 254 | } | ||
| 255 | sub printPartMet { | ||
| 256 | &printInfo; | ||
| 257 | } | ||
| 258 | |||
| 259 | sub readPartMet { | ||
| 260 | my ($fname) = @_; | ||
| 261 | my ($off, $buf, $res); | ||
| 262 | $buf = readFile($fname, MT_PARTMET) or return; | ||
| 263 | $off = 0; | ||
| 264 | $res = unpackPartMet($$buf, $off); | ||
| 265 | $res->{Path} = $fname; | ||
| 266 | if ($res && $off != length $$buf) { | ||
| 267 | warn "Unhandled bytes at the end:\n", hexdump(data=>$$buf, start_position=>$off); | ||
| 268 | } | ||
| 269 | return $res; | ||
| 270 | } | ||
| 271 | |||
| 272 | sub writePartMet { | ||
| 273 | my ($fname, $p) = @_; | ||
| 274 | my $buf = packPartMet($p); | ||
| 275 | return writeFile($fname, \$buf); | ||
| 276 | } | ||
| 277 | |||
| 278 | |||
| 279 | # ----------------------------------------------------------------------------- | ||
| 280 | # known.met | ||
| 281 | sub unpackKnownMet { | ||
| 282 | &unpackB == MT_KNOWNMET or return; | ||
| 283 | return &unpackFileInfoList; | ||
| 284 | } | ||
| 285 | sub packKnownMet { | ||
| 286 | return packB(MT_KNOWNMET) . &packFileInfoList; | ||
| 287 | } | ||
| 288 | sub printKnownMet { | ||
| 289 | &printInfoList; | ||
| 290 | } | ||
| 291 | sub readKnownMet { | ||
| 292 | my ($fname) = @_; | ||
| 293 | my ($off, $buf, $res); | ||
| 294 | $buf = readFile($fname, MT_KNOWNMET) or return; | ||
| 295 | $off = 0; | ||
| 296 | $res = unpackKnownMet($$buf, $off); | ||
| 297 | if ($res && $off != length $$buf) { | ||
| 298 | warn "Unhandled bytes at the end:\n", hexdump(data=>$$buf, start_position=>$off); | ||
| 299 | } | ||
| 300 | return $res; | ||
| 301 | } | ||
| 302 | sub writeKnownMet { | ||
| 303 | my ($fname, $p) = @_; | ||
| 304 | my $buf = packKnownMet($p); | ||
| 305 | return writeFile($fname, \$buf); | ||
| 306 | } | ||
| 307 | |||
| 308 | # ----------------------------------------------------------------------------- | ||
| 309 | # pref.met | ||
| 310 | |||
| 311 | sub unpackPrefMet { | ||
| 312 | my ($ip, $port, $hash, $meta, $pref, $name, $m); | ||
| 313 | ($ip, $port) = &unpackAddr or return; | ||
| 314 | $hash = &unpackHash or return; | ||
| 315 | $meta = &unpackMetaListU or return; | ||
| 316 | $pref = &unpackMetaListU or return; | ||
| 317 | return {IP => $ip, Port => $port, Hash => $hash, Meta => $meta, Pref => $pref}; | ||
| 318 | } | ||
| 319 | sub packPrefMet { | ||
| 320 | my ($p) = @_; | ||
| 321 | return packAddr($p) . packHash($p->{Hash}) | ||
| 322 | . packMetaListU($p->{Meta}) . packMetaListU($p->{Pref}); | ||
| 323 | } | ||
| 324 | sub printPrefMet { | ||
| 325 | my ($d) = @_; | ||
| 326 | print "Address: "; | ||
| 327 | printAddr($d); | ||
| 328 | print "\n"; | ||
| 329 | print "Hash: $d->{Hash}\n"; | ||
| 330 | print "Meta:\n"; | ||
| 331 | printMetaListU($d->{Meta}); | ||
| 332 | print "Preferencies:\n"; | ||
| 333 | printMetaListU($d->{Pref}); | ||
| 334 | } | ||
| 335 | |||
| 336 | sub readPrefMet { | ||
| 337 | my ($fname) = @_; | ||
| 338 | my ($off, $buf, $res); | ||
| 339 | $buf = readFile($fname) or return; | ||
| 340 | $off = 0; | ||
| 341 | $res = unpackPrefMet($$buf, $off); | ||
| 342 | if ($res && $off != length $$buf) { | ||
| 343 | warn "Unhandled bytes at the end:\n", hexdump(data=>$$buf, start_position=>$off); | ||
| 344 | } | ||
| 345 | return $res; | ||
| 346 | } | ||
| 347 | sub writePrefMet { | ||
| 348 | my ($fname, $p) = @_; | ||
| 349 | my $buf = packPrefMet($p); | ||
| 350 | return writeFile($fname, \$buf); | ||
| 351 | } | ||
| 352 | |||
| 353 | 1; | ||
| 354 | __END__ | ||