1#!/usr/bin/env perl
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23#
24# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27#pragma ident	"%Z%%M%	%I%	%E% SMI"
28
29#
30# get.ipv4remote.pl [port] [proto]
31#
32# Find an IPv4 reachable remote host using both ifconfig(1M) and ping(1M).
33# If a port is specified, return a host that is also listening on this
34# port. If the port is specified, the protocol can also be specified and
35# defaults to tcp.  Print the local address and the remote address, or an
36# error message if no suitable remote host was found.  Exit status is 0 if
37# a host was found.
38#
39
40use strict;
41use IO::Socket;
42
43my $MAXHOSTS = 32;			# max hosts to port scan
44my $TIMEOUT = 3;			# connection timeout
45my $port = @ARGV >= 1 ? $ARGV[0] : 0;
46my $proto = @ARGV == 2 ? $ARGV[1] : "tcp";
47
48#
49# Determine local IP address
50#
51my $local = "";
52my $remote = "";
53my %Broadcast;
54my $up;
55open IFCONFIG, '/sbin/ifconfig -a |' or die "Couldn't run ifconfig: $!\n";
56while (<IFCONFIG>) {
57	next if /^lo/;
58
59	# "UP" is always printed first (see print_flags() in ifconfig.c):
60	$up = 1 if /^[a-z].*<UP,/;
61	$up = 0 if /^[a-z].*<,/;
62
63	# assume output is "inet X ... broadcast Z":
64	if (/inet (\S+) .* broadcast (\S+)/) {
65		my ($addr, $bcast) = ($1, $2);
66		$Broadcast{$addr} = $bcast;
67		$local = $addr if $up and $local eq "";
68		$up = 0;
69	}
70}
71close IFCONFIG;
72die "Could not determine local IP address" if $local eq "";
73
74#
75# Find the first remote host that responds to an icmp echo,
76# which isn't a local address.
77#
78open PING, "/sbin/ping -n -s 56 -c $MAXHOSTS $Broadcast{$local} |" or
79    die "Couldn't run ping: $!\n";
80while (<PING>) {
81	if (/bytes from (.*): / and not defined $Broadcast{$1}) {
82		my $addr = $1;
83
84		if ($port != 0) {
85			#
86			# Test TCP
87			#
88			my $socket = IO::Socket::INET->new(
89				Type     => SOCK_STREAM,
90				Proto    => $proto,
91				PeerAddr => $addr,
92				PeerPort => $port,
93				Timeout  => $TIMEOUT,
94			);
95			next unless $socket;
96			close $socket;
97		}
98
99		$remote = $addr;
100		last;
101	}
102}
103close PING;
104die "Can't find a remote host for testing: No suitable response from " .
105    "$Broadcast{$local}\n" if $remote eq "";
106
107print "$local $remote\n";
108