📄 ras.c
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// This source code is licensed under Microsoft Shared Source License
// Version 1.0 for Windows CE.
// For a copy of the license visit http://go.microsoft.com/fwlink/?LinkId=3223.
//
/*****************************************************************************
*
*
* @doc EX_RAS
* ras.c Remote Access Server
*
* Date: 6-22-95
*
*/
// Include Files
#include "windows.h"
#include "cxport.h"
#include "crypt.h"
#include "memory.h"
// VJ Compression Include Files
#include "ndis.h"
#include "tcpip.h"
#include "vjcomp.h"
// PPP Include Files
#include "protocol.h"
#include "ppp.h"
#include "lcp.h"
#include "ipcp.h"
#include "ipv6cp.h"
#include "ncp.h"
#include "mac.h"
#include "raserror.h"
#include "pppserver.h"
#include "auth.h"
#include "netui.h"
// Externs
extern PPP_CONTEXT *pppContextList;
extern CRITICAL_SECTION v_ListCS;
extern DWORD LoadLoggingExtension();
extern DWORD UnloadLoggingExtension();
// Defines
DWORD
ShowUsernamePasswordDialog(pppSession_t *s_p)
{
NETUI_USERPWD UserPwd;
DWORD dwRetCode = NO_ERROR;
HWND hParent = NULL;
BOOL bGotUserInfo;
RASDIALPARAMS RasDialParams;
BOOL bPasswordSaved;
if (s_p->notifierType == 0xFFFFFFFF)
{
hParent = (HWND) s_p->notifier;
}
// Set up the UserPwd structure
memset ((char *)&UserPwd, 0, sizeof(UserPwd));
_tcscpy (UserPwd.szUserName, s_p->rasDialParams.szUserName);
_tcscpy (UserPwd.szPassword, s_p->rasDialParams.szPassword);
_tcscpy (UserPwd.szDomain, s_p->rasDialParams.szDomain);
//
// If a password is currently saved in the registry for the connectoid,
// check the save password box.
//
RasDialParams.dwSize = sizeof(RasDialParams);
wcscpy(RasDialParams.szEntryName, s_p->rasDialParams.szEntryName);
if (RasGetEntryDialParams(NULL, &RasDialParams, &bPasswordSaved) == NO_ERROR)
{
if (bPasswordSaved)
UserPwd.dwFlags |= NETUI_USERPWD_SAVEPWD;
}
// Show the save password box.
UserPwd.dwFlags |= NETUI_USERPWD_SHOW_SAVEPWD;
DEBUGMSG( ZONE_PPP, (TEXT("GetUsernamePassword:About to call CallGetUsernamePassword()\r\n")));
bGotUserInfo = CallGetUsernamePasswordEx(hParent, &UserPwd, &s_p->hPasswordDialog);
s_p->hPasswordDialog = NULL;
pppLock(s_p);
if (bGotUserInfo)
{
// Copy the data back.
_tcscpy (s_p->rasDialParams.szUserName, UserPwd.szUserName);
_tcscpy (s_p->rasDialParams.szPassword, UserPwd.szPassword);
_tcscpy (s_p->rasDialParams.szDomain, UserPwd.szDomain);
RasSetEntryDialParams( NULL, &(s_p->rasDialParams),
!(UserPwd.dwFlags & NETUI_USERPWD_SAVEPWD) );
}
else
{
// Presumably user cancelled the dialog, they do not want to connect.
dwRetCode = ERROR_USER_DISCONNECTION;
}
pppUnLock(s_p);
DEBUGMSG( ZONE_PPP, (TEXT("GetUserThread:GetUsernamePassword() dwRetCode=%d\r\n"), dwRetCode));
return dwRetCode;
}
/*****************************************************************************
*
* @func DWORD | RasDial | RAS Dial Entry point
*
* @rdesc 0 success, otherwise one of the errors from raserror.h
* @ecode ERROR_NOT_ENOUGH_MEMORY | Unable to allocate memory.
* @ecode ERROR_CANNOT_FIND_PHONEBOOK_ENTRY | Invalid RasEntry name specified
* @ecode ERROR_EVENT_INVALID | PPP Internal error
* @ecode ERROR_PORT_ALREADY_OPEN | Port already open
* @ecode ERROR_PPP_MAC | Error initializing MAC layer
* @ecode ERROR_PPP_LCP | Error initializing LCP layer
* @ecode ERROR_PPP_AUTH | Error initializing AUTH layer
* @ecode ERROR_PPP_NCP | Error initializing NCP layer
*
* @parm LPRASDIALEXTENSIONS | dialExtensions | The dial extensions
* @parm LPTSTR | phoneBookPath | The phonebook
* @parm LPRASDIALPARAMS | rasDialParam | Dial Params
* @parm DWORD | NotifierType | Notifier type
* @parm LPVOID | notifier | Pointer to Notifier (or window handle)
* @parm LPHRASCONN | pRasConn | Returned pointer to Ras connection
*
*
*/
DWORD APIENTRY
AfdRasDial
(
LPRASDIALEXTENSIONS dialExtensions,
LPTSTR phoneBookPath,
LPRASDIALPARAMS rasDialParam,
DWORD NotifierType,
LPVOID notifier,
LPHRASCONN pRasConn
)
{
pppStart_t Start;
RASENTRY *RasEntry_p;
DWORD dwSize;
BOOL rc;
DWORD RetVal;
pppSession_t *pSession;
if (!g_bPPPInitialized)
return ERROR_SERVICE_NOT_ACTIVE;
// Parameter Notes:
//
// 1. dialExtensions: TBD
// 2. phoneBookPath: specifies the full pathname of the phone book
// path. If null then the default phone book is
// used.
// 3. rasDialParam: Name, phone, password etc - depending on dialer
// this needs work.
// 4. NotifierType: type of rasdial event handler - used
// 5. notifier: window or call back function for events.
// 6. pRasConn: connection handle - used
DEBUGMSG( ZONE_RAS | ZONE_FUNCTION, (
TEXT( "->AfdRasDial( %08X, %08X, %08X, %08X, %08X, %08X )\r\n" ),
dialExtensions, phoneBookPath, rasDialParam, NotifierType,
notifier, pRasConn ) );
if (NULL == pRasConn)
return ERROR_INVALID_PARAMETER;
//
// Validate the notifier and NotifierType parameters
//
if (notifier != NULL)
{
if (NotifierType != 0xFFFFFFFF)
{
//
// CE RasDial only supports window message notifier types
//
DEBUGMSG(ZONE_ERROR, (TEXT("PPP: -AfdRasDial: Only window message notifier type allowed\n")));
return ERROR_INVALID_PARAMETER;
}
if ((g_pfnPostMessageW == NULL))
{
//
// Window manager support not present
//
DEBUGMSG(ZONE_ERROR, (TEXT("PPP: -AfdRasDial: PostMessage support not available\r\n")));
return ERROR_INVALID_PARAMETER;
}
}
// Allocate room for the ras entry.
RasEntry_p = (RASENTRY *)LocalAlloc( LMEM_FIXED, sizeof( RASENTRY ) );
if( NULL == RasEntry_p )
{
DEBUGMSG( ZONE_RAS | ZONE_ERROR, (
TEXT( "<-AfdRasDial:LocalAlloc failed\r\n" )));
return( ERROR_NOT_ENOUGH_MEMORY );
}
memset( RasEntry_p, 0, sizeof( RASENTRY ) );
// Read the RasEntry
RasEntry_p->dwSize = sizeof( RASENTRY );
dwSize = sizeof( RASENTRY );
Start.dwDevConfigSize = 0;
Start.lpbDevConfig = 0;
RetVal = AfdRasGetEntryProperties( phoneBookPath,
rasDialParam->szEntryName,
(LPBYTE)RasEntry_p,
&dwSize,
NULL,
&Start.dwDevConfigSize);
// We will get a ERROR_BUFFER_TOO_SMALL error
// if there is a devconfig associated with this connection
if (RetVal && ((RetVal != ERROR_BUFFER_TOO_SMALL) &&
(Start.dwDevConfigSize))) {
DEBUGMSG( ZONE_RAS | ZONE_ERROR, (
TEXT("Unable to open RAS Entry %s\r\n"), rasDialParam->szEntryName ));
LocalFree( RasEntry_p );
return( ERROR_CANNOT_FIND_PHONEBOOK_ENTRY );
}
if (Start.dwDevConfigSize) {
Start.lpbDevConfig = LocalAlloc (LPTR, Start.dwDevConfigSize);
// Read the structure in again.
if( AfdRasGetEntryProperties( phoneBookPath,
rasDialParam->szEntryName,
(LPBYTE)RasEntry_p,
&dwSize, Start.lpbDevConfig,
&Start.dwDevConfigSize ))
{
DEBUGMSG( ZONE_RAS | ZONE_ERROR, (
TEXT("Unable to open RAS Entry %s\r\n"),
rasDialParam->szEntryName ));
LocalFree( RasEntry_p );
LocalFree (Start.lpbDevConfig);
return( ERROR_CANNOT_FIND_PHONEBOOK_ENTRY );
}
}
*pRasConn = (HRASCONN )NULL; // init callers handle
// Create the PPP Session - Fill in the start structure:
// DevConfig already filled in.
// Allocate a new IP adapter context
Start.context = pppContextNew();
if (Start.context == NULL)
{
LocalFree( RasEntry_p );
LocalFree (Start.lpbDevConfig);
return ERROR_NOT_ENOUGH_MEMORY;
}
Start.session = NULL; // clear session pointer
Start.notifierType = NotifierType; // type of notifier
Start.notifier = notifier; // notifier function
Start.rasDialParams = rasDialParam; // pntr to dialer parameters
Start.rasEntry = RasEntry_p; // Pntr to the RASENTRY struct.
Start.bIsServer = FALSE; // We're a client, connecting to a server
//
// Note that if pppSessionNew is succesful then the new
// session will be exposed to applications via the RasEnumConnections
// API, and thus could be closed via a call to RasHangup at any time.
//
rc = pppSessionNew( &Start );
pSession = Start.session;
LocalFree(RasEntry_p ); // free ras entry memory
LocalFree(Start.lpbDevConfig);
if( SUCCESS != rc || NULL == pSession)
{
DEBUGMSG( ZONE_RAS | ZONE_ERROR, (TEXT( "PPP: AfdRasDial:SESSION_NEW failed\r\n" )));
return rc;
}
*pRasConn = (HRASCONN )pSession; // set callers handle
//
// Since we just told the app about the session with the above assign,
// the app could call RasHangup at any time. Get a ref to the session
// to make sure that while we are starting things up it does not go
// away unexpectedly...
//
if (PPPADDREF(pSession, REF_RASDIAL))
{
// Start the Session
if(pSession->rasEntry.dwfOptions & RASEO_PreviewUserPw)
{
// User could cancel this dialog, resulting in non-zero rc, aborting the connection
rc = ShowUsernamePasswordDialog(pSession);
}
if (rc == NO_ERROR)
{
rc = pppSessionRun(pSession);
DEBUGMSG(rc && ZONE_ERROR, (TEXT("PPP: ERROR - RasDial: pppSessionRun returned %u\r\n"), rc));
}
PPPDELREF(pSession, REF_RASDIAL);
DEBUGMSG( ZONE_RAS, ( TEXT("<-AfdRasDial:SESSION_RUN OK\r\n" )));
}
return rc;
}
static void
pppSessionCloseComplete(
PVOID pData)
{
HANDLE hCloseCompleteEvent = (HANDLE)pData;
DEBUGMSG( ZONE_FUNCTION, ( TEXT( "PPP:+pppSessionCloseComplete\n" ) ));
SetEvent(hCloseCompleteEvent);
DEBUGMSG( ZONE_FUNCTION, ( TEXT( "PPP:-pppSessionCloseComplete\n" ) ));
}
/*****************************************************************************
*
* @func DWORD | RasHangup | RAS Hangup Entry point
*
* @rdesc 0 success, otherwise one of the following errors:
* @ecode ERROR_INVALID_PARAMETER | Invalid hRasConn parameter.
*
* @parm HRASCONN | hRasConn | Handle to connection to close
*
*
*/
DWORD APIENTRY
AfdRasHangUp( HRASCONN RasConn )
{
DWORD rc;
pppSession_t *s_p = (pppSession_t *)RasConn;
HANDLE hCloseCompleteEvent;
DEBUGMSG( ZONE_RAS, ( TEXT( "PPP:+AfdRasHangUp( %08X )\r\n" ), RasConn ));
if (PPPADDREF(s_p, REF_RASHANGUP))
{
hCloseCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (hCloseCompleteEvent != NULL)
{
// Tell everyone that we're closing
s_p->context->fOpen = FALSE;
// Close any password dialog that is open
if (s_p->hPasswordDialog)
{
CallCloseUsernamePasswordDialog(s_p->hPasswordDialog);
s_p->hPasswordDialog = NULL;
}
// Request Session stop
rc = pppSessionStop(s_p, pppSessionCloseComplete, hCloseCompleteEvent);
if (ERROR_SUCCESS == rc)
WaitForSingleObject(hCloseCompleteEvent, INFINITE);
CloseHandle(hCloseCompleteEvent);
}
else
{
rc = ERROR_OUTOFMEMORY;
}
PPPDELREF(s_p, REF_RASHANGUP);
}
else
{
DEBUGMSG (ZONE_ERROR, (TEXT("PPP:!AfdRasHangup: Invalid RasConn 0x%X\r\n"),
RasConn));
rc = ERROR_INVALID_PARAMETER;
}
DEBUGMSG( ZONE_RAS, (TEXT("PPP:- AfdRasHangUp: Returning %d\r\n"), rc));
return rc;
}
/*****************************************************************************
*
* @func DWORD | RasEnumConnections | Lists all active RAS connections.
* It returns each connection's
* handle and phone book entry name.
*
* @rdesc DWORD 0 success, !0 failure code defined in raserror.h
* @ecode ERROR_INVALID_SIZE | lpRasConn->dwSize is invalid.
* @ecode ERROR_BUFFER_TOO_SMALL | Buffer size is too small.
*
* @parm LPRASCONN | lpRasConn | buffer to receive connections data
* @parm LPDWORD | lpcb | buffer size in bytes
* @parm LPDWORD | lpcConnections | number of connections written
* to buffer
*
*
*/
DWORD APIENTRY
AfdRasEnumConnections
(
LPRASCONN lpRasConn,
LPDWORD lpcb,
LPDWORD lpcConnections
)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -