CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1

    check if IP is private?

    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

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: check if IP is private?

    None that I know but some ip's are reserverd for private LAN http://www.duxcw.com/faq/network/privip.htm
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3

    Re: check if IP is private?

    thanks, looks like i gotta make my own function using a CString.. i hope those IP's will always be private..

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: check if IP is 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???
    Last edited by TheCPUWizard; October 10th, 2008 at 06:50 PM.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5

    Re: check if IP is private?

    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:
    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;
    }
    for anybody that needs something like this..

    thanks
    Last edited by eXistenZ; October 10th, 2008 at 06:49 PM.

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: check if IP is private?

    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
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  7. #7

    Re: check if IP is private?

    there is also the automatic private IP range :P

    169.254.0.0 - 169.254.255.255

  8. #8
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: check if IP is private?

    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.

    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  9. #9

    Re: check if IP is private?

    Quote Originally Posted by TheCPUWizard
    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.
    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 IP

  10. #10
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: check if IP is private?

    Quote Originally Posted by eXistenZ
    there is also the automatic private IP range :P

    169.254.0.0 - 169.254.255.255
    That address range is properly refered to as "Autoconfiguration" range, and is a distinct category from "private use" IP address range.

    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.....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  11. #11

    Re: check if IP is private?

    Quote 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.

    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..

    thanks

  12. #12
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: check if IP is private?

    Quote Originally Posted by eXistenZ
    what the hell there are more? i think im going to skip this whole IP check thing..

    thanks

    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)
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  13. #13
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: check if IP is private?

    Quote Originally Posted by eXistenZ
    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 IP

    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.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured