Max Mind Geolocation anomalies - c#

I have a site that geolocates the user based on the IP Address using a MaxMind local database. I am finding the results on my desktop to be different than when on a phone. For example:
I am geolocating using the X-Forwarded-For header
My phone locates me in the zip code 18036, which is about 50 miles
off.
I look up the ip address on my phone using IPv4/IPv6 Dual-Stack Test
Using a browser extension on a desktop, I set my X-Forwarded-For header to the same IP Address
The desktop places me in 19428, which is still wrong but is MUCH
closer to correct
Why is there a difference?
Here is how I am performing my lookup:
var ip = httpContext.Request.Headers["X-Forwarded-For"];
CityResponse cityResponse;
using (_dbReader = new DatabaseReader(fullPath, FileAccessMode.Memory))
{
cityResponse = _dbReader.City(ip);
}
UPDATE
Is your phone connected to the same WiFi as your desktop? Or is it connected over 3G/4G?
No. To ensure phone and desktop were looking at the same things:
My site explicitly looks at IP for location - I am not tapping into browser location services, which may use a variety of methods to determine your location.
When searching on my phone, I turned off wifi so it was definitely using 3G/4G
After locating on my phone, I retrived my phone's IP address using "What my IP" in a browser
I spoofed the phone's IP in my browser using the X-Forwarded-For header and ensured my website was looking at that header instead of the actual IP (which would have been an internal IP)

If you are using Wifi, and on the same network as your computer, there won't be any difference because the public IP address (WAN IP address) will be the same.
If your phone is on a network other than the one your computer is on, there's no telling which ISP you would be assigned to (at the given time) and which IP address was assigned to your phone. If a cellular tower happens to be down or overloaded, your connection would be

Related

C# Dns.GetHostEntry doesn't return names of mobile devices connected to WiFi

I have a windows form application in C# and I'm trying to get the host name of all the clients that I have in a list. Given below is a code example by ra00l from this link: GetHostEntry is very slow (I have a similar code made but this one is cleaner)
private delegate IPHostEntry GetHostEntryHandler(string ip);
public string GetReverseDNS(string ip, int timeout)
{
try
{
GetHostEntryHandler callback = new GetHostEntryHandler(Dns.GetHostEntry);
IAsyncResult result = callback.BeginInvoke(ip,null,null);
if (result.AsyncWaitHandle.WaitOne(timeout, false))
{
return callback.EndInvoke(result).HostName;
}
else
{
return ip;
}
}
catch(Exception)
{
return ip;
}
}
When it's given an IP of a Windows Machine in the network, it shows the correct host name given that you enter a reasonable timeout. After the tests that I made, I didn't get any response for the host names of android and apple devices. For example, the picture below is the DHCP Client List of the router that I have. It shows android, apple and laptop devices. I'm using the laptop 'Nathu-Laptop' giving me an IP address of '192.168.1.106'.
If I enter '192.168.1.105' in the C# function, the result is 'Nandwani-PC' but if I input '192.168.1.103', '192.168.1.104', '192.168.1.101', '192.168.1.100', I don't get any hostname.
I also tried using nbtstat but it only gets the laptops in the network.
When trying this out on my iPod, I ensure that there is a network activity going on. This is to keep the connection alive because it disconnects from the network when it's on standby.
EDIT:
So I found out that DNS.GetHostEntry calls getaddrinfo if IPv6 is enabled, otherwise, call gethostbyaddr and these functions may access the data from \System32\drivers\etc\hosts or maybe from the NETBIOS. The thing is that the NETBIOS is legacy right? but how about for mobile devices?
About NetBIOS:
In order to answer your specific questions about NetBIOS, and name resolution on network, I'll give more details. If you don't have dns server running on your network, name resolution will rely only on NetBIOS resolution.
It is a standard and implemented on several operating systems. However, it's not very fast.
Even if we're old, we're not legacy from the past and obsolete
You can check on Microsoft support the way names are resolved on Windows and NetBIOS is the last one.
However, NetBios name resolution is not always fully functional (like this bug on Android which was fixed in 2014) on all mobile platforms (depends on Android version for example).
If you want to improve the performance, I suggest you to install a DNS server in the network.
Did you try ping -a <IP address> or nslookup <IP address> to check if the results are in line with your expectations ?
If your problem still persist, then you may investigate the .Net implementation, thanks to the above links. You can also check a more up to date version of .Net DNS implementation here

