Methods Summary |
---|
public java.net.InetAddress | getExternalAddress()
return( readExternalAddress());
|
public java.lang.String | getID()
return( id );
|
public java.net.InetAddress | getInternalAddress()
return( internal_address );
|
public int | getTCPListenPort()
return( tcp_port );
|
public int | getUDPListenPort()
return( udp_port );
|
public int | getUDPNonDataListenPort()
return( udp_non_data_port );
|
protected void | readConfig(boolean first_time)
InetAddress new_internal_address = NetworkAdmin.getSingleton().getDefaultBindAddress();
if ( new_internal_address == null ){
try{
new_internal_address = InetAddress.getByName( "0.0.0.0" );
}catch( Throwable e ){
}
}
int new_tcp_port = TCPNetworkManager.getSingleton().getTCPListeningPortNumber();
int new_udp_port = UDPNetworkManager.getSingleton().getUDPListeningPortNumber();
int new_udp_non_data_port = UDPNetworkManager.getSingleton().getUDPNonDataListeningPortNumber();
boolean same = true;
if ( !first_time ){
same = internal_address.equals( new_internal_address) &&
tcp_port == new_tcp_port &&
udp_port == new_udp_port &&
udp_non_data_port == new_udp_non_data_port;
}
internal_address = new_internal_address;
tcp_port = new_tcp_port;
udp_port = new_udp_port;
udp_non_data_port = new_udp_non_data_port;
if ( !same ){
manager.informChanged( this );
}
|
protected java.net.InetAddress | readExternalAddress()
InetAddress external_address = null;
// no point in kicking off any queries if we're closing
if ( manager.isClosing()){
external_address = last_external_address;
if ( external_address == null ){
try{
external_address = InetAddress.getByName("127.0.0.1");
}catch( Throwable e ){
Debug.printStackTrace(e);
}
}
return( external_address );
}
PluginInterface dht_pi = core.getPluginManager().getPluginInterfaceByClass( DHTPlugin.class );
DHTPlugin dht = null;
if ( dht_pi != null ){
dht = (DHTPlugin)dht_pi.getPlugin();
}
// if DHT has informed us of an address then we use this - most reliable up to date one
// unless the version server cache time is more recent
if ( dht_address != null && dht_address_time <= SystemTime.getCurrentTime()){
long cache_time = VersionCheckClient.getSingleton().getCacheTime( false );
if ( cache_time <= dht_address_time ){
external_address = dht_address;
}
}
if ( external_address == null &&
( dht == null || dht.getStatus() != DHTPlugin.STATUS_RUNNING )){
// use cached version if available and the DHT isn't
String str_address = VersionCheckClient.getSingleton().getExternalIpAddress( true, false );
if ( str_address != null ){
try{
external_address = InetAddress.getByName( str_address );
}catch( Throwable e ){
Debug.printStackTrace(e);
}
}
}
if ( external_address == null && dht != null ){
// no cache, use DHT (this will hang during initialisation, hence the use of cached
// version above
try{
external_address = dht.getLocalAddress().getAddress().getAddress();
}catch( Throwable e ){
}
}
long now = SystemTime.getCurrentTime();
if ( last_force_read_ext > now ){
last_force_read_ext = now;
}
boolean ok_to_try_ext = now - last_force_read_ext > FORCE_READ_EXT_MIN;
// try upnp - limit frequency unless external read is possible in which
// case we try upnp first
// currently we only use UPnP to validate our current external address, not
// to deduce new ones (as for example there may be multiple upnp devices and
// we don't know which one to believe
if ( external_address == null && last_external_address != null ){
if ( last_upnp_read > now ){
last_upnp_read = now;
}
if ( now - last_upnp_read > UPNP_READ_MIN || ok_to_try_ext ){
last_upnp_read = now;
try{
PluginInterface upnp_pi = core.getPluginManager().getPluginInterfaceByClass( UPnPPlugin.class );
if ( upnp_pi != null ){
UPnPPlugin upnp = (UPnPPlugin)upnp_pi.getPlugin();
String[] addresses = upnp.getExternalIPAddresses();
for (int i=0;i<addresses.length;i++){
if ( addresses[i].equals( last_external_address.getHostAddress())){
external_address = last_external_address;
break;
}
}
}
}catch( Throwable e ){
}
}
}
if ( external_address == null ){
// force read it
if ( ok_to_try_ext ){
last_force_read_ext = now;
external_address = core.getPluginManager().getDefaultPluginInterface().getUtilities().getPublicAddress();
}
}
// no good address available
if ( external_address == null ){
if ( last_external_address != null ){
external_address = last_external_address;
}else{
try{
external_address = InetAddress.getByName("127.0.0.1");
}catch( Throwable e ){
Debug.printStackTrace(e);
}
}
}else{
last_external_address = external_address;
}
return( external_address );
|