Jitsi: the OpenSource Java VoIP and Instant Messaging client.

net.java.sip.communicator.util
Class NetworkUtils

java.lang.Object
  extended by net.java.sip.communicator.util.NetworkUtils

public class NetworkUtils
extends Object

Utility methods and fields to use when working with network addresses.

Author:
Emil Ivov, Damian Minkov, Vincent Lucas, Alan Kelly

Field Summary
static String DEFAULT_BACKUP_RESOLVER
          The address of the backup resolver we would use by default.
static String IN_ADDR_ANY
          A string containing the "any" local address.
static String IN4_ADDR_ANY
          A string containing the "any" local address for IPv4.
static String IN6_ADDR_ANY
          A string containing the "any" local address for IPv6.
static int MAX_PORT_NUMBER
          The maximum int value that could correspond to a port number.
static int MIN_PORT_NUMBER
          The minimum int value that could correspond to a port number bindable by the SIP Communicator.
static boolean PDEFAULT_BACKUP_RESOLVER_ENABLED
          The default of the property that users may use to disable our backup DNS resolver.
static boolean PDEFAULT_DNS_ALWAYS_ABSOLUTE
          Default value of PNAME_DNS_ALWAYS_ABSOLUTE.
static String PNAME_BACKUP_RESOLVER
          The name of the property that users may use to override the address of our backup DNS resolver.
static String PNAME_BACKUP_RESOLVER_ENABLED
          The name of the property that users may use to disable our backup DNS resolver.
static String PNAME_BACKUP_RESOLVER_FALLBACK_IP
          The name of the property that users may use to override the IP address of our backup DNS resolver.
static String PNAME_BACKUP_RESOLVER_PORT
          The name of the property that users may use to override the port of our backup DNS resolver.
static String PNAME_DNS_ALWAYS_ABSOLUTE
          The name of the boolean property that defines whether all domain names looked up from Jitsi should be treated as absolute.
 
Constructor Summary
NetworkUtils()
           
 
Method Summary
static int compareDnsNames(String dns1, String dns2)
          Compares two DNS names against each other.
static InetSocketAddress getAAAARecord(String domain, int port)
          Returns array of hosts from the AAAA record of the specified domain.
static InetSocketAddress[] getAandAAAARecords(String domain, int port)
          Returns array of hosts from the A and AAAA records of the specified domain.
static InetSocketAddress getARecord(String domain, int port)
          Returns array of hosts from the A record of the specified domain.
static short getDefaultDnsPort()
          Gets the default port used by DNS servers obtained through SimpleResolver.DEFAULT_PORT.
static InetAddress getInetAddress(String hostAddress)
          Creates an InetAddress from the specified hostAddress.
static String[][] getNAPTRRecords(String domain)
          Makes a NAPTR query and returns the result.
static int getRandomPortNumber()
          Returns a random local port number that user applications could bind to.
static int getRandomPortNumber(int min, int max)
          Returns a random local port number, greater than min and lower than max.
static int getRandomPortNumber(int min, int max, boolean pair)
          Returns a random local port number, greater than min and lower than max.
static SRVRecord getSRVRecord(String service, String proto, String domain)
          Returns an InetSocketAddress representing the first SRV record available for the specified domain or null if there are not SRV records for domain.
static SRVRecord[] getSRVRecords(String domain)
          Returns array of hosts from the SRV record of the specified domain.
static SRVRecord[] getSRVRecords(String service, String proto, String domain)
          Returns an InetSocketAddress representing the first SRV record available for the specified domain or null if there are not SRV records for domain.
static boolean isIPv4Address(String address)
          Verifies whether address could be an IPv4 address string.
static boolean isIPv6Address(String address)
          Verifies whether address could be an IPv6 address string.
static boolean isLinkLocalIPv4Address(InetAddress add)
          Determines whether the address is an IPv4 link local address.
static boolean isValidIPAddress(String address)
          Checks whether address is a valid IP address string.
