#!/bin/perl
#Output the name, address family (probably AF_INET == 2), and IP address of each
#network interface.

use Socket;
sub SIOCGIFCONF {return 0xC0000000 | 8 << 16 | ord(i) << 8 | 20;}

socket(S, AF_INET, SOCK_DGRAM, 0) or die "$0: $!";
ioctl(S, SIOCGIFCONF, $struct) or die "$0: $!";

#Unpack the array of ifreq structures (/usr/include/net/if.h) into $array.
#$len is the size in bytes of each ifreq structure.
#$length is the size in bytes of the array.
$len = 32; 
$length = unpack('I', $struct);
$array = unpack("x4P$length", $struct);

for ($offset = 0; $offset < $length; $offset += $len) {
	#Unpack the name field of each ifreq structure, followed by the first
	#three fields of the sockaddr_in structure in /usr/include/netinet/in.h.
	#The port field is unused.

	($name, $family, $port, $ip) = unpack("x${offset}A16nna4", $array);
	print "$name $family ", inet_ntoa($ip), "\n";
}

exit 0;
