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 = "";
51250685Smarkjmy $interf = "";
52210753Srpaulomy %Local;
53250685Smarkjmy %Addr;
54210753Srpaulomy $up;
55211545Srpauloopen IFCONFIG, '/sbin/ifconfig -a inet6 |'
56210753Srpaulo    or die "Couldn't run ifconfig: $!\n";
57210753Srpaulowhile (<IFCONFIG>) {
58210753Srpaulo	next if /^lo/;
59210753Srpaulo
60210753Srpaulo	# "UP" is always printed first (see print_flags() in ifconfig.c):
61210753Srpaulo	$up = 1 if /^[a-z].*<UP,/;
62210753Srpaulo	$up = 0 if /^[a-z].*<,/;
63210753Srpaulo
64250685Smarkj	if (m:(\S+\d+)\: :) {
65250685Smarkj		$interf = $1;
66250685Smarkj	}
67250685Smarkj
68210753Srpaulo	# assume output is "inet6 ...":
69250685Smarkj	if (m:inet6 (\S+) :) {
70210753Srpaulo		my $addr = $1;
71210753Srpaulo                $Local{$addr} = 1;
72250685Smarkj                $Addr{$interf} = $addr;
73210753Srpaulo		$up = 0;
74250685Smarkj		$interf = "";
75210753Srpaulo	}
76210753Srpaulo}
77210753Srpauloclose IFCONFIG;
78210753Srpaulo
79210753Srpaulo#
80210753Srpaulo# Find the first remote host that responds to an icmp echo,
81250685Smarkj# which isn't a local address. Try each IPv6-enabled interface.
82210753Srpaulo#
83250685Smarkjforeach $interf (split(' ', `ifconfig -l -u inet6`)) {
84250685Smarkj	next if $interf =~ /lo[0-9]+/;
85250685Smarkj	open PING, "/sbin/ping6 -n -s 56 -c $MAXHOSTS $MULTICAST\%$interf |" or next;
86250685Smarkj	while (<PING>) {
87250685Smarkj		if (/bytes from (.*), / and not defined $Local{$1}) {
88250685Smarkj			$remote = $1;
89250685Smarkj			$local = $Addr{$interf};
90250685Smarkj			last;
91250685Smarkj		}
92210753Srpaulo	}
93210753Srpaulo}
94210753Srpauloclose PING;
95210753Srpauloexit 2 if $remote eq "";
96210753Srpaulo
97210753Srpauloprint "$local $remote\n";
98