~ubuntu-branches/ubuntu/raring/eucalyptus/raring

« back to all changes in this revision

Viewing changes to clc/modules/msgs/src/main/java/com/eucalyptus/util/NetworkUtil.java

  • Committer: Package Import Robot
  • Author(s): Brian Thomason
  • Date: 2011-11-29 13:17:52 UTC
  • mfrom: (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 185.
  • Revision ID: package-import@ubuntu.com-20111129131752-rq31al3ntutv2vvl
Tags: upstream-3.0.999beta1
Import upstream version 3.0.999beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*******************************************************************************
2
 
*Copyright (c) 2009  Eucalyptus Systems, Inc.
3
 
4
 
*  This program is free software: you can redistribute it and/or modify
5
 
*  it under the terms of the GNU General Public License as published by
6
 
*  the Free Software Foundation, only version 3 of the License.
7
 
8
 
9
 
*  This file is distributed in the hope that it will be useful, but WITHOUT
10
 
*  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
 
*  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12
 
*  for more details.
13
 
14
 
*  You should have received a copy of the GNU General Public License along
15
 
*  with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
 
*  Please contact Eucalyptus Systems, Inc., 130 Castilian
18
 
*  Dr., Goleta, CA 93101 USA or visit <http://www.eucalyptus.com/licenses/>
19
 
*  if you need additional information or have any questions.
20
 
21
 
*  This file may incorporate work covered under the following copyright and
22
 
*  permission notice:
23
 
24
 
*    Software License Agreement (BSD License)
25
 
26
 
*    Copyright (c) 2008, Regents of the University of California
27
 
*    All rights reserved.
28
 
29
 
*    Redistribution and use of this software in source and binary forms, with
30
 
*    or without modification, are permitted provided that the following
31
 
*    conditions are met:
32
 
33
 
*      Redistributions of source code must retain the above copyright notice,
34
 
*      this list of conditions and the following disclaimer.
35
 
36
 
*      Redistributions in binary form must reproduce the above copyright
37
 
*      notice, this list of conditions and the following disclaimer in the
38
 
*      documentation and/or other materials provided with the distribution.
39
 
40
 
*    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
41
 
*    IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
42
 
*    TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
43
 
*    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
44
 
*    OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45
 
*    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
46
 
*    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
47
 
*    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
48
 
*    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
49
 
*    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
50
 
*    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. USERS OF
51
 
*    THIS SOFTWARE ACKNOWLEDGE THE POSSIBLE PRESENCE OF OTHER OPEN SOURCE
52
 
*    LICENSED MATERIAL, COPYRIGHTED MATERIAL OR PATENTED MATERIAL IN THIS
53
 
*    SOFTWARE, AND IF ANY SUCH MATERIAL IS DISCOVERED THE PARTY DISCOVERING
54
 
*    IT MAY INFORM DR. RICH WOLSKI AT THE UNIVERSITY OF CALIFORNIA, SANTA
55
 
*    BARBARA WHO WILL THEN ASCERTAIN THE MOST APPROPRIATE REMEDY, WHICH IN
56
 
*    THE REGENTS’ DISCRETION MAY INCLUDE, WITHOUT LIMITATION, REPLACEMENT
57
 
*    OF THE CODE SO IDENTIFIED, LICENSING OF THE CODE SO IDENTIFIED, OR
58
 
*    WITHDRAWAL OF THE CODE CAPABILITY TO THE EXTENT NEEDED TO COMPLY WITH
59
 
*    ANY SUCH LICENSES OR RIGHTS.
60
 
*******************************************************************************/
61
 
/*
62
 
 * Author: chris grzegorczyk <grze@eucalyptus.com>
63
 
 */
64
 
package com.eucalyptus.util;
65
 
 
66
 
import java.io.IOException;
67
 
import java.net.Inet4Address;
68
 
import java.net.InetAddress;
69
 
import java.net.InterfaceAddress;
70
 
import java.net.NetworkInterface;
71
 
import java.net.SocketException;
72
 
import java.net.URI;
73
 
import java.net.URISyntaxException;
74
 
import java.net.UnknownHostException;
75
 
import java.util.Collections;
76
 
import java.util.Enumeration;
77
 
import java.util.List;
78
 
 
79
 
import org.apache.log4j.Logger;
80
 
 
81
 
import com.google.common.base.Predicate;
82
 
import com.google.common.collect.Iterables;
83
 
import com.google.common.collect.Lists;
84
 
 
85
 
public class NetworkUtil {
86
 
  private static Logger LOG = Logger.getLogger( NetworkUtil.class );
87
 
  public static List<String> getAllAddresses() throws SocketException  {
88
 
    List<String> addrs = Lists.newArrayList( );
89
 
    Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces( );
90
 
    while( ifaces.hasMoreElements( ) ) {
91
 
      NetworkInterface iface = ifaces.nextElement( );
92
 
      for( InterfaceAddress iaddr : iface.getInterfaceAddresses( ) ) {
93
 
        InetAddress addr = iaddr.getAddress( );
94
 
        if( addr instanceof Inet4Address ) {
95
 
          if( !addr.isMulticastAddress( ) && !addr.isLoopbackAddress( ) && !addr.isLinkLocalAddress( ) && !addr.isSiteLocalAddress( ) && !"192.168.122.1".equals( addr.getHostAddress( ) ) ) {
96
 
            addrs.add( addr.getHostAddress( ) );
97
 
          }
98
 
        }
99
 
        if( addr instanceof Inet4Address ) {
100
 
          if( !addr.isMulticastAddress( ) && !addr.isLoopbackAddress( ) && !addr.isLinkLocalAddress( ) && !addrs.contains( addr.getHostAddress( ) ) && !"192.168.122.1".equals( addr.getHostAddress( ) ) ) {
101
 
            addrs.add( addr.getHostAddress( ) );
102
 
          }
103
 
        }
104
 
      }
105
 
    }
106
 
    return addrs;
107
 
  }
108
 
 
109
 
  public static boolean testReachability( String addr ) throws Exception {
110
 
    Exceptions.ifNullArgument( addr );
111
 
    InetAddress inetAddr = Inet4Address.getByName( addr );
112
 
    return inetAddr.isReachable( 10000 );
113
 
  }
114
 
 
115
 
  public static InetAddress toAddress( URI uri ) {
116
 
    Exceptions.ifNullArgument( uri );
117
 
    try {
118
 
      return InetAddress.getByName( uri.getHost( ) );
119
 
    } catch ( UnknownHostException e ) {
120
 
      throw Exceptions.illegalArgument( "Failed to resolve address for host: " + uri.getHost( ), e );
121
 
    }
122
 
  }
123
 
 
124
 
  public static InetAddress toAddress( String maybeUrlMaybeHostname ) {
125
 
    Exceptions.ifNullArgument( maybeUrlMaybeHostname );
126
 
    if( maybeUrlMaybeHostname.startsWith( "vm:" ) ) {
127
 
      maybeUrlMaybeHostname = "localhost";
128
 
    }
129
 
    URI uri = null;
130
 
    String hostAddress = null;
131
 
    try {
132
 
      uri = new URI( maybeUrlMaybeHostname );
133
 
      hostAddress = uri.getHost( );
134
 
    } catch ( URISyntaxException e ) {
135
 
      hostAddress = maybeUrlMaybeHostname;
136
 
    }
137
 
    InetAddress ret = null;
138
 
    try {
139
 
      ret = InetAddress.getByName( hostAddress );
140
 
    } catch ( UnknownHostException e1 ) {
141
 
      throw Exceptions.fatal( "Failed to resolve address for host: " + maybeUrlMaybeHostname, e1 );
142
 
    }
143
 
    return ret;
144
 
  }
145
 
  
146
 
  public static boolean testLocal( final InetAddress addr ) {
147
 
    Exceptions.ifNullArgument( addr );
148
 
    try {
149
 
      Boolean result = addr.isAnyLocalAddress( ); 
150
 
      result |= Iterables.any( Collections.list( NetworkInterface.getNetworkInterfaces( ) ), new Predicate<NetworkInterface>() {
151
 
        @Override public boolean apply( NetworkInterface arg0 ) {
152
 
          return Iterables.any( arg0.getInterfaceAddresses( ), new Predicate<InterfaceAddress>() {
153
 
            @Override public boolean apply( InterfaceAddress arg0 ) {
154
 
              return arg0.getAddress( ).equals( addr );
155
 
            }} );
156
 
        }} );
157
 
      return result;
158
 
    } catch ( Exception e ) {
159
 
      return Exceptions.eat( e.getMessage( ), e );
160
 
    }    
161
 
  }
162
 
 
163
 
  public static boolean testLocal( String address ) {
164
 
    Exceptions.ifNullArgument( address );
165
 
    InetAddress addr;
166
 
      try {
167
 
        addr = InetAddress.getByName( address );
168
 
        return testLocal( addr );
169
 
      } catch ( UnknownHostException e ) {
170
 
        return Exceptions.eat( e.getMessage( ), e );
171
 
      }
172
 
  }
173
 
  
174
 
  public static boolean testGoodAddress( String address ) throws Exception {
175
 
    InetAddress addr = InetAddress.getByName( address );
176
 
    LOG.debug( addr + " site=" + addr.isSiteLocalAddress( ) );
177
 
    LOG.debug( addr + " any=" + addr.isAnyLocalAddress( ) );
178
 
    LOG.debug( addr + " loop=" + addr.isLoopbackAddress( ) );
179
 
    LOG.debug( addr + " link=" + addr.isLinkLocalAddress( ) );
180
 
    LOG.debug( addr + " multi=" + addr.isMulticastAddress( ) );
181
 
    return addr.isSiteLocalAddress( ) || ( !addr.isAnyLocalAddress( ) && !addr.isLoopbackAddress( ) && !addr.isLinkLocalAddress( ) && !addr.isMulticastAddress( ) );
182
 
  }
183
 
 
184
 
  
185
 
  public static void main( String[] args) throws Exception {
186
 
    for( String addr : NetworkUtil.getAllAddresses( ) ) {
187
 
      System.out.println( addr );
188
 
    }
189
 
    System.out.println("Testing if 192.168.7.8 is reachable: " + NetworkUtil.testReachability( "192.168.7.8" ) );
190
 
  }
191
 
 
192
 
}