.Net ramblings
# Thursday, 29 July 2004
Controlling a dial-up connection via a web service

Background

I just finished writing a simple client/server .Net application that allows windows PCs on a LAN to control the dial-up connection on a web server. 
I'm currently using it at home on a small ad-hoc wireless network because the windows Internet Connection Sharing setting for 'dial on-demand' is very unreliable and it dials up when you don't want it to.  This way you control when it connects and disconnects. Also, i was using terminal services to connect and disconnect which is way overkill!

I have put the source code and executables online for anyone to download, use modify etc. Download: RemoteDialer.zip (94 k).  There is a readme file in there, but here are the instructions anyway which explain how it works and how to set it up.

How it works

The web server has methods for Dial() and Hangup().  They each call a batch file with Process.Start, which is configured to output the result of the operation to a text file.  The batch files use the rasdial.exe tool to connect and disconnect the connection. The web service reads the text file before sending back the contents of it, to the client that requested the operation.

The desktop application for client PCs has a system tray icon, with a right-click menu to Start or Stop the connection.

There is also a web page to connect/disconnect, so you can allow non-windows users (who obviously couldn't run the desktop app) who are on your network to control the connection. To go to this page, just go to the address of the v.dir on the server.

Setup

To configure the web server:

 - set up a new application pool which runs as Local Service
 - copy the 'Web' folder to the web server
 - make it a virtual directory
 - set the AppPool for the v.dir to the appPool you just created
 - Copy the 2 batch files in the 'Batch Files' folder to the server.
 - It's easiest to put them in the c:\ root, otherwise you can
   update the web.config application keys accordingly.

To configure the windows PCs:

 - Locate the RemoteDialer.exe.config file in \WinClient\bin
 - Adjust the http value key to match the server and v.dir name on the
   server.
 - Copy the WinClient\Bin folder to each PC
 - Run the .exe and you will see a new icon the system tray.
 - Right-click the tray icon to start or stop the dial up connection
 - The status of the operation is outputted to a pop up box.

Note: The reason for the new Application Pool is so that the web service
can interact with the system and write the output of operations
to text files.

 

If you have any comments or queries on it, or if there are bugs or problems, email me.  tim@mackeyNO_SPAM_PLEASE.ie


Thursday, 29 July 2004 11:29:17 (GMT Daylight Time, UTC+01:00)  #    Comments [4]  .Net General

Tuesday, 08 November 2005 07:42:00 (GMT Standard Time, UTC+00:00)
Hi
I have seen one of your comments on RASDIAL.You have also provided one zip file too. There you were using rasdial.exe to connect and disconnect.
But rather using RASDIAL.EXE , i have tried to use rasapi's to connect (rasdial) and disconnect(rashangup).I am able to connect to PPTP VPN server but in case of L2TP its failing and rasdial(..) is returing an error code = 2148081668.

So here i am also pasting my code etc whcih i have used to do this plz have a look at this and let me know. If i am doing something wrong.



Not able to connect to L2TP VPN server using RASDIAL API --

OS :Windows XP-Pro

Steps --
1. Created an VPN DUN Entry.
2. Tried to connect to that L2TP VPN Server manually(Succeeded)
3.Tried to connect to that VPN entry programmatically (Unamble to connect)

Here is the code used to perform this operation.
void Connect()
{
DWORD cbEntry = 0;
DWORD dwErr = ERROR_SUCCESS;
cbEntry = sizeof(RASENTRY);
RASENTRY rasEntry;
//RASENTRY rasEntry;
memset(&rasEntry, 0, cbEntry);
rasEntry.dwSize = cbEntry;
dwErr = RasGetEntryProperties(NULL, "Entry_Name", &rasEntry, &cbEntry, NULL, NULL);
if(ERROR_SUCCESS != dwErr)
{
//Set some flag that invalid entry etc
return;
}
RASDIALPARAMS rasDialParams;
BOOL pwd;
memset(&rasDialParams, 0, sizeof(RASDIALPARAMS));
rasDialParams.dwSize = sizeof(RASDIALPARAMS);
_tcscpy(rasDialParams.szEntryName, "entry_name");
DWORD dwRet = RasGetEntryDialParams(NULL, &rasDialParams, &pwd);
HRASCONN hRasConn = NULL;
_tcscpy(rasDialParams.szPhoneNumber,rasEntry.szLocalPhoneNumber);
_tcscpy(rasDialParams.szUserName, userName);
_tcscpy(rasDialParams.szPassword, password);
_tcscpy(rasDialParams.szEntryName, "entry_name");
_tcscpy(rasDialParams.szCallbackNumber, "*");
m_nDialStatus = CONNECTING ;
ret = RasDial(NULL,NULL,par,0,RasDialFunc,&hRasConn); //Actually trying to establish a connection
if(ERROR_SUCCESS != dwret)
{
//Set some flag that rasdial has failed
return;
}


int timeoutSecs = 90;
time_t start = time(NULL);

while (CONNECTING == m_nDialStatus )
{
if ( (time(NULL) - start) > timeoutSecs)
{
m_nDialStatus = CONNECT_FAILED;
RasHangUp(hRasConn);
return ;
}
Sleep(0);
}

if(CONNECT_SUCCESSFUL != m_nDialStatus )
{
RasHangUp(hRasConn);
m_nDialStatus = CONNECT_FAILED;
return ;
}
return;
}

Here is the callback function implementation
void CALLBACK CMSVpnManagedAppThread::RasDialFunc(UINT unMsg,RASCONNSTATE rasConnState,DWORD dwError)
{

if (ERROR_SUCCESS != dwError)
{
DWORD dwErr = GetLastError();
m_nDialStatus = CONNECT_FAILED;
}
else
{
_bstr_t bstrStatusMsg;
switch(rasConnState)
{

case RASCS_ConnectDevice:
m_nDialStatus = CONNECTING;
break;

case RASCS_Connected:
m_nDialStatus = CONNECT_SUCCESSFUL;
break;

case RASCS_Disconnected:
m_nDialStatus = CONNECT_FAILED;
break;

default:
break;
}

}
}
In the connect function RasDial(..)retuns Error_Success, and it starts dialing.

Whenever somce change in the state happens it calls this callback function.
So for states like RASCS_OpenPort ,RASCS_PortOpened,RASCS_ConnectDevice,RASCS_DeviceConnected,RASCS_AllDevicesConnected, RASCS_Authenticate this function gets called with errorcode = 0 and that also just once.But this function get called for the state'RASCS_AuthNotify' with error code = 0 and again it comes in to the function for this state only and with the error code 2148081668.
So please let me know what does this error code mean,And why am i getting this.

Regards
Anant
Tuesday, 08 November 2005 08:19:54 (GMT Standard Time, UTC+00:00)
Hi Anant,
Sorry I don't know anything about that API, you should try posting your question on a newsgroup instead where I'm sure somebody will be able to help you.
Good luck
Tim
Tim Mackey
Wednesday, 25 January 2006 12:06:09 (GMT Standard Time, UTC+00:00)
Hi ,

I tried a lot but not found any sol'n.
Then i saw ur posted Question.

Same thing i want in my application.
My problem is :

----------------------------------------------------------------------
I want to established a VPN connection between my PC(say A)
to the Remote LAN server's client(say B).
Situation is as follow...

|Machine-A|-->>>|Internet|---->>>|Server-S|--->>|Machine B|

Machine A is directed connected to the internet and having
public IP address and Machine B is on a remote LAN in some
other country and is connected to it's LAN server S.
Server S having public IP address.

Now i want to connect my Machine A to Machine B using
VPN(virtual private network) in my VC++ application.

Can someone help me?

Thanks in advance

Harish Dixit
harishdixit@greymatterindia.com
----------------------------------------------------------------------
Wednesday, 25 January 2006 12:10:55 (GMT Standard Time, UTC+00:00)
Hi,
My solution uses .net web services (c#), so my approach is not possible in the C++ environment unfortunately.
a C++ newsgroup would be your best bet. Or, why not upgrade to .net? Microsoft are giving away Visual C# express as a free download on their site until next year, this is a very nice piece of software.

Good luck
Tim
Tim Mackey
OpenID
Please login with either your OpenID above, or your details below.
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

[Captcha]Enter the code shown (prevents robots):

Live Comment Preview