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

前端 未结 17 2077
悲哀的现实
悲哀的现实 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-05 00:02

    Here is the C program to validate a given IPV4 address. I have assumed that IP address is in decimal format. Please give me your thoughts on this.

      // strTokenFunction.cpp : Check if the specified address is a valid numeric IP address.
      // This function is equavalent to the IPAddress.TryParse() method in C#
    
    #include "stdafx.h"
    #include 
    #include 
    #include 
    
    bool isValidIpAddress(char *st)
    {
        int num, i, len;
        char *ch;
    
        //counting number of quads present in a given IP address
        int quadsCnt=0;
    
        printf("Split IP: \"%s\"\n", st);
    
        len = strlen(st);
    
        //  Check if the string is valid
        if(len<7 || len>15)
            return false;
    
        ch = strtok(st, ".");
    
        while (ch != NULL) 
        {
            quadsCnt++;
            printf("Quald %d is %s\n", quadsCnt, ch);
    
            num = 0;
            i = 0;
    
            //  Get the current token and convert to an integer value
            while(ch[i]!='\0')
            {
                num = num*10;
                num = num+(ch[i]-'0');
                i++;
            }
    
            if(num<0 || num>255)
            {
                printf("Not a valid ip\n");
                return false;
            }
    
            if( (quadsCnt == 1 && num == 0) || (quadsCnt == 4 && num == 0))
            {
                printf("Not a valid ip, quad: %d AND/OR quad:%d is zero\n", quadsCnt, quadsCnt);
                return false;
            }
    
            ch = strtok(NULL, ".");
        }
    
        //  Check the address string, should be n.n.n.n format
        if(quadsCnt!=4)
        {
            return false;
        }
    
        //  Looks like a valid IP address
        return true;
    }
    
    int main() 
    {
        char st[] = "192.255.20.30";
        //char st[] = "255.255.255.255";
        //char st[] = "0.255.255.0";
    
        if(isValidIpAddress(st))
        {
            printf("The given IP is a valid IP address\n"); 
        }
        else
        {
            printf("The given IP is not a valid IP address\n");
        }
    }
    

提交回复
热议问题