How do you validate that a string is a valid IPv4 address in C++?

前端 未结 17 2052
悲哀的现实
悲哀的现实 2020-12-04 23:21

I don\'t need to validate that the IP address is reachable or anything like that. I just want to validate that the string is in dotted-quad (xxx.xxx.xxx.xxx) IPv4 format, w

相关标签:
17条回答
  • 2020-12-04 23:55

    If you don't want the overhead of Boost or TR1 you could search for the dots and check if the characters between them are numbers from 0 to 255.

    0 讨论(0)
  • 2020-12-04 23:57

    You can write your own function like this:

    bool isValidIPv4(const char *IPAddress)
    {
       unsigned char a,b,c,d;
       return sscanf(IPAddress,"%d.%d.%d.%d", &a, &b, &c, &d) == 4;
    }
    

    sscanf() and sprintf() are very useful in some situation. :))

    0 讨论(0)
  • 2020-12-04 23:58

    If you wish to receive an IP address in the usual form (8.8.8.8, 192.168.1.1, etc...) Then I've written the following code that expands on Bjorn's answer:

    void validateIP(const std::string &IPv4_address)
    {
        boost::system::error_code error_code;
        auto raw_ipv4_address = boost::asio::ip::address::from_string(IPv4_address, error_code);
        if (error_code)
        {
            throw std::invalid_argument(error_code.message());
        }
    
        std::string raw_to_string_form = raw_ipv4_address.to_string();
        if (raw_to_string_form.compare(IPv4_address))
        {
            throw std::invalid_argument("Input IPv4 address is invalid");
        }
    }
    

    The reason being is that if you pass on an IP such as 8.88.8, or 12345 (decimal form), Bjorn's answer won't throw an error. (at least with boost 1.68) My code converts any form to the internal structure in Boost, then converts it back to a dotted-decimal format, we then compare it to the first IP we received to see if they are equal, if not, we know for sure that the input is not the way we wanted it.

    0 讨论(0)
  • 2020-12-04 23:59

    If you wanted to write this yourself rather than use a library then

    atoi() to convert characters to ints will let you test the range of each number, and some strcmp's between the "."'s. You can also do some quick checks, such as the length of the string (should be less than 16 characters (not including null terminator), number of dots. etc.

    But, it's probably MUCH easier to use existing code.

    0 讨论(0)
  • 2020-12-05 00:00

    You probably want the inet_pton, which returns -1 for invalid AF argument, 0 for invalid address, and +1 for valid IP address. It supports both the IPv4 and future IPv6 addresses. If you still need to write your own IP address handling, remember that a standard 32-bit hex number is a valid IP address. Not all IPv4 addresses are in dotted-decimal notation.

    This function both verifies the address, and also allows you to use the same address in related socket calls.

    0 讨论(0)
  • 2020-12-05 00:00

    The solution that I settled on was:

    #include <arpa/inet.h>
    // ...
    bool Config::validateIpAddress(const string &ipAddress)
    {
        struct sockaddr_in sa;
        int result = inet_pton(AF_INET, ipAddress.c_str(), &(sa.sin_addr));
        return result != 0;
    }
    

    This works for most cases that were mentioned in other answers. It doesn't recognize IP addresses with octal or hex formatting, but that's acceptable for my application.

    0 讨论(0)
提交回复
热议问题