#============================================================# # # # $ID:$ # # # # NaSocket4.pm # # # # Safe and backwards-compatible IPv4-only socket interface # # for ONTAPI APIs # # # # Copyright (c) 2010-2012 NetApp, Inc. All rights reserved. # # Specifications subject to change without notice. # # # # This SDK sample code is provided AS IS, with no support or # # warranties of any kind, including but not limited to # # warranties of merchantability or fitness of any kind, # # expressed or implied. This code is subject to the license # # agreement that accompanies the SDK. # # # # tab size = 8 # # # #============================================================# package NaSocket4; use strict; use warnings; use IO::Socket (); use Socket qw(INADDR_ANY AF_INET PF_INET SOCK_STREAM); # Look up address of hostname-or-IP-address $server_name, and # construct and return a sockaddr structure for its address and port. # Return that sockaddr structure, and also return the protocol family # (suitable for use in socket()) sub make_sockaddr { my ($pkg, $server_name, $port) = @_; my (undef,undef,undef,undef,$addr) = gethostbyname($server_name); my $sockaddr = pack('S n a4 x8',AF_INET,$port,$addr); return ($sockaddr, PF_INET); } # Construct and return a local sockaddr suitable for binding to local # port $port and for connecting to the remote address/port/family # specified by sockaddr structure $remote_sockaddr, which was created # using make_sockaddr, above. # In the case of this module, we know make_sockaddr always produces # IPv4 sockaddrs, and so we can just construct an appropriate IPv4 # sockaddr. sub make_local_sockaddr { my ($pkg, $remote_sockaddr, $port) = @_; my $sockaddr = pack('S n a4 x8',AF_INET,$port,INADDR_ANY); return $sockaddr; } # Return a true value if the given hostname-or-IP-address is suitable # for being connected to from this module. # In the case of this module, we assume for now that any hostname # (non-numeric IP address) or numeric IPv4 address should still be # attempted with IPv4 only. We assume values with colons are # generally IPv6 numeric addresses, and that the value must generally # be colons and periods and hex digits (so "a::b" and "a:b::1.2.3.4" # are IPv6, but "http:" is not) sub can_handle_address { my ($pkg, $addr) = @_; # Note: Once Data::Validate::IP is installed on the windows clients, # the code needs to be updated to use is_ipv6(). # is_ipv6($addr) would be a nicer way to check for ipv6 addresses. if ($addr =~ /:/ && $addr =~ /^[:0-9a-fA-F.]+$/) { return 0; } # Assume non-numeric addresses are all IPv4 return 1; } 1;