hey, is there any way to check if an IP is private, like the ones that are used over LAN, is there a function for this?
thanks
Printable View
hey, is there any way to check if an IP is private, like the ones that are used over LAN, is there a function for this?
thanks
None that I know but some ip's are reserverd for private LAN http://www.duxcw.com/faq/network/privip.htm
thanks, looks like i gotta make my own function using a CString.. i hope those IP's will always be private..
It is actually impossible to determine if an IP is "private".
The best approach is to follow the standards and use the appropriate address range that is reserved for private use (there is a Class "A" [16 million viable addresses] ,"B" [1M viable addresses] and "C" [64K viable addresses] address range). USe the SMALLEST one that is appropriate.
This does NOT prevent there being IP clashes if someone does not follow the standard. But many public (e.g. ISP) routers will not transfer messages to/from these ranges in an effort to control it.
So given that the stated goal is effectively impossible. Lets take a look at what you are trying to actually accomplish....
WHY do you (think you) need this information???
well i got a chat server with a real IP and a client from that server's LAN will connect to it with a private IP then i got a file transfer which is client-server based, the client requests a server for the file transfer sending its IP (the chat server gets its IP) with the request too (this is being sent through the chat server), the request is accepted, then the client needs the IP to connect to.. the chat server sends the IP, then the file transfer client connects to the file transfer server and then the file transfer server checks the connected IP with the one from the request.. did you understand anything? lol
if the IP is private i will just change it to the chat server's IP so that the file transfer server doesn't kick the client for having an unexpected IP..
also.. finished the function:
for anybody that needs something like this..Code:BOOL CTESTDlg::IsIPPrivate(CString IP)
{
int nf;
nf = IP.Find(".", 0);
if(nf != -1)
{
int n;
n = atoi(IP.Left(nf));
IP = IP.Mid(nf + 1);
nf = IP.Find(".", 0);
if(nf != -1)
{
int n2;
n2 = atoi(IP.Left(nf));
IP = IP.Mid(nf + 1);
nf = IP.Find(".", 0);
if(nf != -1)
{
int n3;
int n4;
n3 = atoi(IP.Left(nf));
n4 = atoi(IP.Mid(nf + 1));
if((n == 10 && n2 >= 0 && n2 <= 255 && n3 >= 0 && n3 <= 255 && n4 >= 0 && n4 <= 255) || (n == 172 && n2 >= 16 && n2 <= 31 && n3 >= 0 && n3 <= 255 && n4 >= 0 && n4 <= 255) || (n == 192 && n2 == 168 && n3 >= 0 && n3 <= 255 && n4 >= 0 && n4 <= 255) || (n == 169 && n2 == 254 && n3 >= 0 && n3 <= 255 && n4 >= 0 && n4 <= 255))
{
return TRUE;
}
}
}
}
return FALSE;
}
thanks
That is simply checking agains 10.x.x.x
IANA Reserved Private Network Ranges
Code:24-bit Block (/8 prefix, 1 x A) 10.0.0.0 10.255.255.255 16,777,216
20-bit Block (/12 prefix, 16 x B) 172.16.0.0 172.31.255.255 1,048,576
16-bit Block (/16 prefix, 256 x C) 192.168.0.0 192.168.255.255 65,536
there is also the automatic private IP range :P
169.254.0.0 - 169.254.255.255
But this still does not really address any issues.
Consider if your server is public on the internet. Then it should have an address which is NOT in one of those ranges.
Now a client is on a private LAN (or is a single computer). In order to reach your server, it will have to go through a router which (typically) will perform NAT to hide the actual IP of the computer.
ALL your server will see if the public facing IP of the router. As an example, the machine I am using right now has an actual IP of 192.168.3.118 (within the class "C" private range. However, my router will only expose an IP of 72.229.11.135.
From outside by firewall, it is (hopefully) completely impossible to determine the actual IP's of my machine.
In a public chat type environment, this typically applies to ALL clients. Your server will NEVER see the IP's of the client computers, only their public facing (router/modem) IP's which will NOT be in the private ranges.
Now if you want to detect if a request is coming from your internal network or an external source, then there is a completely different approach which has NOTHING to do with private IP ranges.
You get the actual IP's of each of your NIC cards, along with their sub-net mask. You perform a bit-wise "AND" of the NIC addrerss and mask and compare this with a bit-wise "AND" of the connecting IP and mask.
If the two are the same, then the information came from your network, otherwise it came from an external source.
There may be additional sub-nets which while not physically part of your local net, are to be considered as if they are. In this case you repeat the above process, but instead of using the NIC information, you read the IP and Mask from some type of configuration file.
:wave: :wave:
true but if the chat client connects to its router IP which happens to be the chat server then the chat server will see the client's private IPQuote:
Originally Posted by TheCPUWizard
That address range is properly refered to as "Autoconfiguration" range, and is a distinct category from "private use" IP address range.Quote:
Originally Posted by eXistenZ
Thgere are also the following categories which may need to be handled specially:
* "Loopback" IP addresses
* "Unallocated" IP addresses
* "Multicast IP addresses
See: http://www.iana.org/abuse/faq.html for a good read on the subject...FROM the actual authority.....
what the hell there are more? i think im going to skip this whole IP check thing..Quote:
Originally Posted by TheCPUWizard
thanks
Quote:
Originally Posted by eXistenZ
Actually that is probably the correct approach.
(Still I am amazed at how few people will want to work with something, yet not bother to even look at the Frequently Asked Questions that are readily available for the governing body or vendor) :rolleyes: :rolleyes:
Quote:
Originally Posted by eXistenZ
The server will se the actual IP of the client in that case. But who is to say it will be in the private range. I know of a few companies that have purchased entire class C" ranges so that all their computers are using direct publically accessible IP addresses. (This will become more common with IPv6 because of the much larger range. - eventually every computer, cell phone, home appliance, and things we can not yet imaging will have a globally unique IP address)
This is why "private" vs. "public" is meaningless. You would properly detect that the dclient is "local" by using the algorithm I posted earlier.