Recent

Author Topic: [SOLVED] How to open UPnP port with synapse  (Read 11268 times)

Key-Real

  • Sr. Member
  • ****
  • Posts: 389
Re: How to open UPnP port?
« Reply #15 on: June 13, 2023, 02:41:16 pm »
Quote
[key-real@koshka upnp]$ upnpc -r 3332 UDP
upnpc : miniupnpc library test client, version 2.2.4.
 (c) 2005-2022 Thomas Bernard.
Go to http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/
for more information.
List of UPNP devices found on the network :
 desc: http://192.168.0.1:49155/IGDdevicedesc_brlan0.xml
 st: urn:schemas-upnp-org:device:InternetGatewayDevice:1

Found valid IGD : http://192.168.0.1:49155/upnp/control/WANIPConnection0
Local LAN ip address : 192.168.0.4
ExternalIPAddress = 31.18.77.240
InternalIP:Port = 192.168.0.4:3332
external 31.18.77.240:3332 UDP is redirected to internal 192.168.0.4:3332 (duration=0)
« Last Edit: June 13, 2023, 02:45:59 pm by Key-Real »

rvk

  • Hero Member
  • *****
  • Posts: 6799
Re: How to open UPnP port?
« Reply #16 on: June 13, 2023, 02:51:29 pm »
So it should work.

That one sees port 49155.

Error 402 is
Parameters are missing, extra, or are invalid for this action. See the description of Control in [UPNPARCH1.1] section 3.

If you have tcpdump you could do a tcpdump on one terminal and execute that upnpc command on another.

This will list all traffic outgoing on destination port 49155. Change it to the same port as the latest command.

Code: Bash  [Select][+][-]
  1. sudo tcpdump -n dst port 49155

I catch this:
Code: XML  [Select][+][-]
  1. Host: 192.168.2.1:43493
  2. User-Agent: Debian/bullseye/sid, UPnP/1.1, MiniUPnPc/2.2.1
  3. Content-Length: 596
  4. Content-Type: text/xml
  5. SOAPAction: "urn:schemas-upnp-org:service:WANIPConnection:1#AddPortMapping"
  6. Connection: Close
  7. Cache-Control: no-cache
  8. Pragma: no-cache
  9.  
  10. <?xml version="1.0"?>
  11. <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:AddPortMapping xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"><NewRemoteHost></NewRemoteHost><NewExternalPort>33364</NewExternalPort><NewProtocol>UDP</NewProtocol><NewInternalPort>33364</NewInternalPort><NewInternalClient>192.168.2.21</NewInternalClient><NewEnabled>1</NewEnabled><NewPortMappingDescription>libminiupnpc</NewPortMappingDescription><NewLeaseDuration>0</NewLeaseDuration></u:AddPortMapping></s:Body></s:Envelope>

(pretty xml)
Code: XML  [Select][+][-]
  1. <?xml version="1.0"?>
  2. <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  3.   <s:Body>
  4.     <u:AddPortMapping xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1">
  5.       <NewRemoteHost/>
  6.       <NewExternalPort>33364</NewExternalPort>
  7.       <NewProtocol>UDP</NewProtocol>
  8.       <NewInternalPort>33364</NewInternalPort>
  9.       <NewInternalClient>192.168.2.21</NewInternalClient>
  10.       <NewEnabled>1</NewEnabled>
  11.       <NewPortMappingDescription>libminiupnpc</NewPortMappingDescription>
  12.       <NewLeaseDuration>0</NewLeaseDuration>
  13.     </u:AddPortMapping>
  14.   </s:Body>
  15. </s:Envelope>

Order of arguments could be important.
« Last Edit: June 13, 2023, 02:54:51 pm by rvk »

Key-Real

  • Sr. Member
  • ****
  • Posts: 389
