offbynull / portmapper

Java library that maps ports on NAT-enabled routers (supported protocols: UPnP-IGD/NAT-PMP/PCP).
Apache License 2.0
88 stars 17 forks source link

java.net.BindException: Cannot assign requested address #29

Closed lpetrovic05 closed 7 years ago

lpetrovic05 commented 7 years ago

Hi, I have an issue while running the portmapper on a Raspberry Pi running the Raspbian OS. It throws an exception and it stops working. Here is the log, I added the first line to see on which interface is was failing.

--------- Atempting to bind address /fe80:0:0:0:2a0b:edd3:a126:63d%wlan0 [Network IO] ERROR com.offbynull.portmapper.gateways.network.NetworkRunnable - Encountered unexpected exception java.net.BindException: Cannot assign requested address at sun.nio.ch.Net.bind0(Native Method) at sun.nio.ch.Net.bind(Net.java:433) at sun.nio.ch.DatagramChannelImpl.bind(DatagramChannelImpl.java:691) at sun.nio.ch.DatagramSocketAdaptor.bind(DatagramSocketAdaptor.java:91) at com.offbynull.portmapper.gateways.network.NetworkRunnable.processMessage(NetworkRunnable.java:330) at com.offbynull.portmapper.gateways.network.NetworkRunnable.run(NetworkRunnable.java:127) at java.lang.Thread.run(Thread.java:745) Exception in thread "Network IO" java.lang.RuntimeException: java.net.BindException: Cannot assign requested address at com.offbynull.portmapper.gateways.network.NetworkRunnable.run(NetworkRunnable.java:134) at java.lang.Thread.run(Thread.java:745) Caused by: java.net.BindException: Cannot assign requested address at sun.nio.ch.Net.bind0(Native Method) at sun.nio.ch.Net.bind(Net.java:433) at sun.nio.ch.DatagramChannelImpl.bind(DatagramChannelImpl.java:691) at sun.nio.ch.DatagramSocketAdaptor.bind(DatagramSocketAdaptor.java:91) at com.offbynull.portmapper.gateways.network.NetworkRunnable.processMessage(NetworkRunnable.java:330) at com.offbynull.portmapper.gateways.network.NetworkRunnable.run(NetworkRunnable.java:127) ... 1 more [main] INFO com.offbynull.portmapper.mappers.natpmp.NatPmpPortMapper - Attempting to identify devices [Process IO] ERROR com.offbynull.portmapper.gateways.process.ProcessRunnable - Unable to create process java.io.IOException: Cannot run program "ipconfig": error=2, No such file or directory at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048) at com.offbynull.portmapper.gateways.process.ProcessRunnable.processMessage(ProcessRunnable.java:106) at com.offbynull.portmapper.gateways.process.ProcessRunnable.run(ProcessRunnable.java:67) at java.lang.Thread.run(Thread.java:745) Caused by: java.io.IOException: error=2, No such file or directory at java.lang.UNIXProcess.forkAndExec(Native Method) at java.lang.UNIXProcess.(UNIXProcess.java:248) at java.lang.ProcessImpl.start(ProcessImpl.java:134) at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029) ... 3 more Exception in thread "main" java.lang.IllegalStateException: The validated state is false at org.apache.commons.lang3.Validate.validState(Validate.java:804) at com.offbynull.portmapper.mapper.MapperIoUtils.getLocalIpAddresses(MapperIoUtils.java:545) at com.offbynull.portmapper.mappers.natpmp.NatPmpPortMapper.identify(NatPmpPortMapper.java:142) at com.offbynull.portmapper.PortMapperFactory.discover(PortMapperFactory.java:61) at Test.main(Test.java:24)

offbynull commented 7 years ago

I'll give this a try on my raspberry pi at home tonight.

Please paste the code you used + jre version you used.

On Nov 15, 2016 8:58 AM, "lpetrovic05" notifications@github.com wrote:

