1210753Srpaulo#!/usr/bin/perl -w
2210753Srpaulo#
3210753Srpaulo# CDDL HEADER START
4210753Srpaulo#
5210753Srpaulo# The contents of this file are subject to the terms of the
6210753Srpaulo# Common Development and Distribution License (the "License").
7210753Srpaulo# You may not use this file except in compliance with the License.
8210753Srpaulo#
9210753Srpaulo# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10210753Srpaulo# or http://www.opensolaris.org/os/licensing.
11210753Srpaulo# See the License for the specific language governing permissions
12210753Srpaulo# and limitations under the License.
13210753Srpaulo#
14210753Srpaulo# When distributing Covered Code, include this CDDL HEADER in each
15210753Srpaulo# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16210753Srpaulo# If applicable, add the following below this CDDL HEADER, with the
17210753Srpaulo# fields enclosed by brackets "[]" replaced with your own identifying
18210753Srpaulo# information: Portions Copyright [yyyy] [name of copyright owner]
19210753Srpaulo#
20210753Srpaulo# CDDL HEADER END
21210753Srpaulo#
22210753Srpaulo
23210753Srpaulo#
24210753Srpaulo# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
25210753Srpaulo# Use is subject to license terms.
26210753Srpaulo#
27210753Srpaulo#pragma ident	"%Z%%M%	%I%	%E% SMI"
28210753Srpaulo
29210753Srpaulo#
30210753Srpaulo# get.ipv6remote.pl
31210753Srpaulo#
32210753Srpaulo# Find an IPv6 reachable remote host using both ifconfig(1M) and ping(1M).
33210753Srpaulo# Print the local address and the remote address, or print nothing if either
34210753Srpaulo# no IPv6 interfaces or remote hosts were found.  (Remote IPv6 testing is
35210753Srpaulo# considered optional, and so not finding another IPv6 host is not an error
36210753Srpaulo# state we need to log.)  Exit status is 0 if a host was found.
37210753Srpaulo#
38210753Srpaulo
39210753Srpaulouse strict;
40210753Srpaulouse IO::Socket;
41210753Srpaulo
42210753Srpaulomy $MAXHOSTS = 32;			# max hosts to scan
43210753Srpaulomy $TIMEOUT = 3;			# connection timeout
44210753Srpaulomy $MULTICAST = "FF02::1";		# IPv6 multicast address
45210753Srpaulo
46210753Srpaulo#
47210753Srpaulo# Determine local IP address
48210753Srpaulo#
49210753Srpaulomy $local = "";
50210753Srpaulomy $remote = "";
51210753Srpaulomy %Local;
52210753Srpaulomy $up;
53211545Srpauloopen IFCONFIG, '/sbin/ifconfig -a inet6 |'
54210753Srpaulo    or die "Couldn't run ifconfig: $!\n";
55210753Srpaulowhile (<IFCONFIG>) {
56210753Srpaulo	next if /^lo/;
57210753Srpaulo
58210753Srpaulo	# "UP" is always printed first (see print_flags() in ifconfig.c):
59210753Srpaulo	$up = 1 if /^[a-z].*<UP,/;
60210753Srpaulo	$up = 0 if /^[a-z].*<,/;
61210753Srpaulo
62210753Srpaulo	# assume output is "inet6 ...":
63210753Srpaulo	if (m:inet6 (\S+)/:) {
64210753Srpaulo		my $addr = $1;
65210753Srpaulo                $Local{$addr} = 1;
66210753Srpaulo                $local = $addr if $up and $local eq "";
67210753Srpaulo		$up = 0;
68210753Srpaulo	}
69210753Srpaulo}
70210753Srpauloclose IFCONFIG;
71210753Srpauloexit 1 if $local eq "";
72210753Srpaulo
73210753Srpaulo#
74210753Srpaulo# Find the first remote host that responds to an icmp echo,
75210753Srpaulo# which isn't a local address.
76210753Srpaulo#
77211545Srpauloopen PING, "/sbin/ping -ns -A inet6 $MULTICAST 56 $MAXHOSTS |" or
78210753Srpaulo    die "Couldn't run ping: $!\n";
79210753Srpaulowhile (<PING>) {
80210753Srpaulo	if (/bytes from (.*): / and not defined $Local{$1}) {
81210753Srpaulo		$remote = $1;
82210753Srpaulo		last;
83210753Srpaulo	}
84210753Srpaulo}
85210753Srpauloclose PING;
86210753Srpauloexit 2 if $remote eq "";
87210753Srpaulo
88210753Srpauloprint "$local $remote\n";
89