xref: /illumos-gate/usr/src/cmd/dtrace/test/tst/common/ip/get.ipv6remote.pl (revision 66582b606a8194f7f3ba5b3a3a6dca5b0d346361)
1#!/usr/perl5/bin/perl -w
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
28#
29# get.ipv6remote.pl
30#
31# Find an IPv6 reachable remote host using both ifconfig(1M) and ping(1M).
32# Print the local address and the remote address, or print nothing if either
33# no IPv6 interfaces or remote hosts were found.  (Remote IPv6 testing is
34# considered optional, and so not finding another IPv6 host is not an error
35# state we need to log.)  Exit status is 0 if a host was found.
36#
37
38use strict;
39use IO::Socket;
40
41my $MAXHOSTS = 32;			# max hosts to scan
42my $TIMEOUT = 3;			# connection timeout
43my $MULTICAST = "FF02::1";		# IPv6 multicast address
44
45#
46# Determine local IP address
47#
48my $local = "";
49my $remote = "";
50my %Local;
51my $up;
52open IFCONFIG, '/usr/sbin/ifconfig -a inet6 |'
53    or die "Couldn't run ifconfig: $!\n";
54while (<IFCONFIG>) {
55	next if /^lo/;
56
57	# "UP" is always printed first (see print_flags() in ifconfig.c):
58	$up = 1 if /^[a-z].*<UP,/;
59	$up = 0 if /^[a-z].*<,/;
60
61	# assume output is "inet6 ...":
62	if (m:inet6 (\S+)/:) {
63		my $addr = $1;
64                $Local{$addr} = 1;
65                $local = $addr if $up and $local eq "";
66		$up = 0;
67	}
68}
69close IFCONFIG;
70exit 1 if $local eq "";
71
72#
73# Find the first remote host that responds to an icmp echo,
74# which isn't a local address.
75#
76open PING, "/usr/sbin/ping -ns -A inet6 $MULTICAST 56 $MAXHOSTS |" or
77    die "Couldn't run ping: $!\n";
78while (<PING>) {
79	if (/bytes from (.*): / and not defined $Local{$1}) {
80		$remote = $1;
81		last;
82	}
83}
84close PING;
85exit 2 if $remote eq "";
86
87print "$local $remote\n";
88