how to get a device name for a given ip address

I need to get the network device name for the given IP address.For example a printer connected in a network.I have the IP address of the printer but I need to access its name using the corresponding IP address.
I tried with dns.getHostByaddress(), but it didn't worked out properly.Let me know if any other solution exists.
string c = Dns.GetHostByAddress("xxx.xxx.x.xxx").HostName;
pdoc.PrinterSettings.PrinterName =c ;
It is a windows stand alone application using c# vs2010.
You cannot get the "DeviceName" by the ip itself. DNS-Names only exist at a DNS-Server (or netbios in such an environment) which hold the information about it - if you call "Dns.GetHostByAddress" you are always query the DNS-Server. If you are using DHCP on the devices, the dns entry will be updated automatically (if configured well). Else you have to configure the DNS-Server ip-address on the device and hope that this device is able to Register at the DNS (permissions and supported by your device).
Another way (which i not prefer) is to add the entries to the DNS manually.
You can force dnsregistration on a Windows machine by executing "ipconfig /registerdns"

How to get my correct IP address?

I try to get my correct IP but I can't
I'm using this code:
{
IPHostEntry host;
string localIP = "?";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
localIP = ip.ToString();
}
}
return localIP;
}
But it does not work for me!
For example my real IP is 151.246.147.86 but with this code I get 192.168.1.2.
Note: I have 4 Network adapters and in DOC with IPConfig I see this:
Network adapter: Local Area Connection 6
Media state : Media disconnected
Network adapter: Local Area Connection 4
Media state : Media disconnected
Network adapter: Local Area Connection 3
IP Address: 10.10.255.222
Network adapter: Local Area Connection
IP Address: 192.168.1.2
Now I connect to net and using with "Local Area Connection"; the public IP of my WAN connection is 151.246.147.86. I want to get this public IP (151.246.147.86), how can I do that?
Note : I don't want (and I can't, since I am using a VPN) use third party websites to get my IP
Please help!
-------EDIT :-------------
Note : I using from VPN and my VPN IP (for example) is : 176.227.197.111. But the IP of my WAN is: 151.246.147.86 and i want to get this address.
You (probably) are using a router/modem, therefore 192.168.1.2 is your "real IP". Your modem/router will be on the same (private) network, and its public interface (WAN) will have a public IP. So you need to get the IP on the public interface of your modem/router.
How to get the WAN IP of your modem depends on your mark and model; if it supports UPnP you can probably do it, or maybe if it is an enterprise class router it may also support SNMP...
You should specify your make and model.
Another way without using external sites: do a tracert to a known site (google?) the first hop will be your route.
Also, if you are on a VPN, you may be able to use the same technique. For example, if you know another host on the VPN (A server maybe?) you can ping/tracert it and discover your router from there. I don't know if in this case you will obtain what you call "a real IP" (by the way, how do you know this IP in the first place? You may be able to obtain it in the same way, programmatically).
Another solution for your VPN-based scenario: you can use Windows to help you. Windows has some kind of VPN management (RAS) which may help you; I would suggest starting from here to understand the basics, and then look for a library/SDK to help you (a quick google returned http://dotras.codeplex.com/).
What you ask is not possible unless there is some way to query your router/modem/external-most endpoint for its WAN address. The only IP address your computer knows about is its own (internal IP).
Technical note: there is nothing non-"real" about the IP address 192.168.1.2 - this is your computer's address. It is simply local to your given internal network and all but useless to anything outside.
If your router supports uPnP, you will need to query GetExternalIPAddress (starting point Is there a UPnP Library for .NET (C# or VB.NET)?). However since uPnP is considered dangerous and many security-conscious users turn it off, I would not count on it being enabled.
Querying an external service will be your most reliable bet for getting your external IP, whether it is one you write, or a third party service (see also https://superuser.com/questions/420969/services-that-just-return-your-public-ip-as-text).
There are many methods and tutorials that teach you how to find an ip address,I use this tool http://www.ipgp.net/ to display information about any ip address, just enter any IP address into the box and you will get the country, city, region, ISP, street address and the satellite location map for every query.
you're trying to get your IP address from your computer or a server? It will make a lot of difference. If you're trying to get your IP address from your computer/laptop use http://www.howtochangeipaddress.com. Your IP will pop up in front of you soon you enter the site.

How do I get the local host IP address in a Windows Store app?

A particular rest service I am calling in a Windows Store app wants the IP address of the calling computer as a parameter. None of the tried and true examples using System.Net or System.Net.NetworkInformation compile in a Store app.
What is the magical combination of types and methods available in a windows store app that will get to the local machine's IP address? I feel like it is probably obvious but I am not seeing it!
You will have to contact an external server. Even if the platform supplies an API to retrieve the network address, the host could still be located behind a proxy or a NAT (and you will see something like 192.168.1.4, instead of your external IP address).
Just perform an HTTP request towards services like http://ifconfig.me/ or http://whatismyip.com/ and parse the IP.
Local network IP address:
foreach (HostName localHostName in NetworkInformation.GetHostNames())
{
if (localHostName.IPInformation != null)
{
if (localHostName.Type == HostNameType.Ipv4)
{
// E.g.: 192.168.1.108
Debug.WriteLine(localHostName);
}
}
}

C# - How to get IP Address when connected to (RAS) VPN

Good afternoon,
Can anyone give any examples of how to obtain the IP Address of the local machine when it's connected to a remote windows domain network via VPN (RAS)? i.e. I need the VPN address and not the remote users local network address.
For example, my Server Side Windows Service communicates with my client side application and needs to create a log of all connected users and their IP Addresses.
This solution is easy enough when using a computer on the local network, but I wondered how I can go about getting the IP addresses of the users who are connected to the server via VPN. Please note that the IP address get method will be executed client side and sent to the server.
Here's my current code that works only when locally connected to the domain network:
public static string GetLocalIPv4()
{
string ipv4Address = String.Empty;
foreach (IPAddress currrentIPAddress in Dns.GetHostAddresses(Dns.GetHostName()))
{
if (currrentIPAddress.AddressFamily.ToString() == System.Net.Sockets.AddressFamily.InterNetwork.ToString())
{
ipv4Address = currrentIPAddress.ToString();
break;
}
}
return ipv4Address;
}
Our internal network is controlled by Windows SBS and uses a domain such as mycompany.local.
Thank you very much for your time and I look forward to reading your responses.
Rob
As the comment from #MarcB notes, cannot think of a good reason why you might want that info... so would be interesting if you could explain a use for this in an application just out of curiosity.
However, there are a lot of incorrect answers on here and online regarding enumerating IP addresses for a machine by using Dns.GetHostAddresses. It appears most people are not realizing the difference between looking up a machine name in the configured DNS resolver versus enumerating the machine address. These are very different things and while it might seem to return the right results in many cases, this is absolutely the wrong way to go about it because they are not the same thing. For example, see this link to an article on here where the original poster flagged an incorrect response as the answer but the correct response is below by #StephanCleary. See:
Get IPv4 Addresses
The difference is you want to look at the machines configuration and enumerate whatever IP address you are interested in locating from the machines own TCPIP stack. The code above and many of the incorrect responses try to lookup the name in the DNS resolver. Once you have that part correct, then you should be able to determine the VPN connection based on the network adaptor (by name or other attribute).

Categories

Resources