📄 networkconfigurationdiscoveryprocess.java
字号:
report.setPublicAddress(mappedAddress);
if (mappedAddress.equals(apDescriptor.getAddress()))
{
evt = doTestII(serverAddress);
if (evt == null)
{
//Sym UDP Firewall
report.setNatType(StunDiscoveryReport.SYMMETRIC_UDP_FIREWALL);
return report;
}
else
{
//open internet
report.setNatType(StunDiscoveryReport.OPEN_INTERNET);
return report;
}
}
else
{
evt = doTestII(serverAddress);
if (evt == null)
{
evt = doTestI(backupServerAddress);
if(evt == null)
{
logger.info("Failed to receive a response from backup stun server!");
return report;
}
StunAddress mappedAddress2 =
((MappedAddressAttribute)evt.getMessage().
getAttribute(Attribute.MAPPED_ADDRESS)).getAddress();
if(mappedAddress.equals(mappedAddress2))
{
evt = doTestIII(serverAddress);
if(evt == null)
{
//port restricted cone
report.setNatType(StunDiscoveryReport.PORT_RESTRICTED_CONE_NAT);
return report;
}
else
{
//restricted cone
report.setNatType(StunDiscoveryReport.RESTRICTED_CONE_NAT);
return report;
}
}
else
{
//Symmetric NAT
report.setNatType(StunDiscoveryReport.SYMMETRIC_NAT);
return report;
}
}
else
{
//full cone
report.setNatType(StunDiscoveryReport.FULL_CONE_NAT);
return report;
}
}
}
}
/**
* Sends a binding request to the specified server address. Both change IP
* and change port flags are set to false.
* @param serverAddress the address where to send the bindingRequest.
* @return The returned message encapsulating event or null if no message
* was received.
* @throws StunException if an exception occurs while sending the messge
*/
private StunMessageEvent doTestI(StunAddress serverAddress)
throws StunException
{
Request request = MessageFactory.createBindingRequest();
ChangeRequestAttribute changeRequest = (ChangeRequestAttribute)request.getAttribute(Attribute.CHANGE_REQUEST);
changeRequest.setChangeIpFlag(false);
changeRequest.setChangePortFlag(false);
StunMessageEvent evt =
requestSender.sendRequestAndWaitForResponse(request, serverAddress);
if(evt != null)
logger.info("TEST I res="+evt.getRemoteAddress().toString()
+" - "+ evt.getRemoteAddress().getHostName());
else
logger.info("NO RESPONSE received to TEST I.");
return evt;
}
/**
* Sends a binding request to the specified server address with both change
* IP and change port flags are set to true.
* @param serverAddress the address where to send the bindingRequest.
* @return The returned message encapsulating event or null if no message
* was received.
* @throws StunException if an exception occurs while sending the messge
*/
private StunMessageEvent doTestII(StunAddress serverAddress)
throws StunException
{
Request request = MessageFactory.createBindingRequest();
ChangeRequestAttribute changeRequest = (ChangeRequestAttribute)request.getAttribute(Attribute.CHANGE_REQUEST);
changeRequest.setChangeIpFlag(true);
changeRequest.setChangePortFlag(true);
StunMessageEvent evt =
requestSender.sendRequestAndWaitForResponse(request, serverAddress);
if(evt != null)
logger.info("Test II res="+evt.getRemoteAddress().toString()
+" - "+ evt.getRemoteAddress().getHostName());
else
logger.info("NO RESPONSE received to Test II.");
return evt;
}
/**
* Sends a binding request to the specified server address with only change
* port flag set to true and change IP flag - to false.
* @param serverAddress the address where to send the bindingRequest.
* @return The returned message encapsulating event or null if no message
* was received.
* @throws StunException if an exception occurs while sending the messge
*/
private StunMessageEvent doTestIII(StunAddress serverAddress)
throws StunException
{
Request request = MessageFactory.createBindingRequest();
ChangeRequestAttribute changeRequest = (ChangeRequestAttribute)request.getAttribute(Attribute.CHANGE_REQUEST);
changeRequest.setChangeIpFlag(false);
changeRequest.setChangePortFlag(true);
StunMessageEvent evt =
requestSender.sendRequestAndWaitForResponse(request, serverAddress);
if(evt != null)
logger.info("Test III res="+evt.getRemoteAddress().toString()
+" - "+ evt.getRemoteAddress().getHostName());
else
logger.info("NO RESPONSE received to Test III.");
return evt;
}
/**
* Makes shure the discoverer is operational and throws an
* StunException.ILLEGAL_STATE if that is not the case.
* @throws StunException ILLEGAL_STATE if the discoverer is not operational.
*/
private void checkStarted()
throws StunException
{
if(!started)
throw new StunException(StunException.ILLEGAL_STATE,
"The Discoverer must be started before "
+"launching the discovery process!");
}
//---------- main
/**
* Runs the discoverer and shows a message dialog with the returned report.
* @param args args[0] - stun server address, args[1] - port. in the case of
* no args - defaults are provided.
* @throws java.lang.Exception if an exception occurrs during the discovery
* process.
*/
public static void main(String[] args)
throws Exception
{
StunAddress localAddr = null;
StunAddress serverAddr = null;
if(args.length == 4)
{
localAddr = new StunAddress(args[2], Integer.valueOf(args[3]).intValue());
serverAddr = new StunAddress(args[0],
Integer.valueOf(args[1]).intValue());
}
else
{
localAddr = new StunAddress(InetAddress.getLocalHost(), 5678);
serverAddr = new StunAddress("stun01bak.sipphone.com.", 3479);
}
NetworkConfigurationDiscoveryProcess addressDiscovery =
new NetworkConfigurationDiscoveryProcess(localAddr, serverAddr);
addressDiscovery.start();
StunDiscoveryReport report = addressDiscovery.determineAddress();
System.out.println(report);
// javax.swing.JOptionPane.showMessageDialog(
// null,
// report.toString(),
// "Stun Discovery Process",
// javax.swing.JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
/**
* Sample run results.
*
* TEST I res=/69.0.209.22:3478 - stun01bak.sipphone.com
* mapped address is=193.108.24.226./193.108.24.226:5678, name=193.108.24.226.
* backup server address is=69.0.208.27./69.0.208.27:3478, name=69.0.208.27.
* NO RESPONSE received to Test II.
* TEST I res=/69.0.208.27:3478 - stun01.sipphone.com
* NO RESPONSE received to Test III.
* The detected network configuration is: Port Restricted Cone NAT
* Your mapped public address is: 193.108.24.226./193.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -