Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How to get wildcard addresses using getaddrinfo?

by sushant.ravale (Initiate)
on Aug 21, 2012 at 10:00 UTC ( #988678=perlquestion: print w/ replies, xml ) Need Help??
sushant.ravale has asked for the wisdom of the Perl Monks concerning the following question:

cross post : http://stackoverflow.com/questions/12052864/how-to-get-wildcard-address-using-getaddrinfo I am new to perl. I am trying to fetch all the wildcard interfaces using getaddrinfo (I am using Socket6 (IO:Socket:IP somehow didn't work on my Windows box)), like:
use Socket;
use Socket6;

@res = getaddrinfo('<wildcard>', 3786, AF_UNSPEC, SOCK_STREAM);

while(scalar(@res)>=5){

	 ($family, $socktype, $proto, $saddr, $canonname, @res) = @res;
	 ($host, $port) = getnameinfo($saddr, NI_NUMERICHOST | NI_NUMERICSERV);
	 print ("\nhost= $host port = $port");
}


I am wondering what value should I use for the placeholder <wildcard>, so that I'll get IPv4 as well IPv6 wildcard addresses (0.0.0.0 and ::) in the result, so that I can bind to it independent of the machine I am using (IPv4 or IPv6). In 'c' specifying a null hostname pointer does the job, for perl I tried '', undef but they return loopback addresses.

Comment on How to get wildcard addresses using getaddrinfo?
Re: How to get wildcard addresses using getaddrinfo?
by VinsWorldcom (Priest) on Aug 21, 2012 at 11:48 UTC

    For address family independent DNS lookups, I used this as a test for IPv6 support in Perl. This may not be the most elegant way of doing it, but it returns both IPv4 and IPv6 addresses for my local workstation and domain names you enter.

    # On Windows without modifying Socket compilation use Socket qw(inet_ntoa unpack_sockaddr_in IPPROTO_TCP AF_INET AF_UNSP +EC); use Socket6; use Socket::GetAddrInfo qw(getaddrinfo getnameinfo); # OR just this with Socket modified - http://vinsworldcom.blogspot.com +/2012/08/ipv6-in-perl-on-windows_20.html use Socket 2.005 qw(:addrinfo inet_ntoa inet_ntop unpack_sockaddr_in u +npack_sockaddr_in6 IPPROTO_TCP AF_INET AF_UNSPEC); use Sys::Hostname; if (!@ARGV) { $ARGV[0] = hostname; } my %hints = ( family => AF_UNSPEC, protocol => IPPROTO_TCP ); my ($err, @getaddr) = getaddrinfo($ARGV[0], undef, \%hints); if (defined($getaddr[0])) { for my $addr (@getaddr) { my $address; if ($addr->{family} == AF_INET) { (undef, $address) = unpack_sockaddr_in($addr->{addr}) } else { (undef, $address) = unpack_sockaddr_in6($addr->{addr}) } my $host = inet_ntop($addr->{family}, $address); print "$host\n"; if (0) { # set to 1 to do reverse lookup my ($err, $host, $service) = getnameinfo($addr->{addr}, NI +_NAMEREQD); printf " |_> %s\n", (defined($host)) ? $host : $err } } } else { print "$0: getaddrinfo() failed - error = $err\n" }

    And the results (note my local IPv6 addresses obscured with XXXX ...):

    VinsWorldcom@C:\Users\VinsWorldcom\tmp> test.pl fe80::WWWW:XXXX:YYYY:ZZZZ fe80::WWWW:XXXX:YYYY:ZZZZ fe80::WWWW:XXXX:YYYY:ZZZZ AAAA:BBBB:CCCC:DDDD:WWWW:XXXX:YYYY:ZZZZ fe80::WWWW:XXXX:YYYY:ZZZZ fe80::WWWW:XXXX:YYYY:ZZZZ 192.168.12.1 192.168.58.1 192.168.10.102 10.200.200.254 10.100.100.254 192.168.100.254 VinsWorldcom@C:\Users\VinsWorldcom\tmp> test.pl www.google.com 2001:4860:800a::6a 173.194.73.106 173.194.73.147 173.194.73.99 173.194.73.104 173.194.73.105 173.194.73.103
      thanks for the reply, getaddrinfo('<wildcard>', 3786, AF_UNSPEC, SOCK_STREAM, 0, AI_PASSIVE); solved the issue, I am getting wild card addresses now.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://988678]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2014-09-19 20:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (145 votes), past polls