Re: How to open UPnP port?
« Reply #17 on: June 13, 2023, 02:55:29 pm »
Code: Pascal  [Select][+][-]
  1. [key-real@koshka upnp]$ sudo tcpdump -n dst port 49155
  2. tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
  3. listening on enp7s0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
  4. 14:54:09.934291 IP 192.168.0.4.46338 > 192.168.0.1.49155: Flags [S], seq 1933787470, win 64240, options [mss 1460,sackOK,TS val 622119008 ecr 0,nop,wscale 7], length 0
  5. 14:54:09.935723 IP 192.168.0.4.46338 > 192.168.0.1.49155: Flags [.], ack 1769740677, win 502, options [nop,nop,TS val 622119009 ecr 145991940], length 0
  6. 14:54:09.935835 IP 192.168.0.4.46338 > 192.168.0.1.49155: Flags [P.], seq 0:139, ack 1, win 502, options [nop,nop,TS val 622119009 ecr 145991940], length 139
  7. 14:54:09.937388 IP 192.168.0.4.46338 > 192.168.0.1.49155: Flags [.], ack 273, win 500, options [nop,nop,TS val 622119011 ecr 145991941], length 0
  8. 14:54:09.937454 IP 192.168.0.4.46338 > 192.168.0.1.49155: Flags [.], ack 1721, win 499, options [nop,nop,TS val 622119011 ecr 145991942], length 0
  9. 14:54:09.937496 IP 192.168.0.4.46338 > 192.168.0.1.49155: Flags [.], ack 3169, win 499, options [nop,nop,TS val 622119011 ecr 145991942], length 0
  10. 14:54:09.937550 IP 192.168.0.4.46338 > 192.168.0.1.49155: Flags [F.], seq 139, ack 3298, win 501, options [nop,nop,TS val 622119011 ecr 145991942], length 0
  11. 14:54:09.937590 IP 192.168.0.4.46342 > 192.168.0.1.49155: Flags [S], seq 3418843113, win 64240, options [mss 1460,sackOK,TS val 622119011 ecr 0,nop,wscale 7], length 0
  12. 14:54:09.938823 IP 192.168.0.4.46342 > 192.168.0.1.49155: Flags [.], ack 2029789123, win 502, options [nop,nop,TS val 622119012 ecr 145991943], length 0
  13. 14:54:09.938845 IP 192.168.0.4.46342 > 192.168.0.1.49155: Flags [P.], seq 0:580, ack 1, win 502, options [nop,nop,TS val 622119012 ecr 145991943], length 580
  14. 14:54:09.940798 IP 192.168.0.4.46342 > 192.168.0.1.49155: Flags [.], ack 230, win 501, options [nop,nop,TS val 622119014 ecr 145991945], length 0
  15. 14:54:09.940853 IP 192.168.0.4.46342 > 192.168.0.1.49155: Flags [F.], seq 580, ack 640, win 501, options [nop,nop,TS val 622119014 ecr 145991945], length 0
  16. 14:54:09.940870 IP 192.168.0.4.46350 > 192.168.0.1.49155: Flags [S], seq 1096853470, win 64240, options [mss 1460,sackOK,TS val 622119014 ecr 0,nop,wscale 7], length 0
  17. 14:54:09.942167 IP 192.168.0.4.46350 > 192.168.0.1.49155: Flags [.], ack 137316309, win 502, options [nop,nop,TS val 622119015 ecr 145991946], length 0
  18. 14:54:09.942183 IP 192.168.0.4.46350 > 192.168.0.1.49155: Flags [P.], seq 0:601, ack 1, win 502, options [nop,nop,TS val 622119015 ecr 145991946], length 601
  19. 14:54:09.944014 IP 192.168.0.4.46350 > 192.168.0.1.49155: Flags [.], ack 230, win 501, options [nop,nop,TS val 622119017 ecr 145991948], length 0
  20. 14:54:09.944119 IP 192.168.0.4.46350 > 192.168.0.1.49155: Flags [F.], seq 601, ack 572, win 501, options [nop,nop,TS val 622119017 ecr 145991948], length 0
  21. 14:54:09.944132 IP 192.168.0.4.46360 > 192.168.0.1.49155: Flags [S], seq 3406302317, win 64240, options [mss 1460,sackOK,TS val 622119017 ecr 0,nop,wscale 7], length 0
  22. 14:54:09.945429 IP 192.168.0.4.46360 > 192.168.0.1.49155: Flags [.], ack 3279800914, win 502, options [nop,nop,TS val 622119019 ecr 145991950], length 0
  23. 14:54:09.945440 IP 192.168.0.4.46360 > 192.168.0.1.49155: Flags [P.], seq 0:601, ack 1, win 502, options [nop,nop,TS val 622119019 ecr 145991950], length 601
  24. 14:54:09.947547 IP 192.168.0.4.46360 > 192.168.0.1.49155: Flags [.], ack 230, win 501, options [nop,nop,TS val 622119021 ecr 145991952], length 0
  25. 14:54:09.947635 IP 192.168.0.4.46360 > 192.168.0.1.49155: Flags [F.], seq 601, ack 572, win 501, options [nop,nop,TS val 622119021 ecr 145991952], length 0
  26. 14:54:09.947644 IP 192.168.0.4.46366 > 192.168.0.1.49155: Flags [S], seq 1371474331, win 64240, options [mss 1460,sackOK,TS val 622119021 ecr 0,nop,wscale 7], length 0
  27. 14:54:09.949030 IP 192.168.0.4.46366 > 192.168.0.1.49155: Flags [.], ack 2584397854, win 502, options [nop,nop,TS val 622119022 ecr 145991953], length 0
  28. 14:54:09.949046 IP 192.168.0.4.46366 > 192.168.0.1.49155: Flags [P.], seq 0:905, ack 1, win 502, options [nop,nop,TS val 622119022 ecr 145991953], length 905
  29. 14:54:10.065977 IP 192.168.0.4.46366 > 192.168.0.1.49155: Flags [.], ack 230, win 501, options [nop,nop,TS val 622119139 ecr 145992070], length 0
  30. 14:54:10.066094 IP 192.168.0.4.46366 > 192.168.0.1.49155: Flags [F.], seq 905, ack 499, win 501, options [nop,nop,TS val 622119139 ecr 145992070], length 0
  31. 14:54:10.066187 IP 192.168.0.4.46376 > 192.168.0.1.49155: Flags [S], seq 1038112737, win 64240, options [mss 1460,sackOK,TS val 622119139 ecr 0,nop,wscale 7], length 0
  32. 14:54:10.067396 IP 192.168.0.4.46376 > 192.168.0.1.49155: Flags [.], ack 1781443229, win 502, options [nop,nop,TS val 622119141 ecr 145992071], length 0
  33. 14:54:10.067492 IP 192.168.0.4.46376 > 192.168.0.1.49155: Flags [P.], seq 0:723, ack 1, win 502, options [nop,nop,TS val 622119141 ecr 145992071], length 723
  34. 14:54:10.070661 IP 192.168.0.4.46376 > 192.168.0.1.49155: Flags [.], ack 230, win 501, options [nop,nop,TS val 622119144 ecr 145992074], length 0
  35. 14:54:10.070754 IP 192.168.0.4.46376 > 192.168.0.1.49155: Flags [F.], seq 723, ack 758, win 501, options [nop,nop,TS val 622119144 ecr 145992075], length 0
  36.  

