Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  UPNP library  (Read 6839 times)
0 Members and 1 Guest are viewing this topic.
Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


« Posted 2010-03-06 13:44:36 »

Computers connected to the internet through a NAT (basically anyone using an ADSL modem and the like) are generally unable to accept connections from the internet. This is rubbish if you want them to be able to host games and so on.
Happily, most home routers are UPNP devices, and so can have their NAT tables manipulated to allow incoming connections to be routed to the correct address on the local network.
Here's a little lib that'll make that process easy. Jar here, javadoc zip here, svn repo here.

Using it to add a port mapping looks like this:
1  
2  
3  
4  
5  
6  
7  
8  
InternetGatewayDevice dev = InternetGatewayDevice.getDevices( 1000 )[ 0 ];
String thisWillBeHandy = dev.getExternalIPAddress();
String connType = "TCP"; // or "UDP"
String remoteHost = null; // allow any remote host to connect
int internetSidePort = 1337;
String localHost = "192.168.1.whatever";
int localPort = 7331;
dev.addPortMapping( "A mapping for my game", connType, remoteHost, internetSidePort, localHost, localPort, 0 );

after this, any TCP connections made to the external IP and port will be forwarded to the local IP and port. Remember to clean up after yourself:
1  
dev.deletePortMapping( null, internetSidePort, connType );


Please note that this is just the library found over here, but brutally cut down so it's 60KB rather than 2MB. If it works, gratitude should be directed towards SuperBonBon, if it doesn't work, it's probably because I broke it persecutioncomplex
Offline DzzD
« Reply #1 - Posted 2010-03-06 13:56:29 »

very nice & usefull, couldn't you cut it a little more Smiley

Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #2 - Posted 2010-03-06 14:33:23 »

couldn't you cut it a little more Smiley
Yeah I could, but I'd have to use tiny electrons, and have you priced those things recently? I'm not made of money!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SimonH
« Reply #3 - Posted 2010-03-06 14:33:53 »

Neat - I'll have a play with this...
60K though!? I don't need it to make the tea as well!

People make games and games make people
Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #4 - Posted 2010-03-06 15:39:17 »

Hah! Turns out genjar was pulling in some xml classes already included in rt.jar.
53K. Happy now? Wink
edit: Also added a main method to the jar - it'll print information on any NAT devices on your local network
Offline Nate

JGO Kernel


Medals: 147
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #5 - Posted 2010-03-06 22:59:18 »

Cool! I need to try this out.

Offline noblemaster

JGO Ninja


Medals: 20
Projects: 10


Age of Conquest makes your day!


« Reply #6 - Posted 2010-03-06 23:03:38 »

Very cool! I definitely need to check this out  Grin

Offline markmistry

Junior Member





« Reply #7 - Posted 2010-03-07 00:26:54 »

OMG i Got it To Compile it works  Grin

Ive been trying for ages to get this to work !

now its time to experiment  Grin Thanks a million !
Offline Momoko_Fan

Junior Member


Medals: 2



« Reply #8 - Posted 2010-03-07 03:36:44 »

Will try to see if it works with my router, .. brb

EDIT: Okay its not working for me for some reason. It can see the device fine through the java but when I use your example to add a port forward, first I can't see it in my router config and 2nd is that I tried testing them (two ports one on 1337 and another on 7331) and neither of them works (I used http://www.seemyport.com/ to test).
Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #9 - Posted 2010-03-07 09:20:56 »

Running this
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
   
public static void main( String[] args )
{
   try
   {
      System.out.println( "looking for device" );
      InternetGatewayDevice dev = getDevices( 1000 )[ 0 ];
      System.out.println( "External IP = " + dev.getExternalIPAddress() );
      System.out.print( "adding mapping : " );
      System.out.println( dev.addPortMapping( "foo", "TCP", null, 1337, "my lan address", 7331, 0 ) );

      System.out.println( "waiting for connection" );
      ServerSocket ss = new ServerSocket( 7331 );
      ss.setSoTimeout( 10000 );
      try
      {
         Socket s = ss.accept();
         InputStream in = s.getInputStream();
         while( in.available() > 0 )
         {
            System.out.println( "byte : " + in.read() );
         }
      }
      catch( SocketTimeoutException stoe )
      {
         System.out.println( "Timed out" );
      }
      System.out.print( "removing mapping : " );
      System.out.println( dev.deletePortMapping( null, 1337, "TCP" ) );
   }
   catch( IOException e )
   {
      e.printStackTrace();
   }
   catch( UPNPResponseException e )
   {
      e.printStackTrace();
   }
}

Works for me when testing port 1337 on the seemyport website. They don't seem to send any data, but they do need a serversocket on your end to accept a connection. I'll add this to the jar's main method.

