VTTelnetDaemon.cpp
上传用户:wsk323
上传日期:2007-01-05
资源大小:403k
文件大小:4k
- /*---------------------------------------------------------------------------
- Copyright: E. Brady Trexler
- Creation: March 13, 1998 (based on F. Piette's telnet server demo).
- A lot of code was lifted from his sources.
- Description: VTTelnetDaemon -- a class that owns a TWSocket component.
- The TWSocket listens to the telnet port and creates an
- instance of a VTTelnetSession to start the session.
- Legal issues: Copyright (C) 1998 by E. Brady Trexler and Fran鏾is Piette
- This software is provided 'as-is', without any express or
- implied warranty. In no event will the author be held liable
- for any damages arising from the use of this software.
- Permission is granted to anyone to use this software for any
- purpose, excluding commercial applications, and to alter it
- and redistribute it freely, subject to the following
- restrictions:
- 1. The origin of this software must not be misrepresented,
- you must not claim that you wrote the original software.
- If you use this software in a product, an acknowledgment
- in the product documentation would be appreciated but is
- not required.
- 2. Altered source versions must be plainly marked as such, and
- must not be misrepresented as being the original software.
- 3. This notice may not be removed or altered from any source
- distribution.
- Updates:
- ---------------------------------------------------------------------------*/
- //---------------------------------------------------------------------------
- #include <vclvcl.h>
- #pragma hdrstop
- #include <stdio.h>
- #include <stdlib.h>
- #include <LMACCESS.H>
- #include <LMERR.H>
- #include "VTTelnetDaemon.h"
- #include "VTTelnetSession.h"
- //---------------------------------------------------------------------------
- #pragma link "WSocket"
- #pragma resource "*.dfm"
- TVTDaemon *VTDaemon;
- //---------------------------------------------------------------------------
- __fastcall TVTDaemon::TVTDaemon(TComponent* Owner)
- : TForm(Owner)
- {
- TheSessions = new TList;
- char inifile[1024];
- char *temp;
- int port;
- FILE *fd = NULL;
- //try application directory
- AnsiString s = Application->ExeName;
- int ext = s.LastDelimiter("\.:");
- s.SetLength(ext);
- s = s + "ini";
- fd = fopen(s.c_str(), "rt");
- if (fd == NULL){
- strcpy(inifile, getenv("SYSTEMROOT"));
- strcat(inifile, "\system32\telnetd.ini");
- fd = fopen(inifile, "rt");
- }
- if (fd != NULL){
- fscanf(fd, "port=%dn", &port);
- fread(inifile, 1024, 1, fd);
- strtok(inifile, "=");
- temp = strtok(NULL, "~");
- strcpy(WelcomeMessage, temp);
- temp = strtok(NULL, "=");
- temp = strtok(NULL, "~");
- strcpy(ExitMessage, temp);
- fclose(fd);
- SrvSocket->Port = port;
- }
- SrvSocket->Listen();
- }
- //---------------------------------------------------------------------------
- void __fastcall TVTDaemon::SrvSocketSessionAvailable(TObject *Sender, WORD Error)
- {
- TheSessions->Add(new VTSession(this));
- }
- //---------------------------------------------------------------------------
- void __fastcall TVTDaemon::SrvSocketSessionClosed(TObject *Sender, WORD Error)
- {
- if (SrvSocket->State == wsOpened) SrvSocket->Close();
- Close();
- }
- //---------------------------------------------------------------------------
- void __fastcall TVTDaemon::WMDisconnect(TMessage Message)
- {
- VTSession *Session = (VTSession *)(Message.LParam);
- #ifdef SERVICE
- CloseHandle(Session->UserToken);
- #endif
- Session->Socket->Shutdown(2);
- Session->Socket->Close();
- Session->Socket->Free();
- TheSessions->Remove(Session);
- }
- //---------------------------------------------------------------------------
- int __fastcall TVTDaemon::GetNumSessions()
- {
- return TheSessions->Count;
- }
- //---------------------------------------------------------------------------
-