cwindowsvpndial-upras

RasDial returns 633 (port in use) but it isn't


I can connect to my VPN perfectly through the Windows GUI RasDial interface as well as it's CLI equivalent (c:\windows\rasdial.exe). However, when attempting to automate it in C, RasDial returns 633: ERROR_PORT_NOT_AVAILABLE (source)

This is not specific to me. I have tested it on four different computers, each on separate internet connections.

Here is my source code:

#include <windows.h>
#include <winerror.h>
#include <Ras.h>
#include <raserror.h>
#include <wchar.h>
#include <stdio.h>

int EnumConnections();
int DialOut();
int HangUp();
int Debug();

int main()
{
    printf("Dial out return code: %d\n", DialOut());
    printf("Debug status: %i\n", Debug());
    EnumConnections();
    HangUp();
    return 0;
}

int EnumConnections()
{
    DWORD dwCb = 0;
    DWORD dwRet = ERROR_SUCCESS;
    DWORD dwConnections = 0;
    LPRASCONN lpRasConn = NULL;
    if (dwRet == ERROR_BUFFER_TOO_SMALL)
    {

        lpRasConn = (LPRASCONN) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwCb);
        if (lpRasConn == NULL){
            wprintf(L"HeapAlloc failed!\n");
            return 0;
        }

        lpRasConn[0].dwSize = sizeof(RASCONN);

        dwRet = RasEnumConnections(lpRasConn, &dwCb, &dwConnections);

        if (ERROR_SUCCESS == dwRet){
            wprintf(L"The following RAS connections are currently active:\n");
            DWORD i;
            for (i = 0; i < dwConnections; i++){
                         wprintf(L"%s\n", lpRasConn[i].szEntryName);
                  }
        }

        HeapFree(GetProcessHeap(), 0, lpRasConn);
        lpRasConn = NULL;
    }

    if(dwConnections >= 1)
        wprintf(L"The operation failed to acquire the buffer size.\n");

    else
        wprintf(L"There are no active RAS connections.\n");

    return 0;
}

int DialOut()
{
    LPCTSTR pbkLoc = "C:\\rasphone.pbk\0";
    char* szPhoneNumberToDial = "127.0.0.1";
    char* szUserName = "test\0";
    char* szPassword = "test\0";
    RASDIALPARAMS rdParams;
    rdParams.dwSize = sizeof(RASDIALPARAMS);
    rdParams.szEntryName[0] = '\0';
    lstrcpy(rdParams.szPhoneNumber, szPhoneNumberToDial);
    rdParams.szCallbackNumber[0] = '\0';
    lstrcpy( rdParams.szUserName, szUserName );
    lstrcpy( rdParams.szPassword, szPassword );
    rdParams.szDomain[0] = '\0';

    HRASCONN hRasConn = NULL;
    return RasDial(NULL, pbkLoc, &rdParams, 0L, NULL, &hRasConn);
}

int HangUp()
{
    printf("Hung up\n");
    HRASCONN hRasConn = NULL;
    return RasHangUp(hRasConn);
}

int Debug()
{
    RASCONNSTATUS RasConnStatus;
    HRASCONN hRasConn = NULL;
    RasConnStatus.dwSize = sizeof(RasConnStatus);
    return RasGetConnectStatus(hRasConn,&RasConnStatus);
}

Any ideas? I'm really stuck here. I read ALL of the RasDial documentation. I still have no idea where to start.


Solution

  • For connecting to VPN connections you must have an entry contained within a phonebook. RasDial can only connect to dial-up connections if no entry has been provided. How else would it know whether to use PPTP, L2TP, SSTP, etc?

    Based on your above example, the entry name you're providing for the entry is a null terminated string with no entry name. You'd need to create an entry with all your VPN connection information in "C:\rasphone.pbk" according to your example and pass the entry name to your struct in the szEntryName field.

    LPCTSTR entryName = "Your Entry Name\0";
    rdParams.szEntryName = entryName;
    

    Hope that helps!