rvk

  • Hero Member
  • *****
  • Posts: 6799
Re: How to open UPnP port?
« Reply #18 on: June 13, 2023, 02:57:15 pm »
O sorry, I forgot the -A in that tcpdump command.
It will print out the XML.


BTW. Try this in my code:
Code: Pascal  [Select][+][-]
  1.                   s := '<?xml version="1.0"?>';
  2.                   s := s + '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">';
  3.                   s := s + '<SOAP-ENV:Body>';
  4.                   s := s + '    <m:AddPortMapping xmlns:m="urn:schemas-upnp-org:service:WANIPConnection:1">';
  5.                   s := s + '        <NewRemoteHost></NewRemoteHost>';
  6.                   s := s + '        <NewExternalPort>3331</NewExternalPort>';
  7.                   s := s + '        <NewProtocol>UDP</NewProtocol>';
  8.                   s := s + '        <NewInternalPort>4444</NewInternalPort>';
  9.                   s := s + '        <NewInternalClient>192.168.0.4</NewInternalClient>';
  10.                   s := s + '        <NewEnabled>1</NewEnabled>';
  11.                   s := s + '        <NewPortMappingDescription>test</NewPortMappingDescription>';
  12.                   s := s + '        <NewLeaseDuration>0</NewLeaseDuration>';
  13.                   s := s + '    </m:AddPortMapping>';
  14.                   s := s + '</SOAP-ENV:Body>';
  15.                   s := s + '</SOAP-ENV:Envelope>';

