How to get the IPs of a connection?

Mar 8, 2010 at 8:59 AM

Hi, I'm currently looking for a way to get the server and client IP of a connection to display it to the user. In fact, I would like to send pings to the server that I got my IP from, so I can see whether my connection has dropped or not (the RAS service in windows takes ages to realize that a connection is gone when - for example - the remote gateway reboots). I already tried to get the ip adresses through the connection watcher, phonebook entry and dialer but I didn't find anything. How could I achieve this? Any ideas?

 

By the way, DotRas is really a great, fantastic and awesome library! Great work!

Thx in advance ;)

Coordinator
Mar 8, 2010 at 1:30 PM
Edited Mar 9, 2010 at 2:21 PM

There sure is! With a RasConnection instance you can use the GetProjectionInfo method to retrieve that information. Here's an example:

RasDialer dialer = new RasDialer();

foreach (RasConnection connection in dialer.GetActiveConnections())
{
    RasIPInfo info = (RasIPInfo)connection.GetProjectionInfo(RasProjectionType.IP);
    if (info != null)
    {
        // The information you want is on the RasIPInfo class at this point.
    }
}

Keep in mind, if you want a specific connection you will need to check for it in the loop instead of getting the IP information from all active connections. If multiple connections are open (ie a dial-up and vpn connection) you might not get the connection data you're expecting.

Edit: I thought I might explain why those objects don't have that information available. The RasEntry class describes how a connection to a remote network should be made. The IPAddress information you're providing from that class is telling the server what IP address you want to use. Now that might work in some situations, but if you're using DHCP (which most remote networks do) the server is going to assign that IP address to the client machine. The GetProjectionInfo method simply retrieves the IP information once the connection has been established, thus giving you access to the information that was set by DHCP. Now if you're not using TCP/IP as a protocol for that connection, this method would return null indicating that data isn't available.

The RasDialer's job is to simply dial the connection, it doesn't know anything about the active connections on the machine. This is the reason why the 1.2 SDK under development is moving the GetActiveConnections method off RasDialer and to a static method on the RasConnection class.

RasConnectionWatcher is simply used to receive notifications when a connection is established, disconnected, or the bandwidth of an active connection changes. How long it takes for the event to fire is up to Windows, it only fires the events when Windows tells it to.

I hope that answers your question.

Mar 9, 2010 at 10:16 AM

Ah great! I already found that functions like GetConnectionStatistics(), GetConnectionStatus() and GetProjectionInfo()... while the first two functions were easy to understand und use, I was a little confused about GetProjectionInfo() as it just returns an object... thanks a lot for your help!

I mentioned the dialer, entry and connectionwatcher classes only to show that was really searching for a solution ;)

Coordinator
Mar 9, 2010 at 1:00 PM

The GetProjectionInfo method returns an object because depending which value you pass into it the method changes the object that is returned. RasIPInfo, RasAmbInfo, RasIPv6Info are just a couple of the objects it'll return.

If you have any other questions, feel free to ask.

- Jeff

Mar 9, 2010 at 2:18 PM
Edited Mar 9, 2010 at 2:21 PM

Thanks to your good work it is mostly self-explaining to work with your library. The example projects you provided together with the download and this discussion board serve as a good knowledge base so it was really easy to create a working program. By the way, your code works like a charm. There was only one small modification to be made: the object needs an explicit cast so that it can be used as RasIPInfo. To preserve completeness for the reader here's the final code snippet ;)

 

RasDialer dialer = new RasDialer();

foreach (RasConnection connection in dialer.GetActiveConnections())
{
RasIPInfo info = (RasIPInfo)connection.GetProjectionInfo(RasProjectionType.IP);
if (info != null)
{
// The information you want is on the RasIPInfo class at this point. } }

 

 

 

Coordinator
Mar 9, 2010 at 2:22 PM

Yeah, that was me just being dumb, I corrected my earlier post as well.

Happy coding!

- Jeff