TinyUPnP icon indicating copy to clipboard operation
TinyUPnP copied to clipboard

Option for both local and external port (with code)

Open DennisBorCom opened this issue 4 years ago • 3 comments

Currently this library seems only to support one port number for both local external. It would be nice to be able to specifiy both an internal and external port.

E.g. I'm running a web server at machine X which should me accessible on outside port 80. Also, I'm running an IOT project which runs on port 80 (local), but should have a different external port.

I'm running a modified version of the TinyUPNP library myself, with a couple of little changes:

TinyUPnP.cpp

line 54 from:

void TinyUPnP::addPortMappingConfig(IPAddress ruleIP, int rulePort, String ruleProtocol, int ruleLeaseDuration, String ruleFriendlyName)

to:

void TinyUPnP::addPortMappingConfig(IPAddress ruleIP, int internalPort, int externalPort, String ruleProtocol, int ruleLeaseDuration, String ruleFriendlyName) {

line 59 from:

newUpnpRule->internalPort = rulePort;

to:

newUpnpRule->internalPort = internalPort;

line 60 from:

newUpnpRule->externalPort = rulePort;

to

newUpnpRule->externalPort = externalPort;

line 436 from:

sprintf(integer_string, "%d", rule_ptr->internalPort);

to:

sprintf(integer_string, "%d", rule_ptr->externalPort);

line 810 from:

sprintf(integer_string, "%d", rule_ptr->internalPort);

to:

sprintf(integer_string, "%d", rule_ptr->externalPort);

TinyUPnP.h

line 107 from:

void addPortMappingConfig(IPAddress ruleIP /* can be NULL */, int rulePort, String ruleProtocol, int ruleLeaseDuration, String ruleFriendlyName);

to:

void addPortMappingConfig(IPAddress ruleIP /* can be NULL */ ,int internalPort, int externalPort, String ruleProtocol, int ruleLeaseDuration, String ruleFriendlyName);

Tested and working on my ESP8266 firmware.

DennisBorCom avatar Mar 21 '20 09:03 DennisBorCom

Wanna make the change an I will merge? I think I can get to it next week if not

ofekp avatar Mar 21 '20 21:03 ofekp

Hi Ofek,

Made the changes, including function overloading for users with the current library version for compatibility. I'm pretty new to GitHub and don't know how to upload files here, so I've uploaded it to my server:

https://pondctrl.dennisbor.com/tools/TinyUPnP/TinyUPnP.cpp

https://pondctrl.dennisbor.com/tools/TinyUPnP/TinyUPnP.h

DennisBorCom avatar Mar 22 '20 09:03 DennisBorCom

Awesome, thank you so much for this! I will prepare the PR after issue_51 is resolved.

ofekp avatar Mar 26 '20 19:03 ofekp