Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

natpmpc open a random port #29

Open
stokito opened this issue Nov 20, 2022 · 11 comments
Open

natpmpc open a random port #29

stokito opened this issue Nov 20, 2022 · 11 comments

Comments

@stokito
Copy link

stokito commented Nov 20, 2022

I have a webserver on a port 8000 and need to expose it with natpmpc on WAN on any random port. So then I'll simply generate a link to it and share.
But currently I have to specify explicitly the external port:

natpmpc -a 8000 8000 tcp

The problem here is that I don't know if this external 8000 port is not already taken or allowed by the miniupnpd. So that's why I can't rely on this.
Basically the local port 9000 may be also busy and it would be also useful if the natpmpc can also take some random and free port (maybe ephemeral).

Maybe it can be something like:

natpmpc -a rand rand tcp

Is it possible to implement something like this? Or the PCP spec requires to specify the requested port?

Ideally the natpmpc should print the port numbers to stdout in form

loc_port ext_ip ext_port

Then it would be easy to parse with a script.
Any other logging messages of the natpmpc may be printed to stderr so that a script needs to parse only clear stdout.

@miniupnp
Copy link
Owner

Hello. Just for your information NAT-PMP has been superseded by PCP.

If my memory serves me right, to get a random port, you request 0 :
$ natpmpc -a 0 8000 tcp

The effective ports are printed to the console :
Mapped public port %hu protocol %s to local port %hu lifetime %u\n

@stokito
Copy link
Author

stokito commented Jan 28, 2023 via email

@miniupnp
Copy link
Owner

note that miniupnpd uses the internal port if available when asking for a "random" port :
https://github.com/miniupnp/miniupnp/blob/master/miniupnpd/natpmp.c#L336

@stokito
Copy link
Author

stokito commented Jan 28, 2023 via email

@miniupnp
Copy link
Owner

I'm curious to see the log on the miniupnpd side.

@stokito
Copy link
Author

stokito commented Jan 30, 2023 via email

@stokito
Copy link
Author

stokito commented Jan 30, 2023 via email

@miniupnp
Copy link
Owner

Also for me it looks like not a good idea to take the next free port if it was specified but busy. E.g. if the client requested an 8080 port (not just 0) but it was busy then the 8081 port will be opened. But maybe not all needs for this 8081 port but want exactly the 8080

I don't know if it is a good idea, but I think this is the NAT-PMP specification !

@miniupnp
Copy link
Owner

anyway, I think I need to improve the way miniupnpd chooses an available port.

@stokito
Copy link
Author

stokito commented Jan 31, 2023

If this is in spec that's great that we don't have an undefined behavior here.
But still it makes a little sense from user perspective because it's not clear then why to specify the port at all. Amway, changing the behavior here may broke something.

@miniupnp
Copy link
Owner

miniupnp commented Feb 1, 2023

As said before NAT-PMP has been superseded by PCP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants