InetAddress bind_ip,
int bind_port )
throws NetworkAdminException
{
PRUDPReleasablePacketHandler handler = PRUDPPacketHandlerFactory.getReleasableHandler( bind_port );
PRUDPPacketHandler packet_handler = handler.getHandler();
HashMap data_to_send = new HashMap();
PluginInterface pi_upnp = core.getPluginManager().getPluginInterfaceByClass( UPnPPlugin.class );
String upnp_str = null;
if( pi_upnp != null ) {
UPnPPlugin upnp = (UPnPPlugin)pi_upnp.getPlugin();
/*
UPnPMapping mapping = upnp.getMapping( true, port );
if ( mapping == null ) {
new_mapping = mapping = upnp.addMapping( "NAT Tester", true, port, true );
// give UPnP a chance to work
try {
Thread.sleep( 500 );
}
catch (Throwable e) {
Debug.printStackTrace( e );
}
}
*/
UPnPPluginService[] services = upnp.getServices();
if ( services.length > 0 ){
upnp_str = "";
for (int i=0;i<services.length;i++){
UPnPPluginService service = services[i];
upnp_str += (i==0?"":",") + service.getInfo();
}
}
}
if ( upnp_str != null ){
data_to_send.put( "upnp", upnp_str );
}
NetworkAdminASN net_asn = NetworkAdmin.getSingleton().getCurrentASN();
String as = net_asn.getAS();
String asn = net_asn.getASName();
if ( as.length() > 0 ){
data_to_send.put( "as", as );
}
if ( asn.length() > 0 ){
data_to_send.put( "asn", asn );
}
data_to_send.put( "locale", MessageText.getCurrentLocale().toString());
Random random = new Random();
data_to_send.put( "id", new Long( random.nextLong()));
try{
packet_handler.setExplicitBindAddress( bind_ip );
Throwable last_error = null;
long timeout = 5000;
long timeout_inc = 5000;
try{
for (int i=0;i<3;i++){
data_to_send.put( "seq", new Long(i));
try{
// connection ids for requests must always have their msb set...
// apart from the original darn udp tracker spec....
long connection_id = 0x8000000000000000L | random.nextLong();
NetworkAdminNATUDPRequest request_packet = new NetworkAdminNATUDPRequest( connection_id );
request_packet.setPayload( data_to_send );
listener.reportProgress( "Sending outbound packet and waiting for reply probe (timeout=" + timeout + ")" );
NetworkAdminNATUDPReply reply_packet =
(NetworkAdminNATUDPReply)packet_handler.sendAndReceive(
null,
request_packet,
new InetSocketAddress( UDP_SERVER_ADDRESS, UDP_SERVER_PORT ),
timeout,
PRUDPPacketHandler.PRIORITY_IMMEDIATE );
Map reply = reply_packet.getPayload();
byte[] ip_bytes = (byte[])reply.get( "ip_address" );
if ( ip_bytes == null ){
throw( new NetworkAdminException( "IP address missing in reply" ));
}
byte[] reason = (byte[])reply.get( "reason" );
if ( reason != null ) {
throw( new NetworkAdminException( new String( reason, "UTF8")));
}
return( InetAddress.getByAddress( ip_bytes ));
}catch( Throwable e){
last_error = e;
timeout += timeout_inc;
}
}
if ( last_error != null ){
throw( last_error );
}
throw( new NetworkAdminException( "Timeout" ));
}finally{
try{
data_to_send.put( "seq", new Long(99));
long connection_id = 0x8000000000000000L | random.nextLong();
NetworkAdminNATUDPRequest request_packet = new NetworkAdminNATUDPRequest( connection_id );
request_packet.setPayload( data_to_send );
// fire off one last packet in attempt to inform server of completion
listener.reportProgress( "Sending completion event" );
packet_handler.send( request_packet, new InetSocketAddress( UDP_SERVER_ADDRESS, UDP_SERVER_PORT ));
}catch( Throwable e){
}
}
}catch( NetworkAdminException e ){
throw( e );
}catch( Throwable e ){
throw( new NetworkAdminException( "Inbound test failed", e ));
}finally{
packet_handler.setExplicitBindAddress( null );
handler.release();
}
}