TinyUPnP
TinyUPnP copied to clipboard
How to configure diferent internal and external port?
I don´t know if it´s possible to select 443 for internal router port and 9005 for the external port.
Hey, currently the package does not support that.
You can look here to understand why: https://github.com/ofekp/TinyUPnP/blob/master/src/TinyUPnP.cpp#L54-L63
It does require a change, overloading the API method with a new method that will accept the two ports (instead of only one LISTEN_PORT) and then passing it to all the needed places. If you're interested in making it that will be awesome and will speed things up.
As a side note, I suggest not using the port 443 as it is used for SSL. Actually, you should not use any root port in the range (1..1024) as it is bad practice.
Thanks for the answer! It would be very useful! When i´ll have free time I will try to implement this feature request!
I´m using 443 because it´s a https server port haha.
Greetings =)
Awesome! If you need any help, let me know. I will happily review :)
Any progress on this?
I used the published version on arduino ide …
i kind of succeed by changing the li 131 of .h file that were : (on arduino version was line 107) void addPortMappingConfig(IPAddress ruleIP /* can be NULL */, int rulePort, String ruleProtocol, int ruleLeaseDuration, String ruleFriendlyName);
into this : void addPortMappingConfig(IPAddress ruleIP /* can be NULL */, int inrulePort, int rulePort, String ruleProtocol, int ruleLeaseDuration, String ruleFriendlyName);
than at the cpp file i changed this function to show like this : (adding int inrulePort and changing the internal port to inrulePort
void TinyUPnP::addPortMappingConfig(IPAddress ruleIP, int inrulePort, int rulePort, String ruleProtocol, int ruleLeaseDuration, String ruleFriendlyName) { static int index = 0; upnpRule *newUpnpRule = new upnpRule(); newUpnpRule->index = index++; newUpnpRule->internalAddr = (ruleIP == WiFi.localIP()) ? ipNull : ruleIP; // for automatic IP change handling newUpnpRule->internalPort = inrulePort; newUpnpRule->externalPort = rulePort; newUpnpRule->leaseDuration = ruleLeaseDuration; newUpnpRule->protocol = ruleProtocol; newUpnpRule->devFriendlyName = ruleFriendlyName;
than i had to change something else in this file to work…. there was something calling the external port creation that copy the value to the internal
and add a section for the internal port in the arduino request:
boolean portMappingAdded = false;
tinyUPnP.addPortMappingConfig(WiFi.localIP(), INTERNAL_PORT, LISTEN_PORT, RULE_PROTOCOL_TCP, LEASE_DURATION, FRIENDLY_NAME+userid ); while (!portMappingAdded) { portMappingAdded = tinyUPnP.commitPortMappings(); Serial.println("");
if (!portMappingAdded) {
// for debugging, you can see this in your router too under forwarding or UPnP
tinyUPnP.printAllPortMappings();
Serial.println(F("This was printed because adding the required port mapping failed"));
delay(30000); // 30 seconds before trying again
}
}
Question is there how to query the open ports? to see if there is one with the name i created? please some help?
Please stay tuned for this PR
You will be able to configure different internal and external ports by using the correct method addPortMappingConfig
.
its already working! with the changes i described.. . also i forgot there was another place to change it was where it call NewInternal … one of them is wrong… should look like this
strcat_P(body_tmp, PSTR(""><NewRemoteHost></NewRemoteHost><NewExternalPort>")); sprintf(integer_string, "%d", rule_ptr->externalPort); strcat_P(body_tmp, integer_string); strcat_P(body_tmp, PSTR("</NewExternalPort><NewProtocol>")); strcat_P(body_tmp, rule_ptr->protocol.c_str()); strcat_P(body_tmp, PSTR("</NewProtocol><NewInternalPort>")); sprintf(integer_string, "%d", rule_ptr->internalPort); strcat_P(body_tmp, integer_string); strcat_P(body_tmp, PSTR("</NewInternalPort><NewInternalClient>")); IPAddress ipAddress = (rule_ptr->internalAddr == ipNull) ? WiFi.localIP() : rule_ptr->internalAddr; strcat_P(body_tmp, ipAddress.toString().c_str()); strcat_P(body_tmp, PSTR("</NewInternalClient><NewEnabled>1</NewEnabled><NewPortMappingDescription>")); strcat_P(body_tmp, rule_ptr->devFriendlyName.c_str()); strcat_P(body_tmp, PSTR("</NewPortMappingDescription><NewLeaseDuration>")); sprintf(integer_string, "%d", rule_ptr->leaseDuration); strcat_P(body_tmp, integer_string); strcat_P(body_tmp, PSTR("</NewLeaseDuration></u:AddPortMapping></s:Body></s:Envelope>"));
for some reason it was pointing all to the same external… now its correct
Great, glad you managed to make it work. I will close this once we merge the PR I mentioned as it also includes these changes and a few more fixes that will make the package more stable.
I am closing this since this PR https://github.com/ofekp/TinyUPnP/pull/74 adds support to configure the internal and external ports separately for each rule (please note it does require calling the correct method to do that).