diff options
author | Yorhel <git@yorhel.nl> | 2014-08-29 11:50:41 +0200 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2014-08-29 11:50:41 +0200 |
commit | 1cf4252f2d313bc1e3f460a1d379a49b751d1170 (patch) | |
tree | 4385788e5553fe7376e4063961e8c7a9cbce7663 /lib/VNDBUtil.pm | |
parent | 9a1bd46a568094ff62cfc85bc488b116042718b8 (diff) |
API: Make sure to honor the new login throttle
Diffstat (limited to 'lib/VNDBUtil.pm')
-rw-r--r-- | lib/VNDBUtil.pm | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/lib/VNDBUtil.pm b/lib/VNDBUtil.pm index e9df0a2c..febce0c0 100644 --- a/lib/VNDBUtil.pm +++ b/lib/VNDBUtil.pm @@ -7,8 +7,9 @@ use warnings; use Exporter 'import'; use Encode 'encode_utf8'; use Unicode::Normalize 'NFKD'; +use Socket 'inet_pton', 'inet_ntop', 'AF_INET6'; -our @EXPORT = qw|shorten bb2html gtintype normalize normalize_titles normalize_query imgsize|; +our @EXPORT = qw|shorten bb2html gtintype normalize normalize_titles normalize_query imgsize norm_ip|; sub shorten { @@ -244,5 +245,23 @@ sub imgsize { } +# Normalized IP address to use for duplicate detection/throttling. For IPv4 +# this is just the normal address, but for IPv6 this is the /48 subnet, with +# the rest of the address zero'd. +sub norm_ip { + my $ip = shift; + return $ip if $ip !~ /:/; + + # There's a whole bunch of IPv6 manipulation modules on CPAN, but many seem + # quite bloated and still don't offer the functionality to return an IP + # with its mask applied (admittedly not a common operation). The libc + # socket functions will do fine in parsing and formatting IPv6 addresses, + # and the actual masking is quite trivial in binary form. + $ip = inet_pton AF_INET6, $ip; + return '::' if !$ip; + $ip =~ s/^(.{6}).+$/$1 . "\0"x10/e; + return inet_ntop AF_INET6, $ip; +} + 1; |