Different argument order (and 1 instead of True).

Key-Real

  • Sr. Member
  • ****
  • Posts: 389
Re: How to open UPnP port?
« Reply #19 on: June 13, 2023, 03:02:01 pm »
Quote
Location is http://192.168.0.1:49155/IGDdevicedesc_brlan0.xml
Base is http://192.168.0.1:49155
SCPDURL is http://192.168.0.1:49155/WANIPConnectionServiceSCPD.xml
Control URL is http://192.168.0.1:49155/upnp/control/WANIPConnection0
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body>
<u:AddPortMappingResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"></u:AddPortMappingResponse>
</s:Body> </s:Envelope>

we are done, press enter


rvk

  • Hero Member
  • *****
  • Posts: 6799
Re: How to open UPnP port?
« Reply #20 on: June 13, 2023, 03:03:20 pm »
Quote
Location is http://192.168.0.1:49155/IGDdevicedesc_brlan0.xml
Base is http://192.168.0.1:49155
SCPDURL is http://192.168.0.1:49155/WANIPConnectionServiceSCPD.xml
Control URL is http://192.168.0.1:49155/upnp/control/WANIPConnection0
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body>
<u:AddPortMappingResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"></u:AddPortMappingResponse>
</s:Body> </s:Envelope>

we are done, press enter

I think that's a 200 response (I didn't print that out but looking at the xml, the port should now be forwarded.

But if you used my code... it should query the ports afterwards and that result seems empty  %) %) %)

What does upnpc -l say?

Key-Real

  • Sr. Member
  • ****
  • Posts: 389
Re: How to open UPnP port?
« Reply #21 on: June 13, 2023, 03:06:38 pm »
Quote
[key-real@koshka upnp]$ upnpc -r 49166 UDP
upnpc : miniupnpc library test client, version 2.2.4.
 (c) 2005-2022 Thomas Bernard.
Go to http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/
for more information.
List of UPNP devices found on the network :
 desc: http://192.168.0.1:49155/IGDdevicedesc_brlan0.xml
 st: urn:schemas-upnp-org:device:InternetGatewayDevice:1

Found valid IGD : http://192.168.0.1:49155/upnp/control/WANIPConnection0
Local LAN ip address : 192.168.0.4
ExternalIPAddress = 31.18.77.240
InternalIP:Port = 192.168.0.4:49166
external 31.18.77.240:49166 UDP is redirected to internal 192.168.0.4:49166 (duration=0)




[key-real@koshka upnp]$ upnpc -l
upnpc : miniupnpc library test client, version 2.2.4.
 (c) 2005-2022 Thomas Bernard.
Go to http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/
for more information.
List of UPNP devices found on the network :
 desc: http://192.168.0.1:49155/IGDdevicedesc_brlan0.xml
 st: urn:schemas-upnp-org:device:InternetGatewayDevice:1

Found valid IGD : http://192.168.0.1:49155/upnp/control/WANIPConnection0
Local LAN ip address : 192.168.0.4
Connection Type : IP_Routed
Status : Connected, uptime=0s, LastConnectionError : ERROR_NONE
MaxBitRateDown : 100000000 bps (100.0 Mbps)   MaxBitRateUp 100000000 bps (100.0 Mbps)
ExternalIPAddress = 31.18.77.240
 i protocol exPort->inAddr:inPort description remoteHost leaseTime
GetGenericPortMappingEntry() returned 713 (SpecifiedArrayIndexInvalid)


rvk

  • Hero Member
  • *****
  • Posts: 6799
Re: How to open UPnP port?
« Reply #22 on: June 13, 2023, 03:19:47 pm »
[key-real@koshka upnp]$ upnpc -l
[...]
 i protocol exPort->inAddr:inPort description remoteHost leaseTime
