KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > caramel > util > NetUtils


1 /*
2 ** Caramel - Non-GUI Java Addons
3 ** Copyright (c) 2001, 2002, 2003 by Gerald Bauer
4 **
5 ** This program is free software.
6 **
7 ** You may redistribute it and/or modify it under the terms of the GNU
8 ** Lesser General Public License as published by the Free Software Foundation.
9 ** Version 2.1 of the license should be included with this distribution in
10 ** the file LICENSE, as well as License.html. If the license is not
11 ** included with this distribution, you may find a copy at the FSF web
12 ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
13 ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
14 **
15 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
16 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
17 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
18 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
19 ** REDISTRIBUTION OF THIS SOFTWARE.
20 **
21 */

22
23 package caramel.util;
24
25 import java.net.*;
26 import houston.*;
27
28 public class NetUtils
29 {
30    /**
31     * cache local host name
32     */

33    private static String JavaDoc _localHostName = null;
34
35    public static char getIpClass( InetAddress ip )
36    {
37       // source published in O'Reilly Java Network Programming
38
// p178
39

40       byte address[] = ip.getAddress();
41       if( address.length != 4 )
42          throw new IllegalArgumentException JavaDoc( "address has more than 4 bytes; possibly IPv6 address" );
43
44       int firstByte = address[0];
45       if( ( firstByte & 0x80 ) == 0 )
46          return 'A';
47       else if( ( firstByte & 0xC0 ) == 0x80 )
48          return 'B';
49       else if( ( firstByte & 0xE0 ) == 0xC0 )
50          return 'C';
51       else if( ( firstByte & 0xF0 ) == 0xE0 )
52          return 'D';
53       else if( ( firstByte & 0xF8 ) == 0xF0 )
54          return 'E';
55       else
56          return 'F';
57    }
58
59    public static String JavaDoc getLocalHostName()
60    {
61       if( _localHostName == null )
62       {
63          try
64          {
65             _localHostName = InetAddress.getLocalHost().getHostName();
66          }
67          catch( UnknownHostException e )
68          {
69             Status.error( "*** failed to get local host name: " + e.toString() );
70             // use loopback host address if all else fails
71
_localHostName = "127.0.0.1";
72          }
73       }
74
75       return _localHostName;
76    }
77
78    public static boolean isHostName( String JavaDoc host )
79    {
80       // check if hostName is really a host name and not an host address
81
// that is, 127.0.0.1, for example
82

83       char s[] = host.toCharArray();
84       // if we see a character that is neither a digit nor a period
85
// then host is probably a host name
86

87       for( int i = 0; i < s.length; i++ )
88       {
89          if( !Character.isDigit( s[i] ) && s[i] != '.' )
90             return true;
91       }
92       return false;
93    }
94
95    public static boolean isLoopbackIp( InetAddress ip )
96    {
97       byte address[] = ip.getAddress();
98
99       if( address[0] == 127
100              && address[1] == 0
101              && address[2] == 0
102              && address[3] == 1 )
103          return true;
104
105       return false;
106    }
107
108    public static boolean isPrivateIp( InetAddress ip )
109    {
110       // private network space ip ranges defined by RFC 1918
111
//
112
// 10.0.0.0 through 10.255.255.255 (class A)
113
// 172.16.0.0 through 172.31.255.255 (class B)
114
// 192.168.0.0 through 192.168.255.255 (class C)
115

116       byte address[] = ip.getAddress();
117
118       // note: a byte is signed!
119
// byte values from 128 to 255 are negative numbers!
120
// to make it easier convert it to an unsigned byte
121
int firstQuad = address[0] < 0 ? address[0] + 256 : address[0];
122       int secondQuad = address[1] < 0 ? address[1] + 256 : address[1];
123       int thirdQuad = address[2] < 0 ? address[2] + 256 : address[2];
124       int fourthQuad = address[3] < 0 ? address[3] + 256 : address[3];
125
126       // check 10.0.0.0 through 10.255.255.255 (class A)
127
if( firstQuad == 10 )
128          return true;
129
130       // check 172.16.0.0 through 172.31.255.255 (class B)
131
if( firstQuad == 172 )
132       {
133          if( secondQuad >= 16 && secondQuad <= 31 )
134             return true;
135       }
136
137       // 192.168.0.0 through 192.168.255.255 (class C)
138
if( firstQuad == 192 && secondQuad == 168 )
139          return true;
140
141       return false;
142    }
143
144 }
145
Popular Tags