Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Netzwerke (https://www.delphipraxis.net/14-netzwerke/)
-   -   Delphi UPnP Port Forwarding (https://www.delphipraxis.net/171795-upnp-port-forwarding.html)

Zacherl 25. Nov 2012 22:32

UPnP Port Forwarding
 
Hallo zusammen,

ich versuche per UPnP einen Port bei meinem Router (FritzBox) freizugeben. Hierzu habe ich die UPnPNAT TypeLib importiert und versuche nun über diese Schnittstelle an die Port Mappings zu gelangen:

Delphi-Quellcode:
var
  NAT: TUPnPNAT;
begin
  NAT := TUPnPNAT.Create(nil);
  NAT.StaticPortMappingCollection;
Leider ist StaticPortMappingCollection immer nil.

Im Internet habe ich leider wenig Informationen gefunden. Dort heißt es nur, dass der Wert nil ist, wenn der Router kein UPnP unterstüzt. Allerdings habe ich alles in der Richtung aktiviert und mit einem (Java) Tool funktioniert auch alles tadellos.

Was mache ich also falsch? Ich vermute, dass das diese UPnPNAT Klasse leider zu einem Standarddevice connected, welches nicht mein Router ist. Die Klasse beinhaltet auch eine ConnectTo() Methode, allerdings weiß ich nicht, wie ich alle vorhandenen UPnP Geräte auflisten kann, oder ob das überhaupt die Fehlerursache ist.

Viele Grüße
Zacherl

Sir Rufo 25. Nov 2012 22:37

AW: UPnP Port Forwarding
 
Das Verhalten riecht ein wenig nach Interfaces :)

Schon mal geschaut ob es da ein passendes gibt?

Bei SO gibt es was zu dem Thema
http://stackoverflow.com/questions/1...-object-failed

Zacherl 25. Nov 2012 22:57

AW: UPnP Port Forwarding
 
Jap diesen Post hatte ich auch gefunden, aber dort heißt es auch nur wieder:
Zitat:

this can be caused by many reasons your device doesn't supports UPnP, The UPnP is not enabled on the device, The UPnP User Interface is not installed/active, and so on
Mit dem Java Tool funktioniert das Forwarding aber wunderbar, also muss UPnP ja ordnungsgemäß bei meinem Router aktiviert sein. Wie kann ich prüfen, ob das Interface installiert bzw. aktiv ist?

Zacherl 28. Nov 2012 16:22

AW: UPnP Port Forwarding
 
:duck: push

Aphton 22. Dez 2013 20:32

AW: UPnP Port Forwarding
 
Ich hab mich auch gerade eben damit befasst und an diversen Beispielcodes, die im Internet so dazu zu finden sind, rumgebastelt.. Diese Lösung funktioniert bei mir:

Delphi-Quellcode:
unit aphtonUPnP;

interface

uses
  Windows, ActiveX, oleAuto, Variants, SysUtils;

type
  TUPnP_PortMapTable = class
  public
    class function add(const active: Boolean; const extPort, intPort: DWORD;
      const ip, proto, desc: String): Boolean;
    class function remove(const extPort: DWORD; const proto: String): Boolean;
  end;

implementation

class function TUPnP_PortMapTable.add(const active: Boolean; const extPort, intPort: DWORD;
  const ip, proto, desc: String): Boolean;
var
  n, p: Variant;
Begin
  Result := False;
  try
    n := CreateOleObject('HNetCfg.NATUPnP');
    p := n.StaticPortMappingCollection;
    if not VarIsClear(p) then
    begin
      p.Add(extPort, UpperCase(proto), intPort, ip, active, desc);
      Result := True;
    end;
  except
//    on e: exception do showmessage(e.Message);
  end;
end;

class function TUPnP_PortMapTable.remove(const extPort: DWORD; const proto: String): Boolean;
var
  n, p: Variant;
Begin
  Result := False;
  try
    n := CreateOleObject('HNetCfg.NATUPnP');
    p := n.StaticPortMappingCollection;
    if not VarIsClear(p) then
      Result := p.Remove(extPort, UpperCase(proto)) = S_OK;
  except
//    on e: exception do showmessage(e.Message);
  end;
end;

end.
möglicher Aufruf:
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
begin
  if not TUPnP_PortMapTable.remove(1234, 'tcp') then showmessage('remove failed');
  if not TUPnP_PortMapTable.add(true, 1234, 4444, '192.168.1.4', 'TCP', 'test') then showmessage('add failed');
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 02:39 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz