📄 upnpsswanconnectionimpl.java
字号:
try{
listener.mappingResult( this, ok );
}catch( Throwable e){
Debug.printStackTrace(e);
}
}
}
try{
class_mon.enter();
Iterator it = mappings.iterator();
while( it.hasNext()){
portMapping m = (portMapping)it.next();
if ( m.getExternalPort() == port && m.isTCP() == tcp ){
it.remove();
}
}
mappings.add( new portMapping( port, tcp, "", description ));
}finally{
class_mon.exit();
}
}
}
public void
deletePortMapping(
boolean tcp,
int port )
throws UPnPException
{
UPnPAction act = service.getAction( "DeletePortMapping" );
if ( act == null ){
log( "Action 'DeletePortMapping' not supported, binding not removed" );
}else{
boolean mapping_found = false;
try{
class_mon.enter();
Iterator it = mappings.iterator();
while( it.hasNext()){
portMapping mapping = (portMapping)it.next();
if ( mapping.getExternalPort() == port &&
mapping.isTCP() == tcp ){
it.remove();
mapping_found = true;
break;
}
}
}finally{
class_mon.exit();
}
try{
long start = SystemTime.getCurrentTime();
UPnPActionInvocation inv = act.getInvocation();
inv.addArgument( "NewRemoteHost", "" ); // "" = wildcard for hosts, 0 = wildcard for ports
inv.addArgument( "NewProtocol", tcp?"TCP":"UDP" );
inv.addArgument( "NewExternalPort", "" + port );
inv.invoke();
long elapsed = SystemTime.getCurrentTime() - start;
if ( elapsed > 4000 ){
String info = service.getDevice().getRootDevice().getInfo();
((UPnPImpl)service.getDevice().getRootDevice().getUPnP()).logAlert(
"UPnP device '" + info + "' is taking a long time to release port mappings, consider disabling this via the UPnP configuration.",
false,
UPnPLogListener.TYPE_ONCE_EVER );
}
}catch( UPnPException e ){
// only bitch about the failure if we believed we mapped it in the first place
if ( mapping_found ){
throw( e );
}else{
log( "Removal of mapping failed but not established explicitly so ignoring error" );
}
}
}
}
public UPnPWANConnectionPortMapping[]
getPortMappings()
throws UPnPException
{
boolean ok = true;
try{
//UPnPStateVariable noe = service.getStateVariable("PortMappingNumberOfEntries");
//System.out.println( "NOE = " + noe.getValue());
int entries = 0; //Integer.parseInt( noe.getValue());
// some routers (e.g. Gudy's) return 0 here whatever!
// In this case take mindless approach
// hmm, even for my router the state variable isn't accurate...
UPnPAction act = service.getAction( "GetGenericPortMappingEntry" );
if ( act == null ){
log( "Action 'GetGenericPortMappingEntry' not supported, can't enumerate bindings" );
return( new UPnPWANConnectionPortMapping[0] );
}else{
List res = new ArrayList();
// I've also seen some routers loop here rather than failing when the index gets too large (they
// seem to keep returning the last entry) - check for a duplicate entry and exit if found
portMapping prev_mapping = null;
for (int i=0;i<(entries==0?512:entries);i++){
UPnPActionInvocation inv = act.getInvocation();
inv.addArgument( "NewPortMappingIndex", "" + i );
try{
UPnPActionArgument[] outs = inv.invoke();
int port = 0;
boolean tcp = false;
String internal_host = null;
String description = "";
for (int j=0;j<outs.length;j++){
UPnPActionArgument out = outs[j];
String out_name = out.getName();
if ( out_name.equalsIgnoreCase("NewExternalPort")){
port = Integer.parseInt( out.getValue());
}else if ( out_name.equalsIgnoreCase( "NewProtocol" )){
tcp = out.getValue().equalsIgnoreCase("TCP");
}else if ( out_name.equalsIgnoreCase( "NewInternalClient" )){
internal_host = out.getValue();
}else if ( out_name.equalsIgnoreCase( "NewPortMappingDescription" )){
description = out.getValue();
}
}
if ( prev_mapping != null ){
if ( prev_mapping.getExternalPort() == port &&
prev_mapping.isTCP() == tcp ){
// repeat, get out
break;
}
}
prev_mapping = new portMapping( port, tcp, internal_host, description );
res.add( prev_mapping );
}catch( UPnPException e ){
if ( entries == 0 ){
break;
}
ok = false;
throw(e);
}
}
UPnPWANConnectionPortMapping[] res2= new UPnPWANConnectionPortMapping[res.size()];
res.toArray( res2 );
return( res2 );
}
}finally{
for (int i=0;i<listeners.size();i++){
UPnPWANConnectionListener listener = (UPnPWANConnectionListener)listeners.get(i);
try{
listener.mappingsReadResult( this, ok );
}catch( Throwable e){
Debug.printStackTrace(e);
}
}
}
}
public String
getExternalIPAddress()
throws UPnPException
{
UPnPAction act = service.getAction( "GetExternalIPAddress" );
if ( act == null ){
log( "Action 'GetExternalIPAddress' not supported, binding not established" );
throw( new UPnPException( "GetExternalIPAddress not supported" ));
}else{
UPnPActionInvocation inv = act.getInvocation();
UPnPActionArgument[] args = inv.invoke();
String ip = null;
for (int i=0;i<args.length;i++){
UPnPActionArgument arg = args[i];
String name = arg.getName();
if ( name.equalsIgnoreCase("NewExternalIPAddress")){
ip = arg.getValue();
}
}
return( ip );
}
}
protected void
log(
String str )
{
service.getDevice().getRootDevice().getUPnP().log( str );
}
public void
addListener(
UPnPWANConnectionListener listener )
{
listeners.add( listener );
}
public void
removeListener(
UPnPWANConnectionListener listener )
{
listeners.add( listener );
}
protected class
portMapping
implements UPnPWANConnectionPortMapping
{
protected int external_port;
protected boolean tcp;
protected String internal_host;
protected String description;
protected
portMapping(
int _external_port,
boolean _tcp,
String _internal_host,
String _description )
{
external_port = _external_port;
tcp = _tcp;
internal_host = _internal_host;
description = _description;
}
public boolean
isTCP()
{
return( tcp );
}
public int
getExternalPort()
{
return( external_port );
}
public String
getInternalHost()
{
return( internal_host );
}
public String
getDescription()
{
return( description );
}
protected String
getString()
{
return( getDescription() + " [" + getExternalPort() + ":" + (isTCP()?"TCP":"UDP") + "]");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -