美文网首页
判断字符串是否符合ip:port模式

判断字符串是否符合ip:port模式

作者: 王晓宇_xiaoyuwang | 来源:发表于2019-03-16 23:58 被阅读0次

    Linux下使用inet库

    #include <arpa/inet.h>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    bool validate_ip_address(const char* ip, const bool& ipv6) {
      unsigned char buf[sizeof(struct in6_addr)];
      if (ipv6) { //ipv6
        if (inet_pton(AF_INET6, ip, buf))
          return true;
      }
      else { //ipv4
        if (inet_pton(AF_INET, ip, buf))
          return true;
      }
      return false;
    }
    bool validate_ip_address(const std::string& ip, bool ipv6) {
      return validate_ip_address(ip.data(), ipv6);
    }
    
    int main() {
        std::string s1("10.10.10.10:20");
        std::string s2("10.10.10.10");
        std::string s3("10.10.10:20");
        std::string s4("10.10.10.10:20.20");
        std::string s5("10.a10.10:20");
        std::string s6("10.10.10.a10:20");
        std::string s7("10");
        std::string s8("10.10.10.1001:30");
        std::string s9("10.10.10.1001:");
        std::string s10("10:1");
        std::string s11("10.10.10.10.10:10");
        std::string s12("10.10.10.10:a");
        std::string s13("10.10.10.10.10.10:101");
        std::string s14("300.10.10.10:20");
        std::string s15("0:0:0:0:0:FFFF:204.152.189.116");
    
        std::cout << s1 << " " << std::boolalpha << validate_ip_address(s1, false) << std::endl;
        std::cout << s2 << " " << std::boolalpha << validate_ip_address(s2, false) << std::endl;
        std::cout << s3 << " " << std::boolalpha << validate_ip_address(s3, false) << std::endl;
        std::cout << s4 << " " << std::boolalpha << validate_ip_address(s4, false) << std::endl;
        std::cout << s5 << " " << std::boolalpha << validate_ip_address(s5, false) << std::endl;
        std::cout << s6 << " " << std::boolalpha << validate_ip_address(s6, false) << std::endl;
        std::cout << s7 << " " << std::boolalpha << validate_ip_address(s7, false) << std::endl;
        std::cout << s8 << " " << std::boolalpha << validate_ip_address(s8, false) << std::endl;
        std::cout << s9 << " " << std::boolalpha << validate_ip_address(s9, false) << std::endl;
        std::cout << s10 << " " << std::boolalpha << validate_ip_address(s10, false) << std::endl;
        std::cout << s11 << " " << std::boolalpha << validate_ip_address(s11, false) << std::endl;
        std::cout << s12 << " " << std::boolalpha << validate_ip_address(s12, false) << std::endl;
        std::cout << s13 << " " << std::boolalpha << validate_ip_address(s13, true) << std::endl;
        std::cout << s14 << " " << std::boolalpha << validate_ip_address(s14, false) << std::endl;
        std::cout << s15 << " " << std::boolalpha << validate_ip_address(s15, true) << std::endl;
    }
    
    1. 高版本GCC支持regex
    2. GCC 4.8不支持regex,手动实现
    #include <iostream>
    #include <string>
    #include <cctype>
    #include <cstdlib>
    
    using namespace std;
    
    bool validate_connection(std::string conn, bool ipv6)
    {
        const char dot = '.';
        const char colon = ':';
        const size_t max_dots = ipv6 ? 5 : 3; //three dots in ipv4 and five dots in ipv6
        const size_t max_colons = 1; //one colon seperating ip and port
        const size_t max_digits = 3; //as most three digits in part of ip
        const int max_piece_val = 255; //255 is the max val of a piece of ip
    
        size_t dots = 0;
        size_t colons = 0;
        size_t port_len = 0;
    
        std::string::iterator iter = conn.begin();
        size_t piece_len = 0;
        int piece_val = 0;
        for (; iter != conn.end(); ++iter) {
            if (dots < max_dots) { //former three parts of ip
                if (*iter != dot) {
                    if (!std::isdigit(*iter)) //only digit allowed
                        return false;
                    if (piece_len == max_digits) //a part of ip contains no more than three digits
                        return false;
                    if ((piece_val = piece_val * 10 + *iter - '0') > max_piece_val)
                        return false;
                    ++piece_len;
                }
                else {
                    ++dots;
                    piece_val = 0;
                    piece_len = 0;
                }
            }
            else { //the left is {4th part of ip}:port
                if (colons < max_colons) { //the forth part of ip
                    if (*iter != colon) {
                        if (!std::isdigit(*iter))
                            return false;
                        if (piece_len == max_digits)
                            return false;
                        if ((piece_val = piece_val * 10 + *iter - '0') > max_piece_val)
                            return false;
                        ++piece_len;
                    }
                    else
                    {
                        ++colons;
                        piece_len = 0;
                    }
                }
                else { //port
                    if (!std::isdigit(*iter))
                        return false;
                    ++port_len;
                }
            }
        }
    
        if (dots < max_dots || colons < max_colons || port_len == 0)
            return false;
    
        return true;
    }
    
    int main()
    {
        std::string s1("10.10.10.10:20");
        std::string s2("10.10.10.10");
        std::string s3("10.10.10:20");
        std::string s4("10.10.10.10:20.20");
        std::string s5("10.a10.10:20");
        std::string s6("10.10.10.a10:20");
        std::string s7("10");
        std::string s8("10.10.10.1001:30");
        std::string s9("10.10.10.1001:");
        std::string s10("10:1");
        std::string s11("10.10.10.10.10:10");
        std::string s12("10.10.10.10:a");
        std::string s13("10.10.10.10.10.10:101");
        std::string s14("300.10.10.10:20");
    
        std::cout << s1 << " " << std::boolalpha << validate_connection(s1, false) << std::endl;
        std::cout << s2 << " " << std::boolalpha << validate_connection(s2, false) << std::endl;
        std::cout << s3 << " " << std::boolalpha << validate_connection(s3, false) << std::endl;
        std::cout << s4 << " " << std::boolalpha << validate_connection(s4, false) << std::endl;
        std::cout << s5 << " " << std::boolalpha << validate_connection(s5, false) << std::endl;
        std::cout << s6 << " " << std::boolalpha << validate_connection(s6, false) << std::endl;
        std::cout << s7 << " " << std::boolalpha << validate_connection(s7, false) << std::endl;
        std::cout << s8 << " " << std::boolalpha << validate_connection(s8, false) << std::endl;
        std::cout << s9 << " " << std::boolalpha << validate_connection(s9, false) << std::endl;
        std::cout << s10 << " " << std::boolalpha << validate_connection(s10, false) << std::endl;
        std::cout << s11 << " " << std::boolalpha << validate_connection(s11, false) << std::endl;
        std::cout << s12 << " " << std::boolalpha << validate_connection(s12, false) << std::endl;
        std::cout << s13 << " " << std::boolalpha << validate_connection(s13, true) << std::endl;
        std::cout << s14 << " " << std::boolalpha << validate_connection(s14, false) << std::endl;
    }
    
    

    相关文章

      网友评论

          本文标题:判断字符串是否符合ip:port模式

          本文链接:https://www.haomeiwen.com/subject/kbxymqtx.html