As to the port mapping not showing up in your router config: does the addPortMapping method return true? Are there any port mappings there already? What is your router make/model?

edit: that's the jar updated now. Run "java -jar upnp.jar <externalPort> <internalPort>", wait for the mapping to be made, and then test <externalPort> with seemyport.com
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline markmistry

Junior Member





« Reply #10 - Posted 2010-03-07 11:11:54 »

it successfully added a port forward  to my router config (belkin 54g cable modem router).
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #11 - Posted 2010-03-07 13:41:55 »

it failed for me Tongue.

Ill try again later, and will mess around with it, but I got to go right now.
Offline Momoko_Fan

Junior Member


Medals: 2



« Reply #12 - Posted 2010-03-07 19:41:10 »

Quote
As to the port mapping not showing up in your router config: does the addPortMapping method return true? Are there any port mappings there already? What is your router make/model?

edit: that's the jar updated now. Run "java -jar upnp.jar <externalPort> <internalPort>", wait for the mapping to be made, and then test <externalPort> with seemyport.com
Okay I tried running the jar, here's the output:
1  
2  
3  
4  
5  
6  
looking for device
adding mapping from 67.80.51.61:6115
        to 192.168.1.102:6114
waiting for connection
        Timed out
removing mapping : true

I tried using seemyport.com after "waiting for connection" on the external port and I get:
1  
Could not connect to 6115 on 67.80.51.61 (Connection timed out). Bummer.

I check my router port forwarding list (its was empty) and the port forward is not there.

My router is Linksys WRT160N firmware v3.0.02.

Okay I tried some other things, I tried using port 1337 and 1338, also tried adding the port forward manually to the router and disabling windows firewall.. a few things happen:
1. disabling firewall causes seemyport.com to respond much faster (no more wait of 3-5 seconds, its instant)
2. after disabling firewall, and when I try adding the port forward manually in the router config I get an exception when trying to upnp the same port:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
looking for device
adding mapping from 67.80.51.61:1337
        to 192.168.1.102:1337
net.sbbi.upnp.messages.UPNPResponseException: Detailed error code :501, Detailed
 error description :Action Failed
        at net.sbbi.upnp.messages.ActionMessageResponseParser.startElement(Actio
nMessageResponseParser.java:189)
        at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startEle