GetGenericPortMappingEntry() returned 713 (SpecifiedArrayIndexInvalid)
So upnpc doesn't give an error when setting a port forward.
But it doesn't show any forwarded ports.

Weird.

You can put this around line 150 of my example:
Code: Pascal  [Select][+][-]
  1.                         Response.LoadFromStream(HTTP.Document);
  2.                         Found := Response.Text;
  3.                         writeln(HTTP.ResultCode.ToString + ' ' + found);
  4.  
just to see what the resultcode is.

But if you (and miniupnp) doesn't get an error but still doesn't add the forward, I wonder if something else is broken.

Key-Real

  • Sr. Member
  • ****
  • Posts: 389
Re: How to open UPnP port?
« Reply #23 on: June 13, 2023, 03:21:52 pm »
Quote
Location is http://192.168.0.1:49155/IGDdevicedesc_brlan0.xml
Base is http://192.168.0.1:49155
SCPDURL is http://192.168.0.1:49155/WANIPConnectionServiceSCPD.xml
Control URL is http://192.168.0.1:49155/upnp/control/WANIPConnection0
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body>
<u:AddPortMappingResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"></u:AddPortMappingResponse>
</s:Body> </s:Envelope>

400 <html><body><h1>400 Bad Request</h1></body></html>

we are done, press enter


Key-Real

  • Sr. Member
  • ****
  • Posts: 389
Re: How to open UPnP port?
« Reply #24 on: June 13, 2023, 03:28:28 pm »
A snipet from
http://192.168.0.1:49155/WANIPConnectionServiceSCPD.xml

Quote
<action>
<name>GetGenericPortMappingEntry</name>
<argumentList>
<argument>
<name>NewPortMappingIndex</name>
<direction>in</direction>
<relatedStateVariable>PortMappingNumberOfEntries</relatedStateVariable>
</argument>
<argument>
<name>NewRemoteHost</name>
<direction>out</direction>
<relatedStateVariable>RemoteHost</relatedStateVariable>
</argument>
<argument>
<name>NewExternalPort</name>
<direction>out</direction>
<relatedStateVariable>ExternalPort</relatedStateVariable>
</argument>
<argument>
<name>NewProtocol</name>
<direction>out</direction>
<relatedStateVariable>PortMappingProtocol</relatedStateVariable>
</argument>
<argument>
<name>NewInternalPort</name>
<direction>out</direction>
<relatedStateVariable>InternalPort</relatedStateVariable>
</argument>
<argument>
<name>NewInternalClient</name>
<direction>out</direction>
<relatedStateVariable>InternalClient</relatedStateVariable>
</argument>
<argument>
<name>NewEnabled</name>
<direction>out</direction>
<relatedStateVariable>PortMappingEnabled</relatedStateVariable>
</argument>
<argument>
<name>NewPortMappingDescription</name>
<direction>out</direction>
<relatedStateVariable>PortMappingDescription</relatedStateVariable>
</argument>
<argument>
<name>NewLeaseDuration</name>
<direction>out</direction>
<relatedStateVariable>PortMappingLeaseDuration</relatedStateVariable>
</argument>
</argumentList>
</action>
« Last Edit: June 13, 2023, 03:30:41 pm by Key-Real »

rvk

  • Hero Member
  • *****
  • Posts: 6799
Re: How to open UPnP port?
« Reply #25 on: June 13, 2023, 03:32:56 pm »
New code with all the Resultcode:

