312 |
// Any class derived from InetAddress should override this. |
// Any class derived from InetAddress should override this. |
313 |
|
|
314 |
// 10.0.0.0/8 |
// 10.0.0.0/8 |
315 |
if (addr [0] == 0x0a) |
if (addr [0] == 0x0A) |
316 |
return true; |
return true; |
317 |
|
|
318 |
// XXX: Suns JDK 1.4.1 (on Linux) seems to have a bug here: |
// XXX: Suns JDK 1.4.1 (on Linux) seems to have a bug here: |
319 |
// it says 172.16.0.0 - 172.255.255.255 are site local addresses |
// it says 172.16.0.0 - 172.255.255.255 are site local addresses |
320 |
|
|
321 |
// 172.16.0.0/12 |
// 172.16.0.0/12 |
322 |
if (addr [0] == 0xac |
if (addr [0] == 0xAC |
323 |
&& (addr [1] & 0xf0) == 0x01) |
&& (addr [1] & 0xF0) == 0x01) |
324 |
return true; |
return true; |
325 |
|
|
326 |
// 192.168.0.0/16 |
// 192.168.0.0/16 |
327 |
if (addr [0] == 0xc0 |
if (addr [0] == 0xC0 |
328 |
&& addr [1] == 0xa8) |
&& addr [1] == 0xA8) |
329 |
return true; |
return true; |
330 |
|
|
331 |
// XXX: Do we need to check more addresses here ? |
// XXX: Do we need to check more addresses here ? |
583 |
|
|
584 |
throw new UnknownHostException ("IP address has illegal length"); |
throw new UnknownHostException ("IP address has illegal length"); |
585 |
} |
} |
586 |
|
|
587 |
|
/** |
588 |
|
* If host is a valid numeric IP address, return the numeric address. |
589 |
|
* Otherwise, return null. |
590 |
|
*/ |
591 |
|
private static byte[] aton (String hostname) |
592 |
|
{ |
593 |
|
StringTokenizer st = new StringTokenizer (hostname, "."); |
594 |
|
|
595 |
|
if (st.countTokens() == 4) |
596 |
|
{ |
597 |
|
int index; |
598 |
|
byte[] address = new byte [4]; |
599 |
|
|
600 |
|
for (index = 0; index < 4; index++) |
601 |
|
{ |
602 |
|
try |
603 |
|
{ |
604 |
|
short n = Short.parseShort (st.nextToken()); |
605 |
|
|
606 |
|
if ((n < 0) || (n > 255)) |
607 |
|
break; |
608 |
|
|
609 |
|
address [index] = (byte) n; |
610 |
|
} |
611 |
|
catch (NumberFormatException e) |
612 |
|
{ |
613 |
|
break; |
614 |
|
} |
615 |
|
} |
616 |
|
|
617 |
|
if (index == 4) |
618 |
|
return address; |
619 |
|
} |
620 |
|
|
621 |
|
return null; |
622 |
|
} |
623 |
|
|
624 |
/** |
/** |
625 |
* Returns an InetAddress object representing the IP address of the given |
* Returns an InetAddress object representing the IP address of the given |
626 |
* hostname. This name can be either a hostname such as "www.urbanophile.com" |
* hostname. This name can be either a hostname such as "www.urbanophile.com" |
649 |
if (hostname == null) |
if (hostname == null) |
650 |
return getLocalHost(); |
return getLocalHost(); |
651 |
|
|
652 |
// First, check to see if it is an IP address. If so, then don't |
// Assume that the host string is an IP address. |
653 |
// do a DNS lookup. |
byte[] address = aton (hostname); |
654 |
StringTokenizer st = new StringTokenizer (hostname, "."); |
if (address != null) |
655 |
|
return new InetAddress (address); |
|
if (st.countTokens() == 4) |
|
|
{ |
|
|
int i; |
|
|
short n; |
|
|
byte[] ip = new byte [4]; |
|
|
|
|
|
for (i = 0; i < 4; i++) |
|
|
{ |
|
|
try |
|
|
{ |
|
|
n = Short.parseShort (st.nextToken()); |
|
|
|
|
|
if ((n < 0) || (n > 255)) |
|
|
break; |
|
|
|
|
|
ip [i] = (byte) n; |
|
|
} |
|
|
catch (NumberFormatException e) |
|
|
{ |
|
|
break; |
|
|
} |
|
|
} |
|
|
if (i == 4) |
|
|
{ |
|
|
return new InetAddress (ip); |
|
|
} |
|
|
} |
|
656 |
|
|
657 |
// Try to resolve the host by DNS |
// Try to resolve the host by DNS |
658 |
InetAddress[] addresses = getAllByName (hostname); |
InetAddress[] addresses = getAllByName (hostname); |
803 |
/** |
/** |
804 |
* This native method looks up the hostname of the local machine |
* This native method looks up the hostname of the local machine |
805 |
* we are on. If the actual hostname cannot be determined, then the |
* we are on. If the actual hostname cannot be determined, then the |
806 |
* value "localhost" we be used. This native method wrappers the |
* value "localhost" will be used. This native method wrappers the |
807 |
* "gethostname" function. |
* "gethostname" function. |
808 |
* |
* |
809 |
* @return The local hostname. |
* @return The local hostname. |
810 |
*/ |
*/ |
811 |
private static native String getLocalHostName(); |
private static native String getLocalHostname(); |
812 |
|
|
813 |
/** |
/** |
814 |
* Returns an InetAddress object representing the address of the current |
* Returns an InetAddress object representing the address of the current |
819 |
* @exception UnknownHostException If no IP address for the host could |
* @exception UnknownHostException If no IP address for the host could |
820 |
* be found |
* be found |
821 |
*/ |
*/ |
822 |
public static InetAddress getLocalHost () throws UnknownHostException |
public static InetAddress getLocalHost() throws UnknownHostException |
823 |
{ |
{ |
824 |
String hostname = getLocalHostName (); |
String hostname = getLocalHostname(); |
825 |
return getByName (hostname); |
return getByName (hostname); |
826 |
} |
} |
827 |
|
|
828 |
/** |
/** |
829 |
* Returns the value of the special address INADDR_ANY |
* Returns the value of the special address INADDR_ANY |
830 |
*/ |
*/ |
831 |
private static native byte[] lookupInaddrAny () throws UnknownHostException; |
private static native byte[] lookupInaddrAny() throws UnknownHostException; |
832 |
|
|
833 |
/** |
/** |
834 |
* This method returns the hostname for a given IP address. It will |
* This method returns the hostname for a given IP address. It will |