static boolean isValidPortNumber(int port)
          Determines whether port is a valid port number bindable by an application (i.e.
static boolean isWindowsAutoConfiguredIPv4Address(InetAddress add)
          Determines whether the address is the result of windows auto configuration.
static byte[] mappedIPv4ToRealIPv4(byte[] addr)
          Returns an IPv4 address matching the one mapped in the IPv6 addr.
static void reloadDnsResolverConfig()
          Reloads dns server configuration in the resolver.
static byte[] strToIPv4(String ipv4AddrStr)
          Creates a byte array containing the specified ipv4AddStr.
static byte[] strToIPv6(String ipv6AddrStr)
          Creates a byte array containing the specified ipv6AddStr.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

IN6_ADDR_ANY

public static final String IN6_ADDR_ANY
A string containing the "any" local address for IPv6.

See Also:
Constant Field Values

IN4_ADDR_ANY

public static final String IN4_ADDR_ANY
A string containing the "any" local address for IPv4.

See Also:
Constant Field Values

IN_ADDR_ANY

public static final String IN_ADDR_ANY
A string containing the "any" local address.


MAX_PORT_NUMBER

public static final int MAX_PORT_NUMBER
The maximum int value that could correspond to a port number.

See Also:
Constant Field Values

MIN_PORT_NUMBER

public static final int MIN_PORT_NUMBER
The minimum int value that could correspond to a port number bindable by the SIP Communicator.

See Also:
Constant Field Values

PNAME_BACKUP_RESOLVER

public static final String PNAME_BACKUP_RESOLVER
The name of the property that users may use to override the address of our backup DNS resolver.

See Also:
Constant Field Values

PNAME_BACKUP_RESOLVER_ENABLED

public static final String PNAME_BACKUP_RESOLVER_ENABLED
The name of the property that users may use to disable our backup DNS resolver.

See Also:
Constant Field Values

PDEFAULT_BACKUP_RESOLVER_ENABLED

public static final boolean PDEFAULT_BACKUP_RESOLVER_ENABLED
The default of the property that users may use to disable our backup DNS resolver.

See Also:
Constant Field Values

PNAME_BACKUP_RESOLVER_PORT

public static final String PNAME_BACKUP_RESOLVER_PORT
The name of the property that users may use to override the port of our backup DNS resolver.

See Also:
Constant Field Values

DEFAULT_BACKUP_RESOLVER

public static final String DEFAULT_BACKUP_RESOLVER
The address of the backup resolver we would use by default.

See Also:
Constant Field Values

PNAME_BACKUP_RESOLVER_FALLBACK_IP

public static final String PNAME_BACKUP_RESOLVER_FALLBACK_IP
The name of the property that users may use to override the IP address of our backup DNS resolver. This is only used when the backup resolver name cannot be determined.

See Also:
Constant Field Values

PNAME_DNS_ALWAYS_ABSOLUTE

public static final String PNAME_DNS_ALWAYS_ABSOLUTE
The name of the boolean property that defines whether all domain names looked up from Jitsi should be treated as absolute.

See Also:
Constant Field Values

PDEFAULT_DNS_ALWAYS_ABSOLUTE

public static final boolean PDEFAULT_DNS_ALWAYS_ABSOLUTE
Default value of PNAME_DNS_ALWAYS_ABSOLUTE.

See Also:
Constant Field Values
Constructor Detail

NetworkUtils

public NetworkUtils()
Method Detail

isWindowsAutoConfiguredIPv4Address

public static boolean isWindowsAutoConfiguredIPv4Address(InetAddress add)
Determines whether the address is the result of windows auto configuration. (i.e. One that is in the 169.254.0.0 network)

Parameters:
add - the address to inspect
Returns:
true if the address is autoconfigured by windows, false otherwise.

isLinkLocalIPv4Address

public static boolean isLinkLocalIPv4Address(InetAddress add)
Determines whether the address is an IPv4 link local address. IPv4 link local addresses are those in the following networks: 10.0.0.0 to 10.255.255.255 172.16.0.0 to 172.31.255.255 192.168.0.0 to 192.168.255.255

Parameters:
add - the address to inspect
Returns:
true if add is a link local ipv4 address and false if not.

getRandomPortNumber

public static int getRandomPortNumber()
Returns a random local port number that user applications could bind to. (i.e. above 1024).

Returns:
a random int located between 1024 and 65 535.

getRandomPortNumber

public static int getRandomPortNumber(int min,
                                      int max)
Returns a random local port number, greater than min and lower than max.

Parameters:
min - the minimum allowed value for the returned port number.
max - the maximum allowed value for the returned port number.
Returns:
a random int located between greater than min and lower than max.

getRandomPortNumber

public static int getRandomPortNumber(int min,
                                      int max,
                                      boolean pair)
Returns a random local port number, greater than min and lower than max. If the pair flag is set to true, then the returned port number is guaranteed to be pair. This is useful for protocols that require this such as RTP

Parameters:
min - the minimum allowed value for the returned port number.
max - the maximum allowed value for the returned port number.
pair - specifies whether the caller would like the returned port to be pair.
Returns:
a random int located between greater than min and lower than max.

isIPv4Address

public static boolean isIPv4Address(String address)
Verifies whether address could be an IPv4 address string.

Parameters:
address - the String that we'd like to determine as an IPv4 address.
Returns:
true if the address contained by address is an IPv4 address and false otherwise.

isIPv6Address

public static boolean isIPv6Address(String address)
Verifies whether address could be an IPv6 address string.

Parameters:
address - the String that we'd like to determine as an IPv6 address.
Returns:
true if the address contained by address is an IPv6 address and false otherwise.

isValidIPAddress

public static boolean isValidIPAddress(String address)
Checks whether address is a valid IP address string.

Parameters:
address - the address that we'd like to check
Returns:
true if address is an IPv4 or IPv6 address and false otherwise.

strToIPv4

public static byte[] strToIPv4(String ipv4AddrStr)
Creates a byte array containing the specified ipv4AddStr.

Parameters:
ipv4AddrStr - a String containing an IPv4 address.
Returns:
a byte array containing the four bytes of the address represented by ipv4AddrStr or null if ipv4AddrStr does not contain a valid IPv4 address string.

strToIPv6

public static byte[] strToIPv6(String ipv6AddrStr)
Creates a byte array containing the specified ipv6AddStr.

Parameters:
ipv6AddrStr - a String containing an IPv6 address.
Returns:
a byte array containing the four bytes of the address represented by ipv6AddrStr or null if ipv6AddrStr does not contain a valid IPv6 address string.

getSRVRecords

public static SRVRecord[] getSRVRecords(String domain)
                                 throws ParseException,
                                        DnssecException
Returns array of hosts from the SRV record of the specified domain. The records are ordered against the SRV record priority

Parameters:
domain - the name of the domain we'd like to resolve (_proto._tcp included).
Returns:
an array of SRVRecord containing records returned by the DNS server - address and port .
Throws:
ParseException - if domain is not a valid domain name.
DnssecException - when a DNSSEC validation failure occurred.

getSRVRecord

public static SRVRecord getSRVRecord(String service,
                                     String proto,
                                     String domain)
                              throws ParseException,
                                     DnssecException
Returns an InetSocketAddress representing the first SRV record available for the specified domain or null if there are not SRV records for domain.

Parameters:
domain - the name of the domain we'd like to resolve.
service - the service that we are trying to get a record for.
proto - the protocol that we'd like service on.
Returns:
the first InetSocketAddress containing records returned by the DNS server - address and port .
Throws:
ParseException - if domain is not a valid domain name.
DnssecException - when a DNSSEC validation failure occurred.

getSRVRecords

public static SRVRecord[] getSRVRecords(String service,
                                        String proto,
                                        String domain)
                                 throws ParseException,
                                        DnssecException
Returns an InetSocketAddress representing the first SRV record available for the specified domain or null if there are not SRV records for domain.

Parameters:
domain - the name of the domain we'd like to resolve.
service - the service that we are trying to get a record for.
proto - the protocol that we'd like service on.
Returns:
the InetSocketAddress[] containing records returned by the DNS server - address and port .
Throws:
ParseException - if domain is not a valid domain name.
DnssecException - when a DNSSEC validation failure occurred.

getNAPTRRecords

public static String[][] getNAPTRRecords(String domain)
                                  throws ParseException,
                                         DnssecException
Makes a NAPTR query and returns the result. The returned records are an array of [Order, Service(Transport) and Replacement (the srv to query for servers and ports)] this all for supplied domain.

Parameters:
domain - the name of the domain we'd like to resolve.
Returns:
an array with the values or null if no records found.
Throws:
ParseException - if domain is not a valid domain name.
DnssecException - when a DNSSEC validation failure occurred.

getInetAddress

public static InetAddress getInetAddress(String hostAddress)
                                  throws UnknownHostException
Creates an InetAddress from the specified hostAddress. The point of using the method rather than creating the address by yourself is that it would first check whether the specified hostAddress is indeed a valid ip address. It this is the case, the method would create the InetAddress using the InetAddress.getByAddress() method so that no DNS resolution is attempted by the JRE. Otherwise it would simply use InetAddress.getByName() so that we would an InetAddress instance even at the cost of a potential DNS resolution.

Parameters:
hostAddress - the String representation of the address that we would like to create an InetAddress instance for.
Returns:
an InetAddress instance corresponding to the specified hostAddress.
Throws:
UnknownHostException - if any of the InetAddress methods we are using throw an exception.

getAandAAAARecords

public static InetSocketAddress[] getAandAAAARecords(String domain,
                                                     int port)
                                              throws ParseException,
                                                     DnssecException
Returns array of hosts from the A and AAAA records of the specified domain. The records are ordered against the IPv4/IPv6 protocol priority

Parameters:
domain - the name of the domain we'd like to resolve.
port - the port number of the returned InetSocketAddress
Returns:
an array of InetSocketAddress containing records returned by the DNS server - address and port .
Throws:
ParseException - if domain is not a valid domain name.
DnssecException - when a DNSSEC validation failure occurred.

getARecord

public static InetSocketAddress getARecord(String domain,
                                           int port)
                                    throws ParseException,
                                           DnssecException
Returns array of hosts from the A record of the specified domain. The records are ordered against the A record priority

Parameters:
domain - the name of the domain we'd like to resolve.
port - the port number of the returned InetSocketAddress
Returns:
an array of InetSocketAddress containing records returned by the DNS server - address and port .
Throws:
ParseException - if domain is not a valid domain name.
DnssecException - when a DNSSEC validation failure occurred.

getAAAARecord

public static InetSocketAddress getAAAARecord(String domain,
                                              int port)
                                       throws ParseException,
                                              DnssecException
Returns array of hosts from the AAAA record of the specified domain. The records are ordered against the AAAA record priority

Parameters:
domain - the name of the domain we'd like to resolve.
port - the port number of the returned InetSocketAddress
Returns:
an array of InetSocketAddress containing records returned by the DNS server - address and port .
Throws:
ParseException - if domain is not a valid domain name.
DnssecException

isValidPortNumber

public static boolean isValidPortNumber(int port)
Determines whether port is a valid port number bindable by an application (i.e. an integer between 1024 and 65535).

Parameters:
port - the port number that we'd like verified.
Returns:
true if port is a valid and bindable port number and alse otherwise.

mappedIPv4ToRealIPv4

public static byte[] mappedIPv4ToRealIPv4(byte[] addr)
Returns an IPv4 address matching the one mapped in the IPv6 addr. Both input and returned value are in network order.

Parameters:
addr - a String representing an IPv4-Mapped address in textual format
Returns:
a byte array numerically representing the IPv4 address

getDefaultDnsPort

public static short getDefaultDnsPort()
Gets the default port used by DNS servers obtained through SimpleResolver.DEFAULT_PORT.

Returns:
The default DNS server port

reloadDnsResolverConfig

public static void reloadDnsResolverConfig()
Reloads dns server configuration in the resolver.


compareDnsNames

public static int compareDnsNames(String dns1,
                                  String dns2)
                           throws ParseException
Compares two DNS names against each other. Helper method to avoid the export of DNSJava.

Parameters:
dns1 - The first DNS name
dns2 - The DNS name that is compared against dns1
Returns:
The value 0 if dns2 is a name equivalent to dns1; a value less than 0 if dns2 is less than dns1 in the canonical ordering, and a value greater than 0 if dns2 is greater than dns1 in the canonical ordering.
Throws:
ParseException - if the dns1 or dns2 is not a DNS Name

Jitsi: the OpenSource Java VoIP and Instant Messaging client.

Jitsi, the OpenSource Java VoIP and Instant Messaging client.
Distributable under LGPL license.