Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   API de Windows (https://www.clubdelphi.com/foros/forumdisplay.php?f=7)
-   -   IP's en Terminal Service (https://www.clubdelphi.com/foros/showthread.php?t=45176)

rcuevas 25-06-2007 17:05:26

IP's en Terminal Service
 
Buenas a todos/as,

He estado buscando la solución al siguiente problema, pero no la he encontrado. Como última instancia, acudo al pozo de conocimiento formado por una web como esta.

El caso es que tengo una aplicación, en Delhpi, que debe reconocer si existe una sesión de Terminal Server (TS a partir de ahora) allí donde se está ejecutando. En el caso de que exista, debe reconocer si esa sesión se está realizando desde un ordenador que pertenece a la misma red local o no.

El primer paso (saber si existe una sesión de TS) ya lo he conseguido. El segundo paso, saber si el origen de la sesión pertenece a la misma red local, pensé que lo lograría mediante la función WTSQuerySessionInformation (http://msdn2.microsoft.com/en-us/library/aa383838.aspx), pero no fue así.

Mediante esa función, obtengo la dirección IP del ordenador donde se inicia la sesión, pero esa IP es la IP local del ordenador que inicia la sesión, aunque este se encuentre en otra red. Esto es, obtengo una IP que, si no sé nada más, no puedo garantizar que no sea remota.

Lo ejemplificaré para que quede claro, ya no digo más claro pq comprendo que explicarlo con palabras es complicado. Dispongo de 2 redes, con un ordenador en cada red:
Red 1.1.1.0 (mascara 255.255.255.0) (ip pública 1.1.1.1)
-Ordenador 192.168.253.101

Red 1.1.2.0 (mascara 255.255.255.0) (ip pública 1.1.2.1)
-Ordenador 192.168.253.102

Si hacemos un http://www.whatismyip.com/ desde 1.1.1.101 obtengo, lógicamente, 1.1.1.1. Análogo si lo hago desde 1.1.2.101, obteniendo 1.1.2.1.
En cambio, si me conecto mediante TS del ordenador de la red 1.1.1.0 (192.168.253.101) al ordenador de la red 1.1.2.0 (192.168.253.102), la API de Windows enlazada antes me devuelve 192.168.253.101, por lo que con mi mascara llego a la conclusión de que el ordenador 192.168.253.101 está en mi misma red.

Entonces, la pregunta en cüestión es: Sabiendo las IPs que soy capaz de conocer, o mediante la API de Windows, hay algún modo de llegar a saber que la red del ordenador 192.168.253.101 es la red 1.1.1.0? Alguna API me puede decir la IP de la conexió de TS, no del ordenador que ha iniciado la sesión?

Muchísimas gracias a todos.

PD: Ahora viene cuando alguien me refresca algo básico que he olvidado completamente y que resuelve el problema de un modo trivial.

seoane 25-06-2007 17:28:02

No se mucho de Terminal Service, pero que en la pagina de microsoft pongan lo siguiente, no parece una buena señal:
Cita:

Because the address may not be the actual network address, it should not be used as a form of client authentication.
Por otra lado, se me ocurre que puedes examinar las conexiones TCP establecidas, buscando las que corresponden al "Terminal Service", y ver entonces cual es la ip remota de cada conexión. Tendrías que probarlo, pero yo creo que puede salir bien.

samupe 25-06-2007 22:42:57

IP's en Terminal Service
 
Hola rcuevas:

Yo estoy intentando hacer algo parecido, el servidor al detectar una conexión de TS debería leer la IP del equipo cliente pero no tengo ni idea de como meterle mano.

Gracias,

rcuevas 26-06-2007 08:56:42

Bueno, samupe, te dejo esta unidad a ver si con esto tienes suficiente para lo tuyo.

Tienes 3 funciones:
- una te dice si existe o no sesión de TS en el ordenadorr,
- las otras 2, en caso de que exista sesión, la IP y nombre de ordenador.

Suerte!!

Código Delphi [-]
unit WTSManager;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

const
  // The WM_WTSSESSION_CHANGE message notifies applications of changes in session state.
  WM_WTSSESSION_CHANGE = $2B1;

  // wParam values:
  WTS_CONSOLE_CONNECT = 1;
  WTS_CONSOLE_DISCONNECT = 2;
  WTS_REMOTE_CONNECT = 3;
  WTS_REMOTE_DISCONNECT = 4;
  WTS_SESSION_LOGON = 5;
  WTS_SESSION_LOGOFF = 6;
  WTS_SESSION_LOCK = 7;
  WTS_SESSION_UNLOCK = 8;
  WTS_SESSION_REMOTE_CONTROL = 9;
 
  WTS_CURRENT_SERVER_HANDLE = THandle(0);
  {$EXTERNALSYM WTS_CURRENT_SERVER_HANDLE}
  WTS_CURRENT_SESSION = DWORD(-1);
  {$EXTERNALSYM WTS_CURRENT_SESSION}
 
  // Only session notifications involving the session attached to by the window
  // identified by the hWnd parameter value are to be received.
  NOTIFY_FOR_THIS_SESSION = 0;
  // All session notifications are to be received.
  NOTIFY_FOR_ALL_SESSIONS = 1;
 
  SM_REMOTESESSION = $1000;
 
type
  TWTSConnectStateClass = (WTSActive, WTSConnected, WTSConnectQuery,
    WTSShadow, WTSDisconnected, WTSIdle, WTSListen, WTSReset, WTSDown,
    WTSInit);
 
  TWTSInfoClass = (WTSInitialProgram, WTSApplicationName, WTSWorkingDirectory,
    WTSOEMId, WTSSessionId, WTSUserName, WTSWinStationName, WTSDomainName,
    WTSConnectState, WTSClientBuildNumber, WTSClientName, WTSClientDirectory,
    WTSClientProductId, WTSClientHardwareId, WTSClientAddress, WTSClientDisplay,
    WTSClientProtocolType);
 
  TWTSSessionInfoW = packed record
    SessionID: DWord;
    pWinStationName: PWideChar;
    State: TWTSConnectStateClass;
  end;
  PWTSSessionInfoW = ^TWTSSessionInfoW;
 
  TWTS_CLIENT_ADDRESS = record
    AddressFamily: DWORD;           // AF_INET, AF_IPX, AF_NETBIOS, AF_UNSPEC
    Address: array [0..19] of BYTE; // indirizzo IP del client
  end;
  PWTS_CLIENT_ADDRESS = ^TWTS_CLIENT_ADDRESS;
 
 
 
  TWTSEnumerateSessionsW = function (hServer: THandle; Reserved, Version: DWord;
     out ppSessionInfo: PWTSSessionInfoW; out pCount: DWord): Bool; stdcall;
  {$EXTERNALSYM TWTSEnumerateSessionsW}
  TWTSQuerySessionInformation = function (hServer: THandle; SessionId: DWord;
    WTS_INFO_CLASS: TWTSInfoClass; var ppBuffer: Pointer; var pBytesReturned: DWord): Bool; stdcall;
  {$EXTERNALSYM TWTSQuerySessionInformation}
  TWTSFreeMemory = procedure (PMemory: pointer); stdcall;
  {$EXTERNALSYM TWTSFreeMemory}
 
function WTSInSession: boolean;
function WTSComputerIP: string;
function WTSComputerName: string;
 
var
  hWTSapi32dll: THandle;
  WTSEnumerateSessions: TWTSEnumerateSessionsW;
  WTSQuerySessionInformation: TWTSQuerySessionInformation;
  WTSFreeMemory: TWTSFreeMemory;
 
implementation
 
var
  FWTSApiDispo: boolean;
 
 
function WTSInSession: boolean;
begin
  result := GetSystemMetrics(SM_REMOTESESSION) <> 0;
end;
 
 
function WTSComputerIP: string;
var buf: pointer;
  nbcar: DWord;
  bon: boolean;
begin
  result := '';
  buf := nil;
  if FWTSApiDispo and WTSInSession then
  begin
    bon := WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSClientAddress, buf, nbcar);
    if bon then
    begin
      try
        with TWTS_CLIENT_ADDRESS(buf^) do
          result := IntToStr(Address[2]) + '.' +
                    IntToStr(Address[3]) + '.' +
                    IntToStr(Address[4]) + '.' +
                    IntToStr(Address[5]);
      finally
        WTSFreeMemory(buf);
      end;
    end
  end
end;
 
function WTSComputerName: string;
var pbuf: pointer;
  nbcar: DWord;
  bon: boolean;
begin
  result := '';
  pbuf := nil;
  nbcar := 100;
  if FWTSApiDispo and WTSInSession then
  begin
    bon := WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSClientName, pbuf, nbcar);
    if bon then
    begin
      try
        result := trim(PChar(pbuf));
      finally
        WTSFreeMemory(pbuf);
      end;
    end
  end
end;
 
initialization
  hWTSAPI32DLL := LoadLibrary('Wtsapi32.dll');
  if hWTSAPI32DLL > 0 then
  begin
    FWTSApiDispo := true;
    @WTSEnumerateSessions := GetProcAddress(hWTSAPI32DLL, 'WTSRegisterSessionNotification');
    @WTSQuerySessionInformation := GetProcAddress(hWTSAPI32DLL, 'WTSQuerySessionInformationA');
    @WTSFreeMemory := GetProcAddress(hWTSAPI32DLL, 'WTSFreeMemory');
  end
  else
    FWTSApiDispo := false;
 
finalization
  if hWTSapi32dll > 0 then
    FreeLibrary(hWTSAPI32DLL);
 
end.

samupe 28-06-2007 07:27:24

IP's en Terminal Service
 
Gracias rcuevas, me ha venido como anillo al dedo. Ahora con un poquito de tiempo podré implementarlo...


La franja horaria es GMT +2. Ahora son las 18:24:14.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi