Determine if a string is a valid IPv4 address in C

后端 未结 15 1977
感情败类
感情败类 2020-12-24 08:22

What would be a good way to determine if a string contains an IPv4 address? Should I use isdigit()?

15条回答
  •  一生所求
    2020-12-24 08:36

    Heres the start of a function I've been working on, although not complete, it may spark ideas or comments. The thought behind the function is;

    1. Check the passed char pointer or string is an IPv4 address without port using its min/max size, how many dots in the string, and if the colon : character exists or not.
    2. If the string is not IPv4 with or without a port then check if the string is IPv6, if not IPv6 then IP format not recognized because it isn't implemented yet.

    I think it depends on how deep you want to go into the issue, how deep you want to go to understand what problems could occur.

    #include 
    #include 
    #include 
    #include 
    #include 
    
    int isIP(char *ip)
    {
        char *data_ptr = ip;    // Create a pointer to passed data
        int orig_str_size = 0;  // Create an int to hold passed data size
        int str_index = 0;      // Create an int to iterate individual ip characters
        int dot_count = 0;      // Create an int to check for the number of dots
    
        // Count the number of characters in data_ptr
        while (*data_ptr++ != '\0'){ orig_str_size++; }
    
        if(orig_str_size <= 0) // If nothing
        {
            printf("Get a grip, ip is empty\n\n");
            exit(0);
        }
        else // If within IPv4 size range
        if(orig_str_size >= 7 && orig_str_size <= INET_ADDRSTRLEN)
        {
            char *data1_ptr = ip; // Create a pointer to passed data
            printf("Within IPv4 range, %i characters in length\n\n", orig_str_size);
    
            // Count the number of dots in the string, 3 for IPv4
            for(str_index; str_index < orig_str_size; str_index++)
            {
                if(data1_ptr[str_index] == '.'){ dot_count++; }
            }
    
            // If theres 3 dots, while ignoring dots, check each char is a digit 
            if(dot_count == 3)
            {
    
                printf("There's three dots in the string\n\n");
                data1_ptr = ip;
                str_index = 0;
    
                // Iterate the string char by char
                for(str_index; str_index < orig_str_size; str_index++)
                {
                    // Ignoring dots
                    if(data1_ptr[str_index] != '.')
                    { 
                        // If isdigit() is happy its a digit and isalpha() happy not alphabetic
                        if(isdigit(data1_ptr[str_index]) && !isalpha(data1_ptr[str_index]))
                        {
                            printf("Digit found and is not alphabetic\n\n");
                            continue;
                        }
                        else
                        if(!isdigit(data1_ptr[str_index]) && isalpha(data1_ptr[str_index]))
                        {
                            printf("Not a recognised IPv4 address, character detected in string\n\n");
                            exit(0);
                        }
                    }
                }
    
                return 0;
            }
        }
        else // If IPv6
        if(orig_str_size > 0 && orig_str_size > INET_ADDRSTRLEN && orig_str_size <= INET6_ADDRSTRLEN)
        {
            printf("Within IPv6 range %i\n\n", orig_str_size);
            return 0;
        }
        else
        {
            printf("Unknown target format, the format you provided as a target is not implemented\n\n");
            exit(0);
        }
    
    }
    

    TCP/IP Internetworking

    RFC791 - Internet Protocol - https://tools.ietf.org/html/rfc791

    The CISCO Internetworking handbook http://docwiki.cisco.com/wiki/Internetworking_Technology_Handbook

    The Open Systems Interconnection Reference Model http://docwiki.cisco.com/wiki/Internetworking_Basics#Open_Systems_Interconnection_Reference_Model

    CISCO Troubleshooting TCP/IP Networks https://www.cisco.com/en/US/docs/internetworking/troubleshooting/guide/tr1907.pdf

    What is the largest TCP/IP network port number allowable for IPv4?

提交回复
热议问题