ment(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scan
StartElement(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImp
l$FragmentContentDriver.next(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(U
nknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next
(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImp
l.scanDocument(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(U
nknown Source)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(U
nknown Source)
        at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown So
urce)
        at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Un
known Source)
        at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.p
arse(Unknown Source)
        at javax.xml.parsers.SAXParser.parse(Unknown Source)
        at net.sbbi.upnp.messages.ActionMessage.service(ActionMessage.java:207)
        at net.sbbi.upnp.impls.InternetGatewayDevice.addPortMapping(InternetGate
wayDevice.java:621)
        at net.sbbi.upnp.impls.InternetGatewayDevice.main(InternetGatewayDevice.
java:131)


Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #13 - Posted 2010-03-07 21:01:33 »

You haven't specifically said so, hence I have to ask so we're not chasing our tails on this: You're checking the port mapping table on the router's config pages in the ten seconds between the mapping being made and being deleted right?

Assuming so:
Hrm, from your output it seems that your router is reporting success when you add the mapping, but not actually doing it. Not very helpful Undecided I've had a good trawl through the code and can't see any way for addPortMapping() to return true without the device giving a 200 (HTTP_OK) response.
I guess the thing to do is try another UPNP control point and see if that works, looks like there are windows tools over here that'll let you query your network devices and call actions manually. If that tool works for you, it might be time to bust out wireshark and see what's going over the wire.

The exception you are seeing when you try and add an already-existing mapping is to be expected. The description according to the spec is
Quote
501: Action Failed - May be returned in[sic] current state of service prevents invoking that action.
Which is fair enough under the circumstances - and at least it shows that the router is doing something with the request.
Offline Momoko_Fan

Junior Member


Medals: 2



« Reply #14 - Posted 2010-03-07 23:04:33 »

You haven't specifically said so, hence I have to ask so we're not chasing our tails on this: You're checking the port mapping table on the router's config pages in the ten seconds between the mapping being made and being deleted right?
There are a few "tables" on my router. There's "single port forwarding", "port range forwarding" and "port range triggering". Also there's the "routing table". I checked all of these during the 10 seconds and all don't have the port forward I set up. I also tried the "logging" feature of the router but there's nothing of interest there either.

Assuming so:
Hrm, from your output it seems that your router is reporting success when you add the mapping, but not actually doing it. Not very helpful Undecided I've had a good trawl through the code and can't see any way for addPortMapping() to return true without the device giving a 200 (HTTP_OK) response.
I guess the thing to do is try another UPNP control point and see if that works, looks like there are windows tools over here that'll let you query your network devices and call actions manually. If that tool works for you, it might be time to bust out wireshark and see what's going over the wire.

The exception you are seeing when you try and add an already-existing mapping is to be expected. The description according to the spec isWhich is fair enough under the circumstances - and at least it shows that the router is doing something with the request.
Okay thanks I'll try the tools..brb.

EDIT: Okay I am not sure what am I supposed to be doing.. I can see my device and the values and stuff but how am I supposed to test if port forwarding works? Which tool am I supposed to use?


EDIT2: I think I found the problem.. I don't know how or why, but things started working, but incorrectly, it's really strange.
What I did was try uTorrent, I enabled UPnP there, and then tested using seemyport.com, everything worked. Then I disabled UPnP in uTorrent, chose a different port and set the port forward using the jar, and it worked. wtf? Okay so I close uTorrent down and try doing it with just the jar, I chose yet a different port, then run upnp.jar, try seemyport.com and it tells me this:
1  
2  
Could not connect to 3131 on 67.80.51.61 (Connection refused). Bummer.
It appears you have the port forwarding set up, but nothing is answering on port 3131.

So it seems to me that somehow maybe the jar is incorrectly setting up the listening socket? Why did it start working all of the sudden (or does it even work correctly?). Also, if I close all programs and try seemyport.com with a port that I tried setting up with the jar, it tells me connection refused, but if I use a port I didn't setup with the jar, I get the usual connection timed out.
So, conclusions:
1. somehow something started working after I tried uTorrent with UPnP
2. now the upnp.jar works, but the socket it sets up refuses connections (?)
3. If I use uTorrent with UPnP disabled but use the jar's UPnP for the same port, the connection works fine.
Offline markmistry

Junior Member





« Reply #15 - Posted 2010-03-07 23:28:27 »

Disaster strikes when i try to run the program on my xp machines on the same network

this is my test code..
I imported all the classes but it didnt make a difference
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
import net.sbbi.upnp.*;
import net.sbbi.upnp.impls.*;
import net.sbbi.upnp.devices.*;
import net.sbbi.upnp.services.*;
import net.sbbi.upnp.messages.*;

public class test{

   public static void main(String [] args){
   
      try{
     
         InternetGatewayDevice dev = InternetGatewayDevice.getDevices( 1000 )[ 0 ];
         String thisWillBeHandy = dev.getExternalIPAddress();
         System.out.println(thisWillBeHandy);
      }
     
      catch(Exception a){
     
         System.out.println("oh oh");              
      }  
   }
}



1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(Unknown Source)
        at java.io.BufferedInputStream.fill(Unknown Source)
        at java.io.BufferedInputStream.read1(Unknown Source)
        at java.io.BufferedInputStream.read(Unknown Source)
        at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
        at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
        at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
        at java.net.URL.openStream(Unknown Source)
        at net.sbbi.upnp.XMLUtil.getXMLString(XMLUtil.java:97)
        at net.sbbi.upnp.XMLUtil.getXML(XMLUtil.java:134)
        at net.sbbi.upnp.devices.RootDevice.build(RootDevice.java:119)
        at net.sbbi.upnp.Discovery$1.discoveredDevice(Discovery.java:243)
        at net.sbbi.upnp.DiscoveryListener.listenBroadCast(DiscoveryListener.java:369)
        at net.sbbi.upnp.DiscoveryListener.run(DiscoveryListener.java:241)
        at java.lang.Thread.run(Unknown Source)
java.lang.NullPointerException
        at net.sbbi.upnp.XMLUtil.getXML(XMLUtil.java:135)
        at net.sbbi.upnp.devices.RootDevice.build(RootDevice.java:119)
        at net.sbbi.upnp.Discovery$1.discoveredDevice(Discovery.java:243)
        at net.sbbi.upnp.DiscoveryListener.listenBroadCast(DiscoveryListener.java:369)
        at net.sbbi.upnp.DiscoveryListener.run(DiscoveryListener.java:241)
        at java.lang.Thread.run(Unknown Source)


i am using the jre 6 update 15 i will try again after i have updated to the latest
Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #16 - Posted 2010-03-08 02:19:17 »

EDIT: Okay I am not sure what am I supposed to be doing.. I can see my device and the values and stuff but how am I supposed to test if port forwarding works? Which tool am I supposed to use?
I've only used tools in linux that have been "inspired by" the ones you got from that site, but I think the one you're after is the "Device Spy" program. It should present you with a tree of UPNP devices - each of which contains Services and other Devices. Somewhere in this tree there will be a "addPortMapping" entry (find attached an image of what it looks like on my system). Double-clicking on it should let you set the appropriate arguments and submit the request.
Quote
<utorrent weirdness>
I have got no clue what's going on here. If you've got access to a remote system, try using that to test - then you control how the connection is made. e.g.: Before you mentioned seemyport.com, I was running a connection test from a remote server controlled through ssh.
Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #17 - Posted 2010-03-08 02:37:35 »

Disaster strikes when i try to run the program on my xp machines on the same network

this is my test code..
etc.
First off: import statements only affect compilation. If your code compiles without errors, adding import statements will not make any difference to that code's behaviour.

From the stacktrace you've posted, what seems to have happened is
Quote
upnp.jar: Are there any internet gateway devices out there?
router: yup! you can find my specifications at <url>
upnp.jar: well let me just download that then!
download: ERROR!
upnp.jar: not cool man. not cool.
There seems to be some fundamental problem with getting the router's upnp definition. As with MarkMistry, try using another UPNP control point.
Offline Momoko_Fan

Junior Member


Medals: 2



« Reply #18 - Posted 2010-03-08 02:46:23 »

Okay I found the problem. The jar opens the port forward on the port you ask it in the command line but listens on 7331........ yeah.
1  
Connect to port 7331 on 67.80.51.61 OK!

So it was a bug in the program. I tried Device Spy also and it lets you lookup other mappings that are already there so it's pretty cool. Will play around with it and see what other things I can do.
Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #19 - Posted 2010-03-08 12:19:35 »

My bad persecutioncomplex Fixed now.
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #20 - Posted 2010-03-08 17:36:31 »

My bad persecutioncomplex Fixed now.

haha, that owuld make a differnce

I know the feeling Wink
Offline markmistry

Junior Member





« Reply #21 - Posted 2010-03-09 18:19:55 »

My problem is my router i cant even login to the admin web interface to see whats going on..i have a brand new netgear wireless router to test see if that makes any difference.
Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #22 - Posted 2010-03-09 18:56:24 »

That's going to be tricky. Could be that upnp is just disabled, and the router handles requests in a really passive-aggressive manner. Try with the alternative control point software I linked to earlier. Why can't you log into the admin pages?
Offline markmistry

Junior Member





« Reply #23 - Posted 2010-03-09 19:42:13 »

LOL it was a belkin router it always played up sometimes it would let me login, other times it wouldnt i thought it was because i was running my web server but it was just a rubbish router i have setup my new netgear router and the test code is working perfect again  Grin

But it wont add a port (invalidAction error)theres too many parameters for the netgear

on netgear you need...

Whats it called ?
what type ?
start port ?
end port?
local address ?

going to have a look at the docs see if i can work it out.

EDIT

i took out these lines..
1  
2  
         int nummaps = dev.getNatMappingsCount();
         System.out.println("nummaps = "+ nummaps);


and it succesfully added an entry in the upnp section
Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #24 - Posted 2010-03-09 20:03:37 »

Peculiar - looks like it's trying to do port range forwarding rather than a single port mapping. I've changed the jar so that the device description is printed out. What does that give you?
Offline markmistry

Junior Member





« Reply #25 - Posted 2010-03-09 20:30:42 »

This is the output i got...

EDIT

IT does add an entry and removes it succesfully but it doesnt touch port forwarding..looks to me like it just adds the port mapping Huh

and seemyport couldnt see me  Roll Eyes
Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #26 - Posted 2010-03-11 12:49:56 »

Aha! Your device has both a PPP and IP connection service. I've reinstated some code to handle this that SuperBonBon had commented out as he didn't have a device to test with. Unfortunately it depends on an optional action that your device doesn't support, so I've also added some more code that might work.
Download and run the jar again, post the output and also post your internet connection type (ADSL/Cable/whatever). I reckon we could be getting closer...
Offline markmistry

Junior Member





« Reply #27 - Posted 2010-03-11 16:35:42 »

ok heres the new output file. it added it then it took it away, btw im running on virgin media 10mb cable connection with a Netgear WGR614v9 wireless router. i havent managed to write any code to test out the mapping yet i was wanting to get round to it tonight..
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

UprightPath (20 views)
2014-09-20 20:14:06

BurntPizza (26 views)
2014-09-19 03:14:18

Dwinin (40 views)
2014-09-12 09:08:26

Norakomi (70 views)
2014-09-10 13:57:51

TehJavaDev (95 views)
2014-09-10 06:39:09

Tekkerue (49 views)
2014-09-09 02:24:56

mitcheeb (70 views)
2014-09-08 06:06:29

BurntPizza (52 views)
2014-09-07 01:13:42

Longarmx (39 views)
2014-09-07 01:12:14

Longarmx (45 views)
2014-09-07 01:11:22
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!