application runs is on a local LAN or on the Internet

R

Ragid

How do I know programatically if the PC where my application runs is on a
local LAN or on the Internet?
Regards
Ragid
 
A

Alberto Poblacion

Ragid said:
How do I know programatically if the PC where my application runs is on a
local LAN or on the Internet?

The question is not terribly clear. The PC where the application runs,
form the point of view of the application, is always the local machine. It
does not even have to be connected to a network, or if it is connected to a
LAN, the LAN itself could then have a gateway into the Internet.

The question that is more relevant from the point of view of .Net is
whether the executable file was started from a server in the local intranet
or from a server in the internet zone. The executable is then downloaded
into the memory of the local PC, and it *runs* in the local PC. This
distinction is relevant because the CAS permissions will be different
depending on the zone from which the file was downloaded. You can detect the
zone in your code by performing a Demand for the needed permissions. This
can be done with some code similar to the following (untested):

PermissionSet pset = GetNamedPermissionSet("LocalIntranet");
try
{
myPerm.Demand();
}
catch (SecurityException)
{
//We are NOT in the LocalIntranet zone.
}
....
private static PermissionSet GetNamedPermissionSet(string name)
{
IEnumerator policyEnumerator = SecurityManager.PolicyHierarchy();
while (policyEnumerator.MoveNext())
{
PolicyLevel currentLevel =
(PolicyLevel)policyEnumerator.Current;
if (currentLevel.Label == "Machine")
{
NamedPermissionSet copy =
currentLevel.GetNamedPermissionSet(name);
return (PermissionSet)copy;
}
}
return null;
}
 
R

Ragid

Thanks,
Though you may usually be right, in my case I do have to know if its a local
address , like 10.0.0.1:8080 or its an Internet address. The reason is that
some of the application that are connected without a NAT router serve also
as servers regarding NAT router hole punching.

Regards
Ragid
 
A

Alberto Poblacion

Ragid said:
Though you may usually be right, in my case I do have to know if its a
local address , like 10.0.0.1:8080 or its an Internet address. The reason
is that some of the application that are connected without a NAT router
serve also as servers regarding NAT router hole punching.

Well, the addresses that are reserved for "private" intranets are
"10.x.x.x", "172.16.x.x" through "172.31.x.x", and "192.168.x.x". A properly
configured NAT router should be using one of these ranges, which cannot be
used on the Internet.

However, nothing prevents someone from configuring NAT using a public
address on the private network (which will of course cause trouble in case
one of the internal PCs tries to reach the "real" public address), or using
reverse-NAT to translate the public address of the router into the private
address of an internal PC. I don't believe that there is a way to detect
this situation from a piece of software running exclusively inside the
affected PC.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top