miniupnp.tuxfamily.org Forum Index miniupnp.tuxfamily.org
The forum about miniupnp and libnatpmp
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Opening Ports

 
Post new topic   Reply to topic    miniupnp.tuxfamily.org Forum Index -> miniupnpc Compilation/Installation
View previous topic :: View next topic  
Author Message
PortmappingGuru



Joined: 06 Feb 2011
Posts: 8

PostPosted: Fri Feb 18, 2011 12:03 pm    Post subject: Opening Ports Reply with quote

Hi,

I wrote a small function that should open a port for a certain IP address. It works good for one of my test router. But the other test router always returns a UPNP error. My programm is running in debug mode as console, so this is what miniupnpc prints into the console:

Quote:


This is a UPNP-Test!

Opening port 2300 for 192.168.2.2...
ifIndex=11 nextHop=102a8c0
Num Entries: 2

Interface Index[0]: 11
IP Address[0]: 192.168.2.2
Subnet Mask[0]: 255.255.255.0
BroadCast[0]: 1.0.0.0 (1)
Reassembly size[0]: 65535
Type and State[0]:

Interface Index[1]: 1
IP Address[1]: 127.0.0.1
Subnet Mask[1]: 255.0.0.0
BroadCast[1]: 1.0.0.0 (1)
Reassembly size[1]: 65535
Type and State[1]:
M-SEARCH Reply:
ST: urn:schemas-upnp-org:device:InternetGatewayDevice:1
Location: http://192.168.2.1:1900/igd.xml
header='CONTENT-LENGTH', value='2746'
Content-Length: 2746
header='CONTENT-TYPE', value='text/xml'
header='DATE', value='Sun, 01 Jan 2006 07:15:01 GMT'
header='LAST-MODIFIED', value='Tue, 28 Oct 2003 08:46:08 GMT'
header='SERVER', value='TP-LINK Router R460, UPnP/1.0'
header='CONNECTION', value='close'
End of HTTP content
urlbase = ''
WAN Device (Common interface config) :
serviceType = 'urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1'
controlURL = '/ifc'
eventSubURL = '/ifc'
SCPDURL = '/ifc.xml'
primary WAN Connection Device (IP or PPP Connection):
servicetype = 'urn:schemas-upnp-org:service:WANIPConnection:1'
controlURL = '/ipc'
eventSubURL = '/ipc'
SCPDURL = '/ipc.xml'
secondary WAN Connection Device (IP or PPP Connection):
servicetype = ''
controlURL = ''
eventSubURL = ''
SCPDURL = ''
urls->ipcondescURL='http://192.168.2.1:1900/ipc.xml' 31 n1=41
urls->controlURL='http://192.168.2.1:1900/ipc' 27 n2=37
urls->controlURL_CIF='http://192.168.2.1:1900/ifc' 27 n3=37
SOAP request : POST /ipc HTTP/1.1 - Host: 192.168.2.1:1900
SOAPAction: "urn:schemas-upnp-org:service:WANIPConnection:1#GetStatusInfo" - Con
tent-Length: 271
header='CONNECTION', value='close'
header='SERVER', value='TP-LINK Router R460, UPnP/1.0'
header='CONTENT-LENGTH', value='480'
Content-Length: 480
header='CONTENT-TYPE', value='text/xml; charset="utf-8"'
End of HTTP content
SOAP Response :
<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SO
AP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<u:GetStatusInfoResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1
"><NewConnectionStatus>Connected</NewConnectionStatus><NewLastConnectionError>ER
ROR_NONE</NewLastConnectionError><NewUptime>0 Days, 00:15:02</NewUptime></u:GetS
tatusInfoResponse></SOAP-ENV:Body>
</SOAP-ENV:Envelope>

