Thread: check if IP is private

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    103

    check if IP is private

    any ideas how to check if an IP is private? is there maybe a function for this that i don't know about?

    thanks

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Private IPs have a specified range. Check out this. It looked like it outlined things nicely.

    Those are standard. Thus they will always be private. So just check what IP range the user is falling into. I gather you need to know if they are private in order to use other means to determine their WAN. Or are you trying to automate port settings?

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    103
    well theres a server with a real IP and theres a client from that server's LAN that connects to the real IP of that server, if you get the IP of the client it will be a private one.. im trying to find out if it is private so i will know its real IP is the same as the server's.. im not sure you understood lol

    looks like i gotta make my own function for this using a CString..

    thanks

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yeah you are pretty much on the right track, although why use a CString? An IP is 4bytes. You can can directly compare an IP to another IP using an IP mask. That is far easier to implement than doing string comparisons.

    Example:
    Code:
    unsigned int ip_mask = MAKE_IP(192, 0, 0, 0);
    
    if((ip & ip_mask) == ip_mask)
      private_ip = true;
    Last edited by master5001; 10-11-2008 at 03:34 PM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by master5001 View Post
    Yeah you are pretty much on the right track, although why use a CString? An IP is 4bytes. You can can directly compare an IP to another IP using an IP mask. That is far easier to implement than doing string comparisons.

    Example:
    Code:
    unsigned int ip_mask = MAKE_IP(192, 0, 0, 0);
    
    if(ip & ip_mask)
      private_ip = true;
    Um. Wouldn't 64.63.62.61 trigger that if-statement? Maybe you want
    Code:
    if ((ip & ip_mask) == ip_mask)
    instead.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ah yes, thank you tabstop. I don't know what I would do without you.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    103
    actually this was a bad idea.. i actually want a function to check if an IP can not be used over the internet and this is impossible to know.. as there are some other special IP's which could or could not be allowed to be used over the internet..

    http://www.iana.org/abuse/faq.html


    so im going to give up on this function.. thanks a lot for the help anyway

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    There are RFC's for reserved IP address ranges, existenZ. I was not yanking your leg. I am not saying it is *impossible* for these IPs to exist on the world-wide-web. I am just saying that they are not supposed to be. Do what you wish, friend. I think you are safe designing a function that conforms to the standard.

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    103
    oh don't get me wrong, i wasn't saying that your idea was a bad idea.. just my idea of solving a problem of mine was bad from the beginning.. i just didn't think enough of what i was doing actually..

    sorry i wasn't clear..

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I think your idea is possible, is all I am saying. Lets use some HTTP RFCs for the sake of argument. A server is supposed to give a browser the content type and length of the file that is going to be transmitted. That doesn't mean it is impossible for a rogue server out there to diabolically not conform.

    The same is true for IP addresses. Though IP addresses really are more controlled than web-servers. You will never find 127.0.0.1 as a web address ever. It just cannot happen.

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by eXistenZ View Post
    any ideas how to check if an IP is private? is there maybe a function for this that i don't know about?

    thanks
    There are two ways to check a particular IP.

    Theoretical: If the IP falls into one of the reserved ranges, it is private.

    Empirical: If the IP is addressable from the public internet, it is public, else it is private.

    The empirical test can't really be performed, so you're stuck with theory.

    Note that the private IP ranges are a matter of standard and convention only. If you placed a device on the public network with an address of, say, 192.168.10.1, then it could potentially work but only if all the routers agreed to play ball. And most of them will not.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    brewbuck, that was a very eloquent way to put the point I was trying to explain. The exceptions to the rule are few and far between and could be best addressed in the "Known issues" section of your version information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. Friend cannot inherit from private subclass
    By MWAAAHAAA in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2006, 04:44 PM
  3. Button not performing action
    By crummy in forum C# Programming
    Replies: 5
    Last Post: 03-12-2005, 03:42 PM
  4. IP without connecting to external socket
    By Hunter2 in forum Networking/Device Communication
    Replies: 5
    Last Post: 08-26-2003, 07:50 AM
  5. writing/reading from file produces double results.
    By stumon in forum C Programming
    Replies: 4
    Last Post: 03-20-2003, 04:01 PM