I have just created a function which checks whether a ipv4 is public or not. I have not heavily tested it yet since it is kind of practically impossible to do so (because I do not know where to start).

My algorithm is based on an article from this website.

Public IP addresses will be issued by an Internet Service Provider and will have number ranges from 1 to 191 in the first octet, with the exception of the private address ranges that start at 10.0.0 for Class A private networks and 172.16.0 for the Class B private addresses.

Is this algorithm correct (implemented here in C++) ?

struct IpAddress {
    uint8_t oct1;
    uint8_t oct2;
    uint8_t oct3;
    uint8_t oct4;

bool isIpPublic(const IpAddress &ip){
    if (ip.oct1 >= 1 && ip.oct1 <= 191){            // not class C
        if (ip.oct1 != 10){                         // not class A (all of class A is private)
            if (ip.oct1 != 172 && ip.oct2 != 16){   // not class B (172.16.x.x is private)
                return true;

    return false;

closed as off-topic by Blrfl, gnat, Greg Burghardt, Caleth, Jörg W Mittag Jan 4 at 16:20

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking for assistance in explaining, writing or debugging code are off-topic here. These can be asked on Stack Overflow if they include the desired behavior, a specific problem or error, and the shortest code necessary to reproduce it in the question (see Minimal, Complete, and Verifiable Example)." – Blrfl, gnat, Greg Burghardt, Caleth, Jörg W Mittag
If this question can be reworded to fit the rules in the help center, please edit the question.

  • This question is off topic, but I will tell you that the article is quite wrong. – Blrfl Jan 4 at 12:04
  • please help me then. please help correct my algorithm... – marvinIsSacul Jan 4 at 12:05
  • While it is not authoritative, nor its information always reliable, Wikipedia is a good start. And it lists four ranges of private addresses, and some others you might want to include in that too. – Deduplicator Jan 4 at 12:25
  • @Deduplicator I count three on wikipedia and they are the same as in OP's referenced article... – Christophe Jan 4 at 12:29
  • The quote within the question is totally wrong. – gnasher729 Jan 4 at 13:07

Basic address analysis

There are three IP address ranges reserved for private addresses according to RFC 1918.

Pattern           Class        Remark 
10.xxx.xxx.xxx      A 
172.YYY.xxx.xxx     B          YYY is between 16 and 31 included
192.168.xxx.xxx     C

Unfortunately, your function fails completely. For example:

  • an address in the 194.xxx.xxx.xxx would be evaluated as private, while it is not.
  • an address in the 11.xxx.xxx.xxx range would be evaluated as private, while it is not
  • an address in the 172.31.xxx.xxx range would be evaluated as public while it is not.

You need to rewrite your code, not with nested ifs, but with an if/else chain. Be aware that for class C the 16 is only a part of the truth, as some bit level manipulation would be required; the range 16-31 is more practical here. For example:

bool isIpPublic(const IpAddress &ip){
    if (ip.oct1 == 10)                                   // class A private
        return false;
    else if (ip.oct1==172 && ip.oct2>=16 && ip.oct2<=31) // class B private
        return false;              
    else if (ip.oct1==192 && ip.oct2==168)               // class C private
        return false;                          
                           /* you could also combine all these conditions in a
                           single large boolean expression in a single if */

    return true;                                         // by default it's public

Advanced address analysis

Note that this is a simplified analysis: there are also special purpose addresses mentioned in RFC 6890, which are not to be (or cannot be ) used for public addresses either. For example:

  • Loopback address: 127.0.0.xxx
  • Link local addresses 169.254.xxx.xxx
  • Broadcast in the current network

IANA maintains a registry of such special addresses here. In this note, there's a link for downloading this registry in CSV format. The best would then be to download this file automatically and use the field Globally reachable to determine if an address is public.

  • Administratively scoped multicast addresses are also in some sense "private", depending on how, precisely, the OP defines "private". – Jörg W Mittag Jan 4 at 16:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.