UPNPIGD_IsConnected(http://192.168.2.1:1900/ipc) = 1
SOAP request : POST /ipc HTTP/1.1 - Host: 192.168.2.1:1900
SOAPAction: "urn:schemas-upnp-org:service:WANIPConnection:1#GetStatusInfo" - Con
tent-Length: 271
header='CONNECTION', value='close'
header='SERVER', value='TP-LINK Router R460, UPnP/1.0'
header='CONTENT-LENGTH', value='480'
Content-Length: 480
header='CONTENT-TYPE', value='text/xml; charset="utf-8"'
End of HTTP content
SOAP Response :
<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SO
AP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<u:GetStatusInfoResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1
"><NewConnectionStatus>Connected</NewConnectionStatus><NewLastConnectionError>ER
ROR_NONE</NewLastConnectionError><NewUptime>0 Days, 00:15:02</NewUptime></u:GetS
tatusInfoResponse></SOAP-ENV:Body>
</SOAP-ENV:Envelope>

SOAP request : POST /ipc HTTP/1.1 - Host: 192.168.2.1:1900
SOAPAction: "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1#AddPortMapp
ing" - Content-Length: 602
header='CONTENT-LENGTH', value='476'
Content-Length: 476
header='CONTENT-TYPE', value='text/xml; charset="utf-8"'
header='DATE', value='Sun, 01 Jan 2006 07:15:01 GMT'
header='EXT', value=''
header='SERVER', value='TP-LINK Router R460, UPnP/1.0'
End of HTTP content
SOAP Response :
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SO
AP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode>SOAP-ENV:Client</faultcode>
<faultstring>UPnPError</faultstring>
<detail>
<UPnPError xmlns="urn:schemas-upnp-org:control-1-0">
<errorCode>-111</errorCode>
<errorDescription>Invalid Action</errorDescription>
</UPnPError>
</detail>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>


What does the Error -111 mean?

This is my current function (sorry german comments):
Code:

float upnp_command(char* locIp, unsigned short Port, char Protocol, bool Delete)
{
   //Führt einen UPnP Discover durch
   //--> Findet alle UPnP-Fähigen Geräte
   struct UPNPDev *devices = upnpDiscover(30, NULL, NULL, 0);

   if(devices)
   { //wenn Geräte gefunden wurden

      struct IGDdatas *data = (struct IGDdatas *)malloc(sizeof(struct IGDdatas));
      struct UPNPUrls *urls = (struct UPNPUrls *)malloc(sizeof(struct UPNPUrls));

      if(data == NULL || urls == NULL)
      {
         free((void*)data);
         free((void*)urls);
         freeUPNPDevlist(devices);
         WSACleanup();

         return -1;
      }

      //Ermittelt ob es Geräte die IGD (= Internet Gateway Device Protocol)
      //fähige Geräte gibt:
      int validDevices = UPNP_GetValidIGD(devices, urls, data, NULL, 0);

      bool Found = false; //wird true gesetzt wenn IGD fähige Geräte gefunden wurden
      int Returns = 0; //Rückgabewert der UPNP_Add/Delete Funktionen

      if(validDevices > 0)
      { //wenn es IGD fähige Geräte gibt

         //Konvertiert den Port in einen String:
         char PortStr[32];
         sprintf(PortStr, "%i", Port);

         if(Protocol & PROTOCOL_UDP)
         { //Wenn der Port für UDP geöffnet/geschlossen werden soll
            Found = true;

            if(!Delete)
               Returns = UPNP_AddPortMapping(urls->controlURL, data->CIF.servicetype, PortStr, PortStr, locIp, NULL, "UDP", NULL);
            else
               Returns = UPNP_DeletePortMapping(urls->controlURL, data->CIF.servicetype, PortStr, "UDP", NULL);
         }
         if(Protocol & PROTOCOL_TCP)
         { //Wenn der Port für TCP geöffnet/geschlossen werden soll
            Found = true;

            if(!Delete)
               Returns = UPNP_AddPortMapping(urls->controlURL, data->CIF.servicetype, PortStr, PortStr, locIp, NULL, "TCP", NULL);
            else
               Returns = UPNP_DeletePortMapping(urls->controlURL, data->CIF.servicetype, PortStr, "TCP", NULL);
         }
      }

      //Befreit den benützten Speicher
      free((void*)data);
      free((void*)urls);
      freeUPNPDevlist(devices);

      //Gibt den entsprechenden Fehler zurück
      if(Returns != 0) return -1; //ein UPNP Fehler ist aufgetreten
      else
      {
         if(Found) return 1;
         else return 0;
      }
   }

   return 0;
}


Maybe I wrote the code in a way it works not for all devices correct?

Thanks for your help!
Back to top
View user's profile Send private message
shaidar



Joined: 05 Feb 2011
Posts: 5

PostPosted: Fri Feb 18, 2011 8:43 pm    Post subject: Reply with quote

Hi,

Your code does not work because the service type is not correct, you should replace:
Code:
data->CIF.servicetype
by
Code:
data->first.servicetype
when invoking UPNP_AddPortMapping and UPNP_DeletePortMapping actions.

By this mean, you will use WANPPPConnection or WANIPConnection instead of WANCommonInterfaceConfig.
Back to top
View user's profile Send private message
PortmappingGuru



Joined: 06 Feb 2011
Posts: 8

PostPosted: Sat Feb 19, 2011 10:17 am    Post subject: Reply with quote

Thank you so much! It works now like a charm Smile

I currently noticed, that my second router has a field in it's UPnP table called "App Description". If I forward a port with the function above, miniupnpc is entered into the "App Description" field. Is it possible to add a different text to this field? Which parameter do I have to change?

Thanks a lot!
Back to top
View user's profile Send private message
shaidar



Joined: 05 Feb 2011
Posts: 5

PostPosted: Sat Feb 19, 2011 1:56 pm    Post subject: Reply with quote

You're welcome Smile.

You have to specify the text you want in the sixth parameter of UPNP_AddPortMapping. So, for example you should call:

Code:
UPNP_AddPortMapping(urls->controlURL, data->first.servicetype, PortStr, PortStr, locIp, "MyPortMappingDescription", "UDP", NULL);


instead of:

Code:
UPNP_AddPortMapping(urls->controlURL, data->first.servicetype, PortStr, PortStr, locIp, NULL, "UDP", NULL);
Back to top
View user's profile Send private message
PortmappingGuru



Joined: 06 Feb 2011
Posts: 8

PostPosted: Sat Feb 19, 2011 6:40 pm    Post subject: Reply with quote

Works great!

Thanks again!
Back to top
View user's profile Send private message
miniupnp
Site Admin


Joined: 14 Apr 2007
Posts: 1488

PostPosted: Sun Feb 20, 2011 9:54 am    Post subject: Reply with quote

About the errorCode -111 :
Code:
<UPnPError xmlns="urn:schemas-upnp-org:control-1-0">
<errorCode>-111</errorCode>
<errorDescription>Invalid Action</errorDescription>
</UPnPError>


I think there is a bug in the router because the right error code for "Invalid Action" is 401.
And if you cast the integer 401 to a signed char, you got -111...
401 = 0x191
and (signed char)0x91= -111
_________________
Main miniUPnP author.
http://miniupnp.tuxfamily.org/
Back to top
View user's profile Send private message Visit poster's website
daniel854
Guest





PostPosted: Wed May 11, 2011 6:27 pm    Post subject: same problem Reply with quote

i had the same problem. your solution helped me very much.
Back to top
PortmappingGuru



Joined: 06 Feb 2011
Posts: 8

PostPosted: Fri Sep 09, 2011 12:38 pm    Post subject: Reply with quote

Ok, I have nearly the same problem again, but now on another router. The router returns error code 401 (invalid action):

Code:

Returns = UPNP_AddPortMapping(urls->controlURL, data->first.servicetype, PortOutStr, PortInStr, locIp, "UPnP Port", "UDP", NULL, NULL);


This is how I'm opening the port now. I don't see any reason why this is an invalid action, because it works with most routers...

Do you have any suggestions, what I could do wrong?

Thanks! Smile
Back to top
View user's profile Send private message
miniupnp
Site Admin


Joined: 14 Apr 2007
Posts: 1488

PostPosted: Fri Sep 09, 2011 2:28 pm    Post subject: Reply with quote

PortmappingGuru wrote:
Ok, I have nearly the same problem again, but now on another router. The router returns error code 401 (invalid action):

Code:

Returns = UPNP_AddPortMapping(urls->controlURL, data->first.servicetype, PortOutStr, PortInStr, locIp, "UPnP Port", "UDP", NULL, NULL);


This is how I'm opening the port now. I don't see any reason why this is an invalid action, because it works with most routers...

Do you have any suggestions, what I could do wrong?

Thanks! Smile

The router is most probably bugged... have you successfully tried to open a port with another UPnP client on the same router ?
_________________
Main miniUPnP author.
http://miniupnp.tuxfamily.org/
Back to top
View user's profile Send private message Visit poster's website
PortmappingGuru



Joined: 06 Feb 2011
Posts: 8

PostPosted: Fri Sep 09, 2011 3:37 pm    Post subject: Reply with quote

Yes, it works with another application based on miniupnp (maybe different version though) so it has to be something in my code...

PS: I'm using miniupnpc 1.6 and Microsoft Visual Studio 2008.
Back to top
View user's profile Send private message
PortmappingGuru



Joined: 06 Feb 2011
Posts: 8

PostPosted: Thu Sep 15, 2011 11:51 am    Post subject: Reply with quote

It was indeed a bug in the router firmware. After a firmware update it works again!

Thanks for your help!
Back to top
View user's profile Send private message
miniupnp
Site Admin


Joined: 14 Apr 2007
Posts: 1488

PostPosted: Tue Nov 29, 2011 2:26 pm    Post subject: Reply with quote

PortmappingGuru wrote:
It was indeed a bug in the router firmware. After a firmware update it works again!

Could you please give us the buggy version number and the fixed one of your router, so we can help other people running into the same problem ?
Thanks
_________________
Main miniUPnP author.
http://miniupnp.tuxfamily.org/
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    miniupnp.tuxfamily.org Forum Index -> miniupnpc Compilation/Installation All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP
© 2007 Thomas Bernard, author of MiniUPNP.