Hi, I have an issue while running the portmapper on a Raspberry Pi running the Raspbian OS. It throws an exception and it stops working. Here is the log, I added the first line to see on which interface is was failing.

--------- Atempting to bind address /fe80:0:0:0:2a0b:edd3:a126:63d%wlan0 [Network IO] ERROR com.offbynull.portmapper.gateways.network.NetworkRunnable

  • Encountered unexpected exception java.net.BindException: Cannot assign requested address at sun.nio.ch.Net.bind0(Native Method) at sun.nio.ch.Net.bind(Net.java:433) at sun.nio.ch.DatagramChannelImpl.bind(DatagramChannelImpl.java:691) at sun.nio.ch.DatagramSocketAdaptor.bind(DatagramSocketAdaptor.java:91) at com.offbynull.portmapper.gateways.network.NetworkRunnable. processMessage(NetworkRunnable.java:330) at com.offbynull.portmapper.gateways.network.NetworkRunnable.run( NetworkRunnable.java:127) at java.lang.Thread.run(Thread.java:745) Exception in thread "Network IO" java.lang.RuntimeException: java.net.BindException: Cannot assign requested address at com.offbynull.portmapper.gateways.network.NetworkRunnable.run( NetworkRunnable.java:134) at java.lang.Thread.run(Thread.java:745) Caused by: java.net.BindException: Cannot assign requested address at sun.nio.ch.Net.bind0(Native Method) at sun.nio.ch.Net.bind(Net.java:433) at sun.nio.ch.DatagramChannelImpl.bind(DatagramChannelImpl.java:691) at sun.nio.ch.DatagramSocketAdaptor.bind(DatagramSocketAdaptor.java:91) at com.offbynull.portmapper.gateways.network.NetworkRunnable. processMessage(NetworkRunnable.java:330) at com.offbynull.portmapper.gateways.network.NetworkRunnable.run( NetworkRunnable.java:127) ... 1 more [main] INFO com.offbynull.portmapper.mappers.natpmp.NatPmpPortMapper - Attempting to identify devices [Process IO] ERROR com.offbynull.portmapper.gateways.process.ProcessRunnable
  • Unable to create process java.io.IOException: Cannot run program "ipconfig": error=2, No such file or directory at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048) at com.offbynull.portmapper.gateways.process.ProcessRunnable. processMessage(ProcessRunnable.java:106) at com.offbynull.portmapper.gateways.process.ProcessRunnable.run( ProcessRunnable.java:67) at java.lang.Thread.run(Thread.java:745) Caused by: java.io.IOException: error=2, No such file or directory at java.lang.UNIXProcess.forkAndExec(Native Method) at java.lang.UNIXProcess.(UNIXProcess.java:248) at java.lang.ProcessImpl.start(ProcessImpl.java:134) at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029) ... 3 more Exception in thread "main" java.lang.IllegalStateException: The validated state is false at org.apache.commons.lang3.Validate.validState(Validate.java:804) at com.offbynull.portmapper.mapper.MapperIoUtils.getLocalIpAddresses( MapperIoUtils.java:545) at com.offbynull.portmapper.mappers.natpmp.NatPmpPortMapper.identify( NatPmpPortMapper.java:142) at com.offbynull.portmapper.PortMapperFactory.discover( PortMapperFactory.java:61) at Test.main(Test.java:24)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/offbynull/portmapper/issues/29, or mute the thread https://github.com/notifications/unsubscribe-auth/AGD4IlscRNEdQ7dEiKqJPeQPWo1jRJ5sks5q-eSmgaJpZM4KyxJu .

lpetrovic05 commented 7 years ago

JRE: java version "1.8.0_65" Java(TM) SE Runtime Environment (build 1.8.0_65-b17) Java HotSpot(TM) Client VM (build 25.65-b01, mixed mode)