Code: Pascal  [Select][+][-]
  1. program upnp;
  2.  
  3. {$MODE OBJFPC}{$H+}
  4. uses
  5.   Classes,
  6.   blcksock,
  7.   synautil,
  8.   SysUtils,
  9.   RegExpr,
  10.   httpsend;
  11.  
  12.   function GetStringBetweenAndStrip(var str: string; startStr, endStr: string): string;
  13.   var
  14.     startPos, endPos: integer;
  15.   begin
  16.     startStr := uppercase(startStr);
  17.     endStr := uppercase(endStr);
  18.     startPos := Pos(startStr, uppercase(str));
  19.     if startPos = 0 then exit('');
  20.     startPos := startPos + Length(startStr);
  21.     endPos := Pos(endStr, uppercase(str), startPos);
  22.     if endPos = 0 then exit('');
  23.     Result := Copy(str, startPos, endPos - startPos);
  24.     // strip the string
  25.     startPos := startPos - Length(startStr);
  26.     endPos := endPos + Length(endStr);
  27.     System.Delete(str, startPos, endPos - startPos);
  28.   end;
  29.  
  30. var
  31.   GatewayIP, Location, Base, Found, Found1, Service, S: string;
  32.   ServiceType: string;
  33.   Socket: TUDPBlockSocket;
  34.   Response: TStringList;
  35.   HTTP: THTTPSend;
  36.   ResultCode: integer;
  37.   Idx: integer;
  38.   eH, eP, Pr, iP, iH, iE, iD, iL: string;
  39. begin
  40.  
  41.   GatewayIP := '192.168.0.1';  // I used hardcoded Gateway here. You can also listen for any other upnp device and pick that one.
  42.  
  43.   S := 'M-SEARCH * HTTP/1.1'#13#10 + 'HOST: 239.255.255.250:1900'#13#10 + 'MAN: "ssdp:discover"'#13#10 + 'MX: 3'#13#10 + 'ST: upnp:rootdevice'#13#10#13#10;
  44.   Socket := TUDPBlockSocket.Create;
  45.   Socket.EnableBroadcast(True);
  46.   Socket.Connect(GatewayIP, '1900');
  47.   Socket.SendString(S);
  48.   repeat
  49.     if Socket.CanRead(3000) then
  50.     begin
  51.       Found := Socket.RecvPacket(3000);
  52.       if Pos('LOCATION: ', uppercase(Found)) > 0 then
  53.       begin
  54.         Location := Copy(Found, Pos('LOCATION:', uppercase(Found)) + 9);
  55.         Location := Trim(Copy(Location, 1, Pos(#13#10, Location) - 1));
  56.         writeln('Location is ' + Location);
  57.         Response := TStringList.Create;
  58.         try
  59.  
  60.           HttpGetText(Location, Response);
  61.           Found := Response.Text;
  62.  
  63.           Base := Location; // take base of Location for control
  64.           while (Base <> '') and (Location[Length(Base)] <> '/') do Delete(Base, Length(Base), 1);
  65.           if Base <> '' then Delete(Base, Length(Base), 1);
  66.           writeln('Base is ' + Base);
  67.  
  68.           // loop all services
  69.           repeat
  70.             Service := GetStringBetweenAndStrip(Found, '<service>', '</service>');
  71.             if Pos(uppercase(':WANIPConnection:'), uppercase(service)) > 0 then
  72.             begin
  73.               S := GetStringBetweenAndStrip(Service, '<SCPDURL>', '</SCPDURL>');
  74.               if S <> '' then
  75.               begin
  76.                 Location := Base + S;
  77.                 writeln('SCPDURL is ' + Location);
  78.                 HttpGetText(Location, Response);
  79.                 S := Response.Text;
  80.                 if Pos(uppercase('<name>AddPortMapping</name>'), uppercase(S)) > 0 then
  81.                 begin
  82.                   ServiceType := GetStringBetweenAndStrip(Service, '<serviceType>', '</serviceType>');
  83.                   S := GetStringBetweenAndStrip(Service, '<controlURL>', '</controlURL>');
  84.                   Location := Base + S;
  85.                   writeln('Control URL is ' + Location);
  86.  
  87.                   // HERE WE INSERT
  88.  
  89.                   s := '<?xml version="1.0"?>';
  90.                   s := s + '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">';
  91.                   s := s + '<SOAP-ENV:Body>';
  92.                   s := s + '    <m:AddPortMapping xmlns:m="' + ServiceType + '">';
  93.                   s := s + '        <NewRemoteHost></NewRemoteHost>';
  94.                   s := s + '        <NewExternalPort>3334</NewExternalPort>';
  95.                   s := s + '        <NewProtocol>UDP</NewProtocol>';
  96.                   s := s + '        <NewInternalPort>4444</NewInternalPort>';
  97.                   s := s + '        <NewInternalClient>192.168.0.4</NewInternalClient>';
  98.                   s := s + '        <NewEnabled>1</NewEnabled>';
  99.                   s := s + '        <NewPortMappingDescription>test</NewPortMappingDescription>';
  100.                   s := s + '        <NewLeaseDuration>0</NewLeaseDuration>';
  101.                   s := s + '    </m:AddPortMapping>';
  102.                   s := s + '</SOAP-ENV:Body>';
  103.                   s := s + '</SOAP-ENV:Envelope>';
  104.  
  105.                   HTTP := THTTPSend.Create;
  106.                   try
  107.  
  108.                     WriteStrToStream(HTTP.Document, S);
  109.                     HTTP.MimeType := 'text/xml; charset="utf-8"';
  110.                     HTTP.Headers.Add('SOAPAction: ' + ServiceType + '#AddPortMapping');
  111.  
  112.                     if HTTP.HTTPMethod('POST', Location) then
  113.                     begin
  114.                       Resultcode := HTTP.ResultCode;
  115.                       Response.LoadFromStream(HTTP.Document);
  116.                       Found := Response.Text;
  117.                       writeln('');
  118.                       writeln('Result of AddPort: ' + HTTP.ResultCode.ToString + ' ' + found);
  119.                     end;
  120.  
  121.                   finally
  122.                     HTTP.Free;
  123.                   end;
  124.  
  125.                   // HERE WE CHECK
  126.  
  127.  
  128.                   Idx := 0;
  129.                   repeat
  130.  
  131.                     S := '';
  132.                     S := S + '<?xml version="1.0"?>';
  133.                     S := S + '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">';
  134.                     S := S + ' <s:Body>';
  135.                     S := S + '  <u:GetGenericPortMappingEntry xmlns:u="' + ServiceType + '">';
  136.                     S := S + '   <NewPortMappingIndex>' + Idx.ToString + '</NewPortMappingIndex>';
  137.                     S := S + '  </u:GetGenericPortMappingEntry>';
  138.                     S := S + ' </s:Body>';
  139.                     S := S + '</s:Envelope>';
  140.  
  141.                     HTTP := THTTPSend.Create;
  142.                     try
  143.  
  144.                       WriteStrToStream(HTTP.Document, S);
  145.                       HTTP.MimeType := 'text/xml; charset="utf-8"';
  146.                       HTTP.Headers.Add('SOAPAction: ' + ServiceType + '#GetGenericPortMappingEntry');
  147.  
  148.                       if HTTP.HTTPMethod('POST', Location) then
  149.                       begin
  150.                         Resultcode := HTTP.ResultCode;
  151.                         Response.LoadFromStream(HTTP.Document);
  152.                         Found := Response.Text;
  153.                         writeln('');
  154.                         writeln('Result of GetPortMapping #' + Idx.ToString + ': ' + HTTP.ResultCode.ToString + ' ' + found);
  155.                         repeat
  156.                           Found1 := GetStringBetweenAndStrip(Found, '<u:GetGenericPortMappingEntryResponse', '</u:GetGenericPortMappingEntryResponse>');
  157.                           if Found1 <> '' then
  158.                           begin
  159.                             eH := GetStringBetweenAndStrip(Found1, '<NewRemoteHost>', '</NewRemoteHost>');
  160.                             eP := GetStringBetweenAndStrip(Found1, '<NewExternalPort>', '</NewExternalPort>');
  161.                             Pr := GetStringBetweenAndStrip(Found1, '<NewProtocol>', '</NewProtocol>');
  162.                             iP := GetStringBetweenAndStrip(Found1, '<NewInternalPort>', '</NewInternalPort>');
  163.                             iH := GetStringBetweenAndStrip(Found1, '<NewInternalClient>', '</NewInternalClient>');
  164.                             iE := GetStringBetweenAndStrip(Found1, '<NewEnabled>', '<NewEnabled>');
  165.                             iD := GetStringBetweenAndStrip(Found1, '<NewPortMappingDescription>', '</NewPortMappingDescription>');
  166.                             iL := GetStringBetweenAndStrip(Found1, '<NewLeaseDuration>', '</NewLeaseDuration>');
  167.                             S := Format('%s %s:%s -> %s:%s En: %s  De: %s  Le: %s', [Pr, eH, eP, iH, iP, iE, iD, iL]);
  168.                             writeln(S);
  169.                           end;
  170.                         until Found1 = '';
  171.                       end;
  172.  
  173.                     finally
  174.                       HTTP.Free;
  175.                     end;
  176.                     Inc(Idx);
  177.  
  178.                   until Resultcode <> 200;
  179.  
  180.                 end;
  181.  
  182.               end;
  183.  
  184.             end;
  185.  
  186.           until service = '';
  187.  
  188.         finally
  189.           Response.Free;
  190.         end;
  191.       end;
  192.       break;
  193.     end;
  194.     sleep(100);
  195.   until False;
  196.   Socket.CloseSocket;
  197.   Socket.Free;
  198.   writeln('we are done, press enter');
  199.   readln;
  200.  
  201. end.

upnpc also just loops all the NewPortMappingIndex. But an error 400 should happen if on the same URL the AddPortMap is successful.
« Last Edit: June 13, 2023, 03:34:29 pm by rvk »

Key-Real

  • Sr. Member
  • ****
  • Posts: 389
Re: How to open UPnP port?
« Reply #26 on: June 13, 2023, 03:37:21 pm »
Quote
Location is http://192.168.0.1:49155/IGDdevicedesc_brlan0.xml
Base is http://192.168.0.1:49155
SCPDURL is http://192.168.0.1:49155/WANIPConnectionServiceSCPD.xml
Control URL is http://192.168.0.1:49155/upnp/control/WANIPConnection0

Result of AddPort: 400 <html><body><h1>400 Bad Request</h1></body></html>


Result of GetPortMapping #0: 400 <html><body><h1>400 Bad Request</h1></body></html>

we are done, press enter


rvk

  • Hero Member
  • *****
  • Posts: 6799
Re: How to open UPnP port?
« Reply #27 on: June 13, 2023, 03:43:29 pm »
Quote
Location is http://192.168.0.1:49155/IGDdevicedesc_brlan0.xml
Base is http://192.168.0.1:49155
SCPDURL is http://192.168.0.1:49155/WANIPConnectionServiceSCPD.xml
Control URL is http://192.168.0.1:49155/upnp/control/WANIPConnection0

Result of AddPort: 400 <html><body><h1>400 Bad Request</h1></body></html>


Result of GetPortMapping #0: 400 <html><body><h1>400 Bad Request</h1></body></html>

we are done, press enter

If I look at your post here https://forum.lazarus.freepascal.org/index.php/topic,63652.msg482505.html#msg482505
you got a valid response to the AddPort there.
(you couldn't get a Bad Request if you have valid xml there.

So something has changed  %)

rvk

  • Hero Member
  • *****
  • Posts: 6799
Re: How to open UPnP port?
« Reply #28 on: June 13, 2023, 03:47:59 pm »
So something has changed  %)
Is the ServiceType correctly extracted??

Code: Pascal  [Select][+][-]
  1.                   writeln('Control URL is ' + Location);
  2.                   writeln('ServiceType is ' + ServiceType);

Key-Real

  • Sr. Member
  • ****
  • Posts: 389
Re: How to open UPnP port?
« Reply #29 on: June 13, 2023, 03:50:15 pm »
Quote

Location is http://192.168.0.1:49153/IGDdevicedesc_brlan0.xml
Base is http://192.168.0.1:49153
SCPDURL is http://192.168.0.1:49153/WANIPConnectionServiceSCPD.xml
Control URL is http://192.168.0.1:49153/upnp/control/WANIPConnection0
ServiceType is urn:schemas-upnp-org:service:WANIPConnection:1

Result of AddPort: 400 <html><body><h1>400 Bad Request</h1></body></html>


Result of GetPortMapping #0: 400 <html><body><h1>400 Bad Request</h1></body></html>

we are done, press enter



 

TinyPortal © 2005-2018