int RsaDial(LPCTSTR szEntry, LPCTSTR szUserName, LPCTSTR szPassword, HWND hDlgWnd) { UINT i = 0; DWORD dwRet = 0; RASDIALPARAMS rdParams; ZeroMemory(&rdParams, sizeof(RASDIALPARAMS)); rdParams.dwSize = sizeof(RASDIALPARAMS); lstrcpy(rdParams.szEntryName, szEntry); lstrcpy(rdParams.szUserName, szUserName); lstrcpy(rdParams.szPassword, szPassword); ghRasConn = NULL; dwRet = RasDial(NULL, NULL, &rdParams, 1, &RasDiafunc1, &ghRasConn); TCHAR str[20]; wsprintf(str, L"%u", dwRet); lstrcat(str, L"错误!"); /*if (dwRet != 0) { MessageBox(hDlgWnd, str, L"错误", MB_OK); }*/ return 0; }
// // RasCustomDial // // This export is the custom dial entry when RasDial has been called by // an application - the call is forwarded to here and it is up to this // function then to obtain a connection. This implementation simply // forwards the request back to RasDial. Note that we must add the // RDEOPT_CustomDial flag to the RasDialExtensions structure so we don't // recursively get called. // extern "C" DWORD WINAPI RasCustomDial ( HINSTANCE hInstDll, // handle to DLL instance LPRASDIALEXTENSIONS lpRasDialExtensions, // pointer to function extensions data LPCTSTR lpszPhonebook, // pointer to full path and file name of phone-book file LPRASDIALPARAMS lpRasDialParams, // pointer to calling parameters data DWORD dwNotifierType, // specifies type of RasDial event handler LPVOID lpvNotifier, // specifies a handler for RasDial events LPHRASCONN lphRasConn // pointer to variable to receive connection handle ) { DWORD rc = ERROR_SUCCESS; // return code from RasDial RASDIALEXTENSIONS RasDialExtensions; // used as a copy of the RasDialExtensions structure OutputTraceString(L"RasCustomDial called in customdial.dll\n"); if (NULL == lpRasDialParams) { return ERROR_UNKNOWN; } // set up the RasDialExtensions structure appropriately if (NULL == lpRasDialExtensions) { ZeroMemory(&RasDialExtensions, sizeof(RASDIALEXTENSIONS)); RasDialExtensions.dwSize = sizeof(RASDIALEXTENSIONS); } else { CopyMemory(&RasDialExtensions, lpRasDialExtensions, sizeof(RASDIALEXTENSIONS)); } RasDialExtensions.dwfOptions |= RDEOPT_CustomDial; // added cause we are calling RasDial OutputTraceString(L"--- RasCustomDial RasDialParams: E: '%s' UN: '%s' D: '%s'\n", lpRasDialParams->szEntryName, lpRasDialParams->szUserName, lpRasDialParams->szDomain); // Simply call RasDial with the modified RasDialExtensions structure // and the original parameters passed in. // We are passing lpvNotifier & lphRasConn directly into RasDial. Since we don't use these variable // locally we skip checking for NULLs. rc = RasDial(&RasDialExtensions, lpszPhonebook, lpRasDialParams, dwNotifierType, lpvNotifier, lphRasConn); OutputTraceString(L"--- RasCustomDial returning handle 0x%08x\n", *lphRasConn); OutputTraceString(L"RasCustomDial exiting customdial.dll with return code: %d\n", rc); return rc; }
int Dial(const char * szEntryName) { LPRASDIALPARAMS lpRasDialParams = NULL; // Structure to store the RasDial parameters HRASCONN hRasConn = NULL; // Handle to RAS connection DWORD nRet = 0; // Return value from a function BOOL bPassword; // Initialize the RASDIALPARAMS structure lpRasDialParams = (LPRASDIALPARAMS) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RASDIALPARAMS)); if (NULL == lpRasDialParams) { printf("HeapAlloc failed\r\n"); return -1; } gszLogData[0] = 0; strcpy_s(lpRasDialParams->szEntryName, szEntryName); lpRasDialParams->dwSize =sizeof(RASDIALPARAMS); RasGetEntryDialParams(NULL, lpRasDialParams, &bPassword); nRet = RasDial(NULL, NULL, lpRasDialParams, 0, &RasDialFunc, &hRasConn); return nRet; }
DWORD CWirelessManagerDlg::DialRasNetwork() { //拨号之前先判断上次拨号的句柄是否已释放 if (m_hRasConn != NULL) { RasHangUp(m_hRasConn); m_hRasConn = NULL; } if (m_dwReDialCnt > 5) { RasControl(enRAS_Hangup); m_dwReDialCnt = 0; return 0; } m_dwReDialCnt++; g_bIsDialing = TRUE; RASDIALPARAMS RasDialParams; //RAS结构包含用来建立RAS的参数 //下面开始初始化拨号参数 memset((char*)&RasDialParams, 0, sizeof(RasDialParams)); RasDialParams.dwSize = sizeof(RASDIALPARAMS); //结构大小 wcscpy(RasDialParams.szEntryName, /*_T("cmnet")*/ENTRY_NAME); //拨号网络中建立的电话簿名 wcscpy(RasDialParams.szPhoneNumber , _T("")); //*99***# wcscpy(RasDialParams.szCallbackNumber,_T("")); //回拨号码,不用时置为NULL wcscpy(RasDialParams.szUserName,_T("")); //用户名 wcscpy(RasDialParams.szPassword,_T("")); //用户口令 wcscpy(RasDialParams.szDomain,_T("")); //用户权限验证域 RASDIALPARAMS ras_para_test; memset(&ras_para_test, NULL, sizeof(RASDIALPARAMS)); int nRasResult = RasDial(NULL, NULL, &RasDialParams, 0xFFFFFFFF, m_hWnd, &m_hRasConn); g_bIsDialing = FALSE; return nRasResult; }
BOOL dial() { DWORD error = ERROR_SUCCESS; // get old username RASCREDENTIALS rasCredentials; rasCredentials.dwMask = 0; rasCredentials.dwMask |= RASCM_UserName; rasCredentials.dwMask |= RASCM_Password; rasCredentials.dwSize = sizeof(RASCREDENTIALS); error = RasGetCredentials(PHONE_BOOK, ENTRY_NAME_W, &rasCredentials); if (error != ERROR_SUCCESS) { fwprintf(stderr, L"[dial] RasGetCredentials error=%d\n", error); return FALSE; } if (!(rasCredentials.dwMask & RASCM_UserName)) { fwprintf(stderr, L"[dial] RasGetCredentials doesn't contain username\n"); return FALSE; } if (!(rasCredentials.dwMask & RASCM_Password)) { fwprintf(stderr, L"[dial] RasGetCredentials doesn't contain password\n"); return FALSE; } // Load username to rasCredentials.szUserName, if previously encrypted, restore it if (wcsncmp(rasCredentials.szUserName, L"\r\n", CRLF_LENGTH) == 0) { wprintf(L"Previous encrypted username found: %ls\n", rasCredentials.szUserName); lstrcpy(rasCredentials.szUserName, rasCredentials.szUserName + CRLF_LENGTH + PROC1_OUT_LENGTH + MD5_HEAD_LENGTH); } else { wprintf(L"Previous vanilla username found\n"); } wprintf(L"Vanilla username: %ls\n", rasCredentials.szUserName); // Get ANSI username, calcPIN, and make the result Unicode again. char strUserName[MAX_USERNAME_LENGTH + 1] = {0}; char exinDynaAccount[MAX_USERNAME_LENGTH + 1] = {0}; BOOL ok = WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, rasCredentials.szUserName, -1, // NULL-terminated strUserName, MAX_USERNAME_LENGTH, NULL, NULL); if (!ok) { fwprintf(stderr, L"[dial] WideCharToMultiByte error=%d\n", GetLastError()); return FALSE; } time_t t; time(&t); calcPIN(t, strUserName, RAD, exinDynaAccount); ok = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED | MB_ERR_INVALID_CHARS, exinDynaAccount, -1, // NULL-terminated rasCredentials.szUserName, UNLEN); if (!ok) { fwprintf(stderr, L"[dial] MultiByteToWideChar error=%d\n", GetLastError()); return FALSE; } wprintf(L"Newly encrtpted username: %ls\n", rasCredentials.szUserName); // set credentials error = RasSetCredentials(PHONE_BOOK, ENTRY_NAME_W, &rasCredentials, FALSE); if (error) { fwprintf(stderr, L"[dial] RasSetCredentials error=%d\n", error); return FALSE; } // dial RASDIALPARAMS rdParams; LPWSTR entryName = ENTRY_NAME_W; lstrcpy(rdParams.szEntryName, entryName); rdParams.dwSize = sizeof(RASDIALPARAMS); rdParams.szPhoneNumber[0] = '\0'; rdParams.szCallbackNumber[0] = '\0'; rdParams.szDomain[0] = '\0'; lstrcpy(rdParams.szUserName, rasCredentials.szUserName); lstrcpy(rdParams.szPassword, rasCredentials.szPassword); HRASCONN hRasConn = NULL; error = RasDial(NULL, NULL, &rdParams, 0L, NULL, &hRasConn); if (error) { fwprintf(stderr, L"[dial] RasDial error=%d\n", error); RasHangUp(hRasConn); } return !error; }
value_t c_win32_dial ( value_t _mt, value_t _entryName, value_t phoneNumber, value_t userName, value_t password, value_t domain, value_t callback ) { char * entryName = String_val ( _entryName ); int mt = Bool_val ( _mt ); DWORD dwRet; RASDIALPARAMS rdParams; HRASCONN hRasConn; CAMLparam5 ( mt, _entryName, phoneNumber, userName, password ); CAMLxparam2 ( domain, callback ); printf ( "Callback passed = 0x%08x, deref = 0x%08x\n", (unsigned) callback, (unsigned)(*(void **)callback) ); fflush ( stdout ); hRasConn = NULL; rdParams.dwSize = sizeof(RASDIALPARAMS); lstrcpy(rdParams.szEntryName, entryName ); lstrcpy(rdParams.szPhoneNumber, String_val ( phoneNumber ) ); lstrcpy(rdParams.szCallbackNumber, "" ); lstrcpy(rdParams.szUserName, String_val ( userName ) ); lstrcpy(rdParams.szPassword, String_val ( password ) ); lstrcpy(rdParams.szDomain, String_val ( domain ) ); cb_info.g_status = 0; cb_info.mt = mt; cb_info.p_closure = &callback; cb_info.entryName = entryName; textout ( mtINFO, "Dialing %s", entryName ); if (debug_print) printf ( "I am inside c_win32_dial!\n" ); if ( mt ) enter_blocking_section (); dwRet = RasDial ( NULL, NULL, &rdParams, 1L, (RASDIALFUNC) RasDialFunc1, &hRasConn ); if ( mt ) leave_blocking_section (); if ( dwRet ) { char szBuf[256]; if ( RasGetErrorString( dwRet, szBuf, 256 ) != 0 ) wsprintf( (LPSTR)szBuf, "Undefined RAS Dial Error (%ld).", dwRet ); textout ( mtERR, "Error attempting to connect: %s", szBuf ); hangup ( hRasConn ); } CAMLreturn (Val_bool ( 1 )); return 0; /* dummy, to shut down warning */ }
int __cdecl main(int argc, char **argv) { LPRASDIALPARAMS lpRasDialParams = NULL; HRASCONN hRasConn = NULL; LPRASCONNSTATUS lpRasConnStatus = NULL; RASPPPIP *lpProjection = NULL; int i = 0; int j = 0; DWORD dwRet = 0; DWORD cb = sizeof(RASDIALPARAMS); DWORD dwMaxTickCount = 0; BOOL fRequired = FALSE; BOOL fSuccess = FALSE; TCHAR szTempBuf[256] = {0}; lpRasDialParams = (LPRASDIALPARAMS)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cb); if (NULL == lpRasDialParams) { printf("HeapAlloc failed\n"); return ERROR_OUTOFMEMORY; } lpRasDialParams->dwSize = sizeof(RASDIALPARAMS); // Copy command line arguments into the RASDIALPARAMS structure if (argc > 1) { for(i = 1; i < (argc - 1); i++) { if (argv[i] && ((argv[i][0] == '-') || (argv[i][0] == '/'))) { switch(tolower(argv[i][1])) { case 'e': // Entry name j = ++i; if (argv[j]) { StringCchCopy(lpRasDialParams->szEntryName, CELEMS(lpRasDialParams->szEntryName), argv[j]); fRequired = TRUE; } break; case 'p': // Phone number j = ++i; if (argv[j]) { StringCchCopy(lpRasDialParams->szPhoneNumber, CELEMS(lpRasDialParams->szPhoneNumber), argv[j]); } break; case 'u': // User name j = ++i; if (argv[j]) { StringCchCopy(lpRasDialParams->szUserName, CELEMS(lpRasDialParams->szUserName), argv[j]); } break; case 'z': // Password j = ++i; if (argv[j]) { StringCchCopy(lpRasDialParams->szPassword, CELEMS(lpRasDialParams->szPassword), argv[j]); } break; case 'd': // Domain name j = ++i; if (argv[j]) { StringCchCopy(lpRasDialParams->szDomain, CELEMS(lpRasDialParams->szDomain), argv[j]); } break; default: Usage(argv[0]); dwRet = ERROR_INVALID_PARAMETER; break; } } else { Usage(argv[0]); dwRet = ERROR_INVALID_PARAMETER; goto done; } } } else { Usage(argv[0]); dwRet = ERROR_INVALID_PARAMETER; goto done; } // Check if we got at least required entry name if (FALSE == fRequired) { Usage(argv[0]); dwRet = ERROR_INVALID_PARAMETER; goto done; } printf("Dialing...\n"); // Calling RasDial synchronously dwRet = RasDial(NULL, NULL, lpRasDialParams, 0, 0L, &hRasConn); if (dwRet) { printf("RasDial failed: Error = %d\n", dwRet); goto done; } lpRasConnStatus = (LPRASCONNSTATUS)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RASCONNSTATUS)); if (NULL == lpRasConnStatus) { printf("HeapAlloc failed.\n"); goto disconnect; } // Set the appropriate size lpRasConnStatus->dwSize = sizeof(RASCONNSTATUS); // Checking connection status using RasGetConnectStatus dwRet = RasGetConnectStatus(hRasConn, lpRasConnStatus); if (ERROR_SUCCESS != dwRet) { printf("RasGetConnectStatus failed: Error = %d\n", dwRet); goto disconnect; } else { // Since the call succeeded, let's see what ras connection state we are in // by using the RASCONNSTATUS structure. if (lpRasConnStatus->rasconnstate == RASCS_Connected) { ZeroMemory((LPVOID)szTempBuf, sizeof(szTempBuf)); StringCchPrintf(szTempBuf, CELEMS(szTempBuf), "Connection estabilished using %s\n", lpRasConnStatus->szDeviceName); printf(szTempBuf); } else { // We don't seem to be connected. Just try to terminate the connection by // hanging up. printf("lpRasConnStatus->rasconnstate = %d\n", lpRasConnStatus->rasconnstate); goto disconnect; } } lpProjection = (RASPPPIP *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RASPPPIP)); if (NULL == lpProjection) { printf("HeapAlloc failed.\n"); dwRet = ERROR_OUTOFMEMORY; goto disconnect; } cb = sizeof(RASPPPIP); lpProjection->dwSize = cb; // Getting the Ras client and server IP address using RasGetProjectionInfo dwRet = RasGetProjectionInfo(hRasConn, RASP_PppIp, lpProjection, &cb); switch (dwRet) { case ERROR_BUFFER_TOO_SMALL: if (HeapFree(GetProcessHeap(), 0, (LPVOID)lpProjection)) { lpProjection = (RASPPPIP *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cb); if (NULL == lpProjection) { printf("HeapAlloc failed.\n"); goto disconnect; } dwRet = RasGetProjectionInfo(hRasConn, RASP_PppIp, lpProjection, &cb); if (ERROR_SUCCESS == dwRet) { fSuccess = TRUE; } else { printf("RasGetProjectionInfo failed: Error %d", dwRet); goto disconnect; } } else { printf("HeapFree failed.\n"); goto disconnect; } break; case ERROR_SUCCESS: fSuccess = TRUE; break; default: printf("RasGetProjectionInfo failed: Error %d", dwRet); goto disconnect; break; } if (fSuccess) { // Print out the IP addresses ZeroMemory((LPVOID)szTempBuf, sizeof(szTempBuf)); StringCchPrintf(szTempBuf, CELEMS(szTempBuf), "\nRas Client IP address: %s\n", lpProjection->szIpAddress); printf(szTempBuf); ZeroMemory((LPVOID)szTempBuf, sizeof(szTempBuf)); StringCchPrintf(szTempBuf, CELEMS(szTempBuf), "Ras Server IP address: %s\n\n", lpProjection->szServerIpAddress); printf(szTempBuf); } printf("Pausing for 5 seconds before disconnecting...\n"); Sleep(5000); disconnect: // Terminating the connection using RasHangUp dwRet = RasHangUp(hRasConn); if (ERROR_SUCCESS != dwRet) { printf("RasHangUp failed: Error = %d", dwRet); goto done; } // Keep checking for 10 seconds and make sure we are really disconnected. // Once the connection is disconnected, RasGetConnectStatus returns ERROR_INVALID_HANDLE. // or our timeout is reached we exit the while loop. // This gives the RAS API time to make sure the modem hangs up before we exit this process. // If a process exits with a connected RAS connection, the port could be stranded. dwMaxTickCount = GetTickCount() + 10000; while((RasGetConnectStatus(hRasConn, lpRasConnStatus) != ERROR_INVALID_HANDLE) && (dwMaxTickCount > GetTickCount())) { Sleep(50); } printf("Diconnected\n"); dwRet = ERROR_SUCCESS; done: if (lpProjection) { HeapFree(GetProcessHeap(), 0, (LPVOID)lpProjection); } if (lpRasDialParams) { HeapFree(GetProcessHeap(), 0, (LPVOID)lpRasDialParams); } if (lpRasConnStatus) { HeapFree(GetProcessHeap(), 0, (LPVOID)lpRasConnStatus); } return (int)dwRet; }