Code: try { // Start gateways Gateway network = NetworkGateway.create(); Gateway process = ProcessGateway.create(); Bus networkBus = network.getBus(); Bus processBus = process.getBus();

        // Discover port forwarding devices and take the first one found
        List<PortMapper> mappers = PortMapperFactory.discover(networkBus, processBus);
        PortMapper mapper = mappers.get(0);

        // Map internal port 12345 to some external port (55555 preferred)
        //
        // IMPORTANT NOTE: Many devices prevent you from mapping ports that are <= 1024
        // (both internal and external ports). Be mindful of this when choosing which
        // ports you want to map.
        MappedPort mappedPort = mapper.mapPort(PortType.UDP, 50001, 50001, 60);
        System.out.println("Port mapping added: " + mappedPort);

        // Refresh mapping half-way through the lifetime of the mapping (for example,
        // if the mapping is available for 40 seconds, refresh it every 20 seconds)

// while(true) { // mappedPort = mapper.refreshPort(mappedPort, mappedPort.getLifetime() / 2L); // System.out.println("Port mapping refreshed: " + mappedPort); // Thread.sleep(mappedPort.getLifetime() * 1000L); // }

        // Unmap port 12345
        mapper.unmapPort(mappedPort);

        // Stop gateways
        networkBus.send(new KillNetworkRequest());
        processBus.send(new KillProcessRequest()); // can kill this after discovery
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
offbynull commented 7 years ago

I ran your code on my raspberry pi. It's running "Raspbian GNU/Linux 7 (wheezy)". The output is below.

Are you using the latest version of the lib? 2.0.4? Some of the line numbers in your stack trace aren't matching up.

pi@raspberrypi ~/test $ mvn exec:java -Dexec.mainClass="Main"
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building test 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.5.0:java (default-cli) @ test ---
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Port mapping added: NatPmpMappedPort{internalPort=12345, externalPort=55555, externalAddress=/216.19.189.212, portType=TCP, lifetime=60}
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 21.961 s
[INFO] Finished at: 2016-11-17T03:42:29+00:00
[INFO] Final Memory: 7M/17M
[INFO] ------------------------------------------------------------------------
pi@raspberrypi ~/test $
lpetrovic05 commented 7 years ago

I am using the latest version of the lib and Raspbian GNU/Linux 8 (jessie). The problem seems to be that the wlan0 is not connected, the pi is connected through eth0. When I connect the wlan0 as well, the problem goes away.

offbynull commented 7 years ago
  1. Are you absolutely sure you're using the latest version? like I said, the line numbers in the stack trace are off... NetworkRunnable.java:330 is an empty line, an old version of the file called bind() at 330.
  2. What do you mean by "connect"? Do you mean you unplugged the USB wifi dongle?
offbynull commented 7 years ago

Does the application lock up or does it exit with an exception?

offbynull commented 7 years ago

If it does exit, I'll try making a small patch to the lib tonight to see if it helps. Also, if you have full logs, please post them on pastebin and link here.

lpetrovic05 commented 7 years ago

1) I am using the latest version, I just added a println() to see which address it was failing on. 2) The wifi interface is part of the Raspberry Pi, it's not a dongle, it just isn't connected to any network. 3) The application exits with an exception.

offbynull commented 7 years ago

I have a pi2, not a pi3, so I don't have the builtin wifi. But, I tried making some changes that may help.

Can you please do the following...

  1. git clone https://github.com/offbynull/portmapper.git
  2. cd portmapper
  3. mvn clean install

Then in your project's POM, reference the version you just built (2.0.5-SNAPSHOT).

The latest commit made the following changes:

Increase the wait times for local IPs to 10 seconds (from 3 seocnds). Surround NetworkInterface access calls in try/catch so that 1 failure doesn't break the entire process. If NetworkInterface.isUp() is false, it'll skip that interface.

Give that a try and let me know if it helps. If not, you'll have to wait for me to order a pi3.

lpetrovic05 commented 7 years ago

This fixed the problem, thanks

offbynull commented 7 years ago

I'll cut a new release this weekend.