# Copyright (c) 2011 bivio Software, Inc. All Rights Reserved. # # Visit http://www.bivio.biz for more info. # # This library is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; If not, you may get a copy from: # http://www.opensource.org/licenses/lgpl-license.html # # $Id: IPAddress.pm,v 1.2 2011/11/05 23:50:31 nagler Exp $ package Bivio::Type::IPAddress; use strict; use Bivio::Base 'Type.SyntacticString'; our($VERSION) = sprintf('%d.%02d', q$Revision: 1.2 $ =~ /\d+/g); sub REGEX { return qr{(?:\d{1,3}\.){3}\d{1,3}}i; } sub from_domain { my($proto, $host) = @_; return $proto->from_inet( (gethostbyname($host))[4] || b_die($host, ': gethostbyname: ', "$!")); } sub from_inet { my(undef, $inet_addr) = @_; return join('.', unpack('C4', $inet_addr)); } sub get_min_width { return 7; } sub get_width { return 15; } sub to_inet { my(undef, $value) = @_; return pack('C4', split(/\./, $value)); } sub unsafe_to_domain { my($proto, $value) = @_; return (gethostbyaddr($proto->to_inet($value), Socket::AF_INET()))[0] || undef; } 1;