Identifying the Other End of a Socket
Problem
You have a socket and want to identify the machine at the other end.
Solution
If you’re only interested in the IP address of the remote machine, use:
use Socket; $other_end = getpeername(SOCKET) or die "Couldn't identify other end: $!\n"; ($port, $iaddr) = unpack_sockaddr_in($other_end); $ip_address = inet_ntoa($iaddr);
If you want its actual host name, use:
use Socket; $other_end = getpeername(SOCKET) or die "Couldn't identify other end: $!\n"; ($port, $iaddr) = unpack_sockaddr_in($other_end); $actual_ip = inet_ntoa($iaddr); $claimed_hostname = gethostbyaddr($iaddr, AF_INET); @name_lookup = gethostbyname($claimed_hostname) or die "Could not look up $claimed_hostname : $!\n"; @resolved_ips = map { inet_ntoa($_) } @name_lookup[ 4 .. $#ips_for_hostname ];
Discussion
For a long time, figuring out who connected to you was considered
more straightforward than it really is. The
getpeername
function returns the IP address of the
remote machine in a packed binary structure (or
undef
if an error occurred). To unpack it, use
inet_ntoa
. If you want the name of the remote end,
call gethostbyaddr
to look up the name of the
machine in the DNS tables, right?
Not really. That’s only half the solution. Because a name lookup goes to the name’s owner’s DNS server and a lookup of an IP addresses goes to the address’s owner’s DNS server, you have to contend with the possibility that the machine that connected to you is giving incorrect names. For instance, the ...
Get Perl Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.